|
| 1 | +import { spawn } from 'child_process' |
| 2 | +import * as os from 'os' |
| 3 | +import * as path from 'path' |
| 4 | + |
| 5 | +import { buildArray } from '../../../common/src/util/array' |
| 6 | + |
| 7 | +export function runTerminalCommand({ |
| 8 | + command, |
| 9 | + process_type, |
| 10 | + cwd, |
| 11 | + timeout_seconds, |
| 12 | +}: { |
| 13 | + command: string |
| 14 | + process_type: 'SYNC' | 'BACKGROUND' |
| 15 | + cwd: string |
| 16 | + timeout_seconds: number |
| 17 | +}): Promise<{ output: string }> { |
| 18 | + if (process_type === 'BACKGROUND') { |
| 19 | + throw new Error('BACKGROUND process_type not implemented') |
| 20 | + } |
| 21 | + |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + const isWindows = os.platform() === 'win32' |
| 24 | + const shell = isWindows ? 'cmd.exe' : 'bash' |
| 25 | + const shellArgs = isWindows ? ['/c'] : ['-c'] |
| 26 | + |
| 27 | + // Resolve cwd to absolute path |
| 28 | + const resolvedCwd = path.resolve(cwd) |
| 29 | + |
| 30 | + const childProcess = spawn(shell, [...shellArgs, command], { |
| 31 | + cwd: resolvedCwd, |
| 32 | + env: { |
| 33 | + ...process.env, |
| 34 | + FORCE_COLOR: '1', |
| 35 | + CLICOLOR: '1', |
| 36 | + CLICOLOR_FORCE: '1', |
| 37 | + }, |
| 38 | + stdio: 'pipe', |
| 39 | + }) |
| 40 | + |
| 41 | + let stdout = '' |
| 42 | + let stderr = '' |
| 43 | + let timer: NodeJS.Timeout | null = null |
| 44 | + let processFinished = false |
| 45 | + |
| 46 | + // Set up timeout if timeout_seconds >= 0 (infinite timeout when < 0) |
| 47 | + if (timeout_seconds >= 0) { |
| 48 | + timer = setTimeout(() => { |
| 49 | + if (!processFinished) { |
| 50 | + processFinished = true |
| 51 | + childProcess.kill('SIGTERM') |
| 52 | + reject( |
| 53 | + new Error(`Command timed out after ${timeout_seconds} seconds`), |
| 54 | + ) |
| 55 | + } |
| 56 | + }, timeout_seconds * 1000) |
| 57 | + } |
| 58 | + |
| 59 | + // Collect stdout |
| 60 | + childProcess.stdout.on('data', (data: Buffer) => { |
| 61 | + stdout += data.toString() |
| 62 | + }) |
| 63 | + |
| 64 | + // Collect stderr |
| 65 | + childProcess.stderr.on('data', (data: Buffer) => { |
| 66 | + stderr += data.toString() |
| 67 | + }) |
| 68 | + |
| 69 | + // Handle process completion |
| 70 | + childProcess.on('close', (exitCode) => { |
| 71 | + if (processFinished) return |
| 72 | + processFinished = true |
| 73 | + |
| 74 | + if (timer) { |
| 75 | + clearTimeout(timer) |
| 76 | + } |
| 77 | + |
| 78 | + // Include stderr in stdout for compatibility with existing behavior |
| 79 | + const combinedOutput = buildArray([ |
| 80 | + `\`\`\`stdout\n${stdout}\`\`\``, |
| 81 | + stderr && `\`\`\`stderr\n${stderr}\`\`\``, |
| 82 | + exitCode !== null && `\`\`\`exit_code\n${exitCode}\`\`\``, |
| 83 | + ]).join('\n\n') |
| 84 | + |
| 85 | + resolve({ output: combinedOutput }) |
| 86 | + }) |
| 87 | + |
| 88 | + // Handle spawn errors |
| 89 | + childProcess.on('error', (error) => { |
| 90 | + if (processFinished) return |
| 91 | + processFinished = true |
| 92 | + |
| 93 | + if (timer) { |
| 94 | + clearTimeout(timer) |
| 95 | + } |
| 96 | + |
| 97 | + reject(new Error(`Failed to spawn command: ${error.message}`)) |
| 98 | + }) |
| 99 | + }) |
| 100 | +} |
0 commit comments