2222
2323// MODULES //
2424
25- var resolve = require ( 'path' ) . resolve ;
25+ var path = require ( 'path' ) ;
2626var spawn = require ( 'child_process' ) . spawn ;
27- var readFileSync = require ( './../lib/node_modules/@stdlib/fs/read-file' ) . sync ;
28- var cwd = require ( './../lib/node_modules/@stdlib/process/cwd' ) ;
29- var ENV = require ( './../lib/node_modules/@stdlib/process/env' ) ;
30- var CLI = require ( './../lib/node_modules/@stdlib/cli/ctor' ) ;
31- var getKeys = require ( './../lib/node_modules/@stdlib/utils/keys' ) ;
27+ var CLI = require ( '@stdlib/cli/ctor' ) ;
28+ var parentPath = require ( '@stdlib/fs/resolve-parent-path' ) . sync ;
29+ var readFile = require ( '@stdlib/fs/read-file' ) . sync ;
30+ var hasOwnProp = require ( '@stdlib/assert/has-own-property' ) ;
31+ var objectKeys = require ( '@stdlib/utils/keys' ) ;
32+ var dirname = require ( '@stdlib/utils/dirname' ) ;
33+ var cwd = require ( '@stdlib/process/cwd' ) ;
34+ var ENV = require ( '@stdlib/process/env' ) ;
3235var COMMANDS = require ( './cli_commands.json' ) ;
3336
3437
@@ -55,16 +58,40 @@ function findCommand( cmd ) {
5558* Resolves a CLI file path.
5659*
5760* @private
58- * @param {string } path - package name/path
61+ * @param {string } pkgPath - package name/path
62+ * @throws {Error } unable to resolve CLI
5963* @returns {string } file path
6064*/
61- function getPath ( path ) {
62- if ( / ^ @ s t d l i b / . test ( path ) ) {
63- // WARNING: we assume that CLIs follow the `./bin/cli` convention
64- return resolve ( __dirname , '..' , 'lib' , 'node_modules' , path , 'bin' , 'cli' ) ;
65+ function getPath ( pkgPath ) {
66+ var mpath ;
67+ var cmds ;
68+ var pkg ;
69+
70+ if ( / ^ @ s t d l i b / . test ( pkgPath ) ) {
71+ // Resolve a package's main entry point:
72+ mpath = require . resolve ( pkgPath ) ;
73+
74+ // Resolve a dependency's path by finding the dependency's `package.json`:
75+ mpath = parentPath ( 'package.json' , {
76+ 'dir' : dirname ( mpath )
77+ } ) ;
78+
79+ // Read the package meta data:
80+ pkg = require ( mpath ) ; // eslint-disable-line stdlib/no-dynamic-require
81+
82+ // Resolve a `bin` field:
83+ if ( ! hasOwnProp ( pkg , 'bin' ) ) {
84+ throw new Error ( 'unexpected error. Unable to resolve command.' ) ;
85+ }
86+ cmds = objectKeys ( pkg . bin ) ;
87+ if ( cmds . length === 0 ) {
88+ throw new Error ( 'unexpected error. Unable to resolve command.' ) ;
89+ }
90+ // NOTE: assume that the first command is the desired command:
91+ return path . join ( dirname ( mpath ) , pkg . bin [ cmds [ 0 ] ] ) ;
6592 }
6693 // Assume we are given a path relative to the root directory:
67- return resolve ( __dirname , '..' , path ) ;
94+ return path . resolve ( __dirname , '..' , pkgPath ) ;
6895}
6996
7097
@@ -92,7 +119,7 @@ function main() {
92119 cli = new CLI ( {
93120 'pkg' : require ( './../package.json' ) ,
94121 'options' : require ( './cli_opts.json' ) ,
95- 'help' : readFileSync ( resolve ( __dirname , 'usage.txt' ) , {
122+ 'help' : readFile ( path . resolve ( __dirname , 'usage.txt' ) , {
96123 'encoding' : 'utf8'
97124 } )
98125 } ) ;
@@ -128,7 +155,7 @@ function main() {
128155 for ( i = 1 ; i < args . length ; i ++ ) {
129156 subargs . push ( args [ i ] ) ;
130157 }
131- keys = getKeys ( flags ) ;
158+ keys = objectKeys ( flags ) ;
132159 for ( i = 0 ; i < keys . length ; i ++ ) {
133160 key = keys [ i ] ;
134161 if (
0 commit comments