| 263 | } |
| 264 | |
| 265 | export function runCmdSync(cmd: string, args: string[], options: ExecOptions = {}): ExecResult { |
| 266 | const executable = normalizeExecutableCommand(cmd); |
| 267 | const result = spawnSync(executable, args, { |
| 268 | cwd: options.cwd, |
| 269 | env: options.env, |
| 270 | stdio: ['pipe', 'pipe', 'pipe'], |
| 271 | encoding: options.binaryStdout ? undefined : 'utf8', |
| 272 | input: options.stdin, |
| 273 | timeout: normalizeTimeoutMs(options.timeoutMs), |
| 274 | windowsHide: true, |
| 275 | shell: false, |
| 276 | ...(options.maxBuffer !== undefined ? { maxBuffer: options.maxBuffer } : {}), |
| 277 | }); |
| 278 | |
| 279 | if (result.error) { |
| 280 | const code = (result.error as NodeJS.ErrnoException).code; |
| 281 | if (code === 'ETIMEDOUT') { |
| 282 | throw new AppError( |
| 283 | 'COMMAND_FAILED', |
| 284 | `${executable} timed out after ${normalizeTimeoutMs(options.timeoutMs)}ms`, |
| 285 | { |
| 286 | cmd, |
| 287 | args, |
| 288 | timeoutMs: normalizeTimeoutMs(options.timeoutMs), |
| 289 | }, |
| 290 | result.error, |
| 291 | ); |
| 292 | } |
| 293 | if (code === 'ENOENT') { |
| 294 | throw createMissingToolError(executable, cmd, result.error); |
| 295 | } |
| 296 | throw createCommandFailedError(executable, cmd, args, result.error); |
| 297 | } |
| 298 | |
| 299 | const stdoutBuffer = options.binaryStdout |
| 300 | ? Buffer.isBuffer(result.stdout) |
| 301 | ? result.stdout |
| 302 | : Buffer.from(result.stdout ?? '') |
| 303 | : undefined; |
| 304 | const stdout = options.binaryStdout |
| 305 | ? '' |
| 306 | : typeof result.stdout === 'string' |
| 307 | ? result.stdout |
| 308 | : (result.stdout ?? '').toString(); |
| 309 | const stderr = |
| 310 | typeof result.stderr === 'string' ? result.stderr : (result.stderr ?? '').toString(); |
| 311 | const exitCode = result.status ?? 1; |
| 312 | |
| 313 | if (exitCode !== 0 && !options.allowFailure) { |
| 314 | throw createExitError(executable, cmd, args, exitCode, stdout, stderr); |
| 315 | } |
| 316 | |
| 317 | return { stdout, stderr, exitCode, stdoutBuffer }; |
| 318 | } |
| 319 | |
| 320 | export function runCmdDetached( |
| 321 | cmd: string, |