-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.js
More file actions
63 lines (54 loc) · 1.46 KB
/
count.js
File metadata and controls
63 lines (54 loc) · 1.46 KB
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
58
59
60
61
62
63
import fs from "fs"
import path from "path"
const excludeDirs = ['node_modules', '.git', '.github', 'min'] // add directories to skip
const includeExts = ['.js', '.ejs'] // file extensions to include (empty = all)
function getFiles(dir, fileList = []) {
const files = fs.readdirSync(dir)
for (const file of files) {
const fullPath = path.join(dir, file)
const stat = fs.statSync(fullPath)
if (stat.isDirectory()) {
if (!excludeDirs.includes(file)) {
getFiles(fullPath, fileList)
}
} else {
if (
includeExts.length === 0 ||
includeExts.includes(path.extname(file))
) {
fileList.push(fullPath)
}
}
}
return fileList
}
function analyzeFile(filePath) {
let lineCount = 0
try {
const content = fs.readFileSync(filePath, 'utf8')
lineCount = content.split(/\r?\n/).length
} catch (error) {
console.warn(`Could not read ${filePath}`, error)
}
return {
path: filePath,
lines: lineCount,
ext: path.extname(filePath).toLowerCase(),
}
}
const allFiles = getFiles(process.cwd())
const results = allFiles.map(analyzeFile)
const extSummary = {}
let totalFiles = 0
let totalLines = 0
results.forEach((r) => {
extSummary[r.ext] = (extSummary[r.ext] || 0) + 1
totalFiles++
totalLines += r.lines
})
console.log('File stats:')
console.table(results)
console.log('File type counts:')
console.table(extSummary)
console.log('Totals:')
console.table([{ totalFiles, totalLines }])