1import chalk from 'chalk';
2import fs from 'fs-extra';
3
4import { findFiles } from '../utils';
5import { Task } from './Task';
6
7export type RemoveFilesSettings = {
8  source?: string;
9  filePattern: string;
10};
11
12export class RemoveFiles extends Task {
13  protected readonly source?: string;
14  protected readonly filePattern: string;
15
16  constructor({ source, filePattern }: RemoveFilesSettings) {
17    super();
18    this.source = source;
19    this.filePattern = filePattern;
20  }
21
22  protected overrideWorkingDirectory(): string {
23    return this.source || '<workingDirectory>';
24  }
25
26  async execute() {
27    const workDirectory = this.getWorkingDirectory();
28
29    this.logSubStep(
30      `�� Remove ${chalk.green(this.overrideWorkingDirectory())}/${chalk.yellow(this.filePattern)}`
31    );
32
33    const files = await findFiles(workDirectory, this.filePattern);
34    await Promise.all(
35      files.map((file) => {
36        return fs.remove(file);
37      })
38    );
39  }
40}
41