Skip to content

Commit 49ea61c

Browse files
committed
Update CLI
1 parent 705f20b commit 49ea61c

File tree

5 files changed

+262
-68
lines changed

5 files changed

+262
-68
lines changed

lib/node_modules/@stdlib/assert/is-hex-string/bin/cli

+48-68
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,68 @@
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' );
10-
var pkg = require( './../package.json' );
11-
var opts = require( './opts.json' );
12-
var main = require( './../lib' );
6+
var join = require( 'path' ).join;
7+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
8+
var CLI = require( '@stdlib/tools/cli' );
9+
var stdin = require( '@stdlib/utils/read-stdin' );
10+
var RE_EOL = require( '@stdlib/regexp/eol' );
11+
var isHexString = require( './../lib' );
1312

1413

1514
// FUNCTIONS //
1615

1716
/**
18-
* Performs initialization tasks.
17+
* Callback invoked upon reading from `stdin`.
1918
*
2019
* @private
21-
* @example
22-
* init();
20+
* @param {(Error|null)} error - error object
21+
* @param {Buffer} data - data
22+
* @returns {void}
2323
*/
24-
function init() {
25-
var opts;
24+
function onRead( error, data ) {
25+
/* eslint-disable no-console */
26+
var lines;
27+
var i;
28+
if ( error ) {
29+
process.exitCode = 1;
30+
return console.error( 'Error: %s', error.message );
31+
}
32+
lines = data.toString().split( RE_EOL );
33+
for ( i = 0; i < lines.length; i++ ) {
34+
console.log( isHexString( lines[ i ] ) );
35+
}
36+
} // end FUNCTION onRead()
2637

27-
// Check if newer versions exist for this package:
28-
opts = {
29-
'pkg': pkg
30-
};
31-
notifier( opts ).notify();
3238

33-
// Set the process title to allow the process to be more easily identified:
34-
process.title = pkg.name;
35-
process.stdout.on( 'error', process.exit );
36-
} // end FUNCTION init()
39+
// MAIN //
3740

3841
/**
39-
* Prints usage information.
42+
* Main execution sequence.
4043
*
4144
* @private
42-
* @example
43-
* help();
44-
* // => '...'
45+
* @returns {void}
4546
*/
46-
function help() {
47-
var fpath = path.join( __dirname, 'usage.txt' );
48-
fs.createReadStream( fpath )
49-
.pipe( process.stdout )
50-
.on( 'close', onClose );
51-
52-
function onClose() {
53-
process.exit( 0 );
47+
function main() {
48+
var args;
49+
var cli;
50+
51+
// Create a command-line interface:
52+
cli = new CLI({
53+
'pkg': require( './../package.json' ),
54+
'options': require( './../etc/cli_opts.json' ),
55+
'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), {
56+
'encoding': 'utf8'
57+
})
58+
});
59+
60+
// Get any provided command-line arguments:
61+
args = cli.args();
62+
63+
// Check if we are receiving data from `stdin`...
64+
if ( !process.stdin.isTTY ) {
65+
return stdin( onRead );
5466
}
55-
} // end FUNCTION help()
56-
57-
/**
58-
* Prints the package version.
59-
*
60-
* @private
61-
* @example
62-
* version();
63-
* // => '#.#.#'
64-
*/
65-
function version() {
66-
var msg = pkg.version.toString()+'\n';
67-
process.stdout.write( msg, 'utf8' );
68-
process.exit( 0 );
69-
} // end FUNCTION version()
70-
71-
72-
// VARIABLES //
73-
74-
var args;
75-
76-
77-
// MAIN //
78-
79-
init();
80-
81-
// Parse command-line arguments:
82-
args = parseArgs( process.argv.slice( 2 ), opts );
67+
console.log( isHexString( args[ 0 ] ) ); // eslint-disable-line no-console
68+
} // end FUNCTION main()
8369

84-
if ( args.help ) {
85-
return help();
86-
}
87-
if ( args.version ) {
88-
return version();
89-
}
90-
console.log( main( args._[ 0 ] ) );
70+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var resolve = require( 'path' ).resolve;
2+
var proxyquire = require( 'proxyquire' );
3+
4+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
5+
6+
process.stdin.isTTY = false;
7+
8+
proxyquire( fpath, {
9+
'@stdlib/utils/read-stdin': stdin
10+
});
11+
12+
function stdin( clbk ) {
13+
clbk( new Error( 'beep' ) );
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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 replace = require( '@stdlib/string/replace' );
11+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
12+
13+
14+
// VARIABLES //
15+
16+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
17+
var opts = {
18+
'skip': IS_BROWSER || IS_WINDOWS
19+
};
20+
21+
22+
// FIXTURES //
23+
24+
var PKG_VERSION = require( './../package.json' ).version;
25+
26+
27+
// TESTS //
28+
29+
tape( 'command-line interface', function test( t ) {
30+
t.ok( true, __filename );
31+
t.end();
32+
});
33+
34+
tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
35+
var expected;
36+
var cmd;
37+
38+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
39+
'encoding': 'utf8'
40+
});
41+
cmd = [
42+
process.execPath,
43+
fpath,
44+
'--help'
45+
];
46+
47+
exec( cmd.join( ' ' ), done );
48+
49+
function done( error, stdout, stderr ) {
50+
if ( error ) {
51+
t.fail( error.message );
52+
} else {
53+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
54+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
55+
}
56+
t.end();
57+
}
58+
});
59+
60+
tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
61+
var expected;
62+
var cmd;
63+
64+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
65+
'encoding': 'utf8'
66+
});
67+
cmd = [
68+
process.execPath,
69+
fpath,
70+
'-h'
71+
];
72+
73+
exec( cmd.join( ' ' ), done );
74+
75+
function done( error, stdout, stderr ) {
76+
if ( error ) {
77+
t.fail( error.message );
78+
} else {
79+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
80+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
81+
}
82+
t.end();
83+
}
84+
});
85+
86+
tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
87+
var cmd = [
88+
process.execPath,
89+
fpath,
90+
'--version'
91+
];
92+
93+
exec( cmd.join( ' ' ), done );
94+
95+
function done( error, stdout, stderr ) {
96+
if ( error ) {
97+
t.fail( error.message );
98+
} else {
99+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
100+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
101+
}
102+
t.end();
103+
}
104+
});
105+
106+
tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
107+
var cmd = [
108+
process.execPath,
109+
fpath,
110+
'-V'
111+
];
112+
113+
exec( cmd.join( ' ' ), done );
114+
115+
function done( error, stdout, stderr ) {
116+
if ( error ) {
117+
t.fail( error.message );
118+
} else {
119+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
120+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
121+
}
122+
t.end();
123+
}
124+
});
125+
126+
tape( 'the command-line interface tests if a string argument contains only hexadecimal digits', opts, function test( t ) {
127+
var cmd = [
128+
process.execPath,
129+
'-e',
130+
'"process.stdin.isTTY = true; process.argv[ 2 ] = \'0123456789abcdefABCDEF\'; require( \''+fpath+'\' );"'
131+
];
132+
133+
exec( cmd.join( ' ' ), done );
134+
135+
function done( error, stdout, stderr ) {
136+
if ( error ) {
137+
t.fail( error.message );
138+
} else {
139+
t.strictEqual( stdout.toString(), 'true\n', 'expected value' );
140+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
141+
}
142+
t.end();
143+
}
144+
});
145+
146+
tape( 'the command-line interface supports use as a standard stream', opts, function test( t ) {
147+
var cmd = [
148+
'printf "beep\n0123456789abcdefABCDEF"',
149+
'|',
150+
process.execPath,
151+
fpath
152+
];
153+
154+
exec( cmd.join( ' ' ), done );
155+
156+
function done( error, stdout, stderr ) {
157+
if ( error ) {
158+
t.fail( error.message );
159+
} else {
160+
t.strictEqual( stdout.toString(), 'false\ntrue\n', 'expected value' );
161+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
162+
}
163+
t.end();
164+
}
165+
});
166+
167+
tape( 'when used as a standard stream, if an error is encountered when reading from `stdin`, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) {
168+
var script;
169+
var opts;
170+
var cmd;
171+
172+
script = readFileSync( resolve( __dirname, 'fixtures', 'stdin_error.js.txt' ), {
173+
'encoding': 'utf8'
174+
});
175+
176+
// Replace single quotes with double quotes:
177+
script = replace( script, '\'', '"' );
178+
179+
cmd = [
180+
process.execPath,
181+
'-e',
182+
'\''+script+'\''
183+
];
184+
185+
opts = {
186+
'cwd': __dirname
187+
};
188+
189+
exec( cmd.join( ' ' ), opts, done );
190+
191+
function done( error, stdout, stderr ) {
192+
if ( error ) {
193+
t.pass( error.message );
194+
t.strictEqual( error.code, 1, 'expected exit code' );
195+
}
196+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
197+
t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' );
198+
t.end();
199+
}
200+
});

0 commit comments

Comments
 (0)