Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stdio option for execa.node() #303

Merged
merged 5 commits into from Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/stdio.js
Expand Up @@ -42,20 +42,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 @@ -59,14 +59,17 @@ test(stdioMacro, {stdin: 'inherit', stdio: [undefined, 'pipe']}, new Error('It\'

const forkMacro = createMacro(stdio.node);

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`'));