Skip to content

Commit c8ef930

Browse files
committed
Add CLI
1 parent 3c57cf2 commit c8ef930

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
* Prints data as newline-delimited JSON (ndjson).
72+
*
73+
* @private
74+
*/
75+
function ndjson() {
76+
var data;
77+
var i;
78+
79+
data = require( './../lib' )();
80+
for ( i = 0; i < data.length; i++ ) {
81+
console.log( JSON.stringify( data[i] ) );
82+
}
83+
} // end FUNCTION ndjson()
84+
85+
/**
86+
* Prints data as lines of text.
87+
*
88+
* @private
89+
*/
90+
function txt() {
91+
var FILE_LIST;
92+
var i;
93+
94+
FILE_LIST = require( './../data/file_list.json' );
95+
i = 0;
96+
97+
next();
98+
99+
/**
100+
* Callback which processes the next file.
101+
*
102+
* @private
103+
*/
104+
function next() {
105+
var fstream;
106+
var fpath;
107+
108+
fpath = path.resolve( __dirname, '..', 'data', FILE_LIST[ i ] );
109+
fstream = fs.createReadStream( fpath );
110+
fstream.on( 'close', onClose );
111+
fstream.pipe( process.stdout );
112+
} // end FUNCTION next()
113+
114+
/**
115+
* Callback invoked upon a stream close.
116+
*
117+
* @private
118+
* @return {void}
119+
*/
120+
function onClose() {
121+
i += 1;
122+
if ( i === FILE_LIST.length ) {
123+
return process.exit( 0 );
124+
}
125+
console.log( '\n' );
126+
next();
127+
} // end FUNCTION onClose()
128+
} // end FUNCTION txt()
129+
130+
131+
// VARIABLES //
132+
133+
var args;
134+
135+
136+
// MAIN //
137+
138+
init();
139+
140+
// Parse command-line arguments:
141+
args = parseArgs( process.argv.slice( 2 ), opts );
142+
143+
if ( args.help ) {
144+
return help();
145+
}
146+
if ( args.version ) {
147+
return version();
148+
}
149+
if ( args.format === 'ndjson' ) {
150+
ndjson();
151+
} else {
152+
txt();
153+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"boolean": [
3+
"help",
4+
"version"
5+
],
6+
"string": [
7+
"format"
8+
],
9+
"alias": {
10+
"help": [
11+
"h"
12+
],
13+
"version": [
14+
"V"
15+
]
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
Usage: spam-assassin [options]
3+
4+
Options:
5+
6+
-h, --help Print this message.
7+
-V, --version Print the package version.
8+
--format fmt Output format: 'txt' or 'ndjson'.
9+

0 commit comments

Comments
 (0)