Skip to content

Commit

Permalink
proof of concept - sindresorhus#14 alternative
Browse files Browse the repository at this point in the history
Implements an alternative to sindresorhus#14.
  • Loading branch information
jamestalmage committed Apr 23, 2016
1 parent 3d31e8d commit 70629c6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ function handleShell(fn, cmd, opts) {
return fn(file, args, opts);
}

function extendPromise(promise, property) {
Object.defineProperty(promise, property, {
configurable: true,
get: function () {
var value = promise.then(function (result) {
return result[property];
});
Object.defineProperty(promise, property, {value: value});
return value;
}
});
}

module.exports = function (cmd, args, opts) {
var spawned;

Expand Down Expand Up @@ -120,6 +133,9 @@ module.exports = function (cmd, args, opts) {
promise.kill = spawned.kill.bind(spawned);
promise.pid = spawned.pid;

extendPromise(promise, 'stdout');
extendPromise(promise, 'stderr');

return promise;
};

Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ test('helpful error trying to provide an input stream in sync mode', t => {
);
});

test('stdout is attached as a promise for the property', async t => {
const stdout = await m('stdin', [], {input: 'hello'}).stdout;
t.is(stdout, 'hello');
});

test('still works the other way', async t => {
const {stdout} = await m('stdin', [], {input: 'hello'});
t.is(stdout, 'hello');
});

test('execa() returns a promise with kill() and pid', t => {
const promise = m('noop', ['foo']);
t.is(typeof promise.kill, 'function');
Expand Down

0 comments on commit 70629c6

Please sign in to comment.