Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
Check public method arguments via ow
Browse files Browse the repository at this point in the history
  • Loading branch information
jblandry committed Jul 9, 2018
1 parent e655a28 commit 603f1ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const STATIC = global.___AbsolunetCli___ ? global.___AbsolunetCli___ : global.__



const owIsMeow = ow.create(ow.object.nonEmpty.hasKeys('input', 'flags', 'pkg', 'help').label('meowCli'));



//-- Command details
const cmdDetails = (cmd) => {
const [task, subtask] = cmd.split(' ');
Expand Down Expand Up @@ -109,16 +113,23 @@ module.exports = class Cli {
}

static optional(name) {
ow(name, ow.string.nonEmpty);

return `${chalk.reset('[')}${chalk.yellow(name)}${chalk.reset(']')}`;
}

static optionalPlaceholder(name) {
ow(name, ow.string.nonEmpty);

return `${chalk.reset('[')}${this.placeholder(name)}${chalk.reset(']')}`;
}


//-- Set tasks
static init({ pkgPath, pkg } = {}) {
ow(pkgPath, ow.any(ow.undefined, ow.string.nonEmpty));
ow(pkg, ow.any(ow.undefined, ow.object.nonEmpty));

delete require.cache[__filename];

STATIC.pkgPath = pkgPath || path.dirname(module.parent.filename);
Expand All @@ -128,6 +139,8 @@ module.exports = class Cli {

//-- Set tasks
static setUsageTasks(commands) {
ow(commands, ow.object.nonEmpty);

STATIC.commands = commands;
initAutocomplete();
}
Expand All @@ -141,6 +154,9 @@ module.exports = class Cli {

//-- Set full usage
static setFullUsage(fullUsage, { showBin = true } = {}) {
ow(fullUsage, ow.object.nonEmpty);
ow(showBin, ow.boolean);

STATIC.fullUsage = fullUsage;
STATIC.showBin = showBin;
}
Expand Down Expand Up @@ -180,6 +196,8 @@ module.exports = class Cli {

//-- Get task usage
static getTaskUsage(task) {
ow(task, ow.any(ow.undefined, ow.string.nonEmpty));

if (task) {
const subs = !Array.isArray(STATIC.commands[task]);

Expand Down Expand Up @@ -212,6 +230,8 @@ module.exports = class Cli {

//-- Show task usage and die
static showTaskUsage(meowCli) {
owIsMeow(meowCli);

terminal.echo(`\n${this.getTaskUsage(meowCli.input[0])}`);
terminal.exit();
}
Expand All @@ -231,27 +251,36 @@ module.exports = class Cli {

//-- Refuse arguments
static refuseArguments(meowCli) {
owIsMeow(meowCli);

if (meowCli.input.length > 1) {
this.showTaskUsage(meowCli);
}
}

//-- Refuse flags
static refuseFlags(meowCli) {
owIsMeow(meowCli);

if (Object.keys(meowCli.flags).length) {
this.showTaskUsage(meowCli);
}
}

//-- Refuse flags & arguments
static refuseFlagsAndArguments(meowCli) {
owIsMeow(meowCli);

if (meowCli.input.length > 1 || Object.keys(meowCli.flags).length) {
this.showTaskUsage(meowCli);
}
}

//-- Accept only these flags
static validateFlags(meowCli, flagValidations) {
owIsMeow(meowCli);
ow(flagValidations, ow.object.nonEmpty);

const inputFlags = Object.keys(meowCli.flags);
const allowedFlags = Object.keys(flagValidations);

Expand Down Expand Up @@ -280,6 +309,8 @@ module.exports = class Cli {

//-- List tasks files
static initTasksList(tasksPath) {
ow(tasksPath, ow.string.nonEmpty);

STATIC.tasks.path = tasksPath;

const tasks = [];
Expand All @@ -294,6 +325,8 @@ module.exports = class Cli {

//-- Route to good task
static tasksRouter(meowCli) {
owIsMeow(meowCli);

const [task] = meowCli.input;

if (task) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@absolunet/cli",
"version": "0.7.0",
"version": "0.7.1",
"description": "CLI utilities",
"definition": "",
"homepage": "https://github.com/absolunet/node-cli",
Expand Down

0 comments on commit 603f1ae

Please sign in to comment.