Skip to content

Commit f1f2aa4

Browse files
committed
Move finding a command to a function
1 parent dbcbf9c commit f1f2aa4

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

bin/cli

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ function onError( error ) {
4444
console.error( 'Error: %s', error.message ); // eslint-disable-line no-console
4545
}
4646

47+
/**
48+
* Finds a command in the command list.
49+
*
50+
* @private
51+
* @param {string} name - command name
52+
* @returns {(Object|null)} command info
53+
*/
54+
function findCommand( name ) {
55+
return COMMANDS[ name ] || null;
56+
}
57+
4758

4859
// MAIN //
4960

@@ -78,15 +89,15 @@ function main() {
7889
if ( args[ 1 ] === void 0 ) {
7990
return cli.help();
8091
}
81-
cmd = COMMANDS[ args[ 1 ] ];
82-
if ( cmd === void 0 ) {
92+
cmd = findCommand( args[ 1 ] );;
93+
if ( cmd === null ) {
8394
return onError( new Error( 'invalid argument. Unrecognized/unsupported command. Value: `' + args[ 1 ] + '`.' ) );
8495
}
8596
cmd = resolve( __dirname, '..', cmd.path );
8697
subargs = [ cmd, '--help' ];
8798
} else {
88-
cmd = COMMANDS[ args[ 0 ] ];
89-
if ( cmd === void 0 ) {
99+
cmd = findCommand( args[ 0 ] );;
100+
if ( cmd === null ) {
90101
return onError( new Error( 'invalid argument. Unrecognized/unsupported command. Value: `' + args[ 0 ] + '`.' ) );
91102
}
92103
cmd = resolve( __dirname, '..', cmd.path );

bin/commands.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
{
2+
"help [command]": {
3+
"cmd": "help [command]",
4+
"desc": "Print a help message.",
5+
"path": ""
6+
},
27
"bundle-pkg-list": {
8+
"cmd": "bundle-pkg-list",
39
"desc": "Bundle a list of packages into a single file.",
410
"path": "./lib/node_modules/@stdlib/_tools/bundle/pkg-list/bin/cli"
511
},
612
"datasets": {
13+
"cmd": "datasets",
714
"desc": "Retrieve a dataset.",
815
"path": "./lib/node_modules/@stdlib/datasets/bin/cli"
916
},
10-
"help [command]": {
11-
"desc": "Print a help message.",
12-
"path": ""
13-
},
1417
"ls": {
18+
"cmd": "ls",
1519
"desc": "List package names.",
1620
"path": "./lib/node_modules/@stdlib/_tools/pkgs/names/bin/cli"
1721
},
1822
"ls-tree": {
23+
"cmd": "ls-tree",
1924
"desc": "Show package tree.",
2025
"path": "./lib/node_modules/@stdlib/_tools/pkgs/tree/bin/cli"
2126
},
2227
"ns": {
28+
"cmd": "ns",
2329
"desc": "Print namespace.",
2430
"path": "./lib/node_modules/@stdlib/namespace/bin/cli"
2531
},
2632
"repl": {
33+
"cmd": "repl",
2734
"desc": "Start a REPL.",
2835
"path": "./lib/node_modules/@stdlib/repl/bin/cli"
2936
}

0 commit comments

Comments
 (0)