Skip to content
Source
Sign inSign up

runHook.ts

41 lines1.4 KB

1// @effect-diagnostics nodeBuiltinImport:off - runs inside Vite+ before project services start.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeURL from "node:url";
4import { remoteCacheUrl } from "./remoteCache.ts";
5import { flushTelemetry, observeSummaries } from "./telemetry.ts";
6​
7export default function start({ root }: { readonly root: string }) {
8 const url = remoteCacheUrl(root);
9 if (!url) return;
10 const endpoint = new URL(url);
11 endpoint.username = "";
12 endpoint.password = "";
13 const stop =
14 process.env.T3_TASK_CACHE_TELEMETRY === "off"
15 ? undefined
16 : observeSummaries(root, endpoint.href.replace(/\/$/, ""), process.env);
17 const finish = async () => {
18 if (!stop) return;
19 stop();
20 // GitHub can terminate detached processes as soon as the final step exits.
21 if (process.env.GITHUB_ACTIONS === "true") {
22 await flushTelemetry();
23 return;
24 }
25 // A transient flusher drains the durable queue without delaying tasks or Ctrl-C.
26 const child = NodeChildProcess.spawn(
27 process.execPath,
28 [NodeURL.fileURLToPath(new URL("./flush.ts", import.meta.url))],
29 {
30 detached: true,
31 stdio: "ignore",
32 env: { ...process.env, NODE_OPTIONS: "" },
33 },
34 );
35 child.on("error", () => {});
36 child.unref();
37 };
38 return Object.assign(finish, {
39 remoteCache: process.env.VP_REMOTE_CACHE || process.env.VP_T3_TASK_CACHE_MODE || "read-write",
40 });