Open History to see the last commit
1import { spawn } from "node:child_process"
2import type { ChildProcess } from "node:child_process"
3import { resolve, join } from "node:path"
4import { runtimeConfig } from "../server/config"
5
6const root = resolve(import.meta.dirname, "..")
7const config = runtimeConfig()
8const children: ChildProcess[] = []
9let stopping = false
10
11async function shutdown(code: number) {
12 if (stopping) return
13 stopping = true
14 const exited = children.map(
15 (child) =>
16 new Promise<void>((resolve) => {
17 if (child.exitCode !== null || child.signalCode !== null) return resolve()
18 child.once("exit", () => resolve())
19 child.kill("SIGTERM")
20 }),
21 )
22 await Promise.race([Promise.all(exited), new Promise((resolve) => setTimeout(resolve, 5000))])
23 for (const child of children)
24 if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL")
25 process.exit(code)
26}
27
28function launch(args: string[]) {
29 const child = spawn(process.execPath, args, { cwd: root, env: process.env, stdio: "inherit" })
30 children.push(child)
31 child.on("error", (error) => {
32 console.error(error)
33 void shutdown(1)
34 })
35 child.on("exit", (code) => {
36 if (!stopping) void shutdown(code || 1)
37 })
38}
39
40for (const signal of ["SIGINT", "SIGTERM"] as const)
41 process.on(signal, () => {
42 void shutdown(0)
43 })
44
45// Database and Realtime services belong to the configured Neon project.
46// This launcher runs only the application API and frontend.
47launch([join(root, "node_modules/tsx/dist/cli.mjs"), "watch", "server/index.ts"])
48launch([join(root, "node_modules/vite/bin/vite.js")])
49console.log(`1 Million Boxes: http://127.0.0.1:5173 (API on port ${config.port})`)