forked from chatgptprojects/clear-code
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind-missing-packages.mjs
More file actions
48 lines (44 loc) · 1.54 KB
/
find-missing-packages.mjs
File metadata and controls
48 lines (44 loc) · 1.54 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
// Finds all bare specifier imports in dist/ that can't be resolved from node_modules
import { readFileSync, existsSync, readdirSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const DIST_SRC = join(__dirname, 'dist', 'src');
const NODE_MODULES = join(__dirname, 'node_modules');
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 bare specifier imports (not starting with . or /)
const re = /from\s+["']([^."'/][^"']*)["']/g;
let m;
while ((m = re.exec(code)) !== null) {
const spec = m[1];
// Skip node: builtins
if (spec.startsWith('node:')) continue;
// Get package name (handle scoped packages)
let pkgName;
if (spec.startsWith('@')) {
const parts = spec.split('/');
pkgName = parts[0] + '/' + parts[1];
} else {
pkgName = spec.split('/')[0];
}
// Check if it exists in node_modules
const pkgDir = join(NODE_MODULES, pkgName);
if (!existsSync(pkgDir)) {
missing.add(pkgName);
}
}
}
const sorted = [...missing].sort();
console.log('Missing packages: ' + sorted.length);
sorted.forEach(p => console.log(p));