Skip to content

Commit

Permalink
quick fixes to handle CLI args errors (#306)
Browse files Browse the repository at this point in the history
* add cli-action-error-empty-args-missing-name.test.js

demonstrates correct behavior in this case

* add cli-action-error-undefined-args-missing-name.test.js

reproduces issue 305 in lib/cli-command.js

* add cli-action-error-too-many-args.test.js

which fails at this point, reproducing issue 119
(extra arguments silently ignored)

* throw error in case of too many arguments

(issue 119)

* throw explicit error in case of missing args with name

(issue 305)
  • Loading branch information
brodycj authored Apr 5, 2020
1 parent 5387a76 commit b2297b5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/cli-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ module.exports = {
description: 'creates a React Native library module for one or more platforms',
usage: '[options] <name>',
action: (args, options) => {
if (!args || args.length < 1) {
throw new Error('missing lib module name');
}

if (args.length > 1) {
throw new Error('too many arguments');
}

const name = args[0];

const beforeCreation = Date.now();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const action = require('../../../../../lib/cli-command.js').action;

test('create lib module with empty args - missing name', () => {
const args = [];

expect(() => { action(args, {}); }).toThrow('missing lib module name');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const action = require('../../../../../lib/cli-command.js').action;

test('create module with too many args', () => {
const args = ['name1', 'name2'];

// ref:
// https://github.com/brodybits/create-react-native-module/issues/119
expect(() => { action(args, {}); }).toThrow('too many arguments');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const action = require('../../../../../lib/cli-command.js').action;

test('create lib module with undefined args - missing name', () => {
const args = undefined;

// ref:
// https://github.com/brodybits/create-react-native-module/issues/305
expect(() => { action(args, {}); }).toThrow('missing lib module name');
});

0 comments on commit b2297b5

Please sign in to comment.