Skip to content

Commit 1acf074

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents c403cb0 + eb85c89 commit 1acf074

File tree

153 files changed

+4487
-1368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+4487
-1368
lines changed

lib/node_modules/@stdlib/assert/is-absolute-path/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ bool = isAbsolutePath.win32( '' );
142142
### Usage
143143

144144
```text
145-
Usage: is-absolute-path [options]
145+
Usage: is-absolute-path [options] <path>
146146
147147
Options:
148148

lib/node_modules/@stdlib/assert/is-absolute-path/bin/cli

Lines changed: 65 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,76 @@
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 isAbsolutePath = require( './../lib' );
1312

1413

15-
// FUNCTIONS //
16-
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-
} // end FUNCTION init()
14+
// MAIN //
3715

3816
/**
39-
* Prints usage information.
17+
* Main execution sequence.
4018
*
4119
* @private
42-
* @example
43-
* help();
44-
* // => '...'
20+
* @returns {void}
4521
*/
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 );
22+
function main() {
23+
var flags;
24+
var args;
25+
var cli;
26+
var fun;
27+
28+
// Create a command-line interface:
29+
cli = new CLI({
30+
'pkg': require( './../package.json' ),
31+
'options': require( './../etc/cli_opts.json' ),
32+
'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), {
33+
'encoding': 'utf8'
34+
})
35+
});
36+
37+
// Get any provided command-line arguments:
38+
args = cli.args();
39+
40+
// Get any provided options:
41+
flags = cli.flags();
42+
if ( flags.platform === 'win32' ) {
43+
fun = isAbsolutePath.win32;
44+
} else if ( flags.platform === 'posix' ) {
45+
fun = isAbsolutePath.posix;
46+
} else {
47+
fun = isAbsolutePath;
5448
}
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 );
83-
84-
if ( args.help ) {
85-
return help();
86-
}
87-
if ( args.version ) {
88-
return version();
89-
}
90-
if ( args.platform === 'win32' ) {
91-
console.log( main.win32( args._[0] ) );
92-
} else if ( args.platform === 'posix' ) {
93-
console.log( main.posix( args._[0] ) );
94-
} else {
95-
console.log( main( args._[0] ) );
96-
}
49+
// Check if we are receiving data from `stdin`...
50+
if ( !process.stdin.isTTY ) {
51+
return stdin( onRead );
52+
}
53+
console.log( fun( args[ 0 ] ) ); // eslint-disable-line no-console
54+
55+
/**
56+
* Callback invoked upon reading from `stdin`.
57+
*
58+
* @private
59+
* @param {(Error|null)} error - error object
60+
* @param {Buffer} data - data
61+
* @returns {void}
62+
*/
63+
function onRead( error, data ) {
64+
/* eslint-disable no-console */
65+
var lines;
66+
var i;
67+
if ( error ) {
68+
process.exitCode = 1;
69+
return console.error( 'Error: %s', error.message );
70+
}
71+
lines = data.toString().split( RE_EOL );
72+
for ( i = 0; i < lines.length; i++ ) {
73+
console.log( fun( lines[ i ] ) );
74+
}
75+
} // end FUNCTION onRead()
76+
} // end FUNCTION main()
77+
78+
main();

lib/node_modules/@stdlib/assert/is-absolute-path/bin/usage.txt renamed to lib/node_modules/@stdlib/assert/is-absolute-path/docs/usage.txt

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

2-
Usage: is-absolute-path [options]
2+
Usage: is-absolute-path [options] <path>
33

44
Options:
55

lib/node_modules/@stdlib/assert/is-absolute-path/bin/opts.json renamed to lib/node_modules/@stdlib/assert/is-absolute-path/etc/cli_opts.json

File renamed without changes.
Lines changed: 14 additions & 0 deletions
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+
}
Lines changed: 200 additions & 0 deletions
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 is an absolute path', opts, function test( t ) {
127+
var cmd = [
128+
process.execPath,
129+
'-e',
130+
'"process.stdin.isTTY = true; process.argv[ 2 ] = \'/var/www/\'; 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 "./index.js\n/var/www/"',
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)