File size: 1,657 Bytes
5743bc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { spawn } from "node:child_process";
import path from "node:path";

const rootDir = path.resolve(import.meta.dirname, "..");
const isWindows = process.platform === "win32";
const pnpmExec =
  process.env.npm_execpath && process.env.npm_execpath.toLowerCase().includes("pnpm")
    ? process.env.npm_execpath
    : null;
const nodeBin = process.execPath;

const children = [];

function shutdown(exitCode = 0) {
  for (const child of children) {
    if (!child.killed) {
      child.kill("SIGTERM");
    }
  }
  process.exit(exitCode);
}

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

  child.on("exit", (code) => {
    if (code && code !== 0) {
      console.error(`${name} exited with code ${code}`);
      shutdown(code);
    }
  });

  children.push(child);
  return child;
}

const apiPort = process.env.API_PORT ?? "3001";
const webPort = process.env.WEB_PORT ?? "3000";

const backend = startProcess("backend", nodeBin, ["scripts/dev-api.mjs"], {
  API_PORT: apiPort,
});

const frontend = startProcess(
  "frontend",
  pnpmExec ? nodeBin : isWindows ? "pnpm.cmd" : "pnpm",
  pnpmExec
    ? [pnpmExec, "--filter", "@workspace/guardian-agent", "run", "dev"]
    : ["--filter", "@workspace/guardian-agent", "run", "dev"],
  {
    PORT: webPort,
    BASE_PATH: "/",
    API_BASE_URL: `http://127.0.0.1:${apiPort}`,
  },
);

for (const signal of ["SIGINT", "SIGTERM"]) {
  process.on(signal, () => {
    backend.kill(signal);
    frontend.kill(signal);
    process.exit(0);
  });
}