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

misc(refactor): reduce code duplication use process.exitCode instead of process.exit #272

Merged
merged 3 commits into from
Feb 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,23 +235,14 @@ module.exports = function(...args) {

function processOptions(options) {
function ifArg(name, fn, init, finalize) {
if (Array.isArray(argv[name])) {
if (init) {
init();
}
argv[name].forEach(fn);
if (finalize) {
finalize();
}
} else if (typeof argv[name] !== "undefined" && argv[name] !== null) {
if (init) {
init();
}
fn(argv[name], -1);
if (finalize) {
finalize();
}
}
const isArray = Array.isArray(argv[name]);
const isSet = typeof argv[name] !== "undefined" && argv[name] !== null;
if (!isArray && !isSet) return;

init && init();
if (isArray) argv[name].forEach(fn);
else if (isSet) fn(argv[name], -1);
finalize && finalize();
}

function ifArgPair(name, fn, init, finalize) {
Expand Down
14 changes: 5 additions & 9 deletions bin/prepareOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

module.exports = function prepareOptions(options, argv) {
argv = argv || {};

options = handleExport(options);

if (Array.isArray(options)) {
options = options.map(_options => handleFunction(_options, argv));
} else {
options = handleFunction(options, argv);
}
return options;
return Array.isArray(options) ?
options.map(_options => handleFunction(_options, argv)) :
handleFunction(options, argv);
};

function handleExport(options) {
const isES6DefaultExported =
typeof options === "object" &&
options !== null &&
typeof options.default !== "undefined";
options = isES6DefaultExported ? options.default : options;
return options;

return isES6DefaultExported ? options.default : options;
}

function handleFunction(options, argv) {
Expand Down
2 changes: 1 addition & 1 deletion bin/process-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function processOptions(yargs, argv) {
if (typeof options.then === "function") {
options.then(processOptions).catch(function(err) {
console.error(err.stack || err);
process.exit();
process.exitCode = 1;
});
return;
}
Expand Down