(
child: StreamableChildProcess,
stream: fs.WriteStream,
options: {
endStreamOnClose: boolean;
writer: LineWriter;
},
)
| 65 | }; |
| 66 | |
| 67 | export function attachChildToStream( |
| 68 | child: StreamableChildProcess, |
| 69 | stream: fs.WriteStream, |
| 70 | options: { |
| 71 | endStreamOnClose: boolean; |
| 72 | writer: LineWriter; |
| 73 | }, |
| 74 | ): Promise<ExecResult> { |
| 75 | const stdout = child.stdout; |
| 76 | const stderr = child.stderr; |
| 77 | if (!stdout || !stderr) { |
| 78 | return Promise.resolve({ stdout: '', stderr: 'missing stdio pipes', exitCode: 1 }); |
| 79 | } |
| 80 | stdout.setEncoding('utf8'); |
| 81 | stderr.setEncoding('utf8'); |
| 82 | stdout.on('data', options.writer.onChunk); |
| 83 | stderr.on('data', options.writer.onChunk); |
| 84 | stream.on('error', () => { |
| 85 | if (!child.killed) child.kill('SIGKILL'); |
| 86 | }); |
| 87 | child.on('error', () => stream.destroy()); |
| 88 | return new Promise<ExecResult>((resolve) => { |
| 89 | child.on('close', (code) => { |
| 90 | options.writer.flush(); |
| 91 | if (options.endStreamOnClose) stream.end(); |
| 92 | resolve({ stdout: '', stderr: '', exitCode: code ?? 1 }); |
| 93 | }); |
| 94 | }); |
| 95 | } |
no test coverage detected