Skip to content

Commit f779265

Browse files
committed
Update CLI
1 parent a2db222 commit f779265

File tree

5 files changed

+192
-103
lines changed

5 files changed

+192
-103
lines changed

lib/node_modules/@stdlib/fs/read-dir/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function onRead( error, data ) {
135135
### Usage
136136

137137
```text
138-
Usage: readdir [options] dirpath
138+
Usage: readdir [options] <dirpath>
139139
140140
Options:
141141

lib/node_modules/@stdlib/fs/read-dir/bin/cli

Lines changed: 45 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,113 +3,57 @@
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 join = require( 'path' ).join;
7+
var resolve = require( 'path' ).resolve;
8+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
9+
var CLI = require( '@stdlib/tools/cli' );
1010
var cwd = require( '@stdlib/utils/cwd' );
11-
var pkg = require( './../package.json' );
12-
var opts = require( './opts.json' );
1311
var readDir = require( './../lib' );
1412

1513

16-
// FUNCTIONS //
17-
18-
/**
19-
* Performs initialization tasks.
20-
*
21-
* @private
22-
* @example
23-
* init();
24-
*/
25-
function init() {
26-
var opts;
27-
28-
// Check if newer versions exist for this package:
29-
opts = {
30-
'pkg': pkg
31-
};
32-
notifier( opts ).notify();
33-
34-
// Set the process title to allow the process to be more easily identified:
35-
process.title = pkg.name;
36-
process.stdout.on( 'error', process.exit );
37-
} // end FUNCTION init()
38-
39-
/**
40-
* Prints usage information.
41-
*
42-
* @private
43-
* @example
44-
* help();
45-
* // => '...'
46-
*/
47-
function help() {
48-
var fpath = path.join( __dirname, 'usage.txt' );
49-
fs.createReadStream( fpath )
50-
.pipe( process.stdout )
51-
.on( 'close', onClose );
52-
53-
function onClose() {
54-
process.exit( 0 );
55-
}
56-
} // end FUNCTION help()
57-
58-
/**
59-
* Prints the package version.
60-
*
61-
* @private
62-
* @example
63-
* version();
64-
* // => '#.#.#'
65-
*/
66-
function version() {
67-
var msg = pkg.version.toString()+'\n';
68-
process.stdout.write( msg, 'utf8' );
69-
process.exit( 0 );
70-
} // end FUNCTION version()
14+
// MAIN //
7115

7216
/**
73-
* Callback invoked upon reading the contents of a directory.
17+
* Main execution sequence.
7418
*
7519
* @private
76-
* @param {(Error|null)} error - error object
77-
* @param {string[]} data - directory contents
20+
* @returns {void}
7821
*/
79-
function onRead( error, data ) {
80-
if ( error ) {
81-
process.stderr.write( error.message + '\n', 'utf8' );
82-
return process.exit( 1 );
83-
}
84-
// Cannot write to `stdout` directly and then `exit` due to https://github.com/nodejs/node/issues/6456.
85-
// process.stdout.write( data.join( '\n' ), 'utf8' );
86-
// process.exit( 0 );
87-
88-
// HACK: workaround is to use `console.log` and no exit:
89-
console.log( data.join( '\n' ) );
90-
} // end FUNCTION onRead()
91-
92-
93-
// VARIABLES //
94-
95-
var dpath;
96-
var args;
97-
98-
99-
// MAIN //
100-
101-
init();
102-
103-
// Parse command-line arguments:
104-
args = parseArgs( process.argv.slice( 2 ), opts );
105-
106-
if ( args.help ) {
107-
return help();
108-
}
109-
if ( args.version ) {
110-
return version();
111-
}
112-
113-
// Run main:
114-
dpath = path.resolve( cwd(), args._[0] );
115-
readDir( dpath, onRead );
22+
function main() {
23+
var dpath;
24+
var args;
25+
var cli;
26+
27+
// Create a command-line interface:
28+
cli = new CLI({
29+
'pkg': require( './../package.json' ),
30+
'options': require( './../etc/cli_opts.json' ),
31+
'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), {
32+
'encoding': 'utf8'
33+
})
34+
});
35+
36+
// Get any provided command-line arguments:
37+
args = cli.args();
38+
39+
dpath = resolve( cwd(), args[ 0 ] );
40+
readDir( dpath, onRead );
41+
42+
/**
43+
* Callback invoked upon reading the contents of a directory.
44+
*
45+
* @private
46+
* @param {(Error|null)} error - error object
47+
* @param {string[]} data - directory contents
48+
* @returns {void}
49+
*/
50+
function onRead( error, data ) {
51+
if ( error ) {
52+
console.error( error ); // eslint-disable-line no-console
53+
return cli.exit( 1 );
54+
}
55+
console.log( data.join( '\n' ) ); // eslint-disable-line no-console
56+
} // end FUNCTION onRead()
57+
} // end FUNCTION main()
58+
59+
main();

lib/node_modules/@stdlib/fs/read-dir/bin/usage.txt renamed to lib/node_modules/@stdlib/fs/read-dir/docs/usage.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Usage: readdir [options] dirpath
2+
Usage: readdir [options] <dirpath>
33

44
Options:
55

File renamed without changes.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var resolve = require( 'path' ).resolve;
6+
var exec = require( 'child_process' ).exec;
7+
var tape = require( 'tape' );
8+
var IS_BROWSER = require( '@stdlib/assert/is-browser' );
9+
var IS_WINDOWS = require( '@stdlib/assert/is-windows' );
10+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
11+
12+
13+
// VARIABLES //
14+
15+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
16+
var opts = {
17+
'skip': IS_BROWSER || IS_WINDOWS
18+
};
19+
20+
21+
// FIXTURES //
22+
23+
var PKG_VERSION = require( './../package.json' ).version;
24+
25+
26+
// TESTS //
27+
28+
tape( 'command-line interface', function test( t ) {
29+
t.ok( true, __filename );
30+
t.end();
31+
});
32+
33+
tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
34+
var expected;
35+
var cmd;
36+
37+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
38+
'encoding': 'utf8'
39+
});
40+
cmd = [
41+
process.execPath,
42+
fpath,
43+
'--help'
44+
];
45+
46+
exec( cmd.join( ' ' ), done );
47+
48+
function done( error, stdout, stderr ) {
49+
if ( error ) {
50+
t.fail( error.message );
51+
} else {
52+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
53+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
54+
}
55+
t.end();
56+
}
57+
});
58+
59+
tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
60+
var expected;
61+
var cmd;
62+
63+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
64+
'encoding': 'utf8'
65+
});
66+
cmd = [
67+
process.execPath,
68+
fpath,
69+
'-h'
70+
];
71+
72+
exec( cmd.join( ' ' ), done );
73+
74+
function done( error, stdout, stderr ) {
75+
if ( error ) {
76+
t.fail( error.message );
77+
} else {
78+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
79+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
80+
}
81+
t.end();
82+
}
83+
});
84+
85+
tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
86+
var cmd = [
87+
process.execPath,
88+
fpath,
89+
'--version'
90+
];
91+
92+
exec( cmd.join( ' ' ), done );
93+
94+
function done( error, stdout, stderr ) {
95+
if ( error ) {
96+
t.fail( error.message );
97+
} else {
98+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
99+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
100+
}
101+
t.end();
102+
}
103+
});
104+
105+
tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
106+
var cmd = [
107+
process.execPath,
108+
fpath,
109+
'-V'
110+
];
111+
112+
exec( cmd.join( ' ' ), done );
113+
114+
function done( error, stdout, stderr ) {
115+
if ( error ) {
116+
t.fail( error.message );
117+
} else {
118+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
119+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
120+
}
121+
t.end();
122+
}
123+
});
124+
125+
tape( 'the command-line interface tests reads the entire contents of a directory', opts, function test( t ) {
126+
var cmd = [
127+
process.execPath,
128+
fpath,
129+
'./examples'
130+
];
131+
132+
exec( cmd.join( ' ' ), done );
133+
134+
function done( error, stdout, stderr ) {
135+
var str;
136+
if ( error ) {
137+
t.fail( error.message );
138+
} else {
139+
str = stdout.toString();
140+
t.strictEqual( str, 'index.js\n', 'prints directory contents to `stdout`' );
141+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
142+
}
143+
t.end();
144+
}
145+
});

0 commit comments

Comments
 (0)