#!/usr/bin/env node

/**
 * Dev script — starts both web and api in parallel.
 * Usage: node scripts/dev.mjs
 */

import { spawn } from "node:child_process";

function start(name, command, args, cwd) {
  const child = spawn(command, args, {
    cwd,
    stdio: "inherit",
    shell: true,
    env: { ...process.env, FORCE_COLOR: "1" },
  });
  child.on("error", (err) => console.error(`[${name}] Failed: ${err.message}`));
  child.on("exit", (code) => {
    if (code !== 0 && code !== null) console.error(`[${name}] Exited with code ${code}`);
  });
  return child;
}

const ROOT = new URL("..", import.meta.url).pathname;

console.log("🚀 Starting fullstack dev servers...\n");

const api = start("api", "npm", ["run", "dev"], `${ROOT}/apps/api`);
const web = start("web", "npm", ["run", "dev"], `${ROOT}/apps/web`);

process.on("SIGINT", () => { api.kill(); web.kill(); process.exit(0); });
process.on("SIGTERM", () => { api.kill(); web.kill(); process.exit(0); });
