-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule-graph.ts
More file actions
533 lines (459 loc) · 17.5 KB
/
Copy pathmodule-graph.ts
File metadata and controls
533 lines (459 loc) · 17.5 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/**
* Module Graph — Import/export dependency graph using oxc-parser + oxc-resolver.
*
* Builds a bidirectional graph of import edges across all TypeScript source files
* in the project. Supports incremental updates via fs.watch.
*
* Graph stores absolute paths internally. Consumers convert to relative paths.
*/
import { parseSync } from "oxc-parser";
import { ResolverFactory } from "oxc-resolver";
import * as fs from "node:fs";
import * as path from "node:path";
// ─── Types ───────────────────────────────────────────────────────────────────
export interface ImportEdge {
target: string; // absolute resolved file path
specifiers: string[]; // ["UserService", "createUser"] or ["*"] for star re-exports
isTypeOnly: boolean; // import type { ... } — only reliable for imports, not re-exports
isDynamic: boolean; // import("./lazy")
}
export interface ModuleGraph {
forward: Map<string, ImportEdge[]>; // file → its imports
reverse: Map<string, ImportEdge[]>; // file → files that import it
files: Set<string>; // all known source files
}
export interface BuildGraphResult {
graph: ModuleGraph;
resolver: ResolverFactory;
}
// ─── Logging ─────────────────────────────────────────────────────────────────
const log = (...args: unknown[]) => console.error("[typegraph/graph]", ...args);
// ─── Constants ───────────────────────────────────────────────────────────────
const TS_EXTENSIONS = new Set([".ts", ".tsx", ".mts", ".cts"]);
const SKIP_DIRS = new Set([
"node_modules",
"dist",
"build",
"out",
".wrangler",
".mf",
".git",
".next",
".turbo",
"coverage",
]);
const SKIP_FILES = new Set(["routeTree.gen.ts"]);
// ─── File Discovery ──────────────────────────────────────────────────────────
function normalizeExcludedPaths(rootDir: string, excludedPaths: string[]): string[] {
return excludedPaths.map((excludedPath) =>
path.resolve(rootDir, excludedPath)
);
}
function isExcluded(filePath: string, excludedPaths: string[]): boolean {
return excludedPaths.some(
(excludedPath) =>
filePath === excludedPath || filePath.startsWith(excludedPath + path.sep)
);
}
export function discoverFiles(rootDir: string, excludedPaths: string[] = []): string[] {
const files: string[] = [];
const normalizedExclusions = normalizeExcludedPaths(rootDir, excludedPaths);
function walk(dir: string): void {
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
if (entry.isDirectory()) {
if (SKIP_DIRS.has(entry.name)) continue;
// Skip hidden directories (except the root)
if (entry.name.startsWith(".") && dir !== rootDir) continue;
const childDir = path.join(dir, entry.name);
if (isExcluded(childDir, normalizedExclusions)) continue;
walk(childDir);
} else if (entry.isFile()) {
const name = entry.name;
if (SKIP_FILES.has(name)) continue;
if (name.endsWith(".d.ts") || name.endsWith(".d.mts") || name.endsWith(".d.cts")) continue;
const ext = path.extname(name);
if (TS_EXTENSIONS.has(ext)) {
files.push(path.join(dir, name));
}
}
}
}
walk(rootDir);
return files;
}
// ─── Parsing ─────────────────────────────────────────────────────────────────
interface RawImport {
specifier: string; // module specifier (e.g. "./utils", "effect")
names: string[]; // imported names, or ["*"] for star
isTypeOnly: boolean;
isDynamic: boolean;
}
function parseFileImports(filePath: string, source: string): RawImport[] {
const result = parseSync(filePath, source);
const imports: RawImport[] = [];
// Static imports: import { X, Y } from "./foo"
for (const imp of result.module.staticImports) {
const specifier = imp.moduleRequest.value;
const names: string[] = [];
let allTypeOnly = true;
for (const entry of imp.entries) {
const kind = entry.importName.kind as string;
const name =
kind === "Default"
? "default"
: kind === "All" || kind === "AllButDefault" || kind === "NamespaceObject"
? "*"
: (entry.importName.name ?? entry.localName.value);
names.push(name);
if (!entry.isType) allTypeOnly = false;
}
// If no entries (e.g. `import "./side-effect"`), it's a side-effect import
if (names.length === 0) {
imports.push({ specifier, names: ["*"], isTypeOnly: false, isDynamic: false });
} else {
imports.push({ specifier, names, isTypeOnly: allTypeOnly, isDynamic: false });
}
}
// Static re-exports: export { X } from "./foo", export * from "./foo"
for (const exp of result.module.staticExports) {
for (const entry of exp.entries) {
// Only re-exports have moduleRequest on the entry
const moduleRequest = (entry as { moduleRequest?: { value: string } }).moduleRequest;
if (!moduleRequest) continue;
const specifier = moduleRequest.value;
const entryKind = entry.importName.kind as string;
const name =
entryKind === "AllButDefault" || entryKind === "All" || entryKind === "NamespaceObject"
? "*"
: (entry.importName.name ?? "*");
// Group by specifier — multiple entries from same module
const existing = imports.find((i) => i.specifier === specifier && !i.isDynamic);
if (existing) {
if (!existing.names.includes(name)) existing.names.push(name);
} else {
// oxc-parser doesn't expose isType on export entries, default false
imports.push({ specifier, names: [name], isTypeOnly: false, isDynamic: false });
}
}
}
// Dynamic imports: import("./lazy")
for (const di of result.module.dynamicImports) {
if (di.moduleRequest) {
const sliced = source.slice(di.moduleRequest.start, di.moduleRequest.end);
// Only include string literals (starts with ' or ")
if (sliced.startsWith("'") || sliced.startsWith('"')) {
const specifier = sliced.slice(1, -1); // strip quotes
imports.push({ specifier, names: ["*"], isTypeOnly: false, isDynamic: true });
}
}
}
return imports;
}
// ─── Resolution ──────────────────────────────────────────────────────────────
const SOURCE_EXTS = [".ts", ".tsx", ".mts", ".cts"];
/**
* Map a dist/ path back to its source .ts file.
*
* Monorepo pattern:
* packages: rootDir="src", outDir="dist" → dist/X.js → src/X.ts
* apps: rootDir=".", outDir="dist" → dist/X.js → X.ts
*/
function distToSource(resolvedPath: string, projectRoot: string): string {
// Only remap paths within the project that contain /dist/
if (!resolvedPath.startsWith(projectRoot)) return resolvedPath;
const rel = path.relative(projectRoot, resolvedPath);
const distIdx = rel.indexOf("dist" + path.sep);
if (distIdx === -1) return resolvedPath;
const prefix = rel.slice(0, distIdx); // e.g. "packages/core/" or "apps/gateway/"
const afterDist = rel.slice(distIdx + 5); // e.g. "index.js" or "schemas/index.js"
// Strip .js/.mjs/.cjs extension
const withoutExt = afterDist.replace(/\.(m?j|c)s$/, "");
// Strategy 1: packages pattern — dist/X → src/X
for (const ext of SOURCE_EXTS) {
const candidate = path.resolve(projectRoot, prefix, "src", withoutExt + ext);
if (fs.existsSync(candidate)) return candidate;
}
// Strategy 2: apps pattern — dist/X → X (rootDir is ".")
for (const ext of SOURCE_EXTS) {
const candidate = path.resolve(projectRoot, prefix, withoutExt + ext);
if (fs.existsSync(candidate)) return candidate;
}
// Strategy 3: index file — dist/schemas/index.js → src/schemas/index.ts
// Already covered by strategy 1, but try without /index
if (withoutExt.endsWith("/index")) {
const dirPath = withoutExt.slice(0, -6);
for (const ext of SOURCE_EXTS) {
const candidate = path.resolve(projectRoot, prefix, "src", dirPath + ext);
if (fs.existsSync(candidate)) return candidate;
}
}
return resolvedPath;
}
export function resolveProjectImport(
resolver: ResolverFactory,
fromDir: string,
specifier: string,
projectRoot: string
): string | null {
try {
const result = resolver.sync(fromDir, specifier);
if (result.path && !result.path.includes("node_modules")) {
const mapped = distToSource(result.path, projectRoot);
// Only include TypeScript source files
const ext = path.extname(mapped);
if (!TS_EXTENSIONS.has(ext)) return null;
// Exclude skipped files
if (SKIP_FILES.has(path.basename(mapped))) return null;
return mapped;
}
} catch {
// Resolution failure — external dep, Node builtin, etc.
}
return null;
}
export function createResolver(projectRoot: string, tsconfigPath: string): ResolverFactory {
const configFile = path.resolve(projectRoot, tsconfigPath);
return new ResolverFactory({
...(fs.existsSync(configFile)
? { tsconfig: { configFile, references: "auto" as const } }
: {}),
extensions: [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"],
extensionAlias: {
".js": [".ts", ".tsx", ".js"],
".jsx": [".tsx", ".jsx"],
".mjs": [".mts", ".mjs"],
".cjs": [".cts", ".cjs"],
},
conditionNames: ["import", "require"],
mainFields: ["module", "main"],
});
}
// ─── Graph Construction ──────────────────────────────────────────────────────
function buildForwardEdges(
files: string[],
resolver: ResolverFactory,
projectRoot: string
): { forward: Map<string, ImportEdge[]>; parseFailures: string[] } {
const forward = new Map<string, ImportEdge[]>();
const parseFailures: string[] = [];
for (const filePath of files) {
let source: string;
try {
source = fs.readFileSync(filePath, "utf-8");
} catch {
continue;
}
let rawImports: RawImport[];
try {
rawImports = parseFileImports(filePath, source);
} catch (err) {
parseFailures.push(filePath);
continue;
}
const edges: ImportEdge[] = [];
const fromDir = path.dirname(filePath);
for (const raw of rawImports) {
const target = resolveProjectImport(resolver, fromDir, raw.specifier, projectRoot);
if (target) {
edges.push({
target,
specifiers: raw.names,
isTypeOnly: raw.isTypeOnly,
isDynamic: raw.isDynamic,
});
}
}
forward.set(filePath, edges);
}
return { forward, parseFailures };
}
function buildReverseMap(forward: Map<string, ImportEdge[]>): Map<string, ImportEdge[]> {
const reverse = new Map<string, ImportEdge[]>();
for (const [source, edges] of forward) {
for (const edge of edges) {
let revEdges = reverse.get(edge.target);
if (!revEdges) {
revEdges = [];
reverse.set(edge.target, revEdges);
}
revEdges.push({
target: source, // reverse: the "target" is the file that imports
specifiers: edge.specifiers,
isTypeOnly: edge.isTypeOnly,
isDynamic: edge.isDynamic,
});
}
}
return reverse;
}
export async function buildGraph(
projectRoot: string,
tsconfigPath: string,
excludedPaths: string[] = []
): Promise<BuildGraphResult> {
const startTime = performance.now();
const resolver = createResolver(projectRoot, tsconfigPath);
const fileList = discoverFiles(projectRoot, excludedPaths);
log(`Discovered ${fileList.length} source files`);
const { forward, parseFailures } = buildForwardEdges(fileList, resolver, projectRoot);
const reverse = buildReverseMap(forward);
const files = new Set(fileList);
const edgeCount = [...forward.values()].reduce((sum, edges) => sum + edges.length, 0);
const elapsed = (performance.now() - startTime).toFixed(0);
log(`Graph built: ${files.size} files, ${edgeCount} edges [${elapsed}ms]`);
if (parseFailures.length > 0) {
log(`Parse failures: ${parseFailures.length} files`);
}
return {
graph: { forward, reverse, files },
resolver,
};
}
// ─── Incremental Updates ─────────────────────────────────────────────────────
export function updateFile(
graph: ModuleGraph,
filePath: string,
resolver: ResolverFactory,
projectRoot: string
): void {
// Remove old forward edges from reverse map
const oldEdges = graph.forward.get(filePath) ?? [];
for (const edge of oldEdges) {
const revEdges = graph.reverse.get(edge.target);
if (revEdges) {
const idx = revEdges.findIndex((r) => r.target === filePath);
if (idx !== -1) revEdges.splice(idx, 1);
if (revEdges.length === 0) graph.reverse.delete(edge.target);
}
}
// Re-parse file
let source: string;
try {
source = fs.readFileSync(filePath, "utf-8");
} catch {
// File unreadable — remove it
removeFile(graph, filePath);
return;
}
let rawImports: RawImport[];
try {
rawImports = parseFileImports(filePath, source);
} catch {
log(`Parse error on update: ${filePath}`);
graph.forward.set(filePath, []);
return;
}
// Build new edges
const fromDir = path.dirname(filePath);
const newEdges: ImportEdge[] = [];
for (const raw of rawImports) {
const target = resolveProjectImport(resolver, fromDir, raw.specifier, projectRoot);
if (target) {
newEdges.push({
target,
specifiers: raw.names,
isTypeOnly: raw.isTypeOnly,
isDynamic: raw.isDynamic,
});
}
}
// Update forward map
graph.forward.set(filePath, newEdges);
graph.files.add(filePath);
// Update reverse map
for (const edge of newEdges) {
let revEdges = graph.reverse.get(edge.target);
if (!revEdges) {
revEdges = [];
graph.reverse.set(edge.target, revEdges);
}
revEdges.push({
target: filePath,
specifiers: edge.specifiers,
isTypeOnly: edge.isTypeOnly,
isDynamic: edge.isDynamic,
});
}
}
export function removeFile(graph: ModuleGraph, filePath: string): void {
// Remove forward edges from reverse map
const edges = graph.forward.get(filePath) ?? [];
for (const edge of edges) {
const revEdges = graph.reverse.get(edge.target);
if (revEdges) {
const idx = revEdges.findIndex((r) => r.target === filePath);
if (idx !== -1) revEdges.splice(idx, 1);
if (revEdges.length === 0) graph.reverse.delete(edge.target);
}
}
// Remove reverse edges that point to this file
const revEdges = graph.reverse.get(filePath) ?? [];
for (const revEdge of revEdges) {
const fwdEdges = graph.forward.get(revEdge.target);
if (fwdEdges) {
const idx = fwdEdges.findIndex((e) => e.target === filePath);
if (idx !== -1) fwdEdges.splice(idx, 1);
}
}
graph.forward.delete(filePath);
graph.reverse.delete(filePath);
graph.files.delete(filePath);
}
// ─── File Watcher ────────────────────────────────────────────────────────────
export function startWatcher(
projectRoot: string,
graph: ModuleGraph,
resolver: ResolverFactory,
hooks?: {
onFileUpdated?: (filePath: string) => void | Promise<void>;
onFileDeleted?: (filePath: string) => void | Promise<void>;
},
excludedPaths: string[] = []
): void {
try {
const normalizedExclusions = normalizeExcludedPaths(projectRoot, excludedPaths);
const watcher = fs.watch(
projectRoot,
{ recursive: true },
(_eventType: string, filename: string | null) => {
if (!filename) return;
// Filter to TS files only
const ext = path.extname(filename);
if (!TS_EXTENSIONS.has(ext)) return;
// Skip excluded directories and files
const parts = filename.split(path.sep);
if (parts.some((p: string) => SKIP_DIRS.has(p))) return;
if (SKIP_FILES.has(path.basename(filename))) return;
if (
filename.endsWith(".d.ts") ||
filename.endsWith(".d.mts") ||
filename.endsWith(".d.cts")
)
return;
const absPath = path.resolve(projectRoot, filename);
if (isExcluded(absPath, normalizedExclusions)) return;
if (fs.existsSync(absPath)) {
// File created or modified
updateFile(graph, absPath, resolver, projectRoot);
void hooks?.onFileUpdated?.(absPath);
} else {
// File deleted
removeFile(graph, absPath);
void hooks?.onFileDeleted?.(absPath);
}
}
);
// Cleanup on process exit
process.on("SIGINT", () => watcher.close());
process.on("SIGTERM", () => watcher.close());
log("File watcher started");
} catch (err) {
log("Failed to start file watcher:", err);
}
}