#!/usr/bin/env node
/**
 * Run Vite and the MVFrame DingTalk notify service together.
 * This mirrors `mvframe-b`: the package owns the command, the host may opt in
 * with `yarn exec mvframe-d` or a local script.
 */
const { spawn } = require("child_process");
const path = require("path");

const children = new Set();
let shuttingDown = false;

function spawnChild(name, command, args, options = {}) {
  const child = spawn(command, args, {
    cwd: process.cwd(),
    env: process.env,
    stdio: "inherit",
    shell: false,
    ...options,
  });

  children.add(child);

  child.on("exit", (code, signal) => {
    children.delete(child);
    if (shuttingDown) return;
    if (code !== 0) {
      console.error(`[mvframe-d] ${name} exited with ${signal || code}`);
      shutdown(code || 1);
    }
  });

  child.on("error", (error) => {
    console.error(`[mvframe-d] failed to start ${name}: ${error.message}`);
    shutdown(1);
  });

  return child;
}

function shutdown(code = 0) {
  if (shuttingDown) return;
  shuttingDown = true;
  for (const child of children) {
    if (!child.killed) {
      child.kill("SIGTERM");
    }
  }
  setTimeout(() => process.exit(code), 200);
}

process.on("SIGINT", () => shutdown(0));
process.on("SIGTERM", () => shutdown(0));

const notifyArgs = process.argv.slice(2);
const notifyScript = path.join(__dirname, "notify-server.js");
const viteScript = path.join(__dirname, "dev-vite-server.js");

spawnChild("notify", process.execPath, [notifyScript, ...notifyArgs]);
spawnChild("vite", process.execPath, [viteScript]);
