UNPKG

claude-arcade

Version:

Add classic arcade games to your Claude Code workflow with Ctrl+G

131 lines (130 loc) 4.59 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const fs = __importStar(require("fs")); const readline = __importStar(require("readline")); let claudeProcess = null; let gameProcess = null; let monitoringKeys = true; console.clear(); console.log('\x1b[36m╔════════════════════════════════════════════════╗\x1b[0m'); console.log('\x1b[36m║ Claude Code + Game (Ctrl+G) ║\x1b[0m'); console.log('\x1b[36m╚════════════════════════════════════════════════╝\x1b[0m'); console.log(''); console.log('\x1b[32m→ Starting Claude Code at FULL SPEED...\x1b[0m'); console.log('\x1b[35m→ Press Ctrl+G to launch the game\x1b[0m'); console.log(''); // Open /dev/tty for direct terminal access const ttyFd = fs.openSync('/dev/tty', 'r+'); // Start Claude with /dev/tty as stdin (FULL SPEED!) claudeProcess = (0, child_process_1.spawn)('claude', process.argv.slice(2), { stdio: [ttyFd, 'inherit', 'inherit'], shell: true, detached: false }); claudeProcess.on('error', (err) => { console.log('\x1b[31m✗ Error:\x1b[0m', err.message); cleanup(); process.exit(1); }); claudeProcess.on('exit', (code) => { cleanup(); console.log('\x1b[32m✓ Claude exited.\x1b[0m'); process.exit(code || 0); }); // Monitor for Ctrl+G in a separate stdin listener // This runs IN PARALLEL - doesn't slow down Claude! function setupHotkeyMonitor() { if (!process.stdin.isTTY) return; process.stdin.setRawMode(true); readline.emitKeypressEvents(process.stdin); process.stdin.on('data', () => { // Just consume data to prevent it from echoing // Claude gets the real input from /dev/tty }); process.stdin.on('keypress', (_str, key) => { if (monitoringKeys && key && key.ctrl && key.name === 'g' && !gameProcess) { launchGame(); } }); } function cleanup() { monitoringKeys = false; if (process.stdin.isTTY) { process.stdin.setRawMode(false); } process.stdin.removeAllListeners('keypress'); process.stdin.removeAllListeners('data'); try { fs.closeSync(ttyFd); } catch (e) { // ignore } } function launchGame() { if (!claudeProcess || gameProcess) return; monitoringKeys = false; cleanup(); gameProcess = (0, child_process_1.spawn)('npm', ['run', 'game'], { stdio: 'inherit', shell: true, cwd: __dirname + '/..' }); gameProcess.on('close', () => { gameProcess = null; setupHotkeyMonitor(); monitoringKeys = true; }); gameProcess.on('error', () => { gameProcess = null; setupHotkeyMonitor(); monitoringKeys = true; }); } process.on('SIGINT', () => { if (claudeProcess) claudeProcess.kill('SIGINT'); if (gameProcess) gameProcess.kill('SIGINT'); cleanup(); process.exit(0); }); process.on('exit', cleanup); setupHotkeyMonitor();