68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils"
|
|
import chalk from "chalk"
|
|
|
|
const cliConfig: any = {}
|
|
|
|
const emphasizeText = (...args) => {
|
|
console.log(chalk.bold(...args))
|
|
}
|
|
|
|
const italicEmphasizeGreen = (...args) => {
|
|
console.group(chalk.italic.green.bold(...args))
|
|
}
|
|
|
|
const formatCommandSection = (cmd, prefixCmd, maxLen = 25) => {
|
|
const formattedCmd = prefixCmd ? `${prefixCmd} ${cmd}` : cmd
|
|
let padding = maxLen - formattedCmd.length
|
|
if (padding < 0) padding = 0
|
|
|
|
return "jj " + formattedCmd + " ".repeat(padding)
|
|
}
|
|
|
|
const formatHelpSection = (desc, prefixDesc) => {
|
|
const formattedDesc = prefixDesc ? `${prefixDesc} ${desc}` : desc
|
|
return formattedDesc
|
|
}
|
|
|
|
const generateHelp = ({ cmds, prefixCmd = "", prefixDesc = "" }) => {
|
|
for (const cmd of cmds) {
|
|
const commandLabel = cmd?.postCommand ? `${cmd?.key} ${cmd?.postCommand}` : cmd?.key
|
|
if (cmd?.mainTitle) {
|
|
italicEmphasizeGreen(cmd?.mainTitle)
|
|
if (cmd?.subCommands?.length > 0) {
|
|
generateHelp({
|
|
cmds: cmd?.subCommands || [],
|
|
prefixCmd: cmd?.key,
|
|
prefixDesc: cmd?.desc,
|
|
})
|
|
} else {
|
|
emphasizeText(formatCommandSection(commandLabel, prefixCmd, cmd?.maxLength), formatHelpSection(cmd?.desc, prefixDesc))
|
|
}
|
|
console.groupEnd()
|
|
} else {
|
|
emphasizeText(formatCommandSection(commandLabel, prefixCmd, cmd?.maxLength), formatHelpSection(cmd?.desc, prefixDesc))
|
|
}
|
|
}
|
|
}
|
|
|
|
const cliHelp = async () => {
|
|
console.log(chalk.blue.italic.bold("Usage: jj <COMMAND>"))
|
|
console.log("")
|
|
console.group(chalk.blue.italic.bold("Commands:"))
|
|
console.log("")
|
|
|
|
cliConfig.commandsObject = getJJConfigByKeyChain(`commands`)
|
|
cliConfig.commandList = Object.keys(cliConfig.commandsObject).map((cmdKey) => ({
|
|
key: cmdKey,
|
|
...cliConfig.commandsObject[cmdKey]
|
|
}))
|
|
|
|
generateHelp({
|
|
cmds: cliConfig.commandList,
|
|
})
|
|
|
|
console.groupEnd()
|
|
}
|
|
|
|
export default cliHelp;
|