cli (2221B)
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 27 // NOTE: we explicitly avoid using `@stdlib/fs/read-file` in this particular package in order to avoid circular dependencies. This should not be problematic as this command-line utility will be executed via Node.js. 28 var readFileSync = require( 'fs' ).readFileSync; // eslint-disable-line no-sync 29 var CLI = require( '@stdlib/cli/ctor' ); 30 var manifest = require( './../lib' ); 31 32 33 // MAIN // 34 35 /** 36 * Main execution sequence. 37 * 38 * @private 39 */ 40 function main() { 41 var conditions; 42 var flags; 43 var args; 44 var conf; 45 var opts; 46 var cli; 47 var v; 48 var i; 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 opts = {}; 69 if ( flags.dir ) { 70 opts.basedir = flags.dir; 71 } 72 if ( flags.paths ) { 73 opts.paths = flags.paths; 74 } 75 conditions = {}; 76 for ( i = 0; i < flags[ '--' ].length; i++ ) { 77 v = flags[ '--' ][ i ]; 78 v = v.split( '=' ); 79 if ( v.length === 2 ) { 80 conditions[ v[ 0 ].substring( 2 ) ] = v[ 1 ]; 81 } else { 82 conditions[ v[ 0 ].substring( 2 ) ] = flags[ '--' ][ i+1 ]; 83 i += 1; 84 } 85 } 86 conf = manifest( args[ 0 ], conditions, opts ); 87 console.log( JSON.stringify( conf ) ); // eslint-disable-line no-console 88 } 89 90 main();