UNPKG

rojo

Version:
68 lines (67 loc) 2.3 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBinaryPath = getBinaryPath; exports.main = main; const child_process_1 = require("child_process"); const promises_1 = __importDefault(require("fs/promises")); const path_1 = __importDefault(require("path")); class ProcessError extends Error { constructor(message, code) { super(message); this.code = code; } } async function getBinaryPath() { const folderPath = path_1.default.join(__dirname, "..", "bin", process.platform); const files = await promises_1.default.readdir(folderPath, { withFileTypes: true }); let binaryName; switch (process.platform) { case 'win32': binaryName = "rojo.exe"; break; default: binaryName = "rojo"; break; } const binary = files.find(file => file.isFile() && file.name.includes(binaryName)); return binary ? path_1.default.join(folderPath, binary.name) : null; } async function main() { const binaryPath = await getBinaryPath(); const args = process.argv.slice(2); // Automatically set executable permissions for non-Windows platforms if (binaryPath && process.platform !== 'win32') { try { await promises_1.default.chmod(binaryPath, 0o755); } catch (err) { // Ignore errors if chmod fails, will be caught on spawn } } return await new Promise((resolve, reject) => { if (!binaryPath) { reject(new Error('Binary path is undefined')); return; } const child = (0, child_process_1.spawn)(binaryPath, args, { stdio: 'inherit' }); child.on('error', (error) => reject(error)); child.on('exit', (code) => { if (code === 0 || code === 2) { resolve(); } else { reject(new ProcessError(`Process exited with code ${code}`, code || 1)); } }); }); } if (require.main === module) { main().catch((error) => { console.error(error); process.exit(error.code); }); }