cli (1870B)
1 #!/usr/bin/env node 2 3 /** 4 * @license Apache-2.0 5 * 6 * Copyright (c) 2021 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 resolveParentPath = require( './../lib' ); 29 30 31 // MAIN // 32 33 /** 34 * Main execution sequence. 35 * 36 * @private 37 * @returns {void} 38 */ 39 function main() { 40 var flags; 41 var args; 42 var opts; 43 var cli; 44 45 // Create a command-line interface: 46 cli = new CLI({ 47 'pkg': require( './../package.json' ), 48 'options': require( './../etc/cli_opts.json' ), 49 'help': readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), { 50 'encoding': 'utf8' 51 }) 52 }); 53 54 // Get any provided command-line options: 55 flags = cli.flags(); 56 if ( flags.help || flags.version ) { 57 return; 58 } 59 60 // Get any provided command-line arguments: 61 args = cli.args(); 62 63 opts = {}; 64 if ( flags.dir ) { 65 opts.dir = flags.dir; 66 } 67 resolveParentPath( args[ 0 ], opts, onPath ); 68 69 /** 70 * Callback invoked upon resolving a path. 71 * 72 * @private 73 * @param {(Error|null)} error - error object 74 * @param {string} path - resolved path 75 * @returns {void} 76 */ 77 function onPath( error, path ) { 78 if ( error ) { 79 return cli.error( error ); 80 } 81 console.log( path ); // eslint-disable-line no-console 82 } 83 } 84 85 main();