/* * 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 , 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: Function, args: Array = [] ): Promise { 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'); } } }