Skip to content

Commit 0839a87

Browse files
committed
Remove repl script and add stdlib CLI supporting commands
1 parent ef0a732 commit 0839a87

File tree

5 files changed

+148
-57
lines changed

5 files changed

+148
-57
lines changed

bin/cli

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// MODULES //
5+
6+
var fs = require( 'fs' );
7+
var path = require( 'path' );
8+
var spawn = require( 'child_process' ).spawn;
9+
var parseArgs = require( 'minimist' );
10+
var notifier = require( 'update-notifier' );
11+
var pkg = require( './../package.json' );
12+
var opts = require( './opts.json' );
13+
var commands = require( './commands.json' );
14+
15+
16+
// FUNCTIONS //
17+
18+
/**
19+
* Prints usage information.
20+
*
21+
* @private
22+
* @example
23+
* help();
24+
* // => '...'
25+
*/
26+
function help() {
27+
var fpath = path.join( __dirname, 'usage.txt' );
28+
fs.createReadStream( fpath )
29+
.pipe( process.stdout )
30+
.on( 'close', onClose );
31+
32+
function onClose() {
33+
process.exit( 0 );
34+
}
35+
} // end FUNCTION help()
36+
37+
/**
38+
* Prints the package version.
39+
*
40+
* @private
41+
* @example
42+
* version();
43+
* // => '#.#.#'
44+
*/
45+
function version() {
46+
var msg = pkg.version.toString()+'\n';
47+
process.stdout.write( msg, 'utf8' );
48+
process.exit( 0 );
49+
} // end FUNCTION version()
50+
51+
/**
52+
* Callback invoked upon encountering an error while running a command.
53+
*
54+
* @private
55+
* @param {Error} error - error object
56+
* @throws {Error} unexpected error
57+
*/
58+
function onError( error ) {
59+
throw error;
60+
} // end FUNCTION onError()
61+
62+
63+
// VARIABLES //
64+
65+
var args1;
66+
var args2;
67+
var proc;
68+
var cmd;
69+
var i;
70+
71+
72+
// INIT //
73+
74+
process.title = pkg.name;
75+
process.stdout.on( 'error', process.exit );
76+
77+
78+
// PACKAGE UPDATES //
79+
80+
notifier( { 'pkg': pkg } ).notify();
81+
82+
83+
// ARGUMENTS //
84+
85+
args1 = parseArgs( process.argv.slice( 2 ), opts );
86+
87+
if ( args1.version ) {
88+
return version();
89+
}
90+
cmd = args1._[ 0 ];
91+
92+
93+
// MAIN //
94+
95+
opts = {
96+
'cwd': process.cwd(),
97+
'env': process.env,
98+
'stdio': 'inherit'
99+
};
100+
if ( cmd === 'help' ) {
101+
if ( args1._[1] === void 0 ) {
102+
return help();
103+
}
104+
cmd = commands[ args1._[1] ];
105+
if ( cmd === void 0 ) {
106+
throw new Error( 'invalid argument. Unrecognized/unsupported command. Value: `' + cmd + '`.' );
107+
}
108+
args2 = new Array( 2 );
109+
args2[ 0 ] = cmd;
110+
args2[ 1 ] = '--help';
111+
} else {
112+
if ( cmd === 'repl' ) {
113+
args2 = new Array( 1 );
114+
args2[ 0 ] = commands[ cmd ];
115+
} else {
116+
throw new Error( 'invalid argument. Unrecognized/unsupported command. Value: `' + cmd + '`.' );
117+
}
118+
for ( i = 1; i < args1._.length; i++ ) {
119+
args2.push( args1._[ i ] );
120+
}
121+
}
122+
proc = spawn( 'node', args2, opts );
123+
proc.on( 'error', onError );

bin/commands.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"repl": "./lib/node_modules/@stdlib/repl/bin/cli"
3+
}

bin/opts.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"boolean": [
3+
"version"
4+
],
5+
"alias": {
6+
"version": [
7+
"V"
8+
]
9+
}
10+
}

bin/repl

Lines changed: 0 additions & 57 deletions
This file was deleted.

bin/usage.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Usage: stdlib [options] <command> [args]
3+
4+
Options:
5+
6+
-V, --version Print the package version.
7+
8+
Commands:
9+
10+
help [command] Print a help message.
11+
repl Start a REPL.
12+

0 commit comments

Comments
 (0)