diff --git a/bun.lockb b/bun.lockb index 4e3947e..ae99940 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/cli/help.ts b/cli/help.ts new file mode 100644 index 0000000..18e90b9 --- /dev/null +++ b/cli/help.ts @@ -0,0 +1,67 @@ +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 ")) + 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; diff --git a/helpers/jjCli.ts b/helpers/jjCli.ts index 01e8712..178c97b 100755 --- a/helpers/jjCli.ts +++ b/helpers/jjCli.ts @@ -1,6 +1,38 @@ #!/usr/bin/env bun -import { jjConfigsExp } from "./jjConfigUtils"; +import { + dynamicStringHandler, +} from "portainer_wrapper/src/server/portainerExpressMiddleware" + +import { getJJConfigByKeyChain, getJjYamlObj, jjConfigsExp } from "./jjConfigUtils"; +import _ from 'lodash'; +import { jjLogError } from "./jjLogger"; +import cliHelp from "cli/help"; + +const JJ_CLI_ARG_1 = process.env.JJ_CLI_ARG_1 + +const requiredProcessEnvs = { + JJ_SOURCE_DIR: process.env.JJ_SOURCE_DIR +} + +if (JJ_CLI_ARG_1 == 'help') { + await cliHelp() + process.exit(0) +} + + + + +const jjCommandConfig = getJJConfigByKeyChain(`commands['${JJ_CLI_ARG_1}']`) + +if (!jjCommandConfig || _.isEmpty(jjCommandConfig)) { + jjLogError(`Command ${JJ_CLI_ARG_1} is not available`) + process.exit(0) +} + +const scriptToRunPath = dynamicStringHandler(`${jjCommandConfig.scriptToRun.prePath}/${jjCommandConfig.scriptToRun.path}`, "", requiredProcessEnvs, false) +const scriptToRunFn = (await import(scriptToRunPath)).default +await scriptToRunFn() +process.exit(0) -console.log(jjConfigsExp) diff --git a/helpers/jjCliYargs.ts b/helpers/jjCliYargs.ts index e5113c0..dbe7196 100755 --- a/helpers/jjCliYargs.ts +++ b/helpers/jjCliYargs.ts @@ -71,6 +71,8 @@ try { }); console.log(`export JJ_CLI_OPTIONS="${JSON.stringify(allOptionsMap)}"`); + process.exit(0) + } catch (exception) { console.error(exception); process.exit(1); diff --git a/helpers/jjConfigUtils.ts b/helpers/jjConfigUtils.ts index 0e63169..b0a6f88 100644 --- a/helpers/jjConfigUtils.ts +++ b/helpers/jjConfigUtils.ts @@ -1,5 +1,14 @@ import fs from 'fs-extra'; +import _ from 'lodash'; + const rawConfigData = fs.readFileSync(`${process.env.JJ_SOURCE_DIR}/~~tmp/jjConfigs.json`, 'utf8'); -export const jjConfigsExp = JSON.parse(rawConfigData) \ No newline at end of file +export const jjConfigsExp = JSON.parse(rawConfigData) + +export const getJjYamlObj = () => { return jjConfigsExp.jjYamlObj || {} } + +export const getJJConfigByKeyChain = (chain) => { + + return _.get(getJjYamlObj(), chain, {}) +} \ No newline at end of file diff --git a/helpers/jjEnsure.ts b/helpers/jjEnsure.ts index ee5dc31..12a8ae1 100755 --- a/helpers/jjEnsure.ts +++ b/helpers/jjEnsure.ts @@ -146,6 +146,5 @@ if ( ) { const jj = await jjEnsure({ returnProcessEnvs: true }) - // console.log(jj) - // console.log(jjConfigs) + process.exit(0) } \ No newline at end of file diff --git a/package.json b/package.json index aad00af..7cfc86f 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "moment": "^2.30.1", "pino": "^9.5.0", "pino-pretty": "^12.1.0", + "portainer_wrapper": "1.0.50", "shelljs": "^0.8.5", "winston": "^3.16.0", "yargs": "^17.7.2"