From 635796cb97d9eb99d73bbbb576b4318cc165e92a Mon Sep 17 00:00:00 2001 From: Hookin <45350455+LuckyHookin@users.noreply.github.com> Date: Thu, 3 Feb 2022 18:32:09 +0800 Subject: [PATCH] Update README.md --- README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e5449b..d5c0367 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,65 @@ -本工具用于整理项目代码(软著代码整理),功能包括代码汇总、空行与注释的剔除。 \ No newline at end of file +本工具用于整理项目代码(软著代码整理),功能包括代码汇总、空行与注释的剔除。 + +匹配注释的正则表达式如下: +``` +/\s*(\/\*.*?\*\/||\/\/.*?$)/gms +``` +主要逻辑在 `res\index.js` 里: +```js +let fs=require('fs'); +const { fdir } = require("fdir"); + +console.log(process.argv); + + +const dir = process.argv[2]; +// const dir = 'E:\\data\\web\\vue.js\\vue3\\hir'; + +// 文件类型过滤 +const filterArg = process.argv[3]; +// const filterArg = '\\.js|\\.vue'; +const filter = new RegExp("("+filterArg+")$",'i'); + +// 文件夹过滤 +const excludeArg = process.argv[4]; +// const excludeArg = 'node_modules|dist'; +const exclude = new RegExp(excludeArg,'i'); + +const codeFilePath = process.argv[5]; +// const codeFilePath = './code.txt'; + +try { + fs.unlinkSync(codeFilePath); + console.log(`reset ${codeFilePath}`); +} catch (error) { + +} + +let api = new fdir().withFullPaths().filter( + (path, isDirectory) => { + // console.log(path); + // 文件类型过滤, true 则包含 + return filter.test(path); + } +).exclude((dirName, dirPath) =>{ + // console.log(dirName); + return exclude.test(dirName); +} +); + +api.crawl(dir).withPromise().then((files) => { + for (let index = 0; index < files.length; index++) { + const filePath = files[index]; + console.log(index+1,filePath); + const data = fs.readFileSync(filePath); + const content = data.toString(); + let replaceContent = content.replace(/\s*(\/\*.*?\*\/||\/\/.*?$)/gms, ''); + replaceContent = replaceContent.replace(/^\s*\n/gm, ''); + fs.appendFileSync(codeFilePath, replaceContent); + } + console.log('ok!'); +}); +```