forked from MythicalLTD/FeatherPanel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.js
More file actions
600 lines (518 loc) · 14.8 KB
/
count.js
File metadata and controls
600 lines (518 loc) · 14.8 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");
const { execSync } = require("child_process");
const readFileAsync = promisify(fs.readFile);
const DEFAULT_README_PATH = path.join(__dirname, ".github", "README.md");
const README_MARKER_START = "<!-- COUNT-STATS:START -->";
const README_MARKER_END = "<!-- COUNT-STATS:END -->";
const numberFormatter = new Intl.NumberFormat("en-US");
// File extensions to include
const VALID_EXTENSIONS = [
".vue",
".ts",
".tsx",
".php",
".css",
".yml",
".yaml",
".sql",
".cs",
".rs"
];
// Directories to exclude
const EXCLUDED_DIRS = [
"node_modules",
"vendor",
"cache",
".cache",
"packages",
"dist",
"assets",
".vite",
".vite-cache",
"addons",
".next",
"out",
"build",
"target"
];
// Process arguments
const targetDir = process.argv[2] || process.cwd();
// Regular expressions for detecting comments
const COMMENT_PATTERNS = {
".ts": [/\/\/.*$/m, /\/\*[\s\S]*?\*\//g],
".tsx": [/\/\/.*$/m, /\/\*[\s\S]*?\*\//g],
".vue": [/<!--[\s\S]*?-->/g, /\/\/.*$/m, /\/\*[\s\S]*?\*\//g],
".php": [/\/\/.*$/m, /\/\*[\s\S]*?\*\//g, /#.*$/m],
".css": [/\/\*[\s\S]*?\*\//g],
".gitignore": [/#.*$/m],
};
// Format number to human readable format (e.g. 1000 -> 1k)
function formatNumber(num) {
if (num >= 1000000) {
return (num / 1000000).toFixed(1) + "M";
}
if (num >= 1000) {
return (num / 1000).toFixed(1) + "k";
}
return num.toString();
}
function formatInteger(value) {
return numberFormatter.format(value);
}
function printUsage() {
console.log(
[
"Usage: node count.js [targetDir] [options]",
"",
"Options:",
" --update-readme Update the default README with the latest counts (default).",
" --readme <path> Update the specified README file.",
" --no-readme Do not update a README file (overrides other options).",
" --use-git Only count files tracked by git (default).",
" --no-git Don't use git, traverse filesystem instead.",
" --use-gitignore Use .gitignore patterns when not using git.",
" --no-gitignore Don't use .gitignore patterns.",
" -h, --help Show this help message.",
].join("\n")
);
}
function parseArguments(argv) {
const args = [...argv];
let targetDir = process.cwd();
let readmePath = DEFAULT_README_PATH; // Default to updating README
let useGit = true; // Default to using git
let useGitignore = false; // Git handles ignore patterns
if (args.length > 0 && !args[0].startsWith("-")) {
targetDir = path.resolve(args.shift());
} else {
targetDir = path.resolve(targetDir);
}
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
switch (arg) {
case "--readme": {
const providedPath = args[index + 1];
if (!providedPath || providedPath.startsWith("-")) {
throw new Error("Missing path after --readme option.");
}
readmePath = path.resolve(providedPath);
index += 1;
break;
}
case "--update-readme":
if (!readmePath) {
readmePath = DEFAULT_README_PATH;
}
break;
case "--no-readme":
readmePath = null;
break;
case "--use-git":
useGit = true;
break;
case "--no-git":
useGit = false;
break;
case "--no-gitignore":
useGitignore = false;
break;
case "--use-gitignore":
useGitignore = true;
break;
case "--help":
case "-h":
printUsage();
process.exit(0);
default:
throw new Error(`Unknown argument: ${arg}`);
}
}
return {
targetDir,
readmePath,
shouldUpdateReadme: Boolean(readmePath),
useGit,
useGitignore,
};
}
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function buildReadmeTable(sortedResults, totalFiles, totalLines) {
const header = "| Extension | Files | Lines |\n| --- | ---: | ---: |";
const body =
sortedResults.length > 0
? sortedResults
.map(([ext, { files, lines }]) => {
return `| \`${ext}\` | ${formatInteger(files)} | ${formatInteger(
lines
)} |`;
})
.join("\n")
: "| _No matching files_ | 0 | 0 |";
const totalRow = `| **Total** | ${formatInteger(
totalFiles
)} | ${formatInteger(totalLines)} |`;
return [header, body, totalRow].join("\n");
}
function buildReadmeSection(table, timestampIso) {
return [
README_MARKER_START,
"",
`_Last updated: ${timestampIso}_`,
"",
table,
"",
README_MARKER_END,
].join("\n");
}
function updateReadmeWithResults(
readmePath,
sortedResults,
totalFiles,
totalLines,
timestampIso
) {
if (!fs.existsSync(readmePath)) {
console.warn(
`README file not found at ${readmePath}. Skipping README update.`
);
return false;
}
const readmeContent = fs.readFileSync(readmePath, "utf8");
if (
!readmeContent.includes(README_MARKER_START) ||
!readmeContent.includes(README_MARKER_END)
) {
console.warn(
`README markers not found in ${readmePath}. Add "${README_MARKER_START}" and "${README_MARKER_END}" to enable automatic updates.`
);
return false;
}
const sectionPattern = new RegExp(
`${escapeRegExp(README_MARKER_START)}[\\s\\S]*?${escapeRegExp(
README_MARKER_END
)}`
);
const table = buildReadmeTable(sortedResults, totalFiles, totalLines);
const newSection = buildReadmeSection(table, timestampIso);
const updatedContent = readmeContent.replace(sectionPattern, newSection);
fs.writeFileSync(readmePath, updatedContent);
return true;
}
// Parse .gitignore patterns
function parseGitignore(gitignorePath, baseDir) {
if (!fs.existsSync(gitignorePath)) {
return [];
}
const content = fs.readFileSync(gitignorePath, "utf8");
const patterns = [];
const lines = content.split("\n");
for (const line of lines) {
const trimmed = line.trim();
// Skip empty lines and comments
if (!trimmed || trimmed.startsWith("#")) {
continue;
}
// Convert pattern to regex
let pattern = trimmed;
const isNegation = pattern.startsWith("!");
if (isNegation) {
pattern = pattern.substring(1);
}
// Handle directory patterns (ending with /)
const isDirectory = pattern.endsWith("/");
if (isDirectory) {
pattern = pattern.slice(0, -1);
}
// Convert glob patterns to regex
let regexPattern = pattern
.replace(/\./g, "\\.")
.replace(/\*\*/g, ".*")
.replace(/\*/g, "[^/]*")
.replace(/\?/g, ".");
// If pattern doesn't start with /, it matches anywhere
if (!pattern.startsWith("/")) {
regexPattern = `.*${regexPattern}`;
} else {
regexPattern = regexPattern.substring(1);
}
// Add directory match if needed
if (isDirectory) {
regexPattern = `${regexPattern}.*`;
}
patterns.push({
pattern: new RegExp(`^${regexPattern}`),
isNegation,
baseDir,
});
}
return patterns;
}
// Collect all .gitignore files
function collectGitignoreFiles(dir, gitignoreFiles = [], baseDir = null) {
if (!baseDir) {
baseDir = dir;
}
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
const gitignorePath = path.join(dir, ".gitignore");
if (fs.existsSync(gitignorePath)) {
gitignoreFiles.push(gitignorePath);
}
for (const entry of entries) {
if (entry.isDirectory()) {
const fullPath = path.join(dir, entry.name);
// Skip excluded directories
if (
EXCLUDED_DIRS.some((excluded) =>
entry.name.toLowerCase().includes(excluded.toLowerCase())
)
) {
continue;
}
collectGitignoreFiles(fullPath, gitignoreFiles, baseDir);
}
}
} catch (error) {
// Ignore errors (e.g., permission denied)
}
return gitignoreFiles;
}
// Check if a path should be ignored based on .gitignore patterns
function shouldIgnore(filePath, gitignorePatterns, baseDir) {
const relativePath = path.relative(baseDir, filePath).replace(/\\/g, "/");
// Check patterns in order (later patterns override earlier ones)
let ignored = false;
for (const { pattern, isNegation } of gitignorePatterns) {
if (pattern.test(relativePath) || pattern.test(`/${relativePath}`)) {
if (isNegation) {
ignored = false;
} else {
ignored = true;
}
}
}
return ignored;
}
// Get git-tracked files
function getGitTrackedFiles(targetDir) {
try {
const output = execSync("git ls-files", {
cwd: targetDir,
encoding: "utf8",
});
return new Set(
output
.split("\n")
.filter((line) => line.trim())
.map((line) => path.resolve(targetDir, line))
);
} catch (error) {
return null;
}
}
async function countLinesInFile(filePath) {
try {
let content = await readFileAsync(filePath, "utf8");
const ext = path.extname(filePath).toLowerCase();
// Remove comments based on file extension
if (COMMENT_PATTERNS[ext]) {
COMMENT_PATTERNS[ext].forEach((pattern) => {
content = content.replace(pattern, "");
});
}
const lines = content.split("\n");
const nonEmptyLines = lines.filter((line) => line.trim().length > 0);
return nonEmptyLines.length;
} catch (error) {
console.error(`Error reading file ${filePath}: ${error.message}`);
return 0;
}
}
async function traverseDirectory(
dir,
results = {},
gitignorePatterns = [],
baseDir = null,
gitTrackedFiles = null
) {
if (!baseDir) {
baseDir = dir;
}
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
// Skip excluded directories
if (
EXCLUDED_DIRS.some((excluded) =>
entry.name.toLowerCase().includes(excluded.toLowerCase())
)
) {
continue;
}
// Check if directory should be ignored
if (
gitignorePatterns.length > 0 &&
shouldIgnore(fullPath, gitignorePatterns, baseDir)
) {
continue;
}
await traverseDirectory(
fullPath,
results,
gitignorePatterns,
baseDir,
gitTrackedFiles
);
} else if (entry.isFile()) {
// Skip if using git and file is not tracked
if (gitTrackedFiles !== null && !gitTrackedFiles.has(fullPath)) {
continue;
}
// Check if file should be ignored
if (
gitignorePatterns.length > 0 &&
shouldIgnore(fullPath, gitignorePatterns, baseDir)
) {
continue;
}
const ext = path.extname(entry.name).toLowerCase();
if (VALID_EXTENSIONS.includes(ext)) {
const lineCount = await countLinesInFile(fullPath);
if (!results[ext]) {
results[ext] = { files: 0, lines: 0 };
}
results[ext].files++;
results[ext].lines += lineCount;
}
}
}
} catch (error) {
console.error(`Error traversing directory ${dir}: ${error.message}`);
}
return results;
}
async function main() {
let parsedArgs;
try {
parsedArgs = parseArguments(process.argv.slice(2));
} catch (error) {
console.error(error.message);
process.exit(1);
}
const {
targetDir,
readmePath,
shouldUpdateReadme,
useGit,
useGitignore,
} = parsedArgs;
let countings = "";
countings += `Counting non-empty lines of code in ${targetDir}... \n`;
countings += `Including extensions: ${VALID_EXTENSIONS.join(", ")} \n`;
countings += `Excluding directories: ${EXCLUDED_DIRS.join(", ")} \n`;
countings += "Excluding also comments and empty lines \n";
// Collect gitignore patterns
let gitignorePatterns = [];
let gitTrackedFiles = null;
if (useGit) {
countings += "Using git-tracked files only \n";
gitTrackedFiles = getGitTrackedFiles(targetDir);
if (!gitTrackedFiles) {
console.warn("Warning: Not a git repository or git not available. Falling back to file system traversal.");
}
} else if (useGitignore) {
countings += "Respecting .gitignore patterns \n";
const gitignoreFiles = collectGitignoreFiles(targetDir);
for (const gitignoreFile of gitignoreFiles) {
const gitignoreDir = path.dirname(gitignoreFile);
const patterns = parseGitignore(gitignoreFile, targetDir);
gitignorePatterns.push(...patterns);
}
}
const results = await traverseDirectory(
targetDir,
{},
gitignorePatterns,
targetDir,
gitTrackedFiles
);
countings += "\nResults:\n";
countings += "-".repeat(50) + "\n";
let totalFiles = 0;
let totalLines = 0;
// Sort results by line count in descending order
const sortedResults = Object.entries(results).sort(
(a, b) => b[1].lines - a[1].lines
);
sortedResults.forEach(([ext, { files, lines }]) => {
countings += `${ext.padEnd(6)} | ${formatNumber(files).padStart(
6
)} files | ${formatNumber(lines).padStart(8)} lines \n`;
totalFiles += files;
totalLines += lines;
});
countings += "-".repeat(50) + "\n";
countings += `Total | ${formatNumber(totalFiles).padStart(
6
)} files | ${formatNumber(totalLines).padStart(8)} lines \n`;
const timestampIso = new Date().toISOString();
if (shouldUpdateReadme) {
const readmeUpdated = updateReadmeWithResults(
readmePath,
sortedResults,
totalFiles,
totalLines,
timestampIso
);
if (readmeUpdated) {
countings += `README updated at ${readmePath} \n`;
} else {
countings += `README update skipped for ${readmePath} \n`;
}
}
// Store results in a JSON file
const resultsWithTotal = {
total: {
files: totalFiles,
lines: totalLines,
},
};
// Add sorted results to JSON
sortedResults.forEach(([ext, data]) => {
resultsWithTotal[ext] = data;
});
const resultsDir = path.join(process.cwd(), "count-results");
if (!fs.existsSync(resultsDir)) {
fs.mkdirSync(resultsDir);
}
const safeTimestamp = timestampIso.replace(/[:.]/g, "-");
const resultsFile = path.join(
resultsDir,
`count-results-${safeTimestamp}.json`
);
const resultsFileRaw = path.join(
resultsDir,
`count-results-${safeTimestamp}.txt`
);
fs.writeFileSync(resultsFile, JSON.stringify(resultsWithTotal, null, 2));
fs.writeFileSync(resultsFileRaw, countings);
console.log(
countings +
"\n\n" +
`Results saved to: \n` +
resultsFile +
"\n" +
resultsFileRaw
);
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});