-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputLine.js
44 lines (39 loc) · 1.04 KB
/
computLine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @description 计算代码行数
**/
const ignorePath = [
'protobuf'
];
const path = require('path');
const resolve = path.resolve;
const join = path.join;
const fs = require('fs');
const rootPath = resolve(__dirname, './lib');
let fileNum = 0;
console.log(
`非空行数: ${readDir(rootPath)}`
);
function readDir(path) {
const dirData = fs.readdirSync(path);
let lineNum = 0;
dirData.forEach(item => {
for (let i = 0; i < ignorePath.length; i++) {
if (new RegExp(ignorePath[i]).test(item)) return;
}
const itemPath = join(path, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
lineNum += readDir(itemPath);
} else {
lineNum += readFile(itemPath);
}
});
return lineNum;
}
function readFile(path) {
const file = fs.readFileSync(path, 'utf-8').split(/\n+/g).map(item => item.replace(/\s+/g, ''));
let fileFilter = file;
fileFilter = fileFilter.filter(item => item.length > 3);
fileFilter = fileFilter.filter(item => !item.match(/^(\/\/)/));
return fileFilter.length;
}