Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mixing stream and promise consumption #351

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ declare namespace execa {
/**
Buffer the output from the spawned process. When set to `false`, you must read the output of `stdout` and `stderr` (or `all` if the `all` option is `true`). Otherwise the returned promise will not be resolved/rejected.

Should be set to `false` when the returned promise is not used.

If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.

@default true
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,20 @@ const execa = (file, args, options) => {
return mergePromise(dummySpawned, errorPromise);
}

spawned.all = makeAllStream(spawned, parsed.options);

const spawnedPromise = getSpawnedPromise(spawned);
const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
const processDone = setExitHandler(spawned, parsed.options, timedPromise);
const spawnedResult = getSpawnedResult(spawned, parsed.options, processDone);

const context = {isCanceled: false};

spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));
spawned.cancel = spawnedCancel.bind(null, spawned, context);

const handlePromise = async () => {
const [{error, code, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);
const [{error, code, signal, timedOut}, stdoutResult, stderrResult, allResult] = await spawnedResult;
const stdout = handleOutput(parsed.options, stdoutResult);
const stderr = handleOutput(parsed.options, stderrResult);
const all = handleOutput(parsed.options, allResult);
Expand Down Expand Up @@ -151,8 +154,6 @@ const execa = (file, args, options) => {

handleInput(spawned, parsed.options.input);

spawned.all = makeAllStream(spawned, parsed.options);

return mergePromise(spawned, handlePromiseOnce);
};

Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ Default: `true`

Buffer the output from the spawned process. When set to `false`, you must read the output of [`stdout`](#stdout-1) and [`stderr`](#stderr-1) (or [`all`](#all) if the [`all`](#all-2) option is `true`). Otherwise the returned promise will not be resolved/rejected.

Should be set to `false` when the returned promise is not used.

If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.

#### input
Expand Down
11 changes: 10 additions & 1 deletion test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import stream from 'stream';
import test from 'ava';
import getStream from 'get-stream';
import tempfile from 'tempfile';
import pEvent from 'p-event';
import execa from '..';

process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
Expand Down Expand Up @@ -138,7 +139,7 @@ test('do not buffer stderr when `buffer` set to `false`', async t => {
});

test('do not buffer when streaming', async t => {
const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10});
const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10, buffer: false});
const result = await getStream(stdout);
t.is(result, '....................\n');
});
Expand Down Expand Up @@ -184,3 +185,11 @@ test.serial('buffer: false > promise does not resolve when output is big and "al
const {timedOut} = await t.throwsAsync(cp);
t.true(timedOut);
});

test('can mix promise and streams', async t => {
const promise = execa('noop', ['test'], {all: true});
await pEvent(promise, 'exit');
const {stdout, all} = await promise;
t.is(stdout, 'test');
t.is(all, 'test');
});