time-to-botec

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

cli (2965B)


      1 #!/usr/bin/env node
      2 
      3 /**
      4 * @license Apache-2.0
      5 *
      6 * Copyright (c) 2021 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 stdin = require( '@stdlib/process/read-stdin' );
     29 var stdinStream = require( '@stdlib/streams/node/stdin' );
     30 var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
     31 var isRegExpString = require( '@stdlib/assert/is-regexp-string' );
     32 var reFromString = require( '@stdlib/utils/regexp-from-string' );
     33 var trim = require( '@stdlib/string/trim' );
     34 var acronym = require( './../lib' );
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Main execution sequence.
     41 *
     42 * @private
     43 * @returns {void}
     44 */
     45 function main() {
     46 	var stopwords;
     47 	var flags;
     48 	var args;
     49 	var opts;
     50 	var cli;
     51 	var i;
     52 
     53 	// Create a command-line interface:
     54 	cli = new CLI({
     55 		'pkg': require( './../package.json' ),
     56 		'options': require( './../etc/cli_opts.json' ),
     57 		'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
     58 			'encoding': 'utf8'
     59 		})
     60 	});
     61 
     62 	// Get any provided command-line options:
     63 	flags = cli.flags();
     64 	if ( flags.help || flags.version ) {
     65 		return;
     66 	}
     67 
     68 	// Get any provided command-line arguments:
     69 	args = cli.args();
     70 
     71 	// Check if we are receiving data from `stdin`...
     72 	opts = {};
     73 	if ( !stdinStream.isTTY ) {
     74 		if ( flags.split ) {
     75 			if ( !isRegExpString( flags.split ) ) {
     76 				flags.split = '/'+flags.split+'/';
     77 			}
     78 			opts.split = reFromString( flags.split );
     79 		} else {
     80 			opts.split = RE_EOL;
     81 		}
     82 		if ( flags.stopwords ) {
     83 			stopwords = flags.stopwords.split( ',' );
     84 			for ( i = 0; i < stopwords.length; i++ ) {
     85 				stopwords[ i ] = trim( stopwords[ i ] );
     86 			}
     87 			opts.stopwords = stopwords;
     88 		}
     89 		return stdin( onRead );
     90 	}
     91 	console.log( acronym( args[ 0 ] ) ); // eslint-disable-line no-console
     92 
     93 	/**
     94 	* Callback invoked upon reading from `stdin`.
     95 	*
     96 	* @private
     97 	* @param {(Error|null)} error - error object
     98 	* @param {Buffer} data - data
     99 	* @returns {void}
    100 	*/
    101 	function onRead( error, data ) {
    102 		var lines;
    103 		var i;
    104 		if ( error ) {
    105 			return cli.error( error );
    106 		}
    107 		lines = data.toString().split( opts.split );
    108 
    109 		// Remove any trailing separators (e.g., trailing newline)...
    110 		if ( lines[ lines.length-1 ] === '' ) {
    111 			lines.pop();
    112 		}
    113 		for ( i = 0; i < lines.length; i++ ) {
    114 			console.log( acronym( lines[ i ] ) ); // eslint-disable-line no-console
    115 		}
    116 	}
    117 }
    118 
    119 main();