// Dev server: shadow-cljs watch on the :app build, served by :dev-http on BOTH
// 5173 and 4173 (public/ + dist-dev/ merged roots — the relay's origin
// allowlist covers 4173/5173 only; shadow's default 8080 would be refused).
//
// Reads the SAME env var ardz already exports (VITE_ID_BRIDGE_URL) and
// injects it as a :closure-defines override so `ardz dev game5` keeps
// working unchanged (bridge -> the hub bridge ardz stages under dist-dev/id/).
//
// THE WATCH WRITES TO dist-dev/assets, NEVER dist/ (the :output-dir override
// below). This replaces the canon's known wart where 4173 served dist/ while
// the watch overwrote dist/assets: dist/ is `ardz build`'s RELEASE tree —
// goog.DEBUG=false, the PRODUCTION relay multiaddr — and serving it on a dev
// port handed one tab of a dev drill a production-network client (fresh page
// before the watch's first build, or a browser-cached assets/main.js
// afterwards). The two tabs then sat on different relays and never converged,
// with nothing in either console saying why. Separate trees make the mixture
// impossible. Smoke a release with a standalone static server over dist/,
// never through these ports.
import { spawnSync } from "node:child_process";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const client = dirname(dirname(fileURLToPath(import.meta.url)));
// The npm rewrites shadow needs, and their gates (see the script's header).
// Its exit status is CHECKED: patch-npm is what keeps dev and release running
// the same npm sources, and rule 2 in particular is the only place the dev
// pipeline gets the `__esModule` fix that build.mjs applies to a release dist.
// Letting it fail and starting the watcher anyway is how a lobby that paints
// over a dead p2p layer reaches a developer with a green build log.
{
const r = spawnSync(process.execPath, [join(client, "scripts", "patch-npm.mjs")], { cwd: client, stdio: "inherit" });
if (r.status !== 0) process.exit(r.status ?? 1);
}
const defines = {};
const bridge = process.env.VITE_ID_BRIDGE_URL;
if (bridge) defines["game5.config/ID-BRIDGE-URL"] = bridge;
const body = Object.entries(defines)
.map(([k, v]) => `${k} ${JSON.stringify(v)}`)
.join(" ");
const r = spawnSync(
join(client, "node_modules", ".bin", "shadow-cljs"),
[
"watch",
"app",
"--config-merge",
// :output-dir — the watch's own tree; see the header. dist/ stays the
// release build's alone.
`{:closure-defines {${body}} :output-dir "dist-dev/assets"}`,
],
{ cwd: client, stdio: "inherit" },
);
process.exit(r.status ?? 1);