| 421 | reject(new Error(`Failed to start system grep: ${err.message}`)); |
| 422 | }; |
| 423 | const onClose = (code: number | null) => { |
| 424 | const stdoutData = Buffer.concat(stdoutChunks).toString('utf8'); |
| 425 | const stderrData = Buffer.concat(stderrChunks) |
| 426 | .toString('utf8') |
| 427 | .trim(); |
| 428 | cleanup(); |
| 429 | if (code === 0) resolve(stdoutData); |
| 430 | else if (code === 1) |
| 431 | resolve(''); // No matches |
| 432 | else { |
| 433 | if (stderrData) |
| 434 | reject( |
| 435 | new Error( |
| 436 | `System grep exited with code ${code}: ${stderrData}`, |
| 437 | ), |
| 438 | ); |
| 439 | else resolve(''); // Exit code > 1 but no stderr, likely just suppressed errors |
| 440 | } |
| 441 | }; |
| 442 | |
| 443 | const cleanup = () => { |
| 444 | child.stdout.removeListener('data', onData); |