fp/services/our/src/utils/python.ts
2025-08-25 20:18:56 -08:00

50 lines
1.5 KiB
TypeScript

import { getNanoSpawn } from "./nanoSpawn";
import { join } from "node:path";
import { env } from "../config/env";
import which from "which";
import { existsSync } from "fs-extra";
import logger from "./logger";
export async function preparePython() {
const spawn = await getNanoSpawn();
const venvPath = env.VENV;
const venvBin = join(venvPath, "bin");
// 1. Locate python3
let pythonCmd;
try {
pythonCmd = which.sync("python3");
} catch {
throw new Error("Python not found in PATH.");
}
// 2. Create venv if missing
if (!existsSync(venvPath)) {
logger.debug("Creating Python venv...");
await spawn(pythonCmd, ["-m", "venv", venvPath], {
cwd: env.APP_DIR,
});
logger.debug("Python venv created.");
} else {
logger.debug("Using existing Python venv.");
}
// 3. Install requirements.txt
const pipCmd = join(venvBin, "pip");
logger.debug("Installing requirements.txt...");
await spawn(pipCmd, ["install", "-r", "requirements.txt"], {
cwd: env.APP_DIR,
});
logger.debug("requirements.txt installed.");
// 4. Confirm vcsi CLI binary exists
const vcsiBinary = join(venvBin, "vcsi");
if (!existsSync(vcsiBinary)) {
logger.error("vcsi binary not found in venv after installing requirements.");
logger.error("Make sure 'vcsi' is listed in requirements.txt and that it installs a CLI.");
throw new Error("vcsi installation failed or did not expose CLI.");
}
logger.debug("vcsi CLI is available at", vcsiBinary);
}