WIP
This commit is contained in:
+67
@@ -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 <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;
|
||||
+34
-2
@@ -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)
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
export const getJjYamlObj = () => { return jjConfigsExp.jjYamlObj || {} }
|
||||
|
||||
export const getJJConfigByKeyChain = (chain) => {
|
||||
|
||||
return _.get(getJjYamlObj(), chain, {})
|
||||
}
|
||||
+1
-2
@@ -146,6 +146,5 @@ if (
|
||||
) {
|
||||
|
||||
const jj = await jjEnsure({ returnProcessEnvs: true })
|
||||
// console.log(jj)
|
||||
// console.log(jjConfigs)
|
||||
process.exit(0)
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user