cli (3025B)
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 pad = require( './../lib' ); 34 35 36 // MAIN // 37 38 /** 39 * Main execution sequence. 40 * 41 * @private 42 * @returns {void} 43 */ 44 function main() { 45 var flags; 46 var args; 47 var opts; 48 var cli; 49 var len; 50 var str; 51 52 // Create a command-line interface: 53 cli = new CLI({ 54 'pkg': require( './../package.json' ), 55 'options': require( './../etc/cli_opts.json' ), 56 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { 57 'encoding': 'utf8' 58 }) 59 }); 60 61 // Get any provided command-line options: 62 flags = cli.flags(); 63 if ( flags.help || flags.version ) { 64 return; 65 } 66 67 // Get any provided command-line arguments: 68 args = cli.args(); 69 70 if ( args.length ) { 71 str = args[ 0 ]; 72 } else { 73 // Treat an empty value as an empty string: 74 str = ''; 75 } 76 len = parseInt( flags.len, 10 ); 77 opts = {}; 78 if ( flags.lpad ) { 79 opts.lpad = flags.lpad; 80 } 81 if ( flags.rpad ) { 82 opts.rpad = flags.rpad; 83 } 84 if ( flags.cright ) { 85 opts.centerRight = flags.cright; 86 } 87 88 // Check if we are receiving data from `stdin`... 89 if ( !stdinStream.isTTY ) { 90 if ( flags.split ) { 91 if ( !isRegExpString( flags.split ) ) { 92 flags.split = '/'+flags.split+'/'; 93 } 94 opts.split = reFromString( flags.split ); 95 } else { 96 opts.split = RE_EOL; 97 } 98 return stdin( onRead ); 99 } 100 console.log( pad( str, len, opts ) ); // eslint-disable-line no-console 101 102 /** 103 * Callback invoked upon reading from `stdin`. 104 * 105 * @private 106 * @param {(Error|null)} error - error object 107 * @param {Buffer} data - data 108 * @returns {void} 109 */ 110 function onRead( error, data ) { 111 var lines; 112 var i; 113 if ( error ) { 114 return cli.error( error ); 115 } 116 lines = data.toString().split( opts.split ); 117 118 // Remove any trailing separators (e.g., trailing newline)... 119 if ( lines[ lines.length-1 ] === '' ) { 120 lines.pop(); 121 } 122 for ( i = 0; i < lines.length; i++ ) { 123 console.log( pad( lines[ i ], len, opts ) ); // eslint-disable-line no-console 124 } 125 } 126 } 127 128 main();