time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

cli (2269B)


      1 #!/usr/bin/env node
      2 
      3 /**
      4 * @license Apache-2.0
      5 *
      6 * Copyright (c) 2018 The Stdlib Authors.
      7 *
      8 * Licensed under the Apache License, Version 2.0 (the "License");
      9 * you may not use this file except in compliance with the License.
     10 * You may obtain a copy of the License at
     11 *
     12 *    http://www.apache.org/licenses/LICENSE-2.0
     13 *
     14 * Unless required by applicable law or agreed to in writing, software
     15 * distributed under the License is distributed on an "AS IS" BASIS,
     16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17 * See the License for the specific language governing permissions and
     18 * limitations under the License.
     19 */
     20 
     21 'use strict';
     22 
     23 // MODULES //
     24 
     25 var resolve = require( 'path' ).resolve;
     26 var readFileSync = require( '@stdlib/fs/read-file' ).sync;
     27 var CLI = require( '@stdlib/cli/ctor' );
     28 var trim = require( '@stdlib/string/trim' );
     29 var stdin = require( '@stdlib/process/read-stdin' );
     30 var stdinStream = require( '@stdlib/streams/node/stdin' );
     31 var reEOL = require( '@stdlib/regexp/eol' );
     32 var isUNCPath = require( './../lib' );
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Main execution sequence.
     39 *
     40 * @private
     41 * @returns {void}
     42 */
     43 function main() {
     44 	var flags;
     45 	var args;
     46 	var cli;
     47 
     48 	// Create a command-line interface:
     49 	cli = new CLI({
     50 		'pkg': require( './../package.json' ),
     51 		'options': require( './../etc/cli_opts.json' ),
     52 		'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
     53 			'encoding': 'utf8'
     54 		})
     55 	});
     56 
     57 	// Get any provided command-line options:
     58 	flags = cli.flags();
     59 	if ( flags.help || flags.version ) {
     60 		return;
     61 	}
     62 
     63 	// Get any provided command-line arguments:
     64 	args = cli.args();
     65 
     66 	// Check if we are receiving data from `stdin`...
     67 	if ( !stdinStream.isTTY ) {
     68 		return stdin( onRead );
     69 	}
     70 	console.log( isUNCPath( trim( args[ 0 ] ) ) ); // eslint-disable-line no-console
     71 
     72 	/**
     73 	* Callback invoked upon reading from `stdin`.
     74 	*
     75 	* @private
     76 	* @param {(Error|null)} error - error object
     77 	* @param {Buffer} data - data
     78 	* @returns {void}
     79 	*/
     80 	function onRead( error, data ) {
     81 		var lines;
     82 		var i;
     83 		if ( error ) {
     84 			return cli.error( error );
     85 		}
     86 		lines = data.toString().split( reEOL.REGEXP );
     87 		for ( i = 0; i < lines.length; i++ ) {
     88 			console.log( isUNCPath( lines[ i ] ) ); // eslint-disable-line no-console
     89 		}
     90 	}
     91 }
     92 
     93 main();