Skip to content

Commit

Permalink
feat: Validate cli flags
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Feb 28, 2018
1 parent 35b95d2 commit 9f9bb51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[ignore]
.*/.nyc_output/.*
.*/coverage/.*
.*/fixtures/.*
.*/.vscode/.*
.*/assets/.*
.*/node_modules/.*
34 changes: 34 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,50 @@
/* @flow */

const meow = require("meow");
const chalk = require("chalk");
const defaultCommand = require("./commands/default");
const byCommand = require("./commands/by");
const helpCommand = require("./commands/help");
const knownFlags = [
"by",
"modulesOnly",
"filesOnly",
"directOnly",
"transitiveOnly",
"duplicatesOnly",
"limit",
"version",
"help"
];

const validateFlags = flags => {
const invalidFlags = Object.keys(flags).reduce((acc, flag) => {
if (!knownFlags.includes(flag)) {
acc.push(flag);
}
return acc;
}, []);

return invalidFlags;
};

const { pkg, input, flags, showHelp } = meow(helpCommand(), {
argv: process.argv.slice(2),
autoHelp: false
});

const start = Date.now();
const invalidFlags = validateFlags(flags);

if (invalidFlags.length) {
console.log();
console.log(chalk.red(` Unsupported option: ${invalidFlags.join(", ")}`));
console.log();
console.log(
` Use ${chalk.yellow("--help")} to see a list of available options...`
);
process.exit(1);
}

if (!input || !input.length || !input[0].match(".json") || flags.help) {
showHelp(0);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
},
"pre-commit": [
"lint:flow",
"lint:staged",
"test"
"lint:staged"
],
"lint-staged": {
"*.js": [
Expand Down

0 comments on commit 9f9bb51

Please sign in to comment.