assistant/scripts/python.mjs

16 lines
847 B
JavaScript

// 在 Windows/macOS 上使用本项目虚拟环境,不依赖 shell 的路径转义规则。
import { spawn } from 'node:child_process';
import { existsSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const python = path.join(root, '.venv', process.platform === 'win32' ? 'Scripts/python.exe' : 'bin/python');
if (!existsSync(python)) {
console.error('缺少项目 Python 环境,请先运行 npm run backend:setup');
process.exit(1);
}
const child = spawn(python, process.argv.slice(2), { cwd: root, stdio: 'inherit', env: { ...process.env, PYTHONUTF8: '1' } });
child.once('error', error => { console.error(error.message); process.exitCode = 1; });
child.once('exit', code => { process.exitCode = code ?? 1; });