time-to-botec

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

cli (2500B)


      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 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 replace = require( './../lib' );
     34 
     35 
     36 // MAIN //
     37 
     38 /**
     39 * Main execution sequence.
     40 *
     41 * @private
     42 * @returns {void}
     43 */
     44 function main() {
     45 	var search;
     46 	var flags;
     47 	var args;
     48 	var cli;
     49 
     50 	// Create a command-line interface:
     51 	cli = new CLI({
     52 		'pkg': require( './../package.json' ),
     53 		'options': require( './../etc/cli_opts.json' ),
     54 		'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
     55 			'encoding': 'utf8'
     56 		})
     57 	});
     58 
     59 	// Get any provided command-line options:
     60 	flags = cli.flags();
     61 	if ( flags.help || flags.version ) {
     62 		return;
     63 	}
     64 
     65 	// Get any provided command-line arguments:
     66 	args = cli.args();
     67 
     68 	search = flags.search;
     69 	if ( isRegExpString( search ) ) {
     70 		search = reFromString( search );
     71 	}
     72 
     73 	// Check if we are receiving data from `stdin`...
     74 	if ( !stdinStream.isTTY ) {
     75 		return stdin( onRead );
     76 	}
     77 	console.log( replace( args[ 0 ], search, flags.newval ) ); // eslint-disable-line no-console
     78 
     79 	/**
     80 	* Callback invoked upon reading from `stdin`.
     81 	*
     82 	* @private
     83 	* @param {(Error|null)} error - error object
     84 	* @param {Buffer} data - data
     85 	* @returns {void}
     86 	*/
     87 	function onRead( error, data ) {
     88 		var lines;
     89 		var i;
     90 		if ( error ) {
     91 			return cli.error( error );
     92 		}
     93 		lines = data.toString().split( RE_EOL );
     94 		for ( i = 0; i < lines.length; i++ ) {
     95 			console.log( replace( lines[ i ], search, flags.newval ) ); // eslint-disable-line no-console
     96 		}
     97 	}
     98 }
     99 
    100 main();