|
| 1 | +import { buildResolver } from 'esm-resolve'; |
| 2 | +import { LoadResult, loadAndMaybeTransform, parse } from '../lib/load.ts'; |
| 3 | +import { aggregateImports } from '../../lib/internal/analyze/module.ts'; |
| 4 | +import * as path from 'node:path'; |
| 5 | +import { isLocalImport, relativize } from '../../lib/helper.ts'; |
| 6 | + |
| 7 | +export type ValidArgs = { |
| 8 | + paths: string[]; |
| 9 | +}; |
| 10 | + |
| 11 | +type FoundInfo = { |
| 12 | + importer: string[]; |
| 13 | + tags?: { local?: string[]; nested?: string[] }; |
| 14 | + found?: boolean; |
| 15 | +}; |
| 16 | + |
| 17 | +const matchTag = /@kuto-(\w+)/g; |
| 18 | + |
| 19 | +export default async function cmdGraph(args: ValidArgs) { |
| 20 | + const pending = new Set<string>(); |
| 21 | + const mod: Record<string, FoundInfo> = {}; |
| 22 | + |
| 23 | + for (const raw of args.paths) { |
| 24 | + const entrypoint = relativize(raw); |
| 25 | + pending.add(entrypoint); |
| 26 | + mod[entrypoint] = { importer: [] }; |
| 27 | + } |
| 28 | + |
| 29 | + for (const p of pending) { |
| 30 | + pending.delete(p); |
| 31 | + if (!isLocalImport(p)) { |
| 32 | + continue; // rn we ignore "foo" |
| 33 | + } |
| 34 | + |
| 35 | + const info = mod[p]!; |
| 36 | + |
| 37 | + let x: LoadResult; |
| 38 | + try { |
| 39 | + x = await loadAndMaybeTransform(p); |
| 40 | + } catch (e) { |
| 41 | + info.found = false; |
| 42 | + continue; |
| 43 | + } |
| 44 | + info.found = true; |
| 45 | + const prog = parse(x.source, (comment) => { |
| 46 | + comment.replaceAll(matchTag, (_, tag) => { |
| 47 | + info.tags ??= {}; |
| 48 | + info.tags.local ??= []; |
| 49 | + if (!info.tags.local.includes(tag)) { |
| 50 | + info.tags.local.push(tag); |
| 51 | + } |
| 52 | + return ''; |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + // -- resolve additional imports |
| 57 | + |
| 58 | + const resolver = buildResolver(p, { |
| 59 | + allowImportingExtraExtensions: true, |
| 60 | + resolveToAbsolute: true, |
| 61 | + }); |
| 62 | + |
| 63 | + const imports = aggregateImports(prog); |
| 64 | + |
| 65 | + const resolved: string[] = []; |
| 66 | + for (const source of imports.mod.importSources()) { |
| 67 | + let key = source.name; |
| 68 | + |
| 69 | + if (isLocalImport(source.name)) { |
| 70 | + const r = resolver(source.name); |
| 71 | + if (!r) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + key = relativize(path.relative(process.cwd(), r)); |
| 75 | + resolved.push(key); |
| 76 | + } |
| 77 | + |
| 78 | + // create graph to thingo |
| 79 | + const prev = mod[key]; |
| 80 | + if (prev !== undefined) { |
| 81 | + prev.importer.push(p); |
| 82 | + } else { |
| 83 | + mod[key] = { importer: [p] }; |
| 84 | + pending.add(key); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // descend tags |
| 90 | + |
| 91 | + const expandTree = (key: string) => { |
| 92 | + const all = new Set<string>(); |
| 93 | + const pending = [key]; |
| 94 | + |
| 95 | + while (pending.length) { |
| 96 | + const next = pending.pop()!; |
| 97 | + all.add(next); |
| 98 | + |
| 99 | + for (const importer of mod[next].importer) { |
| 100 | + if (all.has(importer)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + pending.push(importer); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + all.delete(key); |
| 108 | + return [...all]; |
| 109 | + }; |
| 110 | + |
| 111 | + for (const key of Object.keys(mod)) { |
| 112 | + const o = mod[key]; |
| 113 | + const tree = expandTree(key); |
| 114 | + |
| 115 | + for (const localTag of o.tags?.local ?? []) { |
| 116 | + for (const okey of tree) { |
| 117 | + const o = mod[okey]; |
| 118 | + o.tags ??= {}; |
| 119 | + o.tags.nested ??= []; |
| 120 | + if (!o.tags.nested.includes(localTag)) { |
| 121 | + o.tags.nested.push(localTag); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + const out = { |
| 128 | + mod, |
| 129 | + }; |
| 130 | + console.info(JSON.stringify(out, undefined, 2)); |
| 131 | +} |
0 commit comments