|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { execSync } from "node:child_process"; |
| 6 | + |
| 7 | +const projectPath = process.argv[2]; |
| 8 | +console.log("projectPath", projectPath); |
| 9 | + |
| 10 | +function pythonCommand() { |
| 11 | + const candidates = ["python3", "python"]; |
| 12 | + for (const cmd of candidates) { |
| 13 | + try { |
| 14 | + execSync(`${cmd} --version`, { stdio: "ignore" }); |
| 15 | + return cmd; |
| 16 | + } catch { |
| 17 | + // silent fail |
| 18 | + } |
| 19 | + } |
| 20 | + throw new Error("Python not found"); |
| 21 | +} |
| 22 | + |
| 23 | +function createVenv() { |
| 24 | + console.log("🔧 Setting up Python virtual environment..."); |
| 25 | + const python = pythonCommand(); |
| 26 | + console.log(`→ Using Python: ${python}`); |
| 27 | + const venvPath = path.join(projectPath, "venv"); |
| 28 | + if (fs.existsSync(venvPath)) { |
| 29 | + console.log(`→ Found existing venv → ${venvPath}`); |
| 30 | + } else { |
| 31 | + console.log(`→ Creating virtual environment → ${venvPath}`); |
| 32 | + try { |
| 33 | + execSync(`${python} -m venv ${venvPath}`, { stdio: "inherit" }); |
| 34 | + } catch (err) { |
| 35 | + console.error("Failed to create virtual environment"); |
| 36 | + console.error(err.message); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + } |
| 40 | + if (process.platform === "win32") { |
| 41 | + return path.join(venvPath, "Scripts", "python.exe"); |
| 42 | + } else { |
| 43 | + return path.join(venvPath, "bin", "python"); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function installDependecies(pythonExe) { |
| 48 | + console.log(`→ Installing dependencies ...`); |
| 49 | + const pipCommand = `pip install ${projectPath} pyinstaller`; |
| 50 | + try { |
| 51 | + console.log(`→ Running: ${pythonExe} -m ${pipCommand}`); |
| 52 | + execSync(`${pythonExe} -m ${pipCommand}`, { stdio: "inherit" }); |
| 53 | + } catch (err) { |
| 54 | + console.error("Failed to install requirements"); |
| 55 | + console.error(err.message); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + console.log("✅ Python virtual environment setup complete"); |
| 59 | +} |
| 60 | + |
| 61 | +function runPyInstaller(pythonExe) { |
| 62 | + console.log(`→ Running PyInstaller ...`); |
| 63 | + const specFiles = fs.readdirSync(projectPath, { withFileTypes: true }) |
| 64 | + .filter(file => file.isFile() && file.name.endsWith(".spec")) |
| 65 | + .map(file => path.join(projectPath, file.name)); |
| 66 | + if (specFiles.length !== 1) { |
| 67 | + console.error("Expected 1 spec file, found " + specFiles.length); |
| 68 | + process.exit(1); |
| 69 | + } |
| 70 | + const pyinstallerCommand = `PyInstaller ${specFiles[0]} --distpath ${process.cwd()} --clean`; |
| 71 | + try { |
| 72 | + console.log(`→ Running: ${pythonExe} -m ${pyinstallerCommand}`); |
| 73 | + execSync(`${pythonExe} -m ${pyinstallerCommand}`, { stdio: "inherit" }); |
| 74 | + } catch (err) { |
| 75 | + console.error("Failed to run pyinstaller"); |
| 76 | + console.error(err.message); |
| 77 | + process.exit(1); |
| 78 | + } |
| 79 | + console.log("✅ PyInstaller complete"); |
| 80 | +} |
| 81 | + |
| 82 | +function main() { |
| 83 | + const pythonExe = createVenv(); |
| 84 | + installDependecies(pythonExe); |
| 85 | + runPyInstaller(pythonExe); |
| 86 | +} |
| 87 | + |
| 88 | +main(); |
0 commit comments