Skip to content

Commit

Permalink
Add fork method - fixes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Aug 19, 2017
1 parent dc7e21b commit 1b3b4f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fixtures/ipc-process
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node
'use strict';
process.on('message', m => {
if (m === 'ping') {
process.send('pong');
} else {
process.send('rainbow');
}
});
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ module.exports = (cmd, args, opts) => {
return spawned;
};

module.exports.fork = function (cmd, args, opts) {
opts = Object.assign({
stdio: 'ipc'
}, opts);

if (opts.stdio === 'ipc') {
opts.stdio = [0, 1, 2, 'ipc'];
}

// TODO throw `new TypeError('Forked processes must have an IPC channel')` if no IPC channel is provided
// TODO throw `new Error('Child process can have only one IPC pipe')` when multiple IPC channels are provided
return module.exports(cmd, args, opts);
};

module.exports.stdout = function () {
// TODO: set `stderr: 'ignore'` when that option is implemented
return module.exports.apply(null, arguments).then(x => x.stdout);
Expand Down
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ test('execa.shellSync()', t => {
t.is(stdout, 'foo');
});

test.cb('execa.fork()', t => {
const cp = m.fork('fixtures/ipc-process');

cp.on('message', m => {
t.is(m, 'pong');
t.end();
});

cp.send('ping');
});

test('stripEof option', async t => {
const {stdout} = await m('noop', ['foo'], {stripEof: false});
t.is(stdout, 'foo\n');
Expand Down

0 comments on commit 1b3b4f2

Please sign in to comment.