-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.mjs
More file actions
57 lines (48 loc) · 1.35 KB
/
dev.mjs
File metadata and controls
57 lines (48 loc) · 1.35 KB
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
import { spawn } from "node:child_process";
import { readFileSync } from "node:fs";
try {
readFileSync(".env", "utf8")
.split("\n")
.filter((line) => line.includes("=") && !line.startsWith("#"))
.forEach((line) => {
const parts = line.split("=");
const key = parts[0].trim();
const value = parts.slice(1).join("=").trim();
process.env[key] ??= value;
});
} catch {
// .env is optional
}
const npmBin = process.platform === "win32" ? "npm.cmd" : "npm";
const children = [];
const spawnChild = (command, args, name, isShell = false) => {
const child = spawn(command, args, {
stdio: "inherit",
env: process.env,
shell: isShell,
});
child.on("exit", (code) => {
if (!child.killed) {
console.log(`${name} exited with code ${code ?? 0}.`);
children.forEach((current) => {
if (current !== child && !current.killed) {
current.kill();
}
});
process.exit(code ?? 0);
}
});
children.push(child);
};
spawnChild(process.execPath, ["server/server.mjs"], "API server", false);
spawnChild(npmBin, ["run", "start:client"], "React app", process.platform === "win32");
const shutdown = () => {
children.forEach((child) => {
if (!child.killed) {
child.kill();
}
});
process.exit(0);
};
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);