-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathcli
executable file
·78 lines (68 loc) · 1.7 KB
/
cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
'use strict';
// MODULES //
var join = require( 'path' ).join;
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var CLI = require( '@stdlib/tools/cli' );
var stdin = require( '@stdlib/utils/read-stdin' );
var RE_EOL = require( '@stdlib/regexp/eol' );
var isAbsolutePath = require( './../lib' );
// MAIN //
/**
* Main execution sequence.
*
* @private
* @returns {void}
*/
function main() {
var flags;
var args;
var cli;
var fun;
// Create a command-line interface:
cli = new CLI({
'pkg': require( './../package.json' ),
'options': require( './../etc/cli_opts.json' ),
'help': readFileSync( join( __dirname, '..', 'docs', 'usage.txt' ), {
'encoding': 'utf8'
})
});
// Get any provided command-line arguments:
args = cli.args();
// Get any provided options:
flags = cli.flags();
if ( flags.platform === 'win32' ) {
fun = isAbsolutePath.win32;
} else if ( flags.platform === 'posix' ) {
fun = isAbsolutePath.posix;
} else {
fun = isAbsolutePath;
}
// Check if we are receiving data from `stdin`...
if ( !process.stdin.isTTY ) {
return stdin( onRead );
}
console.log( fun( args[ 0 ] ) ); // eslint-disable-line no-console
/**
* Callback invoked upon reading from `stdin`.
*
* @private
* @param {(Error|null)} error - error object
* @param {Buffer} data - data
* @returns {void}
*/
function onRead( error, data ) {
/* eslint-disable no-console */
var lines;
var i;
if ( error ) {
process.exitCode = 1;
return console.error( 'Error: %s', error.message );
}
lines = data.toString().split( RE_EOL );
for ( i = 0; i < lines.length; i++ ) {
console.log( fun( lines[ i ] ) );
}
} // end FUNCTION onRead()
} // end FUNCTION main()
main();