snippeteer/lib/snippets/copyright/file_mapper.ts

28 lines
795 B
TypeScript

/* eslint-disable no-await-in-loop */
import path from 'path';
import fs from 'fs-extra';
export class FileMapper {
public static async map_all_files (
folder: string,
mutator: Function,
args: Array<unknown> = []
): Promise<void> {
const files = await fs.readdir (folder);
for (const file of files) {
if ([ 'node_modules' ].includes (file))
continue;
const abs_path = path.join (folder, file);
if ((await fs.stat (abs_path)).isDirectory ()) {
await FileMapper.map_all_files (abs_path, mutator, args);
continue;
}
const data = await fs.readFile (abs_path, 'utf-8');
const res = mutator (data, file, args);
if (res === null)
continue;
await fs.writeFile (abs_path, res, 'utf-8');
}
}
}