UNPKG

ninjs-lodash

Version:
151 lines (118 loc) 4.96 kB
/** * Command Line Interpreter (CLI) Utils */ 'use strict' const _ = require('lodash') const libs = require('./libs') const core = require('./core') const config = require('./config') const debug = require('./debug') const fs = require('./fs') const MARGIN = _.repeat(' ', 2) const EOL = require('os').EOL // NOT USED -> just illustrates meaning of Command Object Properties const COMMAND_OBJECT_MODEL = { // command default options, args, switches "options": { key: '' }, // command handler // options = _.merge(options, 'interpreted' options from command line args/switches) "handler": (options) => { /* Do Something -> run a a command, invoke an api, .... */ }, // command help (--help) "text": "This is printed when jav.key === settings && jav.isHelp", // command detailed help (--help -l) "body": "This is printed when jav.key === settings && jav.isHelp && jav.isLong.. If no body set -> obj.text is used instead" } exports = module.exports = { "cli": cli, "clik": clik, "commands": commands, "writeCommands": writeCommands } _.mixin(exports) function commands(obj, nsroot) { if(!obj) return '' nsroot = nsroot || '' const items = _.functionsIn(obj) const map = _.map(items, function(item) { return 'npm t -- ' + _.join(_.compact([nsroot, item]), '.') }) return _.join(map, EOL) } function writeCommands(dest, iface, nsroot, callback) { if(!dest || !_.isString(dest)) return _.fail('Invalid dest', callback) if(!iface) return _.fail('Invalid interface', callback) return _.outputFile(dest, commands(iface, nsroot), _.cb(callback)) } function cli(map) { let jav = _.jav() let keypath = _.get(jav, 'ns.keypath') let switches = _.get(jav, 'sw') || {} let obj = keypath ? _.get(map, keypath) : null let handler = obj ? _.get(obj, 'handler') : null let opts = obj ? _.merge({}, _.get(obj, 'options'), switches) : null let cb = obj ? _.get(obj, 'cb') || null : null if(!obj || !_.isPlainObject(obj)) return _.log(`Command not found for namespace ${keypath}`) if(!_.isFunction(handler)) return _.log(`Command handler not found for namespace ${keypath}`) return _.isFunction(cb) ? handler(opts, cb) : handler(opts) } function clik(iapi, options, callback) { let key = _.get(options, "k") || "" let fn = key && iapi ? iapi[key] : null if(!_.isFunction(fn)) return _.log(`\nCommand Key (--k="install|publish|monitor|...") Not Found for k: ${key}\n`) let res = _.attempt(fn, _.omit(options, ['k']), callback) if(_.isError(res)) _.log(res) } // function parseHelp(name, lookup, memo, pk) { // memo = memo && _.isPlainObject(memo) ? memo : { keys: name ? [name] : [], accum: {} } // if(pk) memo.keys.push(pk) // _.forIn(lookup, (v, k) => { // if(!_.isPlainObject(v)) return // let helpText = _.get(v, 'help') // if(_.isArray(helpText)) { // let arr = _.clone(helpText) // let mkey = _.join(_.concat(memo.keys, k), '.') // let opts = _.get(v, 'options') // let opts_str = _.isEmpty(opts) ? '' : _.join(_.map(opts, (ov, ok) => { return `--${ok}${ ov ? '=' + ov : '' }` }), ' ') // arr.unshift(_.join(_.compact([mkey, opts_str]), ' '), '') // memo.accum[mkey] = arr // } // return parseHelp(name, v, memo, k) // }) // memo.keys.pop() // return memo // } // function cli() { // let root = _.$('paths.root') // let api = _.$('jav.ns.api') // let rpath = root && api ? _.path.join(root, 'cli', api) : '' // let ns = _.$('jav.ns.path') // let keypath = _.$('jav.ns.keypath') // let switches = _.$('jav.sw') // let is_help = _.$('jav.is_help') // if(!root) return _.log('Invalid root') // if(!api) return _.log('Invalid api') // if(!rpath) return _.log('Invalid api') // if(!ns) return _.log('Invalid ns') // if(!keypath) return _.log('Invalid keypath') // _.log() // add line after command prompt // let mod = _.attempt(require, rpath) // if(_.isError(mod)) return _.log(mod) // let obj = _.get(mod, keypath) // if(!_.isPlainObject(obj)) return _.log(`Invalid command for namespace ${keypath}`) // if(is_help) { // let memo = parseHelp(api, mod) // let accum = _.get(memo, 'accum') // let helpArray = _.get(accum, ns) // if(!helpArray) return _.log(`No HELP found for namespace ${keypath}`) // helpArray = _.map(helpArray, (text, idx) => { // if(!text) return idx === 0 ? '' : MARGIN // return idx === 0 ? text : `${MARGIN}${text}` // }) // return _.log(_.join(helpArray, EOL)) // } // let prop = _.get(obj, 'prop') // if(!_.isFunction(prop)) return _.log(`No COMMAND found for namespace ${keypath}`) // let opts = _.merge({}, _.get(obj, 'options'), switches) // let cb = _.get(obj, 'cb') || null // return _.isFunction(cb) ? prop(opts, cb) : prop(opts) // }