import { spawn } from "node:child_process"; import { readFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { parseEnv } from "node:util"; const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); const config = parseEnv( readFileSync(resolve(repositoryRoot, "infrastructure/database/.env"), "utf8") ); if (config.DB_HOST !== "127.0.0.1" || config.DB_NAME !== "factuges_erp_development") { throw new Error("Local server requires the development database configuration."); } const environment: NodeJS.ProcessEnv = { ...process.env, NODE_ENV: "development", DB_SYNC_MODE: "none", DB_SSL: "false", }; environment.DB_ROOT_PASS = undefined; for (const key of ["DB_HOST", "DB_PORT", "DB_NAME", "DB_USER", "DB_PASS"]) { const value = config[key]; if (!value) { throw new Error("Run pnpm db:dev:up before starting the local server."); } environment[key] = value; } const child = spawn( process.execPath, ["--env-file-if-exists=.env", "--import=tsx", "--watch", "src/index.ts"], { cwd: resolve(repositoryRoot, "apps/server"), env: environment, stdio: "inherit", } ); const stop = () => { child.kill("SIGTERM"); }; process.once("SIGINT", stop); process.once("SIGTERM", stop); child.once("error", () => { console.error("Unable to start the local server."); process.exitCode = 1; }); child.once("close", (code) => { process.removeListener("SIGINT", stop); process.removeListener("SIGTERM", stop); process.exitCode = code ?? 1; });