|
| 1 | +import { spawn } from "child_process"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import { logger } from "../utils/logger.js"; |
| 5 | +import { getAppPath } from "../utils/paths.js"; |
| 6 | + |
| 7 | +let blcuProgrammingProcess = null; |
| 8 | + |
| 9 | +function getBlcuProgrammingRepoPath() { |
| 10 | + return path.join(getAppPath(), "..", "blcu-programming"); |
| 11 | +} |
| 12 | + |
| 13 | +function getPythonExecutable(repoPath) { |
| 14 | + if (process.platform === "win32") { |
| 15 | + return path.join(repoPath, ".venv", "Scripts", "python.exe"); |
| 16 | + } |
| 17 | + |
| 18 | + return path.join(repoPath, ".venv", "bin", "python"); |
| 19 | +} |
| 20 | + |
| 21 | +async function startBlcuProgramming() { |
| 22 | + if (blcuProgrammingProcess && !blcuProgrammingProcess.killed) { |
| 23 | + return blcuProgrammingProcess; |
| 24 | + } |
| 25 | + |
| 26 | + const repoPath = getBlcuProgrammingRepoPath(); |
| 27 | + const pythonBin = getPythonExecutable(repoPath); |
| 28 | + const entrypointPath = path.join(repoPath, "api", "main.py"); |
| 29 | + |
| 30 | + if (!fs.existsSync(entrypointPath)) { |
| 31 | + logger.process( |
| 32 | + "BLCU Programming", |
| 33 | + `Entrypoint not found at ${entrypointPath}`, |
| 34 | + ); |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + if (!fs.existsSync(pythonBin)) { |
| 39 | + logger.process( |
| 40 | + "BLCU Programming", |
| 41 | + `Python executable not found at ${pythonBin}`, |
| 42 | + ); |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + blcuProgrammingProcess = spawn( |
| 47 | + pythonBin, |
| 48 | + ["-m", "uvicorn", "api.main:app"], |
| 49 | + { |
| 50 | + cwd: repoPath, |
| 51 | + env: { |
| 52 | + ...process.env, |
| 53 | + PYTHONUNBUFFERED: "1", |
| 54 | + }, |
| 55 | + }, |
| 56 | + ); |
| 57 | + |
| 58 | + blcuProgrammingProcess.stdout.on("data", (data) => { |
| 59 | + logger.process("BLCU Programming", data.toString().trim()); |
| 60 | + }); |
| 61 | + |
| 62 | + blcuProgrammingProcess.stderr.on("data", (data) => { |
| 63 | + logger.process("BLCU Programming", data.toString().trim()); |
| 64 | + }); |
| 65 | + |
| 66 | + blcuProgrammingProcess.on("close", (code) => { |
| 67 | + logger.process("BLCU Programming", `Process exited with code ${code}`); |
| 68 | + blcuProgrammingProcess = null; |
| 69 | + }); |
| 70 | + |
| 71 | + return blcuProgrammingProcess; |
| 72 | +} |
| 73 | + |
| 74 | +async function stopBlcuProgramming() { |
| 75 | + if (!blcuProgrammingProcess || blcuProgrammingProcess.killed) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + blcuProgrammingProcess.kill("SIGTERM"); |
| 80 | + blcuProgrammingProcess = null; |
| 81 | +} |
| 82 | + |
| 83 | +export { startBlcuProgramming, stopBlcuProgramming }; |
0 commit comments