刚接手了一个项目,老板让我统计一下代码行数,不看不知道,一看吓一跳,一起看看你写了多少代码吧。
Node Version: 14.18.1
终端执行node ./line.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 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| const fs = require("fs"); const path = require("path");
const folderPaths = [path.resolve("./", "src")]; const excludes = [ "nide_modules", "demo", "static", "docs", "__mocks__", "dist", ];
let fileCount = 0; let lineCount = 0;
const getLines = (filePath) => { const res = fs.readFileSync(filePath); const lines = res.toString().split("\n"); return lines.length; };
const avaliableFile = (filePath) => { const currentSplitPaths = filePath.split("/"); const paths = new Set(excludes.concat(currentSplitPaths)); return ( Array.from(paths).length === excludes.length + currentSplitPaths.length ); };
const scanDir = (folderPath, dir) => { try { const files = fs.readdirSync(folderPath); files.forEach((filename) => { const director = path.join(`${folderPath}/`, filename); const stats = fs.statSync(director); if (stats.isDirectory()) { scanDir(director, dir ? `${dir}.${filename}` : filename); } if (stats.isFile() && avaliableFile(director)) { fileCount += 1; lineCount += getLines(director); } }); } catch (err) { console.info(err); } };
folderPaths.forEach((folderPath) => { scanDir(folderPath); });
console.log(fileCount, lineCount);
|