-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-commmand-mapper.js
1 lines (1 loc) · 4.03 KB
/
cli-commmand-mapper.js
1
require("./classes/globals");const commandLineArgs=require("command-line-args"),MAIN_ARGS=require("./consts/default-cmd-args");const mainOptions=commandLineArgs(MAIN_ARGS,{stopAtFirstUnknown:true});const argv=mainOptions._unknown||[];const TAKEN_COMMAND_ALIASES=["h"];module.exports={cmds:{},cmdsLoaded:false,initCmds:function(cmdsPath,ignoreRoutes=[],cmdsPrefix="cmds",logger){const fs=require("fs");let self=this;fs.readdirSync(cmdsPath).forEach((cmd=>{if(ignoreRoutes.indexOf(`${cmdsPrefix}/${cmd}`)>=0)return;if(!test("-f",`${cmdsPath}/${cmd}/run.js`)&&!test("-f",`${cmdsPath}/${cmd}/options.js`)){fs.readdirSync(`${cmdsPath}/${cmd}`).forEach((cmdh=>{let options=require(`${cmdsPath}/${cmd}/${cmdh}/options`)["params"];options.forEach((p=>{if(TAKEN_COMMAND_ALIASES.includes(p.alias)){logger.critical(`Command alias "${p.name}:${p.alias}" is using a taken alias. The following command aliases are already reserved "${TAKEN_COMMAND_ALIASES.join(", ")}"`)}}));self.setCmdAndExample(options);self.cmds[`${cmd.replace(/_/g,":")}:${cmdh}`.replace(/_/g,":")]={isListCmd:false,cmd:cmd,options:options,runnable:require(`${cmdsPath}/${cmd}/${cmdh}/run`)}}))}else{let options=require(`${cmdsPath}/${cmd}/options`);options["params"].forEach((p=>{if(TAKEN_COMMAND_ALIASES.includes(p.alias)){logger.critical(`Command alias "${p.name}:${p.alias}" is using a taken alias. The following command aliases are already reserved "${TAKEN_COMMAND_ALIASES.join(", ")}"`)}}));if(options["type"]=="list"){let opts=this.getOptions(undefined,options["params"]);let cmdList=[];fs.readdirSync(`${cmdsPath}/${opts["cmdGroup"]}`).forEach((cmdh=>{let options=require(`${cmdsPath}/${opts["cmdGroup"]}/${cmdh}/options`)["params"];options["params"].forEach((p=>{if(TAKEN_COMMAND_ALIASES.includes(p.alias)){logger.critical(`Command alias "${p.name}:${p.alias}" is using a taken alias. The following command aliases are already reserved "${TAKEN_COMMAND_ALIASES.join(", ")}"`)}}));cmdList.push({cmd:`${opts["cmdGroup"]}:${cmdh}`,options:options,runnable:require(`${cmdsPath}/${opts["cmdGroup"]}/${cmdh}/run`)})}));self.cmds[cmd.replace(/_/g,":")]={isListCmd:true,runnables:cmdList}}else{self.cmds[cmd.replace(/_/g,":")]={isListCmd:false,cmd:cmd,options:options["params"],runnable:require(`${cmdsPath}/${cmd}/run`)}}}}));this.cmdsLoaded=true},getLogoContents:function(projectConfCont){let str="";try{str="\n"+exec(`figlet -w 200 -c '${projectConfCont.projectName}'`,{silent:true}).stdout}catch(e){str=`\n\t${projectConfCont.projectName}\n\n\tTo use ASCI Art logo the please install figlet with "sudo apt-get install figlet"`}return str},setCmdAndExample:function(options){let str="";for(let o of options){str+=o["alias"]+' "example" '}options["cmd"]=options["cmd"];options["example"]=str},getHelpContents:function(cmd,cmdsPath,ignoreRoutes,projectConfPath){return require(__dirname+"/help")(cmd,cmdsPath,ignoreRoutes,projectConfPath)},cmdExists:function(cmd){return typeof this.cmds[cmd]!=="undefined"},getOptions:function(cmd,options){if(cmd&&!this.cmdExists(cmd)){throw`Command "${cmd}" did not exist\n`}let argv=mainOptions._unknown||[];if(this.cmdsLoaded){let argGIndex=argv.indexOf("-g");if(argGIndex>=0){argv.splice(argGIndex+1,1);argv.splice(argGIndex,1)}}let out,opts;if(options!=undefined){opts=options;out=commandLineArgs(options,{argv:argv})}else{opts=this.cmds[cmd].options;out=commandLineArgs(this.cmds[cmd].options,{argv:argv})}if(Array.isArray(opts)){for(let o of opts){if(typeof o["format"]=="function"&&typeof out[o.name]!="undefined"){out[o.name]=o["format"](out[o.name])}}}return out},run:async function(cmd,logger){if(DO_TOOL_LOGGING)logger.info("CMD TO RUN: ",cmd);if(!this.cmdExists(cmd))throw`Command "${cmd}" did not exist`;if(this.cmds[cmd].isListCmd){for(let c of this.cmds[cmd].runnables){let options=this.getOptions(c["cmd"],undefined);try{ckCmdArgs(c["options"],options)}catch(e){logger.critical(e)}await c["runnable"](options,logger,cmd)}return}let options=this.getOptions(cmd,undefined);let cmdI=this.cmds[cmd];try{ckCmdArgs(cmdI["options"],options,cmd)}catch(e){logger.critical(e)}await cmdI["runnable"](options,logger,cmd)}};