Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import * as NodeReadline from "node:readline";
7import * as NodeURL from "node:url";
8
9import { afterEach, expect, it } from "@effect/vitest";
10import * as HostProcess from "@t3tools/shared/HostProcess";
11
12const cleanups: Array<() => Promise<void>> = [];
13afterEach(async () => {
14 for (const cleanup of cleanups.splice(0)) await cleanup();
15});
16
17const launch = (files: Record<string, string>) => {
18 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-dev-watch-"));
19 for (const [file, content] of Object.entries({ ...files, "package.json": '{"type":"module"}' })) {
20 NodeFS.mkdirSync(NodePath.dirname(NodePath.join(root, file)), { recursive: true });
21 NodeFS.writeFileSync(NodePath.join(root, file), content);
22 }
23 NodeFS.mkdirSync(NodePath.join(root, "scripts"));
24 NodeFS.copyFileSync(
25 NodeURL.fileURLToPath(new URL("./dev.ts", import.meta.url)),
26 NodePath.join(root, "scripts", "dev.ts"),
27 );
28 const dev = NodeChildProcess.spawn(process.execPath, ["scripts/dev.ts"], {
29 cwd: root,
30 stdio: ["ignore", "pipe", "inherit"],
31 });
32 const done = new Promise((resolve) => {
33 dev.once("close", resolve);
34 dev.once("error", resolve);
35 });
36 cleanups.push(async () => {
37 try {
38 dev.kill("SIGTERM");
39 await done;
40 } finally {
41 NodeFS.rmSync(root, { recursive: true, force: true });
42 }
43 });
44 const lines = NodeReadline.createInterface({ input: dev.stdout })[Symbol.asyncIterator]();
45 return { root, nextLine: async () => (await lines.next()).value };
46};
47
48const bootScript = (marker: string) =>
49 `console.log(${JSON.stringify(marker)});\nsetInterval(() => {}, 1000);\n`;
50
51it.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
52 "follows a same-name nested source directory replacement",
53 async () => {
54 const { root, nextLine } = launch({
55 "src/bin.ts": `import { readFileSync } from "node:fs";
56console.log("BOOT " + readFileSync("src/nested/value", "utf8"));
57setInterval(() => {}, 1000);
58`,
59 "src/nested/value": "original",
60 "replacement/value": "replacement",
61 });
62 const at = (...parts: string[]) => NodePath.join(root, ...parts);
63 expect(await nextLine()).toBe("BOOT original");
64 NodeFS.renameSync(at("src", "nested"), at("removed"));
65 NodeFS.renameSync(at("replacement"), at("src", "nested"));
66 expect(await nextLine()).toBe("[dev] Restarting 'src/bin.ts': src/nested changed");
67 expect(await nextLine()).toBe("BOOT replacement");
68 NodeFS.writeFileSync(at("src", "nested", "value"), "edited");
69 expect(await nextLine()).toBe("[dev] Restarting 'src/bin.ts': src/nested/value changed");
70 expect(await nextLine()).toBe("BOOT edited");
71 // The moved-away tree must not count; the src/bin.ts edit shows the watcher is still live.
72 NodeFS.writeFileSync(at("removed", "value"), "stale");
73 NodeFS.appendFileSync(at("src", "bin.ts"), "\n");
74 expect(await nextLine()).toBe("[dev] Restarting 'src/bin.ts': src/bin.ts changed");
75 expect(await nextLine()).toBe("BOOT edited");
76 },
77);
78
79it.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
80 "restarts when a populated directory is moved into src",
81 async () => {
82 const { root, nextLine } = launch({
83 "src/bin.ts": `import * as fs from "node:fs";
84console.log("BOOT " + fs.existsSync(new URL("./incoming/module.ts", import.meta.url)));
85setInterval(() => {}, 1000);
86`,
87 "incoming/module.ts": "export {};\n",
88 });
89 expect(await nextLine()).toBe("BOOT false");
90 NodeFS.renameSync(NodePath.join(root, "incoming"), NodePath.join(root, "src", "incoming"));
91 expect(await nextLine()).toBe("[dev] Restarting 'src/bin.ts': src/incoming changed");
92 expect(await nextLine()).toBe("BOOT true");
93 },
94);
95
96it.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
97 "restarts and keeps watching when the src root is replaced",
98 async () => {
99 const { root, nextLine } = launch({
100 "src/bin.ts": bootScript("BOOT original"),
101 "incoming/bin.ts": bootScript("BOOT replacement"),
102 });
103 expect(await nextLine()).toBe("BOOT original");
104 NodeFS.renameSync(NodePath.join(root, "src"), NodePath.join(root, "old"));
105 NodeFS.renameSync(NodePath.join(root, "incoming"), NodePath.join(root, "src"));
106 expect(await nextLine()).toBe("[dev] Restarting 'src/bin.ts': src changed");
107 expect(await nextLine()).toBe("BOOT replacement");
108 NodeFS.writeFileSync(NodePath.join(root, "src", "bin.ts"), bootScript("BOOT second-edit"));
109 expect(await nextLine()).toBe("[dev] Restarting 'src/bin.ts': src/bin.ts changed");
110 expect(await nextLine()).toBe("BOOT second-edit");
111 },
112);