| import { spawn } from "node:child_process"; |
| import fs from "node:fs"; |
| import path from "node:path"; |
|
|
| const repoRoot = process.cwd(); |
| const mode = process.argv[2] ?? "dev"; |
|
|
| function loadEnv(filePath) { |
| if (!fs.existsSync(filePath)) { |
| return {}; |
| } |
|
|
| const env = {}; |
| const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/); |
| for (const raw of lines) { |
| const line = raw.trim(); |
| if (!line || line.startsWith("#")) { |
| continue; |
| } |
| const idx = line.indexOf("="); |
| if (idx < 0) { |
| continue; |
| } |
| const key = line.slice(0, idx).trim(); |
| const value = line.slice(idx + 1); |
| env[key] = value; |
| } |
|
|
| return env; |
| } |
|
|
| const rootEnv = loadEnv(path.join(repoRoot, ".env.local")); |
| const appEnv = loadEnv(path.join(repoRoot, "apps", "web", ".env.local")); |
|
|
| const allowedModes = new Set(["dev", "build", "start", "lint", "typecheck"]); |
| if (!allowedModes.has(mode)) { |
| console.error(`Unsupported mode: ${mode}`); |
| process.exit(1); |
| } |
|
|
| const child = spawn("cmd.exe", ["/d", "/s", "/c", "npm.cmd", "run", mode, "--workspace", "apps/web"], { |
| cwd: repoRoot, |
| env: { |
| ...rootEnv, |
| ...appEnv, |
| ...process.env |
| }, |
| stdio: "inherit", |
| shell: false |
| }); |
|
|
| child.on("exit", (code, signal) => { |
| if (signal) { |
| process.kill(process.pid, signal); |
| return; |
| } |
| process.exit(code ?? 0); |
| }); |
|
|
| child.on("error", (error) => { |
| console.error(`Failed to run web command: ${error.message}`); |
| process.exit(1); |
| }); |
|
|