Skip to content

Commit daca0c4

Browse files
committed
Update CLI
1 parent f779265 commit daca0c4

File tree

5 files changed

+202
-107
lines changed

5 files changed

+202
-107
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function onFile( error, data ) {
138138
### Usage
139139

140140
```text
141-
Usage: readfile [options] filepath
141+
Usage: readfile [options] <filepath>
142142
143143
Options:
144144

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

+53-105
Original file line numberDiff line numberDiff line change
@@ -3,121 +3,69 @@
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 readFile = require( './../lib' );
1412

1513

16-
// FUNCTIONS //
14+
// MAIN //
1715

1816
/**
19-
* Performs initialization tasks.
17+
* Main execution sequence.
2018
*
2119
* @private
22-
* @example
23-
* init();
20+
* @returns {void}
2421
*/
25-
function init() {
22+
function main() {
23+
var fpath;
24+
var flags;
25+
var args;
2626
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 );
27+
var cli;
28+
29+
// Create a command-line interface:
30+
cli = new CLI({
31+
'pkg': require( './../package.json' ),
32+
'options': require( './../etc/cli_opts.json' ),
33+
'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), {
34+
'encoding': 'utf8'
35+
})
36+
});
37+
38+
// Get any provided command-line arguments:
39+
args = cli.args();
40+
41+
// Get any provided command-line options:
42+
flags = cli.flags();
43+
opts = {};
44+
if ( flags.encoding ) {
45+
opts.encoding = flags.encoding;
5546
}
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()
71-
72-
/**
73-
* Callback invoked upon reading the contents of a file.
74-
*
75-
* @private
76-
* @param {(Error|null)} error - error object
77-
* @param {(Buffer|string)} file - file contents
78-
*/
79-
function onFile( error, file ) {
80-
if ( error ) {
81-
process.stderr.write( error.message + '\n', 'utf8' );
82-
return process.exit( 1 );
47+
if ( flags.flag ) {
48+
opts.flag = flags.flag;
8349
}
84-
// Cannot write to `stdout` directly and then `exit` due to https://github.com/nodejs/node/issues/6456.
85-
// process.stdout.write( file );
86-
// process.exit( 0 );
87-
88-
// HACK: workaround is to use `console.log` and no exit:
89-
console.log( file.toString() );
90-
} // end FUNCTION onFile()
91-
92-
93-
// VARIABLES //
94-
95-
var fpath;
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-
opts = {};
114-
if ( args.encoding ) {
115-
opts.encoding = args.encoding;
116-
}
117-
if ( args.flag ) {
118-
opts.flag = args.flag;
119-
}
12050

121-
// Run main:
122-
fpath = path.resolve( cwd(), args._[0] );
123-
readFile( fpath, opts, onFile );
51+
fpath = resolve( cwd(), args[ 0 ] );
52+
readFile( fpath, opts, onFile );
53+
54+
/**
55+
* Callback invoked upon reading the contents of a file.
56+
*
57+
* @private
58+
* @param {(Error|null)} error - error object
59+
* @param {(Buffer|string)} file - file contents
60+
* @returns {void}
61+
*/
62+
function onFile( error, file ) {
63+
if ( error ) {
64+
console.error( error ); // eslint-disable-line no-console
65+
return cli.exit( 1 );
66+
}
67+
console.log( file.toString() ); // eslint-disable-line no-console
68+
} // end FUNCTION onFile()
69+
} // end FUNCTION main()
70+
71+
main();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Usage: readfile [options] filepath
2+
Usage: readfile [options] <filepath>
33

44
Options:
55

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

0 commit comments

Comments
 (0)