From 70629c6225ce5278184fa8136a45769e7a04fbec Mon Sep 17 00:00:00 2001 From: James Talmage Date: Sat, 23 Apr 2016 13:27:32 -0400 Subject: [PATCH] proof of concept - #14 alternative Implements an alternative to #14. --- index.js | 16 ++++++++++++++++ test.js | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/index.js b/index.js index 4a5b951a8d..dc749e848f 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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; }; diff --git a/test.js b/test.js index 4ba568e1ba..c6e0f4794e 100644 --- a/test.js +++ b/test.js @@ -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');