WIP
This commit is contained in:
+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()
|
||||
}
|
||||
Reference in New Issue
Block a user