claude-arcade
Version:
Add classic arcade games to your Claude Code workflow with Ctrl+G
141 lines (140 loc) • 5.44 kB
JavaScript
;
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 readline = __importStar(require("readline"));
let claudeProcess = null;
let gameProcess = null;
console.clear();
console.log('\x1b[36m╔════════════════════════════════════════════════╗\x1b[0m');
console.log('\x1b[36m║ Claude Code + Game (Ctrl+G) - FAST ║\x1b[0m');
console.log('\x1b[36m╚════════════════════════════════════════════════╝\x1b[0m');
console.log('');
console.log('\x1b[32m→ Starting Claude Code with instant forwarding...\x1b[0m');
console.log('\x1b[35m→ Press Ctrl+G to launch the game\x1b[0m');
console.log('');
// Give terminal time to clear before starting Claude
setTimeout(() => {
console.log('\x1b[33m[DEBUG] Spawning Claude process...\x1b[0m');
// Start Claude with piped stdin
claudeProcess = (0, child_process_1.spawn)('claude', process.argv.slice(2), {
stdio: ['pipe', 'inherit', 'inherit'],
shell: false // Don't use shell - direct spawn
});
console.log('\x1b[33m[DEBUG] Claude process spawned, PID:\x1b[0m', claudeProcess.pid);
claudeProcess.on('error', (err) => {
console.log('\x1b[31m✗ Error starting Claude:\x1b[0m', err.message);
console.log('\x1b[33m→ Make sure "claude" command is installed and in PATH\x1b[0m');
cleanup();
process.exit(1);
});
claudeProcess.on('spawn', () => {
console.log('\x1b[32m✓ Claude started successfully\x1b[0m');
// Wait a bit before setting up forwarding to let Claude initialize
setTimeout(() => {
setupForwarding();
}, 200);
});
claudeProcess.on('exit', (code, signal) => {
console.log('\x1b[33m[DEBUG] Claude exited with code:\x1b[0m', code, 'signal:', signal);
cleanup();
console.log('\x1b[32m✓ Claude exited.\x1b[0m');
process.exit(code || 0);
});
// Debug stderr if Claude outputs anything
claudeProcess.stderr?.on('data', (data) => {
console.log('\x1b[31m[CLAUDE STDERR]:\x1b[0m', data.toString());
});
}, 100);
// Set up instant forwarding with Ctrl+G detection
function setupForwarding() {
if (!process.stdin.isTTY)
return;
process.stdin.setRawMode(true);
readline.emitKeypressEvents(process.stdin);
process.stdin.on('keypress', (str, key) => {
// Check for Ctrl+G FIRST
if (key && key.ctrl && key.name === 'g' && !gameProcess) {
// DON'T forward Ctrl+G to Claude, just launch game
launchGame();
return;
}
// Forward EVERYTHING ELSE immediately to Claude
if (claudeProcess && claudeProcess.stdin && claudeProcess.stdin.writable) {
if (key && key.sequence) {
claudeProcess.stdin.write(key.sequence);
}
else if (str) {
claudeProcess.stdin.write(str);
}
}
});
}
function cleanup() {
if (process.stdin.isTTY) {
process.stdin.setRawMode(false);
}
process.stdin.removeAllListeners('keypress');
}
function launchGame() {
if (!claudeProcess || gameProcess)
return;
// Disable forwarding while game runs
cleanup();
gameProcess = (0, child_process_1.spawn)('npm', ['run', 'play'], {
stdio: 'inherit',
shell: true,
cwd: __dirname + '/..'
});
gameProcess.on('close', () => {
gameProcess = null;
// Re-enable forwarding
setupForwarding();
});
gameProcess.on('error', () => {
gameProcess = null;
setupForwarding();
});
}
process.on('SIGINT', () => {
if (claudeProcess)
claudeProcess.kill('SIGINT');
if (gameProcess)
gameProcess.kill('SIGINT');
cleanup();
process.exit(0);
});
process.on('exit', cleanup);