| ... | ... | @@ -0,0 +1,124 @@ |
| 1 | import { spawn } from "node:child_process"; |
| 2 | |
| 3 | const BASE_SERVER_PORT = 3773; |
| 4 | const BASE_WEB_PORT = 5173; |
| 5 | const MAX_HASH_OFFSET = 3000; |
| 6 | const MODE_ARGS = { |
| 7 | dev: [ |
| 8 | "run", |
| 9 | "dev", |
| 10 | "--ui=tui", |
| 11 | "--filter=@t3tools/contracts", |
| 12 | "--filter=@t3tools/web", |
| 13 | "--filter=t3", |
| 14 | "--parallel", |
| 15 | ], |
| 16 | "dev:server": ["run", "dev", "--filter=t3"], |
| 17 | "dev:web": ["run", "dev", "--filter=@t3tools/web"], |
| 18 | "dev:desktop": ["run", "dev", "--filter=@t3tools/desktop", "--filter=@t3tools/web", "--parallel"], |
| 19 | }; |
| 20 | |
| 21 | function parseInteger(value, envName) { |
| 22 | const parsed = Number(value); |
| 23 | if (!Number.isInteger(parsed)) { |
| 24 | throw new Error(`Invalid ${envName}: ${value}`); |
| 25 | } |
| 26 | return parsed; |
| 27 | } |
| 28 | |
| 29 | function hashSeed(seed) { |
| 30 | let hash = 0x811c9dc5; |
| 31 | for (let i = 0; i < seed.length; i += 1) { |
| 32 | hash ^= seed.charCodeAt(i); |
| 33 | hash = Math.imul(hash, 0x01000193); |
| 34 | } |
| 35 | return hash >>> 0; |
| 36 | } |
| 37 | |
| 38 | function resolveOffset() { |
| 39 | const explicitOffset = process.env.T3CODE_PORT_OFFSET?.trim(); |
| 40 | if (explicitOffset) { |
| 41 | const parsed = parseInteger(explicitOffset, "T3CODE_PORT_OFFSET"); |
| 42 | if (parsed < 0) { |
| 43 | throw new Error(`Invalid T3CODE_PORT_OFFSET: ${explicitOffset}`); |
| 44 | } |
| 45 | return { offset: parsed, source: `T3CODE_PORT_OFFSET=${explicitOffset}` }; |
| 46 | } |
| 47 | |
| 48 | const seed = process.env.T3CODE_DEV_INSTANCE?.trim(); |
| 49 | if (!seed) { |
| 50 | return { offset: 0, source: "default ports" }; |
| 51 | } |
| 52 | |
| 53 | if (/^\d+$/.test(seed)) { |
| 54 | return { offset: Number(seed), source: `numeric T3CODE_DEV_INSTANCE=${seed}` }; |
| 55 | } |
| 56 | |
| 57 | const offset = (hashSeed(seed) % MAX_HASH_OFFSET) + 1; |
| 58 | return { offset, source: `hashed T3CODE_DEV_INSTANCE=${seed}` }; |
| 59 | } |
| 60 | |
| 61 | function main() { |
| 62 | const mode = process.argv[2]; |
| 63 | const isDryRun = process.argv.includes("--dry-run"); |
| 64 | if (!mode || !(mode in MODE_ARGS)) { |
| 65 | const supportedModes = Object.keys(MODE_ARGS).join(", "); |
| 66 | throw new Error(`Usage: bun scripts/dev-runner.mjs <mode>. Supported modes: ${supportedModes}`); |
| 67 | } |
| 68 | |
| 69 | const { offset, source } = resolveOffset(); |
| 70 | const serverPort = BASE_SERVER_PORT + offset; |
| 71 | const webPort = BASE_WEB_PORT + offset; |
| 72 | |
| 73 | if (serverPort > 65535 || webPort > 65535) { |
| 74 | throw new Error( |
| 75 | `Port offset too large (${offset}). Computed ports: server=${serverPort}, web=${webPort}`, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | const env = { |
| 80 | ...process.env, |
| 81 | T3CODE_PORT: String(serverPort), |
| 82 | PORT: String(webPort), |
| 83 | ELECTRON_RENDERER_PORT: String(webPort), |
| 84 | VITE_WS_URL: `ws://localhost:${serverPort}`, |
| 85 | VITE_DEV_SERVER_URL: `http://localhost:${webPort}`, |
| 86 | }; |
| 87 | if (mode === "dev" && !env.T3CODE_LOG_WS_EVENTS) { |
| 88 | env.T3CODE_LOG_WS_EVENTS = "1"; |
| 89 | } |
| 90 | |
| 91 | console.info( |
| 92 | `[dev-runner] mode=${mode} source=${source} serverPort=${serverPort} webPort=${webPort}`, |
| 93 | ); |
| 94 | |
| 95 | if (isDryRun) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | const command = process.platform === "win32" ? "turbo.cmd" : "turbo"; |
| 100 | const child = spawn(command, MODE_ARGS[mode], { |
| 101 | stdio: "inherit", |
| 102 | env, |
| 103 | }); |
| 104 | |
| 105 | child.on("exit", (code, signal) => { |
| 106 | if (signal) { |
| 107 | process.kill(process.pid, signal); |
| 108 | return; |
| 109 | } |
| 110 | process.exit(code ?? 0); |
| 111 | }); |
| 112 | |
| 113 | child.on("error", (error) => { |
| 114 | console.error("[dev-runner] failed to start turbo", error); |
| 115 | process.exit(1); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | try { |
| 120 | main(); |
| 121 | } catch (error) { |
| 122 | console.error("[dev-runner]", error); |
| 123 | process.exit(1); |
| 124 | } |