Repository navigation
Expand file tree
/
Copy pathbuild-entries.mjs
More file actions
96 lines (85 loc) · 3.54 KB
/
Copy pathbuild-entries.mjs
File metadata and controls
96 lines (85 loc) · 3.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
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
#!/usr/bin/env node
// Emits entries.json for the depgraph viewer: the declared entry point of every
// workspace package subpath, as the source file ids the graph already knows.
//
// node tools/depgraph-view/build-entries.mjs [--graph <graph.json>] [--out <entries.json>]
//
// Root entry sources come from tsdown's entry map.
import fs from 'node:fs';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { pathToFileURL } from 'node:url';
const repoRoot = execFileSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' }).trim();
const argv = process.argv.slice(2);
const flag = (name, fallback) => {
const i = argv.indexOf(name);
return i >= 0 && argv[i + 1] ? path.resolve(argv[i + 1]) : fallback;
};
const graphPath = flag('--graph', path.join(repoRoot, 'tools/depgraph-view/graph.json'));
const outPath = flag('--out', path.join(repoRoot, 'tools/depgraph-view/entries.json'));
const graph = JSON.parse(fs.readFileSync(graphPath, 'utf8'));
const ids = new Set(graph.nodes.map((n) => n.id));
const targetsOf = (value) =>
[value]
.flat()
.flatMap((v) => {
const d = v && typeof v === 'object' ? (v.default ?? v.import ?? v.require) : v;
return [d];
})
.filter((d) => typeof d === 'string');
function packageEntries(exports, probe) {
return Object.entries(exports).flatMap(([subpath, value]) =>
targetsOf(value).flatMap((target) => {
if (!target.startsWith('./')) return [];
const id = probe(target).find((candidate) => ids.has(candidate));
return id ? [{ id, subpath }] : [];
}),
);
}
const normalizeExports = (exports) =>
typeof exports === 'string' ? { '.': exports } : (exports ?? {});
const packages = {};
const rootPkg = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
const rootExports = normalizeExports(rootPkg.exports);
const buildConfig = await import(pathToFileURL(path.join(repoRoot, 'tsdown.config.ts')).href);
const root = packageEntries(rootExports, (target) => {
const name = target.replace('./dist/src/', '').replace(/\.js$/, '');
const source = buildConfig.default.entry[name];
return source ? [source.replace(/^src\//, '')] : [];
});
if (root.length) packages['src (agent-device)'] = root;
const packageDirs = new Set();
for (const node of graph.nodes) {
const m = /^(packages\/[^/]+)\//.exec(node.id);
if (m) packageDirs.add(m[1]);
}
for (const pkgDir of [...packageDirs].sort()) {
const manifest = JSON.parse(fs.readFileSync(path.join(repoRoot, pkgDir, 'package.json'), 'utf8'));
const exports = normalizeExports(manifest.exports);
const entries = packageEntries(exports, (target) => [
`${pkgDir}/${target.slice(2)}`,
`${pkgDir}/${target.slice(2).replace(/\.js$/, '.ts')}`,
]);
if (entries.length) packages[pkgDir] = entries;
}
const declared = Object.values(packages).reduce((sum, list) => sum + list.length, 0);
const expected =
Object.keys(rootExports).length +
[...packageDirs].reduce(
(sum, dir) =>
sum +
Object.keys(
normalizeExports(
JSON.parse(fs.readFileSync(path.join(repoRoot, dir, 'package.json'), 'utf8')).exports,
),
).length,
0,
);
fs.writeFileSync(outPath, `${JSON.stringify(packages, null, 2)}\n`);
process.stdout.write(
`entries: ${declared} of ${expected} declared subpaths across ${Object.keys(packages).length} packages\n` +
(declared === expected
? ' every declared subpath resolved to a graph node\n'
: ' some subpaths had no graph node (excluded or renamed)\n') +
` wrote ${path.relative(repoRoot, outPath)}\n`,
);