import { fileURLToPath } from "node:url";

const projectRoot = fileURLToPath(new URL("..", import.meta.url));

const apiServer = Bun.spawn(["bun", "--watch", "src/server/dev.ts", ...process.argv.slice(2)], {
  cwd: projectRoot,
  stderr: "inherit",
  stdout: "inherit",
});

const viteServer = Bun.spawn(["bunx", "--bun", "vite", "--host"], {
  cwd: projectRoot,
  stderr: "inherit",
  stdout: "inherit",
});

function shutdown() {
  apiServer.kill();
  viteServer.kill();
}

process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);

await Promise.race([apiServer.exited, viteServer.exited]);
shutdown();
