1import { Task } from './Task'; 2import chalk from 'chalk'; 3import fs from 'fs-extra'; 4import { findFiles } from '../utils'; 5 6export type AppendSettings = { 7 source?: string; 8 filePattern: string; 9 append: string; 10}; 11 12/** 13 * A task which will append to files content. 14 */ 15export class Append extends Task { 16 protected readonly source?: string; 17 protected readonly filePattern: string; 18 protected readonly append: string; 19 20 constructor({ source, filePattern, append }: AppendSettings) { 21 super(); 22 this.source = source; 23 this.filePattern = filePattern; 24 this.append = append; 25 } 26 27 protected overrideWorkingDirectory(): string | undefined { 28 return this.source; 29 } 30 31 async execute() { 32 const workDirectory = this.getWorkingDirectory(); 33 34 this.logSubStep( 35 `➕ append to ${chalk.green( 36 this.overrideWorkingDirectory() || '<workingDirectory>' 37 )}/${chalk.yellow(this.filePattern)}` 38 ); 39 40 const files = await findFiles(workDirectory, this.filePattern); 41 42 await Promise.all( 43 files.map(async (file) => { 44 return await fs.appendFile(file, this.append); 45 }) 46 ); 47 } 48} 49