WIP
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
export const dynamicStringHandler = (string: string, prefix: string, objectSource = {}, forceString = true) => {
|
||||
let newString = string
|
||||
Object.keys(objectSource).forEach((key) => {
|
||||
const value = objectSource[key]
|
||||
const keyReg = new RegExp(`__${prefix}${key}__`, "g")
|
||||
if (forceString) {
|
||||
newString = newString.replace(keyReg, typeof value == "string" ? `"${value}"` : value)
|
||||
} else {
|
||||
newString = newString.replace(keyReg, value)
|
||||
}
|
||||
})
|
||||
return newString
|
||||
}
|
||||
+13
-23
@@ -1,38 +1,28 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
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
|
||||
import { jjCliYargs } from "./jjCliYargs";
|
||||
jjCliYargs(false)
|
||||
|
||||
const requiredProcessEnvs = {
|
||||
JJ_SOURCE_DIR: process.env.JJ_SOURCE_DIR
|
||||
}
|
||||
|
||||
if (JJ_CLI_ARG_1 == 'help') {
|
||||
await cliHelp()
|
||||
process.exit(0)
|
||||
}
|
||||
import { getJJConfigByKeyChain, getJjYamlObj, jjConfigsExp } from "./jjConfigUtils";
|
||||
import _ from 'lodash';
|
||||
import { dynamicStringHandler } from "./dynamicStringHandler";
|
||||
|
||||
|
||||
|
||||
|
||||
const jjCommandConfig = getJJConfigByKeyChain(`commands['${JJ_CLI_ARG_1}']`)
|
||||
const jjCommandConfig = getJJConfigByKeyChain(`commands['${process.env.JJ_CLI_ARG_1}']`)
|
||||
|
||||
if (!jjCommandConfig || _.isEmpty(jjCommandConfig)) {
|
||||
jjLogError(`Command ${JJ_CLI_ARG_1} is not available`)
|
||||
console.error(`Command ${process.env.JJ_CLI_ARG_1} is not available`)
|
||||
process.exit(0)
|
||||
} else {
|
||||
const scriptToRunPath = dynamicStringHandler(`${jjCommandConfig.scriptToRun.prePath}/${jjCommandConfig.scriptToRun.path}`, "", requiredProcessEnvs, false)
|
||||
const scriptToRunFn = (await import(scriptToRunPath)).default
|
||||
await scriptToRunFn()
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
+87
-58
@@ -5,75 +5,104 @@ import { hideBin } from "yargs/helpers";
|
||||
import fs from "fs-extra";
|
||||
import { jjConfigsExp } from "./jjConfigUtils";
|
||||
|
||||
try {
|
||||
const parsedConfig = jjConfigsExp;
|
||||
const availableEnvironments = parsedConfig?.jjYamlObj?.environments || {};
|
||||
|
||||
const cliArgs = yargs(hideBin(process.argv)).help(false).argv;
|
||||
const argumentsObj = { ...cliArgs };
|
||||
|
||||
const doubleDashIndex = process.argv.findIndex((arg) => arg === "--");
|
||||
const remainingArgs = doubleDashIndex === -1 ? [] : process.argv.filter((_, index) => index > doubleDashIndex);
|
||||
const mainCommands = (argumentsObj?._ || []).filter((cmd) => !remainingArgs.includes(cmd.toString()));
|
||||
export const jjCliYargs = (cli = true) => {
|
||||
const envsObj = {
|
||||
|
||||
let commandString = "";
|
||||
const commandList = [];
|
||||
mainCommands.forEach((command, index) => {
|
||||
console.log(`export JJ_CLI_ARG_${index + 1}="${command}"`);
|
||||
commandString += `"${command}" `;
|
||||
commandList.push(command);
|
||||
});
|
||||
|
||||
console.log(`export JJ_CLI_COMMANDS="${commandString}"`);
|
||||
console.log(`export JJ_CLI_COMMANDS_COMMA_SEPTATE="${commandList.join(",")}"`);
|
||||
console.log(`export JJ_CLI_ADDITIONAL_ARGS="${remainingArgs.reduce((str, arg) => (str += `${arg} `), "").trim()}"`);
|
||||
console.log(`export JJ_CLI_ADDITIONAL_ARGS_COMMA_SEPTATE="${remainingArgs.join(",")}"`);
|
||||
|
||||
delete argumentsObj._;
|
||||
delete argumentsObj["$0"];
|
||||
|
||||
if (!argumentsObj["env"]) {
|
||||
argumentsObj["env"] = "development";
|
||||
}
|
||||
try {
|
||||
const parsedConfig = jjConfigsExp;
|
||||
const availableEnvironments = parsedConfig?.jjYamlObj?.environments || {};
|
||||
|
||||
const allOptionsMap = {};
|
||||
Object.keys(argumentsObj).forEach((key) => {
|
||||
argumentsObj[`${key}_alias`] = argumentsObj[key];
|
||||
if (key === "env") {
|
||||
const selectedEnv = argumentsObj["env"];
|
||||
const cliArgs = yargs(hideBin(process.argv)).help(false).argv;
|
||||
const argumentsObj = { ...cliArgs };
|
||||
|
||||
const envKeys = [];
|
||||
const envAliases = [];
|
||||
Object.keys(availableEnvironments).forEach((envKey) => {
|
||||
const envConfig = availableEnvironments[envKey];
|
||||
envKeys.push(`${envKey}`);
|
||||
envAliases.push(`${envConfig?.alias}`);
|
||||
});
|
||||
const doubleDashIndex = process.argv.findIndex((arg) => arg === "--");
|
||||
const remainingArgs = doubleDashIndex === -1 ? [] : process.argv.filter((_, index) => index > doubleDashIndex);
|
||||
const mainCommands = (argumentsObj?._ || []).filter((cmd) => !remainingArgs.includes(cmd.toString()));
|
||||
|
||||
const keyIndex = envKeys.findIndex((item) => item === selectedEnv);
|
||||
const aliasIndex = envAliases.findIndex((item) => item === selectedEnv);
|
||||
let commandString = "";
|
||||
const commandList = [];
|
||||
mainCommands.forEach((command, index) => {
|
||||
envsObj[`JJ_CLI_ARG_${index + 1}`] = command
|
||||
commandString += `"${command}" `;
|
||||
commandList.push(command);
|
||||
});
|
||||
|
||||
if (keyIndex < 0 && aliasIndex < 0) {
|
||||
return;
|
||||
}
|
||||
envsObj[`JJ_CLI_COMMANDS`] = commandString
|
||||
envsObj[`JJ_CLI_COMMANDS_COMMA_SEPTATE`] = commandList.join(",")
|
||||
envsObj[`JJ_CLI_ADDITIONAL_ARGS`] = remainingArgs.reduce((str, arg) => (str += `${arg} `), "").trim()
|
||||
envsObj[`JJ_CLI_ADDITIONAL_ARGS_COMMA_SEPTATE`] = remainingArgs.join(",")
|
||||
|
||||
if (keyIndex < 0) {
|
||||
argumentsObj["env"] = envKeys[aliasIndex];
|
||||
}
|
||||
delete argumentsObj._;
|
||||
delete argumentsObj["$0"];
|
||||
|
||||
const matchedEnvIndex = keyIndex > -1 ? keyIndex : aliasIndex;
|
||||
argumentsObj[`${key}_alias`] = envAliases[matchedEnvIndex];
|
||||
if (!argumentsObj["env"]) {
|
||||
argumentsObj["env"] = "development";
|
||||
}
|
||||
console.log(`export JJ_CLI_OPTION_${key.toUpperCase().split("-").join("_")}="${argumentsObj[key]}"`);
|
||||
console.log(`export JJ_CLI_OPTION_${key.toUpperCase().split("-").join("_")}_ALIAS="${argumentsObj[`${key}_alias`]}"`);
|
||||
allOptionsMap[`${key.toUpperCase().split("-").join("_")}`] = argumentsObj[key];
|
||||
allOptionsMap[`${key.toUpperCase().split("-").join("_")}_ALIAS`] = argumentsObj[`${key}_alias`];
|
||||
});
|
||||
console.log(`export JJ_CLI_OPTIONS="${JSON.stringify(allOptionsMap)}"`);
|
||||
|
||||
process.exit(0)
|
||||
const allOptionsMap = {};
|
||||
Object.keys(argumentsObj).forEach((key) => {
|
||||
argumentsObj[`${key}_alias`] = argumentsObj[key];
|
||||
if (key === "env") {
|
||||
const selectedEnv = argumentsObj["env"];
|
||||
|
||||
} catch (exception) {
|
||||
console.error(exception);
|
||||
process.exit(1);
|
||||
const envKeys = [];
|
||||
const envAliases = [];
|
||||
Object.keys(availableEnvironments).forEach((envKey) => {
|
||||
const envConfig = availableEnvironments[envKey];
|
||||
envKeys.push(`${envKey}`);
|
||||
envAliases.push(`${envConfig?.alias}`);
|
||||
});
|
||||
|
||||
const keyIndex = envKeys.findIndex((item) => item === selectedEnv);
|
||||
const aliasIndex = envAliases.findIndex((item) => item === selectedEnv);
|
||||
|
||||
if (keyIndex < 0 && aliasIndex < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (keyIndex < 0) {
|
||||
argumentsObj["env"] = envKeys[aliasIndex];
|
||||
}
|
||||
|
||||
const matchedEnvIndex = keyIndex > -1 ? keyIndex : aliasIndex;
|
||||
argumentsObj[`${key}_alias`] = envAliases[matchedEnvIndex];
|
||||
}
|
||||
envsObj[`JJ_CLI_OPTION_${key.toUpperCase().split("-").join("_")}`] = argumentsObj[key]
|
||||
envsObj[`JJ_CLI_OPTION_${key.toUpperCase().split("-").join("_")}_ALIAS`] = argumentsObj[`${key}_alias`]
|
||||
allOptionsMap[`${key.toUpperCase().split("-").join("_")}`] = argumentsObj[key];
|
||||
allOptionsMap[`${key.toUpperCase().split("-").join("_")}_ALIAS`] = argumentsObj[`${key}_alias`];
|
||||
});
|
||||
envsObj[`JJ_CLI_OPTIONS`] = JSON.stringify(allOptionsMap)
|
||||
|
||||
if(!envsObj['JJ_CLI_OPTION_COMMIT']){
|
||||
envsObj['JJ_CLI_SELECTED_COMMIT'] = envsObj['JJ_WORKSPACE_CURRENT_COMMIT']
|
||||
} else {
|
||||
envsObj['JJ_CLI_SELECTED_COMMIT'] = envsObj['JJ_CLI_OPTION_COMMIT']
|
||||
}
|
||||
|
||||
|
||||
if (cli) {
|
||||
Object.keys(envsObj).forEach(envKey => {
|
||||
console.log(`export ${envKey}="${envsObj[envKey]}"`)
|
||||
})
|
||||
process.exit(0)
|
||||
} else {
|
||||
process.env = {
|
||||
...process.env,
|
||||
...envsObj
|
||||
}
|
||||
}
|
||||
|
||||
} catch (exception) {
|
||||
console.error(exception);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if(process.argv[process.argv.length - 1]== "cli"){
|
||||
process.argv.pop()
|
||||
jjCliYargs()
|
||||
}
|
||||
+28
-21
@@ -19,6 +19,7 @@ export const jjConfigs: any = {
|
||||
export const jjEnsure = async ({
|
||||
returnProcessEnvs = false
|
||||
}) => {
|
||||
// return
|
||||
jjConfigs.modulePath = path.resolve(__dirname, "../")
|
||||
jjConfigs.tmpDir = `${jjConfigs.modulePath}/~~tmp`
|
||||
jjConfigs.configSaveFilePath = `${jjConfigs.tmpDir}/configSaveFilePath.txt`
|
||||
@@ -39,20 +40,21 @@ export const jjEnsure = async ({
|
||||
message: '',
|
||||
},
|
||||
])
|
||||
if (!fs.existsSync(answer.jjYamlPath)) {
|
||||
jjLogError(`can not find ${answer.jjYamlPath}`)
|
||||
jjConfigs.jjYamlPath = answer.jjYamlPath
|
||||
if (!fs.existsSync(jjConfigs.jjYamlPath)) {
|
||||
jjLogError(`can not find ${jjConfigs.jjYamlPath}`)
|
||||
}
|
||||
|
||||
const jjYamlRawContent = await fs.readFile(answer.jjYamlPath, "utf8")
|
||||
const jjYamlRawContent = await fs.readFile(jjConfigs.jjYamlPath, "utf8")
|
||||
const jjYamlObj: any = YAML.load(jjYamlRawContent)
|
||||
if (_.isEmpty(jjYamlObj?.environments || {})) {
|
||||
jjLogError(`Wrong ${answer.jjYamlPath} file`)
|
||||
jjLogError(`Wrong ${jjConfigs.jjYamlPath} file`)
|
||||
}
|
||||
jjConfigs.jjWorkSpacePath = path.dirname(answer.jjYamlPath)
|
||||
jjConfigs.jjWorkSpacePath = path.dirname(jjConfigs.jjYamlPath)
|
||||
|
||||
jjConfigs.jjYamlObj = jjYamlObj
|
||||
|
||||
await fs.writeFile(jjConfigs.configSaveFilePath, answer.jjYamlPath, "utf8")
|
||||
await fs.writeFile(jjConfigs.configSaveFilePath, jjConfigs.jjYamlPath, "utf8")
|
||||
|
||||
} else {
|
||||
jjConfigs.jjYamlPath = await fs.readFile(jjConfigs.configSaveFilePath, "utf8")
|
||||
@@ -104,8 +106,8 @@ export const jjEnsure = async ({
|
||||
email: jjConfigs.processEnvs.JJ_GIT_USER_EMAIL,
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(
|
||||
jjConfigs.jjYamlPath,
|
||||
|
||||
saveFileIfItChanged(
|
||||
YAML.dump(
|
||||
jjConfigs.jjYamlObj,
|
||||
{
|
||||
@@ -115,36 +117,41 @@ export const jjEnsure = async ({
|
||||
// sortKeys: true
|
||||
}
|
||||
),
|
||||
"utf8"
|
||||
jjConfigs.jjYamlPath
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
saveFileIfItChanged(JSON.stringify(jjConfigs, null, 4), `${jjConfigs.tmpDir}/jjConfigs.json`)
|
||||
|
||||
|
||||
if (returnProcessEnvs) {
|
||||
|
||||
Object.keys(jjConfigs.processEnvs).forEach((envKey) => {
|
||||
console.log(`export ${envKey.toUpperCase().split("-").join("_")}="${jjConfigs.processEnvs[envKey]}"`)
|
||||
})
|
||||
} else {
|
||||
console.log(jjConfigs.jjYamlObj)
|
||||
let shEnvs = ''
|
||||
|
||||
Object.keys(jjConfigs.processEnvs).forEach((envKey) => {
|
||||
const command = `export ${envKey.toUpperCase().split("-").join("_")}="${jjConfigs.processEnvs[envKey]}"`
|
||||
shEnvs += `${command}\n`
|
||||
console.log(command)
|
||||
})
|
||||
|
||||
fs.writeFileSync(`${jjConfigs.tmpDir}/shEnvs.sh`, shEnvs, 'utf-8');
|
||||
}
|
||||
|
||||
|
||||
return jjConfigs
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (
|
||||
//
|
||||
_.get(process.argv, "[1]", "")?.includes('helpers/jjEnsure.ts')
|
||||
&& _.get(process.argv, "[2]", "") == 'returnProcessEnvs'
|
||||
) {
|
||||
|
||||
const jj = await jjEnsure({ returnProcessEnvs: true })
|
||||
if (_.get(process.argv, "[2]", "") == 'returnProcessEnvs') {
|
||||
const jj = await jjEnsure({ returnProcessEnvs: true })
|
||||
process.exit(0)
|
||||
}
|
||||
if (_.get(process.argv, "[2]", "") == 'init') {
|
||||
const jj = await jjEnsure({ returnProcessEnvs: false })
|
||||
process.exit(0)
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
Reference in New Issue
Block a user