-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-unused-frontend.js
More file actions
287 lines (236 loc) · 8.29 KB
/
analyze-unused-frontend.js
File metadata and controls
287 lines (236 loc) · 8.29 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
#!/usr/bin/env node
/**
* Script para detectar arquivos TypeScript/JavaScript não utilizados no viewer.
* Analisa imports e referências para identificar arquivos órfãos.
*/
const fs = require('fs');
const path = require('path');
// Configurações
const VIEWER_DIR = path.join(__dirname, 'viewer');
const EXCLUDE_DIRS = new Set(['node_modules', 'dist', '.git']);
const ENTRY_POINTS = ['index.tsx', 'index.html'];
const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
/**
* Encontra todos os arquivos TS/JS no viewer
*/
function findSourceFiles(dir, files = []) {
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
if (!EXCLUDE_DIRS.has(item)) {
findSourceFiles(fullPath, files);
}
} else if (stat.isFile()) {
const ext = path.extname(item);
if (EXTENSIONS.includes(ext)) {
files.push(fullPath);
}
}
}
return files;
}
/**
* Extrai imports de um arquivo
*/
function extractImports(filePath) {
const imports = new Set();
try {
const content = fs.readFileSync(filePath, 'utf-8');
// Regex para imports ES6
const importRegex = /import\s+(?:{[^}]+}|[^\s]+|\*\s+as\s+\w+)\s+from\s+['"]([^'"]+)['"]/g;
let match;
while ((match = importRegex.exec(content)) !== null) {
imports.add(match[1]);
}
// Regex para require
const requireRegex = /require\(['"]([^'"]+)['"]\)/g;
while ((match = requireRegex.exec(content)) !== null) {
imports.add(match[1]);
}
// Regex para dynamic imports
const dynamicRegex = /import\(['"]([^'"]+)['"]\)/g;
while ((match = dynamicRegex.exec(content)) !== null) {
imports.add(match[1]);
}
} catch (error) {
console.log(` ⚠️ Erro ao ler ${path.basename(filePath)}: ${error.message}`);
}
return imports;
}
/**
* Resolve um import para um arquivo real
*/
function resolveImport(importPath, fromFile) {
// Ignorar imports de node_modules
if (!importPath.startsWith('.')) {
return null;
}
const dir = path.dirname(fromFile);
const resolved = path.resolve(dir, importPath);
// Tentar com extensões diferentes
for (const ext of EXTENSIONS) {
const withExt = resolved + ext;
if (fs.existsSync(withExt)) {
return withExt;
}
}
// Tentar como diretório com index
for (const ext of EXTENSIONS) {
const indexFile = path.join(resolved, `index${ext}`);
if (fs.existsSync(indexFile)) {
return indexFile;
}
}
return null;
}
/**
* Constrói grafo de dependências
*/
function buildDependencyGraph(files) {
const graph = new Map();
for (const file of files) {
const imports = extractImports(file);
const dependencies = new Set();
for (const imp of imports) {
const resolved = resolveImport(imp, file);
if (resolved) {
dependencies.add(resolved);
}
}
graph.set(file, dependencies);
}
return graph;
}
/**
* Encontra arquivos alcançáveis a partir dos entry points
*/
function findReachableFiles(entryPoints, graph) {
const reachable = new Set();
const toVisit = [...entryPoints];
while (toVisit.length > 0) {
const current = toVisit.pop();
if (reachable.has(current)) {
continue;
}
reachable.add(current);
const dependencies = graph.get(current);
if (dependencies) {
for (const dep of dependencies) {
if (!reachable.has(dep)) {
toVisit.push(dep);
}
}
}
}
return reachable;
}
/**
* Agrupa arquivos por diretório
*/
function groupByDirectory(files, baseDir) {
const groups = new Map();
for (const file of files) {
const relative = path.relative(baseDir, file);
const dir = path.dirname(relative);
const name = path.basename(relative);
if (!groups.has(dir)) {
groups.set(dir, []);
}
groups.get(dir).push(name);
}
return groups;
}
/**
* Função principal
*/
function main() {
console.log('🔍 Análise de Arquivos TypeScript/JavaScript Não Utilizados');
console.log('='.repeat(60));
console.log();
if (!fs.existsSync(VIEWER_DIR)) {
console.log('❌ Diretório viewer/ não encontrado!');
process.exit(1);
}
console.log('1️⃣ Encontrando arquivos TypeScript/JavaScript...');
const allFiles = findSourceFiles(VIEWER_DIR);
console.log(` Encontrados: ${allFiles.length} arquivos\n`);
console.log('2️⃣ Identificando entry points...');
const entryPoints = [];
for (const entry of ENTRY_POINTS) {
const entryPath = path.join(VIEWER_DIR, entry);
if (fs.existsSync(entryPath)) {
entryPoints.push(entryPath);
console.log(` - ${entry}`);
}
}
if (entryPoints.length === 0) {
console.log('\u274c Nenhum entry point encontrado!');
process.exit(1);
}
console.log();
console.log('3️⃣ Construindo grafo de dependências...');
const graph = buildDependencyGraph(allFiles);
console.log(` Arquivos no grafo: ${graph.size}\n`);
console.log('4️⃣ Encontrando arquivos alcançáveis...');
const reachable = findReachableFiles(entryPoints, graph);
console.log(` Arquivos alcançáveis: ${reachable.size}\n`);
console.log('5️⃣ Identificando arquivos NÃO utilizados...');
console.log();
const unused = allFiles.filter(file => !reachable.has(file));
if (unused.length > 0) {
console.log(`\u274c Encontrados ${unused.length} arquivos possivelmente não utilizados:\n`);
const grouped = groupByDirectory(unused, VIEWER_DIR);
const sortedDirs = Array.from(grouped.keys()).sort();
for (const dir of sortedDirs) {
console.log(` 📁 ${dir}/`);
const files = grouped.get(dir).sort();
for (const file of files) {
console.log(` - ${file}`);
}
console.log();
}
} else {
console.log('✅ Todos os arquivos parecem estar em uso!\n');
}
// Estatísticas
console.log('\n📊 Estatísticas:');
console.log('='.repeat(60));
console.log(`Total de arquivos TS/JS: ${allFiles.length}`);
console.log(`Entry points: ${entryPoints.length}`);
console.log(`Arquivos alcançáveis: ${reachable.size}`);
console.log(`Arquivos não utilizados: ${unused.length}`);
console.log(`Taxa de utilização: ${((allFiles.length - unused.length) / allFiles.length * 100).toFixed(1)}%`);
console.log();
// Avisos
console.log('⚠️ Notas Importantes:');
console.log(' - Esta análise é estática e pode ter falsos positivos');
console.log(' - Arquivos usados dinamicamente podem aparecer como não usados');
console.log(' - Arquivos de teste standalone podem não aparecer no grafo');
console.log(' - Sempre revise manualmente antes de deletar');
console.log();
// Detalhes extras
if (unused.length > 0) {
console.log('🔍 Arquivos para revisar manualmente:');
const suspects = unused.filter(file => {
const name = path.basename(file).toLowerCase();
return name.includes('antigo') ||
name.includes('old') ||
name.includes('backup') ||
name.includes('test') ||
name.includes('temp') ||
name.match(/-\d+\.(ts|tsx|js|jsx)$/);
});
if (suspects.length > 0) {
console.log('\nProváveis candidatos para remoção (nomes suspeitos):');
for (const file of suspects) {
console.log(` 🗑️ ${path.relative(VIEWER_DIR, file)}`);
}
}
}
console.log();
}
if (require.main === module) {
main();
}