Skip to content

Commit

Permalink
add execa.stdout() shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 24, 2016
1 parent 5fe7261 commit 1f25008
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
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
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`.

### 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

0 comments on commit 1f25008

Please sign in to comment.