app.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. let fs = require('fs')
  2. let path = require('path')
  3. // 获取命令行参数
  4. let parm = process.argv.splice(2)
  5. // 第一个参数是路径
  6. let rootPath = parm[0]
  7. // 后面的所有参数都是文件后缀
  8. let types = parm.splice(1)
  9. // 需要过滤的文件夹
  10. let filter = ['./node_modules']
  11. // 统计结果
  12. let num = 0
  13. // 获取行数
  14. async function line(path) {
  15. let rep = await fs.readFileSync(path)
  16. rep = rep.toString()
  17. let lines = rep.split('\n')
  18. console.log(path + ' ' + lines.length)
  19. num += lines.length
  20. }
  21. // 递归所有文件夹统计
  22. async function start(pt) {
  23. let files = fs.readdirSync(pt)
  24. files
  25. .map(file => {
  26. return `${pt}/${file}`
  27. })
  28. .forEach(file => {
  29. let stat = fs.statSync(file)
  30. if (stat.isDirectory()) {
  31. if (filter.indexOf(pt) != -1) {
  32. return
  33. }
  34. start(file)
  35. return
  36. }
  37. let ext = path.extname(file)
  38. if (types.indexOf(ext) != -1) {
  39. line(file)
  40. }
  41. })
  42. }
  43. ;(async () => {
  44. await start(rootPath)
  45. console.log(`总代码行数:${num}`)
  46. })()