#!/usr/bin/env node const path = require('path'); const { spawn } = require('child_process'); const os = require('os'); const fs = require('fs'); function getBinaryName() { const platform = process.platform; const arch = (process.arch === 'arm64' || process.arch === 'aarch64') ? 'arm64' : 'amd64'; if (platform === 'darwin') { return `pinchtab-darwin-${arch}`; } else if (platform === 'linux') { return `pinchtab-linux-${arch}`; } else if (platform === 'win32') { return `pinchtab-windows-${arch}.exe`; } throw new Error(`Unsupported platform: ${platform}`); } function getVersion() { const pkgPath = path.join(__dirname, '..', 'package.json'); const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); return pkg.version; } function getBinaryPath() { // Allow override via environment variable (for Docker, custom builds) if (process.env.PINCHTAB_BINARY_PATH) { return process.env.PINCHTAB_BINARY_PATH; } // Try version-specific path first: ~/.pinchtab/bin/0.7.0/pinchtab-darwin-arm64 const version = getVersion(); const versionedPath = path.join(os.homedir(), '.pinchtab', 'bin', version, getBinaryName()); if (fs.existsSync(versionedPath)) { return versionedPath; } // Fallback to version-less for backwards compat return path.join(os.homedir(), '.pinchtab', 'bin', getBinaryName()); } const binaryPath = getBinaryPath(); // Check binary exists if (!fs.existsSync(binaryPath)) { console.error('❌ Pinchtab binary not found at:'); console.error(` ${binaryPath}`); console.error('\nTo fix this, run:'); console.error(' npm rebuild pinchtab'); console.error('\nOr set a custom binary path:'); console.error(` PINCHTAB_BINARY_PATH=/path/to/binary pinchtab`); process.exit(1); } const proc = spawn(binaryPath, process.argv.slice(2), { stdio: 'inherit', shell: false, }); proc.on('error', (err) => { console.error(`Failed to run Pinchtab: ${err.message}`); process.exit(1); }); process.on('SIGINT', () => { proc.kill(); }); process.on('SIGTERM', () => { proc.kill(); });