|
| 1 | +import * as fs from 'fs'; |
| 2 | + |
| 3 | +interface Stat { |
| 4 | + fileType: string; |
| 5 | + count: number; |
| 6 | + lines: number; |
| 7 | +} |
| 8 | + |
| 9 | +abstract class StatsBase { |
| 10 | + parent: StatsBase | undefined; |
| 11 | + path: string; |
| 12 | + name: string; |
| 13 | + stats: Stat[]; |
| 14 | + |
| 15 | + constructor(parent: StatsBase | undefined, path: string, name: string, stats: Stat[]) { |
| 16 | + this.parent = parent; |
| 17 | + |
| 18 | + this.path = path; |
| 19 | + this.name = name; |
| 20 | + this.stats = stats; |
| 21 | + } |
| 22 | + |
| 23 | + abstract addChild(child: StatsBase): void; |
| 24 | + |
| 25 | + abstract mergeStats(stats: Stat[]): void; |
| 26 | + |
| 27 | + abstract print(depth: number, lastChildArr: boolean[]): void; |
| 28 | + |
| 29 | + abstract sort(): void; |
| 30 | + |
| 31 | + getPrefix(depth: number, lastChildArr: boolean[]): string { |
| 32 | + let prefix = ''; |
| 33 | + for (let i = 0; i < depth; i++) { |
| 34 | + prefix += lastChildArr[i] ? ' ' : '│ '; |
| 35 | + } |
| 36 | + |
| 37 | + if (lastChildArr.at(-1)) { |
| 38 | + prefix += '└─ '; |
| 39 | + } else { |
| 40 | + prefix += '├─ '; |
| 41 | + } |
| 42 | + |
| 43 | + return prefix; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class FolderStats extends StatsBase { |
| 48 | + children: StatsBase[]; |
| 49 | + |
| 50 | + constructor(parent: StatsBase | undefined, path: string, name: string) { |
| 51 | + super(parent, path, name, []); |
| 52 | + |
| 53 | + this.children = []; |
| 54 | + } |
| 55 | + |
| 56 | + addChild(child: StatsBase) { |
| 57 | + this.children.push(child); |
| 58 | + this.mergeStats(child.stats); |
| 59 | + } |
| 60 | + |
| 61 | + mergeStats(stats: Stat[]): void { |
| 62 | + // console.log(this, stats); |
| 63 | + for (const stat of stats) { |
| 64 | + const existingStat = this.stats.find(s => s.fileType === stat.fileType); |
| 65 | + if (existingStat) { |
| 66 | + existingStat.count += stat.count; |
| 67 | + existingStat.lines += stat.lines; |
| 68 | + } else { |
| 69 | + this.stats.push(structuredClone(stat)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + this.parent?.mergeStats(stats); |
| 74 | + } |
| 75 | + |
| 76 | + print(depth: number, lastChildArr: boolean[]): void { |
| 77 | + console.log( |
| 78 | + `${this.getPrefix(depth, lastChildArr)}${this.name} | ${this.stats.reduce((acc, s) => acc + s.count, 0)} files | ${this.stats.reduce((acc, s) => acc + s.lines, 0)} lines`, |
| 79 | + ); |
| 80 | + for (let i = 0; i < this.children.length; i++) { |
| 81 | + const child = this.children[i]; |
| 82 | + child.print(depth + 1, [...lastChildArr, i === this.children.length - 1]); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + sort(): void { |
| 87 | + this.children.sort((a, b) => { |
| 88 | + if (a instanceof FolderStats && b instanceof FileStats) { |
| 89 | + return 1; |
| 90 | + } else if (a instanceof FileStats && b instanceof FolderStats) { |
| 91 | + return -1; |
| 92 | + } else { |
| 93 | + return a.name.localeCompare(b.name); |
| 94 | + } |
| 95 | + }); |
| 96 | + this.children.forEach(c => c.sort()); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +class FileStats extends StatsBase { |
| 101 | + constructor(parent: StatsBase, path: string, name: string, stats: Stat[]) { |
| 102 | + super(parent, path, name, stats); |
| 103 | + } |
| 104 | + |
| 105 | + addChild(_child: StatsBase): void { |
| 106 | + throw new Error('Cannot add child to file'); |
| 107 | + } |
| 108 | + |
| 109 | + mergeStats(_stats: Stat[]): void { |
| 110 | + throw new Error('Cannot merge stats to file'); |
| 111 | + } |
| 112 | + |
| 113 | + print(depth: number, lastChildArr: boolean[]): void { |
| 114 | + console.log(`${this.getPrefix(depth, lastChildArr)}${this.name} | ${this.stats[0].lines} lines`); |
| 115 | + } |
| 116 | + |
| 117 | + sort(): void {} |
| 118 | +} |
| 119 | + |
| 120 | +function collectStats() { |
| 121 | + const root = new FolderStats(undefined, './packages', 'packages'); |
| 122 | + const ignore = ['node_modules', 'extraTypes', 'bun.lockb']; |
| 123 | + |
| 124 | + const todo: FolderStats[] = [root]; |
| 125 | + |
| 126 | + while (todo.length > 0) { |
| 127 | + const current = todo.pop()!; |
| 128 | + const children = fs.readdirSync(current.path, { withFileTypes: true }); |
| 129 | + |
| 130 | + for (const child of children) { |
| 131 | + if (ignore.includes(child.name)) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if (child.isDirectory()) { |
| 136 | + const folder = new FolderStats(current, `${current.path}/${child.name}`, child.name); |
| 137 | + current.addChild(folder); |
| 138 | + |
| 139 | + todo.push(folder); |
| 140 | + } else { |
| 141 | + const content = fs.readFileSync(`${current.path}/${child.name}`, 'utf-8'); |
| 142 | + |
| 143 | + const file = new FileStats(current, `${current.path}/${child.name}`, child.name, [ |
| 144 | + { |
| 145 | + fileType: child.name.split('.').splice(1).join('.'), |
| 146 | + count: 1, |
| 147 | + lines: content.split('\n').length, |
| 148 | + }, |
| 149 | + ]); |
| 150 | + current.addChild(file); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + root.sort(); |
| 156 | + |
| 157 | + root.print(0, [true]); |
| 158 | +} |
| 159 | + |
| 160 | +collectStats(); |
0 commit comments