const { spawn } = require("child_process"); const readline = require("readline"); const path = require("path"); const fs = require("fs"); const logStart = 12; const colors = { reset: "\x1b[0m", emerald: "\x1b[38;5;35m", amber: "\x1b[38;5;214m", }; function buildLog(prefix, color, line) { const pre = `${color}[${prefix}]${colors.reset}`; const filler = " ".repeat(logStart - prefix.length - 2); return `${pre}${filler}${line}\n`; } function start(command, args, { cwd, prefix, color }) { const proc = spawn(command, args, { cwd, shell: true }); for (const [stream, write] of [ [ proc.stdout, (line) => process.stdout.write(buildLog(prefix, color, line)), ], [ proc.stderr, (line) => process.stderr.write(buildLog(prefix, color, line)), ], ]) { readline.createInterface({ input: stream }).on("line", write); } return proc; } function findVenvPython(cwd) { const scriptDir = process.platform === "win32" ? "Scripts" : "bin"; const candidates = ["venv", ".venv", "env", ".env"]; for (const name of candidates) { const python = path.join(cwd, name, scriptDir, "python"); if (fs.existsSync(python) || fs.existsSync(python + ".exe")) { return path.join(name, scriptDir, "python"); } } throw new Error( `No virtual environment found in ${cwd}. Looked for: ${candidates.join(", ")}`, ); } const procs = [ start("npm", ["run", "dev"], { cwd: "./src/frontend", prefix: "FRONTEND", color: colors.amber, }), start(findVenvPython("./src/backend"), ["-u", "run_server.py"], { cwd: "./src/backend", prefix: "BACKEND", color: colors.emerald, }), ]; procs.forEach((proc) => { proc.on("exit", (_code) => { procs.forEach((p) => p.kill()); }); });