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

add execa.stdout() shortcut #14

Merged
merged 1 commit into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixtures/noop-err
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node
'use strict';
console.error(process.argv[2]);
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ module.exports = function (cmd, args, opts) {
return spawned;
};

module.exports.stdout = function () {
// TODO: set `stderr: 'ignore'` when that option is implemented
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferring this until #24 is done.

return module.exports.apply(null, arguments).then(function (x) {
return x.stdout;
});
};

module.exports.stderr = function () {
// TODO: set `stdout: 'ignore'` when that option is implemented
return module.exports.apply(null, arguments).then(function (x) {
return x.stderr;
});
};

module.exports.shell = function (cmd, opts) {
return handleShell(module.exports, cmd, opts);
};
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#c

The `child_process` instance is enhanced to also be promise for a result object with `stdout` and `stderr` properties.

### execa.stdout(file, [arguments], [options])

Same as `execa()`, but returns only `stdout`.

Copy link
Owner Author

@sindresorhus sindresorhus Apr 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamestalmage You mentioning something about memory benefits. Should we mention that? Do you have any source for it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not true until you set stderr: ignore and stop buffering the output.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know, but it will be true, so let's pretend it is, and we can add that info later. Just wondering now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No source, I just assumed if you ignore it it dumps the output and does not concat it all together in memory. I think it's a safe assumption.

### execa.stderr(file, [arguments], [options])

Same as `execa()`, but returns only `stderr`.

### execa.shell(command, [options])

Execute a command through the system shell. Prefer `execa()` whenever possible, as it's both faster and safer.
Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ test('buffer', async t => {
t.is(stdout.toString(), 'foo');
});

test('execa.stdout()', async t => {
const stdout = await m.stdout('noop', ['foo']);
t.is(stdout, 'foo');
});

test('execa.stderr()', async t => {
const stderr = await m.stderr('noop-err', ['foo']);
t.is(stderr, 'foo');
});

test('stdout/stderr available on errors', async t => {
const err = await t.throws(m('exit', ['2']));
t.is(typeof err.stdout, 'string');
Expand Down