Skip to content

Commit 480c3f3

Browse files
committed
Refactor CLI code and add tests
1 parent 1362923 commit 480c3f3

File tree

4 files changed

+330
-115
lines changed

4 files changed

+330
-115
lines changed

lib/node_modules/@stdlib/namespace/bin/cli

+80-115
Original file line numberDiff line numberDiff line change
@@ -22,144 +22,109 @@
2222

2323
// MODULES //
2424

25-
var fs = require( 'fs' );
26-
var path = require( 'path' );
27-
var parseArgs = require( 'minimist' );
25+
var resolve = require( 'path' ).resolve;
26+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
2827
var indexOf = require( '@stdlib/utils/index-of' );
29-
var pkg = require( './../package.json' );
30-
var opts = require( './opts.json' );
31-
var main = require( './../lib' );
28+
var CLI = require( '@stdlib/cli/ctor' );
29+
var namespace = require( './../lib' );
3230

3331

34-
// FUNCTIONS //
32+
// VARIABLES //
3533

36-
/**
37-
* Performs initialization tasks.
38-
*
39-
* @private
40-
* @example
41-
* init();
42-
*/
43-
function init() {
44-
// Set the process title to allow the process to be more easily identified:
45-
process.title = pkg.name;
46-
process.stdout.on( 'error', process.exit );
47-
}
34+
var FIELDS = [ 'alias', 'path', 'type', 'related' ];
4835

49-
/**
50-
* Prints usage information.
51-
*
52-
* @private
53-
* @example
54-
* help();
55-
* // => '...'
56-
*/
57-
function help() {
58-
var fpath = path.join( __dirname, 'usage.txt' );
59-
fs.createReadStream( fpath )
60-
.pipe( process.stderr )
61-
.on( 'close', onClose );
62-
63-
function onClose() {
64-
process.exit( 0 );
65-
}
66-
}
6736

68-
/**
69-
* Prints the package version.
70-
*
71-
* @private
72-
* @example
73-
* version();
74-
* // => '#.#.#'
75-
*/
76-
function version() {
77-
var msg = pkg.version.toString()+'\n';
78-
process.stdout.write( msg, 'utf8' );
79-
process.exit( 0 );
80-
}
37+
// MAIN //
8138

8239
/**
83-
* Prints data as newline-delimited JSON (ndjson).
40+
* Main execution sequence.
8441
*
8542
* @private
86-
* @param {ObjectArray} data - data to print
43+
* @throws {Error} unrecognized/unsupported field
44+
* @returns {void}
8745
*/
88-
function ndjson( data ) {
46+
function main() {
47+
var fields;
48+
var flags;
49+
var cli;
8950
var i;
9051

91-
for ( i = 0; i < data.length; i++ ) {
92-
console.log( JSON.stringify( data[i] ) );
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( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
57+
'encoding': 'utf8'
58+
})
59+
});
60+
61+
// Get any provided command-line options:
62+
flags = cli.flags();
63+
if ( flags.help || flags.version ) {
64+
return;
9365
}
94-
}
95-
96-
/**
97-
* Prints data as lines of comma-separated values (CSV).
98-
*
99-
* @private
100-
* @param {ObjectArray} data - data to print
101-
* @param {StringArray} fields - output fields
102-
*/
103-
function csv( data, fields ) {
104-
var line;
105-
var f;
106-
var i;
107-
var j;
108-
var n;
109-
var m;
110-
111-
n = fields.length;
112-
m = n - 1;
113-
line = '';
114-
for ( j = 0; j < n; j++ ) {
115-
line += '"' + fields[ j ] + '"';
116-
if ( j < m ) {
117-
line += ',';
66+
if ( flags.fields ) {
67+
fields = flags.fields.split( ',' );
68+
for ( i = 0; i < fields.length; i++ ) {
69+
if ( indexOf( FIELDS, fields[ i ] ) === -1 ) {
70+
throw new Error( 'invalid option. Unrecognized/unsupported field. Option: `' + fields[ i ] + '`.' );
71+
}
11872
}
73+
return csv( namespace(), fields );
11974
}
120-
console.log( line );
121-
for ( i = 0; i < data.length; i++ ) {
75+
ndjson( namespace() );
76+
77+
/**
78+
* Prints data as newline-delimited JSON (ndjson).
79+
*
80+
* @private
81+
* @param {ObjectArray} data - data to print
82+
*/
83+
function ndjson( data ) {
84+
var i;
85+
86+
for ( i = 0; i < data.length; i++ ) {
87+
console.log( JSON.stringify( data[i] ) );
88+
}
89+
}
90+
91+
/**
92+
* Prints data as lines of comma-separated values (CSV).
93+
*
94+
* @private
95+
* @param {ObjectArray} data - data to print
96+
* @param {StringArray} fields - output fields
97+
*/
98+
function csv( data, fields ) {
99+
var line;
100+
var f;
101+
var i;
102+
var j;
103+
var n;
104+
var m;
105+
106+
n = fields.length;
107+
m = n - 1;
122108
line = '';
123109
for ( j = 0; j < n; j++ ) {
124-
f = fields[ j ];
125-
line += '"' + data[ i ][ f ] + '"';
110+
line += '"' + fields[ j ] + '"';
126111
if ( j < m ) {
127112
line += ',';
128113
}
129114
}
130115
console.log( line );
131-
}
132-
}
133-
134-
135-
// VARIABLES //
136-
137-
var FIELDS = [ 'alias', 'path', 'type', 'related' ];
138-
var fields;
139-
var args;
140-
var i;
141-
142-
143-
// MAIN //
144-
145-
init();
146-
147-
// Parse command-line arguments:
148-
args = parseArgs( process.argv.slice( 2 ), opts );
149-
150-
if ( args.help ) {
151-
return help();
152-
}
153-
if ( args.version ) {
154-
return version();
155-
}
156-
if ( args.fields ) {
157-
fields = args.fields.split( ',' );
158-
for ( i = 0; i < fields.length; i++ ) {
159-
if ( indexOf( FIELDS, fields[ i ] ) === -1 ) {
160-
throw new Error( 'invalid option. Unrecognized/unsupported field. Option: `' + fields[ i ] + '`.' );
116+
for ( i = 0; i < data.length; i++ ) {
117+
line = '';
118+
for ( j = 0; j < n; j++ ) {
119+
f = fields[ j ];
120+
line += '"' + data[ i ][ f ] + '"';
121+
if ( j < m ) {
122+
line += ',';
123+
}
124+
}
125+
console.log( line );
161126
}
162127
}
163-
return csv( main(), fields );
164128
}
165-
ndjson( main() );
129+
130+
main();

0 commit comments

Comments
 (0)