| import { spawn } from "node:child_process"; |
| import fs from "node:fs"; |
| import path from "node:path"; |
|
|
| const repoRoot = process.cwd(); |
| const envLocalPath = path.join(repoRoot, ".env.local"); |
|
|
| 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; |
| } |
|
|
| function normalizeDatabaseUrl(env) { |
| const current = env.DATABASE_URL; |
| if (!current) { |
| return env; |
| } |
|
|
| let normalized = current.trim(); |
| if ( |
| (normalized.startsWith('"') && normalized.endsWith('"')) || |
| (normalized.startsWith("'") && normalized.endsWith("'")) |
| ) { |
| normalized = normalized.slice(1, -1); |
| } |
|
|
| if (normalized.startsWith(":postgresql://")) { |
| normalized = normalized.slice(1); |
| } |
|
|
| normalized = normalized.replace( |
| /(postgres(?:ql)?:\/\/[^:/?#]+:)\[([^\]]+)\](@)/i, |
| "$1$2$3" |
| ); |
|
|
| return { |
| ...env, |
| DATABASE_URL: normalized |
| }; |
| } |
|
|
| const localEnv = loadEnv(envLocalPath); |
| const normalizedLocalEnv = normalizeDatabaseUrl(localEnv); |
| const pythonExe = |
| process.env.PYTHON_EXE || |
| normalizedLocalEnv.PYTHON_EXE || |
| "C:\\Users\\Angelah\\AppData\\Local\\Programs\\Python\\Python311\\python.exe"; |
|
|
| const mergedEnv = normalizeDatabaseUrl({ |
| ...normalizedLocalEnv, |
| ...process.env |
| }); |
|
|
| const child = spawn(pythonExe, ["apps/worker/src/worker/main.py"], { |
| cwd: repoRoot, |
| env: mergedEnv, |
| stdio: "inherit" |
| }); |
|
|
| 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 start worker: ${error.message}`); |
| process.exit(1); |
| }); |
|
|