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