Skip to content

Commit

Permalink
Merge branch 'master' of github.com:LuckyHookin/copyt
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyHookin committed Feb 13, 2022
2 parents 0dfe25a + 635796c commit 60888cf
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,65 @@



本工具用于整理项目代码(软著代码整理),功能包括代码汇总、空行与注释的剔除。
本工具用于整理项目代码(软著代码整理),功能包括代码汇总、空行与注释的剔除。

匹配注释的正则表达式如下:
```
/\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!');
});
```

0 comments on commit 60888cf

Please sign in to comment.