|
| 1 | +import { spawn, spawnSync } from "node:child_process"; |
| 2 | + |
| 3 | +const DEFAULT_HOST = "127.0.0.1"; |
| 4 | +const CLIENT_PORT = "7600"; |
| 5 | +const API_PORT = "7601"; |
| 6 | + |
| 7 | +function printHelp() { |
| 8 | + console.log(`Usage: npm run dev -- [--host [bind-host]] [--allow-host hostname] |
| 9 | +
|
| 10 | +Examples: |
| 11 | + npm run dev |
| 12 | + npm run dev -- --host |
| 13 | + npm run dev -- --host 192.168.1.42 |
| 14 | + npm run dev -- --host --allow-host waystone |
| 15 | +
|
| 16 | +Flags: |
| 17 | + --host Bind both the Vite dev server and the API to a host. |
| 18 | + When passed without a value, uses 0.0.0.0 so other |
| 19 | + computers on the LAN can connect. |
| 20 | + --allow-host Allow Vite to answer requests for a hostname such as |
| 21 | + a Tailscale MagicDNS name. |
| 22 | + -h, --help Show this help output.`); |
| 23 | +} |
| 24 | + |
| 25 | +function parseArgs(argv) { |
| 26 | + let host = DEFAULT_HOST; |
| 27 | + const allowedHosts = []; |
| 28 | + |
| 29 | + for (let index = 0; index < argv.length; index += 1) { |
| 30 | + const arg = argv[index]; |
| 31 | + |
| 32 | + if (arg === "-h" || arg === "--help") { |
| 33 | + printHelp(); |
| 34 | + process.exit(0); |
| 35 | + } |
| 36 | + |
| 37 | + if (arg === "--host") { |
| 38 | + const next = argv[index + 1]; |
| 39 | + if (next && !next.startsWith("-")) { |
| 40 | + host = next; |
| 41 | + index += 1; |
| 42 | + } else { |
| 43 | + host = "0.0.0.0"; |
| 44 | + } |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + if (arg === "--allow-host") { |
| 49 | + const next = argv[index + 1]; |
| 50 | + if (!next || next.startsWith("-")) { |
| 51 | + console.error("[arcane-agents] missing value for --allow-host."); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | + allowedHosts.push(next); |
| 55 | + index += 1; |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + if (arg.startsWith("--host=")) { |
| 60 | + const value = arg.slice("--host=".length).trim(); |
| 61 | + if (!value) { |
| 62 | + console.error("[arcane-agents] missing value for --host."); |
| 63 | + process.exit(1); |
| 64 | + } |
| 65 | + host = value; |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (arg.startsWith("--allow-host=")) { |
| 70 | + const value = arg.slice("--allow-host=".length).trim(); |
| 71 | + if (!value) { |
| 72 | + console.error("[arcane-agents] missing value for --allow-host."); |
| 73 | + process.exit(1); |
| 74 | + } |
| 75 | + allowedHosts.push(value); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + console.error(`[arcane-agents] unknown dev flag: ${arg}`); |
| 80 | + printHelp(); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + return { host, allowedHosts }; |
| 85 | +} |
| 86 | + |
| 87 | +function isIpLiteral(host) { |
| 88 | + return /^[\d.]+$/.test(host) || host.includes(":"); |
| 89 | +} |
| 90 | + |
| 91 | +function isDefaultAllowedHost(host) { |
| 92 | + return host === "localhost" || host.endsWith(".localhost") || isIpLiteral(host); |
| 93 | +} |
| 94 | + |
| 95 | +function shouldAutoAllowHost(host) { |
| 96 | + return host.length > 0 && host !== "0.0.0.0" && host !== "::" && !isDefaultAllowedHost(host); |
| 97 | +} |
| 98 | + |
| 99 | +function resolveProxyHost(host) { |
| 100 | + if (host === "0.0.0.0") { |
| 101 | + return "127.0.0.1"; |
| 102 | + } |
| 103 | + |
| 104 | + if (host === "::") { |
| 105 | + return "::1"; |
| 106 | + } |
| 107 | + |
| 108 | + return host; |
| 109 | +} |
| 110 | + |
| 111 | +function getNpmCommand() { |
| 112 | + return process.platform === "win32" ? "npm.cmd" : "npm"; |
| 113 | +} |
| 114 | + |
| 115 | +function runChecked(command, args, env) { |
| 116 | + const result = spawnSync(command, args, { |
| 117 | + stdio: "inherit", |
| 118 | + env |
| 119 | + }); |
| 120 | + |
| 121 | + if (result.error) { |
| 122 | + throw result.error; |
| 123 | + } |
| 124 | + |
| 125 | + if (result.status !== 0) { |
| 126 | + process.exit(result.status ?? 1); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +const { host, allowedHosts: cliAllowedHosts } = parseArgs(process.argv.slice(2)); |
| 131 | +const allowedHosts = new Set(cliAllowedHosts); |
| 132 | + |
| 133 | +if (shouldAutoAllowHost(host)) { |
| 134 | + allowedHosts.add(host); |
| 135 | +} |
| 136 | + |
| 137 | +const env = { |
| 138 | + ...process.env, |
| 139 | + ARCANE_AGENTS_API_HOST: host, |
| 140 | + ARCANE_AGENTS_API_PORT: API_PORT, |
| 141 | + ARCANE_AGENTS_DEV_CLIENT_HOST: host, |
| 142 | + ARCANE_AGENTS_DEV_CLIENT_PORT: CLIENT_PORT, |
| 143 | + ARCANE_AGENTS_DEV_API_HOST: resolveProxyHost(host), |
| 144 | + ARCANE_AGENTS_DEV_API_PORT: API_PORT, |
| 145 | + ARCANE_AGENTS_DEV_ALLOWED_HOSTS: Array.from(allowedHosts).join(",") |
| 146 | +}; |
| 147 | + |
| 148 | +if (host === DEFAULT_HOST) { |
| 149 | + console.log("[arcane-agents] starting dev mode on localhost only."); |
| 150 | +} else if (host === "0.0.0.0") { |
| 151 | + console.log("[arcane-agents] starting dev mode on all network interfaces."); |
| 152 | + console.log("[arcane-agents] open http://<this-machine-ip>:7600 from another computer on your LAN."); |
| 153 | +} else { |
| 154 | + console.log(`[arcane-agents] starting dev mode on ${host}.`); |
| 155 | +} |
| 156 | + |
| 157 | +if (allowedHosts.size > 0) { |
| 158 | + console.log(`[arcane-agents] allowing dev requests for: ${Array.from(allowedHosts).join(", ")}`); |
| 159 | +} |
| 160 | + |
| 161 | +const npmCommand = getNpmCommand(); |
| 162 | + |
| 163 | +runChecked(npmCommand, ["run", "dev:clean"], env); |
| 164 | + |
| 165 | +const child = spawn(npmCommand, ["run", "dev:stack"], { |
| 166 | + stdio: "inherit", |
| 167 | + env |
| 168 | +}); |
| 169 | + |
| 170 | +for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) { |
| 171 | + process.on(signal, () => { |
| 172 | + if (!child.killed) { |
| 173 | + child.kill(signal); |
| 174 | + } |
| 175 | + }); |
| 176 | +} |
| 177 | + |
| 178 | +child.on("exit", (code, signal) => { |
| 179 | + if (signal) { |
| 180 | + process.kill(process.pid, signal); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + process.exit(code ?? 0); |
| 185 | +}); |
0 commit comments