通过NodeJS统计静态文件的代码行数
Js代码如下:
let fs = require('fs')
let path = require('path')
// 获取命令行参数
let parm = process.argv.splice(2)
// 第一个参数是路径
let rootPath = parm[0]
// 后面的所有参数都是文件后缀
let types = parm.splice(1)
// 需要过滤的文件夹
//let filter = ['./node_modules']
let filter1 = "node_modules"
let filter2 = "test"
let filter3 = "lib"
let filter4 = "QModbusLib"
let filter5 = "Text"
let filter6 = "RemoteDataBroker"
let filter7 = "UITest"
// 需要过滤的文件
let filterFile1 = "system-keypad-config.j"
let filterFile2 = "qcustomplot."
let filterFile3 = "CSqlite."
//let filterFile4 = "qcustomplot."
// 统计结果
let num = 0
// 获取行数
async function line(path) {
let rep = await fs.readFileSync(path)
rep = rep.toString()
let lines = rep.split('\n')
if(lines.length > 1)
console.log(path + ' ' + lines.length)
num += lines.length
}
// 递归所有文件夹统计
async function start(pt) {
let files = fs.readdirSync(pt)
files.map(file => {
return `${pt}/${file}`
})
.forEach(file => {
let stat = fs.statSync(file)
if (stat.isDirectory()) {
if (file.indexOf(filter1) != -1 || file.indexOf(filter2) != -1
|| file.indexOf(filter3) != -1 || file.indexOf(filter4) != -1
|| file.indexOf(filter5) != -1 || file.indexOf(filter6) != -1
|| file.indexOf(filter7) != -1 ) {
return
}
start(file)
return
}
if(file.indexOf(filterFile1) != -1 || file.indexOf(filterFile2) != -1
|| file.indexOf(filterFile3) != -1)
{
return
}
let ext = path.extname(file)
if (types.indexOf(ext) != -1) {
line(file)
}
})
}
;(async () => {
await start(rootPath)
console.log(`总代码行数:${num}`)
})()
调用js文件扫描文件
// <文件路径> <文件类型>
node CheckCodeLines.js drawing .js .json .h .cpp