Timo Hocker da8c39c91b
All checks were successful
continuous-integration/drone/push Build is passing
switch node template to jasmine
2020-09-24 12:08:31 +02:00

35 lines
1.1 KiB
TypeScript

/*
* Copyright (C) SapphireCode - All Rights Reserved
* This file is part of Snippeteer which is released under BSD-3-Clause.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
/* 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: (data: string, file: string, args: Array<unknown>) => string|null,
opts: 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, opts);
continue;
}
const data = await fs.readFile (abs_path, 'utf-8');
const res = mutator (data, file, opts);
if (res === null)
continue;
await fs.writeFile (abs_path, res, 'utf-8');
}
}
}