({
command,
process_type,
cwd,
timeout_seconds,
env,
}: {
command: string
process_type: 'SYNC' | 'BACKGROUND'
cwd: string
timeout_seconds: number
env?: NodeJS.ProcessEnv
})
| 117 | } |
| 118 | |
| 119 | export function runTerminalCommand({ |
| 120 | command, |
| 121 | process_type, |
| 122 | cwd, |
| 123 | timeout_seconds, |
| 124 | env, |
| 125 | }: { |
| 126 | command: string |
| 127 | process_type: 'SYNC' | 'BACKGROUND' |
| 128 | cwd: string |
| 129 | timeout_seconds: number |
| 130 | env?: NodeJS.ProcessEnv |
| 131 | }): Promise<CodebuffToolOutput<'run_terminal_command'>> { |
| 132 | if (process_type === 'BACKGROUND') { |
| 133 | throw new Error('BACKGROUND process_type not implemented') |
| 134 | } |
| 135 | |
| 136 | return new Promise((resolve, reject) => { |
| 137 | const isWindows = os.platform() === 'win32' |
| 138 | const processEnv = { |
| 139 | ...getSystemProcessEnv(), |
| 140 | ...(env ?? {}), |
| 141 | } as NodeJS.ProcessEnv |
| 142 | |
| 143 | let shell: string |
| 144 | let shellArgs: string[] |
| 145 | |
| 146 | if (isWindows) { |
| 147 | const bashPath = findWindowsBash(processEnv) |
| 148 | if (!bashPath) { |
| 149 | reject(createWindowsBashNotFoundError()) |
| 150 | return |
| 151 | } |
| 152 | shell = bashPath |
| 153 | shellArgs = ['-c'] |
| 154 | } else { |
| 155 | shell = 'bash' |
| 156 | shellArgs = ['-c'] |
| 157 | } |
| 158 | |
| 159 | // Resolve cwd to absolute path |
| 160 | const resolvedCwd = path.resolve(cwd) |
| 161 | |
| 162 | const childProcess = spawn(shell, [...shellArgs, command], { |
| 163 | cwd: resolvedCwd, |
| 164 | env: processEnv, |
| 165 | stdio: 'pipe', |
| 166 | }) |
| 167 | |
| 168 | let stdout = '' |
| 169 | let stderr = '' |
| 170 | let timer: NodeJS.Timeout | null = null |
| 171 | let processFinished = false |
| 172 | |
| 173 | // Set up timeout if timeout_seconds >= 0 (infinite timeout when < 0) |
| 174 | if (timeout_seconds >= 0) { |
| 175 | timer = setTimeout(() => { |
| 176 | if (!processFinished) { |
no test coverage detected