/** * 工具对象 * @author 韦胜健 * @date 2022.4.20 11:52 */ const utils = (() => { const path = require('path'); const fs = require('fs'); const join = path.join; const cwd = process.cwd(); const resolve = (...pathList: string[]) => path.resolve(cwd, ...pathList); /** * 获取文件状态 * @author 韦胜健 * @date 2022.4.20 11:51 */ function getState(path: string) { try { return fs.statSync(path); } catch (e) { return null; } } /** * 文件是否匹配规则 * @author 韦胜健 * @date 2022.4.20 11:52 */ function matchPattern(path: string, pattern: RegExp | RegExp[]) { const patterns = Array.isArray(pattern) ? pattern : [pattern]; return patterns.some(p => p.test(path)); } /** * 自动创建文件夹 * @author 韦胜健 * @date 2022.4.20 12:40 */ function mkdirSync(path: string) { if (fs.existsSync(path)) { return; } let arr = path.split(/[\/\\]/); for (let i = 0; i < arr.length; i++) { let current = arr.slice(0, i + 1).join('/'); if (!fs.existsSync(current)) { fs.mkdirSync(current); } } } /** * 写入文件,如果没有所在的文件夹则自动创建文件夹 * @author 韦胜健 * @date 2023/8/9 17:24 */ function writeFileSync(filePath: string, content: string) { const dirPath = path.dirname(filePath); mkdirSync(dirPath); fs.writeFileSync(filePath, content); } /** * 递归删除文件夹(在删除文件夹之前,需要先删除该文件夹下的子文件夹以及子文件) * @author 韦胜健 * @date 2019/12/11 22:27 */ function rmdirSync(dir: string) { if (!fs.existsSync(dir)) { return; } let dirs = fs.readdirSync(dir); dirs = dirs.map((item: string) => path.join(dir, item)); for (let i = 0; i < dirs.length; i++) { const current = dirs[i]; let statObj = fs.statSync(current); if (statObj.isDirectory()) { rmdirSync(current); } else { fs.unlinkSync(current); } } fs.rmdirSync(dir); } return { path, join, cwd, resolve, fs, getState, matchPattern, mkdirSync, rmdirSync, writeFileSync, }; })(); interface iScanOption { entry: string, // 扫描入口 handler: { file?: (fileRelativePath: string) => void, // 处理文件 dir?: (directoryRelativePath: string) => boolean | void | undefined, // 处理文件夹。如果是返回false,则不再遍历文件夹内部文件 } pattern?: { /*同时匹配include以及exclude的话,是否一include优先*/ includeFirst?: boolean, /*如果存在该正则,如果路径不符合这个正则就不会处理这个文件(夹)*/ include?: { file?: RegExp | RegExp[], // 要扫描的文件 dir?: RegExp | RegExp[], // 要扫描的文件夹 }, /*如果存在该正则,如果路径符合这个正则就不会处理这个文件(夹)*/ exclude?: { file?: RegExp | RegExp[], // 要排除的文件 dir?: RegExp | RegExp[], // 要排除的文件夹 }, } } function scan(option: iScanOption) { const { entry, handler, pattern } = option; const iterator = (path: string) => { path = utils.path.normalize(path); const state = utils.getState(path); if (!!state) { if (state.isFile()) { if (option.pattern?.includeFirst) { const isInclude = !!pattern?.include?.file && utils.matchPattern(path, pattern.include.file); const isExclude = !!pattern?.exclude?.file && utils.matchPattern(path, pattern.exclude.file); if (isExclude && !isInclude) {return;} } else { if (!!pattern?.include?.file && !utils.matchPattern(path, pattern.include.file)) {return;} if (!!pattern?.exclude?.file && utils.matchPattern(path, pattern.exclude.file)) {return;} } handler.file?.(path); } else { if (option.pattern?.includeFirst) { const isInclude = !!pattern?.include?.dir && utils.matchPattern(path, pattern.include.dir); const isExclude = !!pattern?.exclude?.dir && utils.matchPattern(path, pattern.exclude.dir); if (isExclude && !isInclude) {return;} } else { if (!!pattern?.include?.dir && !utils.matchPattern(path, pattern.include.dir)) {return;} if (!!pattern?.exclude?.dir && utils.matchPattern(path, pattern.exclude.dir)) {return;} } const flag = handler.dir?.(path); if (flag === false) {return;} const files = utils.fs.readdirSync(path); files.forEach((file: string) => {iterator(utils.join(path, file));}); } } }; iterator(entry); } exports.scan = scan; exports.utils = utils;