forked from chatgptprojects/clear-code
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind-missing-imports.mjs
More file actions
35 lines (31 loc) · 1.22 KB
/
find-missing-imports.mjs
File metadata and controls
35 lines (31 loc) · 1.22 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
// Finds all relative imports in dist/ that point to non-existent files
import { readFileSync, existsSync, readdirSync } from 'fs';
import { join, dirname, resolve, relative } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const DIST_SRC = join(__dirname, 'dist', 'src');
function walkDir(dir) {
const results = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory()) walkDir(full).forEach(f => results.push(f));
else if (entry.name.endsWith('.js')) results.push(full);
}
return results;
}
const missing = new Set();
for (const f of walkDir(DIST_SRC)) {
const code = readFileSync(f, 'utf-8');
// Match: from "./foo.js" or from '../bar.js'
const re = /from\s+["'](\.[^"']+)["']/g;
let m;
while ((m = re.exec(code)) !== null) {
const resolved = resolve(dirname(f), m[1]);
if (!existsSync(resolved) && !existsSync(resolved + '.js') && !existsSync(resolved + '/index.js')) {
missing.add(resolved);
}
}
}
const sorted = [...missing].sort();
console.log(`Missing files: ${sorted.length}`);
sorted.forEach(f => console.log(relative(join(__dirname, 'dist'), f)));