Define a module stat.js

const fs = require('fs'); const path = require('path'); Class Stat {constructor(rootPath, opts) {this.countInfo = {filesCount: 0, FileLineCount: 0, // total fileLineCount maxFileLine: 0, // maximum fileLineCount classCount: 0, // bigFilesList: [] / / large file list} this. BigFileLimit = opts. BigFileLimit | | 400; This. rootPath = rootPath; / / scan the root directory of the enclosing ingoreDirs = opts. IngoreDirs | | []; / / ignore this. IngoreFiles = opts. IngoreFiles | | []; } getFileCount() {} readFileList(dir, collectObj) {let {filesList, dirsList, bigFilesList } = collectObj const files = fs.readdirSync(dir); files.forEach((item, index) => { var fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { if (this.ingoreDirs.includes(item)) { return; } dirsList.push(fullPath); this.readFileList(path.join(dir, item), collectObj); / / recursive read the file} else {the if (this. IngoreFiles. Includes (item)) {return; } let fileContent = fs.readFileSync(fullPath, 'utf-8'); let fineLine = fileContent.split('\\n').length; if (fineLine > this.bigFileLimit) { collectObj.bigFilesList.push(fullPath) } if (fineLine === 8963) { console.log(fullPath) } collectObj.fileLinesList.push(fineLine); collectObj.classCount += this.getClassCountFromFileContent(fileContent); filesList.push(fullPath); }}); return collectObj; } / / number of class file here to replace regular more accurately with the AST getClassCountFromFileContent (fileContent) {let lines. = the fileContent split (" \ \ n "); let count = 0; for (var line of lines) { if (/class\\s((.) *)/.exec(line)) { count++; } } return count; } // boot() {let collectObj = {filesList: [], dirsList: [], fileLinesList: [], classCount: 0, bigFilesList: [] } this.readFileList(this.rootPath, collectObj); this.countInfo.filesCount = collectObj.filesList.length; this.countInfo.dirsCount = collectObj.dirsList.length; this.countInfo.maxFileLine = collectObj.fileLinesList.sort(function (a, b) { return b - a; }) [0]; // console.log( collectObj.fileLinesList) this.countInfo.fileLineCount = collectObj.fileLinesList.reduce(function (prev,  cur) { return prev + cur; }, 0) this.countInfo.classCount = collectObj.classCount; this.countInfo.bigFilesList = collectObj.bigFilesList; return this; } logStatDesc(desc) {let logs = [] desc? logs.push(desc) : null; Logs. Push (` statistics on the number of files: ${this. CountInfo. FilesCount} a `); Logs.push (' count of directories: ${this.countinfo.dirscount} '); Logs. Push (` code number statistics: ${this. CountInfo. FileLineCount} line `); Logs. Push (` single file line biggest statistics: ${this. CountInfo. MaxFileLine} line `); / / the console log (` JavaScript Class statistics: ${this. CountInfo. ClassCount} a `); console.log(logs.join('\\n')); this.statLogs = logs; return this; } / / printing engineering advice logSuggestion () {let logs = [] logs. Push (' -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- '); Logs.push ('------- project proposal --------'); Logs.push (' these files exceed ${this.bigFilelimit} line '); for (let file of this.countInfo.bigFilesList) { logs.push(file) } console.warn(logs.join('\\n')); } // Statistics log write file writeStatRecords(key, dirName) {fs.writefilesync (path.join(dirName, key + '.txt'), this.statLogs.join('\\n'), 'utf8'); MergeStat (statInstance) {for (let key in this.countinfo) {if (Object.prototype.toString.call(this.countInfo[key]) === '[object Number]') { this.countInfo[key] += statInstance.countInfo[key]; } if (Object.prototype.toString.call(this.countInfo[key]) === '[object Array]') { this.countInfo[key].push(statInstance.countInfo[key]); } } return this; } } module.exports = Stat;Copy the code

use

const Stat = require('./stat'); const path = require('path'); const app = new Stat(path.join('/Users/jack/src/helloword', 'src'), { ingoreDirs: ['dist'], ingoreFiles:['eslint.json'] }); app.boot(); App.logstatdesc (' HelloWord statistics '); // app.logStatdesc (' helloWord statistics '); .writeStatRecords('./records')Copy the code