Repository navigation
Expand file tree
/
Copy pathentries.ts
More file actions
34 lines (32 loc) · 1.55 KB
/
Copy pathentries.ts
File metadata and controls
34 lines (32 loc) · 1.55 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
import fs from 'node:fs';
import path from 'node:path';
import { buildEntries } from '../build-entries.ts';
import { readWorkspacePackages } from '../layering/package-boundaries.ts';
export type EntryManifest = Record<string, { id: string; subpath: string }[]>;
export function graphEntries(repoRoot: string, ids: ReadonlySet<string>): EntryManifest {
const entries: EntryManifest = {};
const add = (pkg: string, id: string, subpath: string) => {
const source = ids.has(id) ? id : id.replace(/\.js$/, '.ts');
if (ids.has(source)) (entries[pkg] ??= []).push({ id: source.replace(/^src\//, ''), subpath });
};
const root = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8')) as {
exports:
| string
| Record<string, { default?: string; import?: string; require?: string } | string>;
};
const rootExports = typeof root.exports === 'string' ? { '.': root.exports } : root.exports;
for (const [subpath, value] of Object.entries(rootExports)) {
const target =
typeof value === 'string' ? value : (value.default ?? value.import ?? value.require);
if (!target) continue;
const name = target.replace(/^\.\/dist\/src\//, '').replace(/\.js$/, '');
const source = (buildEntries as Record<string, string>)[name];
if (source) add('src (agent-device)', source, subpath);
}
for (const pkg of readWorkspacePackages(repoRoot)) {
for (const [specifier, target] of pkg.exportTargets) {
add(pkg.dir, target, specifier === pkg.name ? '.' : `.${specifier.slice(pkg.name.length)}`);
}
}
return entries;
}