forked from ServiceNowDevProgram/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount-files.js
More file actions
142 lines (119 loc) · 4.42 KB
/
count-files.js
File metadata and controls
142 lines (119 loc) · 4.42 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Folders to exclude from counting
const EXCLUDED_FOLDERS = new Set([
'.github',
'.git',
'node_modules',
'.vscode',
'.idea',
'assets'
]);
// Root files to exclude
const EXCLUDED_ROOT_FILES = new Set([
'README.md',
'CONTRIBUTING.md',
'CLAUDE.md',
'PAGES.md',
'LICENSE',
'.gitignore',
'package.json',
'package-lock.json',
'_config.yml',
'sitemap.xml',
'index.html',
'core-apis.html',
'server-side-components.html',
'client-side-components.html',
'modern-development.html',
'integration.html',
'specialized-areas.html',
'count-files.js'
]);
// File extensions to count
const COUNTED_EXTENSIONS = new Set([
'.js', '.ts', '.json', '.html', '.css', '.py', '.java', '.c', '.cpp',
'.cs', '.php', '.rb', '.go', '.rs', '.swift', '.kt', '.md', '.txt',
'.xml', '.sql', '.sh', '.bat', '.ps1', '.yml', '.yaml'
]);
function shouldCountFile(fileName, isRoot = false) {
// Exclude root-level config files
if (isRoot && EXCLUDED_ROOT_FILES.has(fileName)) {
return false;
}
// Check if file has a counted extension
const ext = path.extname(fileName).toLowerCase();
return COUNTED_EXTENSIONS.has(ext);
}
function countFilesRecursively(dirPath, isRoot = true) {
let count = 0;
let folderCounts = {};
try {
const items = fs.readdirSync(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
// Skip excluded folders
if (EXCLUDED_FOLDERS.has(item)) {
console.log(`Excluded folder: ${item}`);
continue;
}
// Recursively count files in subdirectory
const subCount = countFilesRecursively(itemPath, false);
count += subCount;
folderCounts[item] = subCount;
if (isRoot) {
console.log(`${item}: ${subCount} files`);
}
} else if (stat.isFile()) {
// Count relevant files
if (shouldCountFile(item, isRoot)) {
count++;
if (isRoot) {
console.log(`Root file counted: ${item}`);
}
}
}
}
} catch (error) {
console.error(`Error reading directory ${dirPath}:`, error.message);
}
return count;
}
// Count files starting from current directory
console.log('Counting files in repository...');
console.log('='.repeat(50));
const totalFiles = countFilesRecursively('.');
// Round to nearest 100 and add + for marketing display
const roundedFiles = Math.floor(totalFiles / 100) * 100;
const displayCount = `${roundedFiles}+`;
console.log('='.repeat(50));
console.log(`Total files counted: ${totalFiles}`);
console.log(`Rounded display count: ${displayCount}`);
// Update the index.html file with the rounded count
const indexPath = './index.html';
if (fs.existsSync(indexPath)) {
try {
let indexContent = fs.readFileSync(indexPath, 'utf8');
// Replace the estimated number with the rounded count
const oldPattern = /totalFiles = \d+;.*\/\/ Actual count from local files/g;
const newLine = `totalFiles = "${displayCount}"; // Rounded count from ${totalFiles} local files`;
// Also handle the old pattern if it exists
const oldPattern2 = /totalFiles = \d+;.*\/\/ Realistic estimate/g;
indexContent = indexContent.replace(oldPattern, newLine);
indexContent = indexContent.replace(oldPattern2, newLine);
// Update the textContent assignment to handle string instead of number
indexContent = indexContent.replace(
/snippetsElement\.textContent = totalFiles\.toLocaleString\(\);/g,
'snippetsElement.textContent = totalFiles;'
);
fs.writeFileSync(indexPath, indexContent);
console.log(`Updated index.html with rounded count: ${displayCount} (from actual ${totalFiles})`);
} catch (error) {
console.error('Error updating index.html:', error.message);
}
} else {
console.log('index.html not found - could not update automatically');
}