The best way to control Claude Code, Codex and more from any computer or phone

main
ts 74 lines 2.86 KB
Raw
1 import { realpathSync } from "node:fs";
2 import path from "node:path";
3 import { DependencyWatcher, type DependencyWatcherOptions } from "./dependency-watcher.ts";
4
5 export interface BunImportTrackerOptions extends DependencyWatcherOptions {
6 /**
7 * Directory whose modules belong to the tracked graph. Files outside it and
8 * anything under a `node_modules` directory load untouched.
9 */
10 readonly root: string;
11 }
12
13 const loaders: Record<string, "js" | "jsx" | "ts" | "tsx"> = {
14 ".js": "js",
15 ".mjs": "js",
16 ".cjs": "js",
17 ".jsx": "jsx",
18 ".ts": "ts",
19 ".mts": "ts",
20 ".cts": "ts",
21 ".tsx": "tsx",
22 };
23
24 const escapeRegExp = (value: string) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
25
26 /**
27 * Records every project-local module Bun loads after registration and watches
28 * those files for changes.
29 *
30 * Bun has no loader hooks that can evict or re-namespace an evaluated module,
31 * so unlike Node's `ImportWatcher` (./import-watcher.ts) this cannot import
32 * a fresh generation in-process. A runtime `Bun.plugin` `onLoad` hook is used purely
33 * as a dependency probe: it hands the source back unchanged with the loader
34 * Bun would have picked itself. It stays registered after `close()`, only
35 * echoing sources. Callers react to a change by exiting so a
36 * supervisor can start a fresh process.
37 */
38 export class BunImportTracker extends DependencyWatcher {
39 readonly #loaded = new Set<string>();
40
41 constructor(options: BunImportTrackerOptions) {
42 if (process.versions.bun === undefined) {
43 throw new Error("BunImportTracker requires Bun; Node callers should use watchImport.");
44 }
45 super(options);
46 // Bun reports real paths (`/private/tmp/...` for `/tmp/...` on macOS);
47 // match them against the root's real path too.
48 const root = realpathSync.native(path.resolve(options.root)) + path.sep;
49 const nodeModules = `node_modules${path.sep}`;
50 // `root` already ends in a separator, so the segment right after it has no
51 // leading one: a `.*${sep}node_modules${sep}` lookahead alone would miss
52 // `<root>node_modules/...` and intercept every dependency. Bun cannot
53 // return CommonJS source from `onLoad` (oven-sh/bun#19279), so a CJS
54 // dependency that reaches this probe loses its exports.
55 const filter = new RegExp(
56 `^${escapeRegExp(root)}(?!(?:.*${escapeRegExp(path.sep)})?${escapeRegExp(nodeModules)}).*\\.[cm]?[jt]sx?$`,
57 );
58 Bun.plugin({
59 name: "@alchemy.run/node-utils/watch-import-bun",
60 setup: (build) => {
61 build.onLoad({ filter }, async (args) => {
62 this.#loaded.add(args.path);
63 this.set(new Set(this.#loaded));
64 return {
65 contents: await Bun.file(args.path).text(),
66 loader: loaders[path.extname(args.path)] ?? "js",
67 };
68 });
69 },
70 });
71 }
72 }
73
74 export const trackBunImports = (options: BunImportTrackerOptions) => new BunImportTracker(options);