-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.js
1 lines (1 loc) · 2.96 KB
/
help.js
1
const fs=require("fs");module.exports=function(cmd,rootCmds,ignoreRoutes,projectConfCont){let str="";try{str="\n\n"+exec(`figlet -w 150 -c '${projectConfCont.projectName}'`,{silent:true}).stdout}catch(e){str=`\n\n\t${projectConfCont.projectName}\n\n\tTo use ASCI Art logo the please install figlet with "sudo apt-get install figlet"`}function getJobSection(index,job,cmd){let allExamples=[],params="",example="";for(let k in job["params"]){let param=job["params"][k];if(typeof param=="function")continue;if(!DO_LOGGING_EXTRA_SPACING){params+=`${k>0?" ":""}${param["alias"]} - ${param["name"]}`}else{params+=`${k>0?"\t\t\t\t\t":""}${param["alias"]} - ${param["name"]}`}let exValue="";if(typeof param["defaultValue"]!="undefined"){params+=" - Default: "+(typeof param["defaultValue"]=="object"?JSON.stringify(param["defaultValue"],null,4):param["defaultValue"]);exValue=param["defaultValue"]}else if((typeof param["example"]).includes(["string","number"])){exValue=param["example"]}else{exValue="Not Set"}example=`-${param["alias"]} ${typeof exValue=="string"?`"${exValue}"`:exValue}`;allExamples.push(example);params+="\n"}if(!DO_LOGGING_EXTRA_SPACING){return`${index}. ${job["title"].white.bold}\n${job["desc"]} \n - command "${projectConfCont.cmdAlias.bold} ${String(cmd).bold}"\n - Usage: \`${projectConfCont.cmdAlias.yellow.bold} ${cmd.yellow.bold}${allExamples.length>0?` ${allExamples.join(" ").yellow.bold}`:""}\`\n - Params:\n ${params}`}return`\n\t\t\t${index}. ${job["title"].white.bold}\n\n\t\t\t\t${job["desc"]}\n\t\t\t\t- command "${projectConfCont.cmdAlias.yellow.bold} ${cmd.yellow.bold}"\n\n\t\t\t\t- example usage: \`${projectConfCont.cmdAlias.yellow.bold} ${cmd.yellow.bold}${allExamples.length>0?` ${allExamples.join(" ").yellow.bold}`:""}\`\n\t\t\t\t - Params:\n\t\t\t\t ${params}\n\t\t\t\t - Example Use: ${example}`}let inStr="",cmds={},counter=1;fs.readdirSync(rootCmds).forEach((cmd=>{if(ignoreRoutes.indexOf(`${rootCmds}/${cmd}`)>=0)return;if(!test("-f",`${rootCmds}/${cmd}/run.js`)&&!test("-f",`${rootCmds}/${cmd}/options.js`)){if(ignoreRoutes.indexOf(`${cmd}`)>=0)return;fs.readdirSync(`${rootCmds}/${cmd}`).forEach((cmdh=>{if(ignoreRoutes.indexOf(`${cmd}/${cmdh}`)>=0)return;let options=require(`${rootCmds}/${cmd}/${cmdh}/options`);options["command"]=`${cmd}:${cmdh}`;cmds[`${cmd.replace(/_/g,":")}:${cmdh.replace(/_/g,":")}`]={pos:counter,options:options};counter++}))}else{let options=require(`${rootCmds}/${cmd}/options`);cmds[cmd.replace(/_/g,":")]={pos:counter,options:options}}counter++}));if(cmd&&!cmds[cmd]){console.log(`GIVEN COMMAND DOES NOT EXIST. GIVEN CMD: ${cmd}. AVAILABLE COMMANDS: ${Object.keys(cmds)}`)}else if(cmd){inStr=`${getJobSection(cmds[cmd].pos,cmds[cmd].options,cmd)}`}else{let cmdi;for(cmdi in cmds){inStr+=`${getJobSection(cmds[cmdi].pos,cmds[cmdi].options,cmdi)}\n`}}if(!DO_LOGGING_EXTRA_SPACING){return str+`\n${projectConfCont.description}\n\n${inStr}\n\t\t`}return str+`\n\n\t\t${projectConfCont.description}\n\n\t\t${inStr}\n\n\t`};