generated from mProjectsCode/lemons-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstats.ts
160 lines (126 loc) · 3.67 KB
/
stats.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import * as fs from 'fs';
interface Stat {
fileType: string;
count: number;
lines: number;
}
abstract class StatsBase {
parent: StatsBase | undefined;
path: string;
name: string;
stats: Stat[];
constructor(parent: StatsBase | undefined, path: string, name: string, stats: Stat[]) {
this.parent = parent;
this.path = path;
this.name = name;
this.stats = stats;
}
abstract addChild(child: StatsBase): void;
abstract mergeStats(stats: Stat[]): void;
abstract print(depth: number, lastChildArr: boolean[]): void;
abstract sort(): void;
getPrefix(depth: number, lastChildArr: boolean[]): string {
let prefix = '';
for (let i = 0; i < depth; i++) {
prefix += lastChildArr[i] ? ' ' : '│ ';
}
if (lastChildArr.at(-1)) {
prefix += '└─ ';
} else {
prefix += '├─ ';
}
return prefix;
}
}
class FolderStats extends StatsBase {
children: StatsBase[];
constructor(parent: StatsBase | undefined, path: string, name: string) {
super(parent, path, name, []);
this.children = [];
}
addChild(child: StatsBase) {
this.children.push(child);
this.mergeStats(child.stats);
}
mergeStats(stats: Stat[]): void {
// console.log(this, stats);
for (const stat of stats) {
const existingStat = this.stats.find(s => s.fileType === stat.fileType);
if (existingStat) {
existingStat.count += stat.count;
existingStat.lines += stat.lines;
} else {
this.stats.push(structuredClone(stat));
}
}
this.parent?.mergeStats(stats);
}
print(depth: number, lastChildArr: boolean[]): void {
console.log(
`${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`,
);
for (let i = 0; i < this.children.length; i++) {
const child = this.children[i];
child.print(depth + 1, [...lastChildArr, i === this.children.length - 1]);
}
}
sort(): void {
this.children.sort((a, b) => {
if (a instanceof FolderStats && b instanceof FileStats) {
return 1;
} else if (a instanceof FileStats && b instanceof FolderStats) {
return -1;
} else {
return a.name.localeCompare(b.name);
}
});
this.children.forEach(c => c.sort());
}
}
class FileStats extends StatsBase {
constructor(parent: StatsBase, path: string, name: string, stats: Stat[]) {
super(parent, path, name, stats);
}
addChild(_child: StatsBase): void {
throw new Error('Cannot add child to file');
}
mergeStats(_stats: Stat[]): void {
throw new Error('Cannot merge stats to file');
}
print(depth: number, lastChildArr: boolean[]): void {
console.log(`${this.getPrefix(depth, lastChildArr)}${this.name} | ${this.stats[0].lines} lines`);
}
sort(): void {}
}
function collectStats() {
const root = new FolderStats(undefined, './src', 'src');
const ignore = ['node_modules', 'extraTypes', 'bun.lockb'];
const todo: FolderStats[] = [root];
while (todo.length > 0) {
const current = todo.pop()!;
const children = fs.readdirSync(current.path, { withFileTypes: true });
for (const child of children) {
if (ignore.includes(child.name)) {
continue;
}
if (child.isDirectory()) {
const folder = new FolderStats(current, `${current.path}/${child.name}`, child.name);
current.addChild(folder);
todo.push(folder);
} else {
const content = fs.readFileSync(`${current.path}/${child.name}`, 'utf-8');
const file = new FileStats(current, `${current.path}/${child.name}`, child.name, [
{
fileType: child.name.split('.').splice(1).join('.'),
count: 1,
lines: content.split('\n').length,
},
]);
current.addChild(file);
}
}
}
root.sort();
root.print(0, [true]);
}
collectStats();