Skip to content

Commit

Permalink
childProcess: Fix passing in streams
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Yen <mark.yen@suse.com>
  • Loading branch information
mook-as committed May 12, 2021
1 parent 9e11763 commit 9585be5
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/utils/childProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,31 @@ export async function spawnFile(

// Spawn the child, overriding options.stdio. This is necessary to support
// transcoding the output.
const child = spawn(command, args || [], Object.create(options, { stdio: { value: stdio } }));
const child = spawn(command, args || [], { ...options, stdio });
const resultMap: Record<number, 'stdout' | 'stderr'> = { 1: 'stdout', 2: 'stderr' };
const result: { stdout?: string, stderr?: string } = {};

if (Array.isArray(stdio)) {
for (const i of [1, 2]) {
if (stdio[i] === 'pipe') {
const encoding = encodings[i];
const childStream = child[resultMap[i]];

if (!stdStreams[i]) {
result[resultMap[i]] = '';
}
if (encoding) {
child[resultMap[i]].setEncoding(encoding);
}
child[resultMap[i]].on('data', (chunk) => {
if (stdStreams[i]) {
stdStreams[i]?.write(chunk);
} else {
result[resultMap[i]] += chunk;
if (childStream) {
if (encoding) {
childStream.setEncoding(encoding);
}
});
childStream.on('data', (chunk) => {
if (stdStreams[i]) {
stdStreams[i]?.write(chunk);
} else {
result[resultMap[i]] += chunk;
}
});
}
}
}
}
Expand Down

0 comments on commit 9585be5

Please sign in to comment.