From ce8392e4a3207e31366f44f616f66114a0300440 Mon Sep 17 00:00:00 2001 From: Chris Andreae Date: Tue, 28 May 2019 16:39:15 +0900 Subject: [PATCH] Don't wait until process exit to consume from stdout/err pipes Otherwise they will fill up and cause the child process to block. --- source/index.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/source/index.js b/source/index.js index 912f27b..bbda57e 100644 --- a/source/index.js +++ b/source/index.js @@ -52,19 +52,18 @@ module.exports = { commands.push(cp); const commandPromise = new Promise((resolve, reject) => { - cp.on('exit', exitCode => { - const res = timer.stop(); + let stdoutPromise = Promise.resolve(''); + let stderrPromise = Promise.resolve(''); - let stdoutPromise = Promise.resolve(''); - let stderrPromise = Promise.resolve(''); - - if (cp.stdout) { - stdoutPromise = streamToString(cp.stdout); - } + if (cp.stdout) { + stdoutPromise = streamToString(cp.stdout); + } + if (cp.stderr) { + stderrPromise = streamToString(cp.stderr); + } - if (cp.stderr) { - stderrPromise = streamToString(cp.stderr); - } + cp.on('exit', exitCode => { + const res = timer.stop(); Promise.all([stdoutPromise, stderrPromise]).then(results => { const args = {