diff --git a/index.js b/index.js index f9407ffea4..f368b6e40d 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const crossSpawn = require('cross-spawn'); const stripFinalNewline = require('strip-final-newline'); const npmRunPath = require('npm-run-path'); const pFinally = require('p-finally'); +const onetime = require('onetime'); const makeError = require('./lib/error'); const normalizeStdio = require('./lib/stdio'); const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill'); @@ -152,13 +153,15 @@ const execa = (file, args, options) => { }; }; + const handlePromiseOnce = onetime(handlePromise); + crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed); handleInput(spawned, parsed.options.input); spawned.all = makeAllStream(spawned); - return mergePromise(spawned, handlePromise); + return mergePromise(spawned, handlePromiseOnce); }; module.exports = execa; diff --git a/package.json b/package.json index 49c3db1410..57e69814c0 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "is-stream": "^2.0.0", "merge-stream": "^2.0.0", "npm-run-path": "^3.0.0", + "onetime": "^5.1.0", "p-finally": "^2.0.0", "signal-exit": "^3.0.2", "strip-final-newline": "^2.0.0" diff --git a/test/test.js b/test/test.js index 3aa68b47c1..45b6cc2c00 100644 --- a/test/test.js +++ b/test/test.js @@ -138,6 +138,12 @@ test('child_process.spawnSync() errors are propagated with a correct shape', t = t.true(failed); }); +test('do not try to consume streams twice', async t => { + const cp = execa('noop', ['foo']); + t.is((await cp).stdout, 'foo'); + t.is((await cp).stdout, 'foo'); +}); + test('use relative path with \'..\' chars', async t => { const pathViaParentDir = path.join('..', path.basename(path.dirname(__dirname)), 'test', 'fixtures', 'noop'); const {stdout} = await execa(pathViaParentDir, ['foo']);