Skip to content

Commit

Permalink
Refactor stdio option for execa.node() (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 19, 2019
1 parent 2c65870 commit 825f8a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
19 changes: 10 additions & 9 deletions lib/stdio.js
Expand Up @@ -32,20 +32,21 @@ const stdio = opts => {

module.exports = stdio;

// `ipc` is pushed unless it is already present
module.exports.node = opts => {
const defaultOption = 'pipe';
const stdioOption = stdio(opts);

let stdioOption = stdio(opts || {stdio: defaultOption});
if (stdioOption === 'ipc') {
return 'ipc';
}

if (typeof stdioOption === 'string') {
stdioOption = [...new Array(3)].fill(stdioOption);
} else if (Array.isArray(stdioOption)) {
stdioOption = stdioOption.map((channel = defaultOption) => channel);
if (stdioOption === undefined || typeof stdioOption === 'string') {
return [stdioOption, stdioOption, stdioOption, 'ipc'];
}

if (!stdioOption.includes('ipc')) {
stdioOption.push('ipc');
if (stdioOption.includes('ipc')) {
return stdioOption;
}

return stdioOption;
return [...stdioOption, 'ipc'];
};
9 changes: 6 additions & 3 deletions test/stdio.js
Expand Up @@ -50,14 +50,17 @@ test(stdioMacro, {stdin: 0, stdio: 'pipe'}, new Error('It\'s not possible to pro
const forkMacro = (...args) => macro(...args, stdio.node);
forkMacro.title = macroTitle('execa.fork()');

test(forkMacro, undefined, ['pipe', 'pipe', 'pipe', 'ipc']);
test(forkMacro, undefined, [undefined, undefined, undefined, 'ipc']);
test(forkMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore', 'ipc']);
test(forkMacro, {stdio: 'ipc'}, 'ipc');
test(forkMacro, {stdio: [0, 1, 2]}, [0, 1, 2, 'ipc']);
test(forkMacro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3, 'ipc']);
test(forkMacro, {stdio: [0, 1, 2, 'ipc']}, [0, 1, 2, 'ipc']);

test(forkMacro, {stdout: 'ignore'}, ['pipe', 'ignore', 'pipe', 'ipc']);
test(forkMacro, {stdout: 'ignore', stderr: 'ignore'}, ['pipe', 'ignore', 'ignore', 'ipc']);
test(forkMacro, {stdio: [0, 1, undefined]}, [0, 1, undefined, 'ipc']);
test(forkMacro, {stdio: [0, 1, 2, undefined]}, [0, 1, 2, undefined, 'ipc']);
test(forkMacro, {stdout: 'ignore'}, [undefined, 'ignore', undefined, 'ipc']);
test(forkMacro, {stdout: 'ignore', stderr: 'ignore'}, [undefined, 'ignore', 'ignore', 'ipc']);

test(forkMacro, {stdio: {foo: 'bar'}}, new TypeError('Expected `stdio` to be of type `string` or `Array`, got `object`'));
test(forkMacro, {stdin: 'inherit', stdio: 'pipe'}, new Error('It\'s not possible to provide `stdio` in combination with one of `stdin`, `stdout`, `stderr`'));

0 comments on commit 825f8a7

Please sign in to comment.