cli (2874B)
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 rpad = require( './../lib' ); 34 35 36 // MAIN // 37 38 /** 39 * Main execution sequence. 40 * 41 * @private 42 * @returns {void} 43 */ 44 function main() { 45 var split; 46 var flags; 47 var args; 48 var cli; 49 var len; 50 var pad; 51 var str; 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 if ( args.length ) { 72 str = args[ 0 ]; 73 } else { 74 // Treat an empty value as an empty string: 75 str = ''; 76 } 77 len = parseInt( flags.len, 10 ); 78 pad = flags.pad || ' '; 79 80 // Check if we are receiving data from `stdin`... 81 if ( !stdinStream.isTTY ) { 82 if ( flags.split ) { 83 if ( !isRegExpString( flags.split ) ) { 84 flags.split = '/'+flags.split+'/'; 85 } 86 split = reFromString( flags.split ); 87 } else { 88 split = RE_EOL; 89 } 90 return stdin( onRead ); 91 } 92 console.log( rpad( str, len, pad ) ); // eslint-disable-line no-console 93 94 /** 95 * Callback invoked upon reading from `stdin`. 96 * 97 * @private 98 * @param {(Error|null)} error - error object 99 * @param {Buffer} data - data 100 * @returns {void} 101 */ 102 function onRead( error, data ) { 103 var lines; 104 var i; 105 if ( error ) { 106 return cli.error( error ); 107 } 108 lines = data.toString().split( split ); 109 110 // Remove any trailing separators (e.g., trailing newline)... 111 if ( lines[ lines.length-1 ] === '' ) { 112 lines.pop(); 113 } 114 for ( i = 0; i < lines.length; i++ ) { 115 console.log( rpad( lines[ i ], len, pad ) ); // eslint-disable-line no-console 116 } 117 } 118 } 119 120 main();