33
44// MODULES //
55
6- var fs = require ( 'fs' ) ;
7- var path = require ( 'path' ) ;
8- var parseArgs = require ( 'minimist' ) ;
9- var notifier = require ( 'update-notifier' ) ;
6+ var resolve = require ( 'path' ) . resolve ;
7+ var spawn = require ( 'child_process' ) . spawn ;
8+ var join = require ( 'path' ) . join ;
109var getKeys = require ( 'object-keys' ) . shim ( ) ;
1110var hasOwnProp = require ( '@stdlib/assert/has-own-property' ) ;
12- var pkg = require ( './../package.json' ) ;
11+ var readFileSync = require ( '@stdlib/fs/read-file' ) . sync ;
12+ var CLI = require ( '@stdlib/tools/cli' ) ;
1313var NAMES = require ( './../lib/names.json' ) ;
14- var opts = require ( './opts.json' ) ;
1514
1615
1716// FUNCTIONS //
1817
19- /**
20- * Performs initialization tasks.
21- *
22- * @private
23- * @example
24- * init();
25- */
26- function init ( ) {
27- var opts ;
28-
29- // Check if newer versions exist for this package:
30- opts = {
31- 'pkg' : pkg
32- } ;
33- notifier ( opts ) . notify ( ) ;
34-
35- // Set the process title to allow the process to be more easily identified:
36- process . title = pkg . name ;
37- process . stdout . on ( 'error' , process . exit ) ;
38- } // end FUNCTION init()
39-
40- /**
41- * Prints usage information.
42- *
43- * @private
44- * @example
45- * help();
46- * // => '...'
47- */
48- function help ( ) {
49- var fpath = path . join ( __dirname , 'usage.txt' ) ;
50- fs . createReadStream ( fpath )
51- . pipe ( process . stdout )
52- . on ( 'close' , onClose ) ;
53-
54- function onClose ( ) {
55- process . exit ( 0 ) ;
56- }
57- } // end FUNCTION help()
58-
59- /**
60- * Prints the package version.
61- *
62- * @private
63- * @example
64- * version();
65- * // => '#.#.#'
66- */
67- function version ( ) {
68- var msg = pkg . version . toString ( ) + '\n' ;
69- process . stdout . write ( msg , 'utf8' ) ;
70- process . exit ( 0 ) ;
71- } // end FUNCTION version()
72-
7318/**
7419* Prints a list of datasets.
7520*
@@ -83,42 +28,66 @@ function ls() {
8328
8429 keys = getKeys ( NAMES ) ;
8530 for ( i = 0 ; i < keys . length ; i ++ ) {
86- console . log ( keys [ i ] ) ;
31+ console . log ( keys [ i ] ) ; // eslint-disable-line no-console
8732 }
8833} // end FUNCTION ls()
8934
9035
91- // VARIABLES //
36+ // MAIN //
37+
38+ /**
39+ * Main execution sequence.
40+ *
41+ * @private
42+ * @returns {void }
43+ */
44+ function main ( ) {
45+ var dataset ;
46+ var child ;
47+ var flags ;
48+ var args ;
49+ var opts ;
50+ var cli ;
51+
52+ // Create a command-line interface:
53+ cli = new CLI ( {
54+ 'pkg' : require ( './../package.json' ) ,
55+ 'options' : require ( './../etc/cli_opts.json' ) ,
56+ 'help' : readFileSync ( join ( __dirname , '..' , 'docs' , 'usage.txt' ) , {
57+ 'encoding' : 'utf8'
58+ } )
59+ } ) ;
9260
93- var dataset ;
94- var args ;
95- var cli ;
61+ // Get any provided command-line options:
62+ flags = cli . flags ( ) ;
9663
64+ // Get any provided command-line arguments:
65+ args = cli . args ( ) ;
9766
98- // MAIN //
67+ if ( flags . ls ) {
68+ return ls ( ) ;
69+ }
70+ if ( hasOwnProp ( NAMES , flags . name ) ) {
71+ dataset = NAMES [ flags . name ] ;
72+ } else {
73+ throw new Error ( 'invalid option. Unrecognized/unsupported dataset name. Option: `' + flags . name + '`.' ) ;
74+ }
75+ opts = {
76+ 'stdio' : 'inherit'
77+ } ;
78+ child = spawn ( resolve ( __dirname , '../' + dataset + '/bin/cli' ) , args , opts ) ;
79+ child . on ( 'close' , onClose ) ;
80+
81+ /**
82+ * Exits the CLI.
83+ *
84+ * @param {integer } code - exit code
85+ * @private
86+ */
87+ function onClose ( code ) {
88+ cli . exit ( code ) ;
89+ } // end FUNCTION onClose()
90+ } // end FUNCTION main()
91+
92+ main ( ) ;
9993
100- init ( ) ;
101-
102- // Parse command-line arguments:
103- args = parseArgs ( process . argv . slice ( 2 ) , opts ) ;
104-
105- if ( args . help ) {
106- return help ( ) ;
107- }
108- if ( args . version ) {
109- return version ( ) ;
110- }
111- if ( args . ls ) {
112- return ls ( ) ;
113- }
114- if ( hasOwnProp ( NAMES , args . name ) ) {
115- dataset = NAMES [ args . name ] ;
116- } else {
117- throw new Error ( 'invalid option. Unrecognized/unsupported dataset name. Option: `' + args . name + '`.' ) ;
118- }
119- args = args . _ . slice ( ) ;
120-
121- // Run main:
122- process . argv = process . argv . slice ( 0 , 2 ) . concat ( args ) ;
123- cli = path . resolve ( __dirname , '../' + dataset + '/bin/cli' ) ;
124- require ( cli ) ;
0 commit comments