cli (2254B)
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 cwd = require( '@stdlib/process/cwd' ); 29 var readFileList = require( './../lib' ); 30 31 32 // MAIN // 33 34 /** 35 * Main execution sequence. 36 * 37 * @private 38 * @returns {void} 39 */ 40 function main() { 41 var flags; 42 var files; 43 var opts; 44 var cli; 45 var dir; 46 var i; 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 files = cli.args(); 65 66 opts = {}; 67 if ( flags.encoding ) { 68 opts.encoding = flags.encoding; 69 } 70 if ( flags.flag ) { 71 opts.flag = flags.flag; 72 } 73 74 dir = cwd(); 75 for ( i = 0; i < files.length; i++ ) { 76 files[ i ] = resolve( dir, files[ i ] ); 77 } 78 readFileList( files, opts, onFiles ); 79 80 /** 81 * Callback invoked upon reading the contents of one or more files. 82 * 83 * @private 84 * @param {(Error|null)} error - error object 85 * @param {ObjectArray} results - results 86 * @returns {void} 87 */ 88 function onFiles( error, results ) { 89 var i; 90 if ( error ) { 91 return cli.error( error ); 92 } 93 for ( i = 0; i < results.length; i++ ) { 94 results[ i ].data = results[ i ].data.toString(); 95 console.log( JSON.stringify( results[ i ] ) ); // eslint-disable-line no-console 96 } 97 } 98 } 99 100 main();