78 lines
3.1 KiB
TypeScript
Executable File
78 lines
3.1 KiB
TypeScript
Executable File
#!/usr/bin/env bun
|
|
|
|
import yargs from "yargs";
|
|
import { hideBin } from "yargs/helpers";
|
|
import fs from "fs-extra";
|
|
|
|
try {
|
|
const rawConfigData = fs.readFileSync(`${process.env.JJ_SOURCE_DIR}/~~tmp/jjConfigs.json`, 'utf8');
|
|
const parsedConfig = JSON.parse(rawConfigData);
|
|
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()));
|
|
|
|
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";
|
|
}
|
|
|
|
const allOptionsMap = {};
|
|
Object.keys(argumentsObj).forEach((key) => {
|
|
argumentsObj[`${key}_alias`] = argumentsObj[key];
|
|
if (key === "env") {
|
|
const selectedEnv = argumentsObj["env"];
|
|
|
|
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];
|
|
}
|
|
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)}"`);
|
|
|
|
} catch (exception) {
|
|
console.error(exception);
|
|
process.exit(1);
|
|
}
|