Skip to content

Commit 4c55344

Browse files
committed
Add CLI
1 parent 0d29c24 commit 4c55344

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
// MODULES //
5+
6+
var fs = require( 'fs' );
7+
var path = require( 'path' );
8+
var parseArgs = require( 'minimist' );
9+
var notifier = require( 'update-notifier' );
10+
var pkg = require( './../package.json' );
11+
var opts = require( './opts.json' );
12+
13+
14+
// FUNCTIONS //
15+
16+
/**
17+
* Performs initialization tasks.
18+
*
19+
* @private
20+
* @example
21+
* init();
22+
*/
23+
function init() {
24+
var opts;
25+
26+
// Check if newer versions exist for this package:
27+
opts = {
28+
'pkg': pkg
29+
};
30+
notifier( opts ).notify();
31+
32+
// Set the process title to allow the process to be more easily identified:
33+
process.title = pkg.name;
34+
process.stdout.on( 'error', process.exit );
35+
} // end FUNCTION init()
36+
37+
/**
38+
* Prints usage information.
39+
*
40+
* @private
41+
* @example
42+
* help();
43+
* // => '...'
44+
*/
45+
function help() {
46+
var fpath = path.join( __dirname, 'usage.txt' );
47+
fs.createReadStream( fpath )
48+
.pipe( process.stdout )
49+
.on( 'close', onClose );
50+
51+
function onClose() {
52+
process.exit( 0 );
53+
}
54+
} // end FUNCTION help()
55+
56+
/**
57+
* Prints the package version.
58+
*
59+
* @private
60+
* @example
61+
* version();
62+
* // => '#.#.#'
63+
*/
64+
function version() {
65+
var msg = pkg.version.toString()+'\n';
66+
process.stdout.write( msg, 'utf8' );
67+
process.exit( 0 );
68+
} // end FUNCTION version()
69+
70+
/**
71+
* Callback invoked upon starting a REPL.
72+
*
73+
* @private
74+
* @param {(Error|null)} error - error object
75+
* @param {REPL} r - REPL instance
76+
*/
77+
function onStart( error ) {
78+
if ( error ) {
79+
console.error( error.message );
80+
return process.exit( 1 );
81+
}
82+
} // end FUNCTION onStart()
83+
84+
85+
// VARIABLES //
86+
87+
var args;
88+
89+
90+
// MAIN //
91+
92+
init();
93+
94+
// Parse command-line arguments:
95+
args = parseArgs( process.argv.slice( 2 ), opts );
96+
97+
if ( args.help ) {
98+
return help();
99+
}
100+
if ( args.version ) {
101+
return version();
102+
}
103+
104+
// Run main:
105+
console.log( 'Starting REPL...' );
106+
require( './../lib' )( onStart );
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"boolean": [
3+
"help",
4+
"version"
5+
],
6+
"alias": {
7+
"help": [
8+
"h"
9+
],
10+
"version": [
11+
"V"
12+
]
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Usage: stdlib-repl [options]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+

0 commit comments

Comments
 (0)