Skip to content

Commit d4a0904

Browse files
committed
Update CLI and start adding tests
1 parent 5dc5964 commit d4a0904

File tree

7 files changed

+522
-105
lines changed

7 files changed

+522
-105
lines changed

lib/node_modules/@stdlib/_tools/readme/to-html/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function onFinish( error, html ) {
148148
### Usage
149149

150150
```text
151-
Usage: readme-to-html [options] file
151+
Usage: readme-to-html [options] <file>
152152
153153
Options:
154154

lib/node_modules/@stdlib/_tools/readme/to-html/bin/cli

Lines changed: 61 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,14 @@
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 resolve = require( 'path' ).resolve;
7+
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
8+
var CLI = require( '@stdlib/tools/cli' );
9+
var toHTML = require( './../lib' );
1310

1411

1512
// FUNCTIONS //
1613

17-
/**
18-
* Performs initialization tasks.
19-
*
20-
* @private
21-
* @example
22-
* init();
23-
*/
24-
function init() {
25-
var opts;
26-
27-
// Check if newer versions exist for this package:
28-
opts = {
29-
'pkg': pkg
30-
};
31-
notifier( opts ).notify();
32-
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-
}
37-
38-
/**
39-
* Prints usage information.
40-
*
41-
* @private
42-
* @example
43-
* help();
44-
* // => '...'
45-
*/
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 );
54-
}
55-
}
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-
}
70-
7114
/**
7215
* Callback invoked upon completion.
7316
*
@@ -79,53 +22,68 @@ function onFinish( error, html ) {
7922
if ( error ) {
8023
throw error;
8124
}
82-
console.log( html );
25+
console.log( html ); // eslint-disable-line no-console
8326
}
8427

8528

86-
// VARIABLES //
87-
88-
var args;
89-
90-
9129
// MAIN //
9230

93-
init();
94-
95-
// Parse command-line arguments:
96-
args = parseArgs( process.argv.slice( 2 ), opts );
97-
98-
if ( args.help ) {
99-
return help();
100-
}
101-
if ( args.version ) {
102-
return version();
103-
}
104-
opts = {};
105-
if ( args.fragment ) {
106-
opts.fragment = args.fragment;
107-
}
108-
if ( !opts.fragment ) {
109-
if ( args.tests ) {
110-
opts.tests = args.tests;
111-
}
112-
if ( args.benchmarks ) {
113-
opts.benchmarks = args.benchmarks;
114-
}
115-
if ( args.source ) {
116-
opts.source = args.source;
117-
}
118-
if ( args.title ) {
119-
opts.title = args.title;
120-
}
121-
if ( args.head ) {
122-
opts.head = args.head;
123-
}
124-
if ( args.prepend ) {
125-
opts.prepend = args.prepend;
31+
/**
32+
* Main execution sequence.
33+
*
34+
* @private
35+
*/
36+
function main() {
37+
var flags;
38+
var args;
39+
var opts;
40+
var cli;
41+
42+
// Create a command-line interface:
43+
cli = new CLI({
44+
'pkg': require( './../package.json' ),
45+
'options': require( './../etc/cli_opts.json' ),
46+
'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
47+
'encoding': 'utf8'
48+
})
49+
});
50+
51+
// Get any provided command-line arguments:
52+
args = cli.args();
53+
54+
// Get any provided command-line options:
55+
flags = cli.flags();
56+
57+
// Extract options...
58+
opts = {};
59+
if ( flags.fragment ) {
60+
opts.fragment = flags.fragment;
12661
}
127-
if ( args.append ) {
128-
opts.append = args.append;
62+
if ( !opts.fragment ) {
63+
if ( flags.tests ) {
64+
opts.tests = flags.tests;
65+
}
66+
if ( flags.benchmarks ) {
67+
opts.benchmarks = flags.benchmarks;
68+
}
69+
if ( flags.source ) {
70+
opts.source = flags.source;
71+
}
72+
if ( flags.title ) {
73+
opts.title = flags.title;
74+
}
75+
if ( flags.head ) {
76+
opts.head = flags.head;
77+
}
78+
if ( flags.prepend ) {
79+
opts.prepend = flags.prepend;
80+
}
81+
if ( flags.append ) {
82+
opts.append = flags.append;
83+
}
12984
}
85+
toHTML( args[ 0 ], opts, onFinish );
13086
}
131-
main( args._[ 0 ], opts, onFinish );
87+
88+
main();
89+

lib/node_modules/@stdlib/_tools/readme/to-html/bin/usage.txt renamed to lib/node_modules/@stdlib/_tools/readme/to-html/docs/usage.txt

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

2-
Usage: readme-to-html [options] file
2+
Usage: readme-to-html [options] <file>
33

44
Options:
55

lib/node_modules/@stdlib/_tools/readme/to-html/bin/opts.json renamed to lib/node_modules/@stdlib/_tools/readme/to-html/etc/cli_opts.json

File renamed without changes.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
// TODO: Add tests
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var tape = require( 'tape' );
6+
var toHTML = require( './../lib' );
7+
8+
9+
// TESTS //
10+
11+
tape( 'main export is a function', function test( t ) {
12+
t.ok( true, __filename );
13+
t.strictEqual( typeof toHTML, 'function', 'main export is a function' );
14+
t.end();
15+
});
16+
17+
// TODO: Add tests

0 commit comments

Comments
 (0)