-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscan.cjs
More file actions
91 lines (78 loc) · 2.22 KB
/
scan.cjs
File metadata and controls
91 lines (78 loc) · 2.22 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const fs = require('node:fs')
const path = require('node:path')
const SAMPLE_DIR = path.resolve('D:/xdnote/GitCodeViewer')
function readFiles(dir) {
return fs.readdirSync(dir)
.filter((name) => fs.statSync(path.join(dir, name)).isFile())
.map((name) => ({
name,
ext: path.extname(name).slice(1).toLowerCase(),
fullPath: path.join(dir, name),
}))
}
function toSummary(entry) {
const text = fs.readFileSync(entry.fullPath, 'utf8')
const lines = text.split(/\r?\n/).length
const bytes = Buffer.byteLength(text)
return {
name: entry.name,
ext: entry.ext || '(none)',
lines,
bytes,
}
}
function groupByExtension(rows) {
return rows.reduce((map, row) => {
if (!map.has(row.ext)) {
map.set(row.ext, [])
}
map.get(row.ext).push(row)
return map
}, new Map())
}
function formatRow(row) {
const lineText = String(row.lines).padStart(4, ' ')
const byteText = String(row.bytes).padStart(6, ' ')
return `${lineText} lines ${byteText} bytes ${row.name}`
}
function printReport(groups) {
const exts = [...groups.keys()].sort()
for (const ext of exts) {
const rows = groups.get(ext).sort((a, b) => a.name.localeCompare(b.name))
console.log(`\n[${ext}] ${rows.length} file(s)`)
for (const row of rows.slice(0, 4)) {
console.log(` ${formatRow(row)}`)
}
if (rows.length > 4) {
console.log(` ... ${rows.length - 4} more`)
}
}
}
function findRangeViolations(rows) {
return rows.filter((row) => row.lines < 80 || row.lines > 200)
}
function main() {
const summaries = readFiles(SAMPLE_DIR).map(toSummary)
const groups = groupByExtension(summaries)
const violations = findRangeViolations(summaries)
console.log(`Scanned ${summaries.length} sample files in ${SAMPLE_DIR}`)
console.log(`Found ${groups.size} unique extensions`)
if (violations.length) {
console.log('\nLine range violations:')
for (const row of violations) {
console.log(` ${row.name} -> ${row.lines} lines`)
}
} else {
console.log('\nAll files are inside the 80-200 line target range.')
}
printReport(groups)
}
if (require.main === module) {
main()
}
module.exports = {
readFiles,
toSummary,
groupByExtension,
findRangeViolations,
}