| 1 | // @effect-diagnostics nodeBuiltinImport:off - Bootstrap metadata is read synchronously before the main bundle loads. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | |
| 5 | export interface ActiveForkRuntime { |
| 6 | readonly directory: string; |
| 7 | readonly version: string; |
| 8 | readonly shell: string; |
| 9 | readonly runtime: string; |
| 10 | } |
| 11 | |
| 12 | export function readActiveForkRuntime( |
| 13 | stateDir: string, |
| 14 | bundledRoot: string, |
| 15 | // oxlint-disable-next-line t3code/no-global-process-runtime -- The synchronous bootstrap runs before Effect services exist. |
| 16 | platform: NodeJS.Platform = process.platform, |
| 17 | ): ActiveForkRuntime | undefined { |
| 18 | try { |
| 19 | const baseline: unknown = JSON.parse( |
| 20 | NodeFS.readFileSync(NodePath.join(bundledRoot, "knife-runtime.json"), "utf8"), |
| 21 | ); |
| 22 | const active: unknown = JSON.parse( |
| 23 | NodeFS.readFileSync(NodePath.join(stateDir, "fork-releases", "current.json"), "utf8"), |
| 24 | ); |
| 25 | if ( |
| 26 | typeof baseline !== "object" || |
| 27 | baseline === null || |
| 28 | !("shell" in baseline) || |
| 29 | typeof active !== "object" || |
| 30 | active === null || |
| 31 | !("directory" in active) || |
| 32 | typeof active.directory !== "string" || |
| 33 | !("version" in active) || |
| 34 | typeof active.version !== "string" || |
| 35 | !("shell" in active) || |
| 36 | active.shell !== baseline.shell || |
| 37 | typeof active.shell !== "string" || |
| 38 | !("runtime" in active) || |
| 39 | typeof active.runtime !== "string" || |
| 40 | !/^[a-f0-9]{64}$/.test(active.runtime) || |
| 41 | !/^release-[A-Za-z0-9-]+$/.test(active.directory) |
| 42 | ) |
| 43 | return; |
| 44 | |
| 45 | const bundledVersion = |
| 46 | "version" in baseline && typeof baseline.version === "string" ? baseline.version : undefined; |
| 47 | if ( |
| 48 | bundledVersion !== undefined && |
| 49 | /^0\.0\.[0-9]+$/.test(bundledVersion) && |
| 50 | /^0\.0\.[0-9]+$/.test(active.version) && |
| 51 | BigInt(active.version.slice(4)) < BigInt(bundledVersion.slice(4)) |
| 52 | ) |
| 53 | return; |
| 54 | |
| 55 | const directory = NodePath.join(stateDir, "fork-releases", active.directory); |
| 56 | const serverRoot = platform === "win32" ? "server" : "."; |
| 57 | for (const file of [ |
| 58 | "apps/desktop/dist-electron/main.cjs", |
| 59 | "apps/desktop/dist-electron/compileCache.cjs", |
| 60 | "apps/desktop/dist-electron/preload.cjs", |
| 61 | NodePath.join(serverRoot, "apps/server/dist/bin.mjs"), |
| 62 | NodePath.join(serverRoot, "apps/server/dist/client/index.html"), |
| 63 | ]) { |
| 64 | if (!NodeFS.statSync(NodePath.join(directory, file)).isFile()) return; |
| 65 | } |
| 66 | return { directory, version: active.version, shell: active.shell, runtime: active.runtime }; |
| 67 | } catch { |
| 68 | return; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | export function activeForkRuntime(): ActiveForkRuntime | undefined { |
| 73 | const raw = process.env.T3CODE_DESKTOP_PAYLOAD; |
| 74 | return raw === undefined ? undefined : JSON.parse(raw); |
| 75 | } |