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 #302

Merged
merged 4 commits into from Jun 19, 2019
Merged
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
36 changes: 13 additions & 23 deletions lib/stdio.js
@@ -1,43 +1,33 @@
'use strict';
const alias = ['stdin', 'stdout', 'stderr'];
const aliases = ['stdin', 'stdout', 'stderr'];

const hasAlias = opts => alias.some(x => Boolean(opts[x]));
const hasAlias = opts => aliases.some(alias => Boolean(opts[alias]));

const stdio = opts => {
if (!opts) {
return;
}

if (opts.stdio && hasAlias(opts)) {
throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${alias.map(x => `\`${x}\``).join(', ')}`);
const {stdio} = opts;

if (stdio === undefined) {
return aliases.map(alias => opts[alias]);
}

if (typeof opts.stdio === 'string') {
return opts.stdio;
if (hasAlias(opts)) {
throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`);
}

const stdio = opts.stdio || [];
if (typeof stdio === 'string') {
return stdio;
}

if (!Array.isArray(stdio)) {
throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);
}

const result = [];
const len = Math.max(stdio.length, alias.length);

for (let i = 0; i < len; i++) {
let value;

if (stdio[i] !== undefined) {
value = stdio[i];
} else if (opts[alias[i]] !== undefined) {
value = opts[alias[i]];
}

result[i] = value;
}

return result;
const length = Math.max(stdio.length, aliases.length);
return Array.from({length}, (value, index) => stdio[index]);
};

module.exports = stdio;
Expand Down