118 lines
3.8 KiB
TypeScript
Executable File
118 lines
3.8 KiB
TypeScript
Executable File
#!/usr/bin/env bun
|
|
|
|
import { bashRunAndReturnSync } from "@jjHelpers/bashUtils";
|
|
import { dynamicStringHandler } from "@jjHelpers/dynamicStringHandler";
|
|
import { getJJConfigByKeyChain } from "@jjHelpers/jjConfigUtils";
|
|
|
|
async function main() {
|
|
const inputArgs = [...process.argv];
|
|
inputArgs.shift();
|
|
inputArgs.shift();
|
|
const displayHelp = inputArgs[0];
|
|
inputArgs.shift();
|
|
inputArgs.shift();
|
|
|
|
let remainingArgsCount = inputArgs.length;
|
|
const commandConfig = getJJConfigByKeyChain(`commands`);
|
|
const jjCommandsList = Object.keys(commandConfig).map((cmdKey) => ({
|
|
key: cmdKey,
|
|
...commandConfig[cmdKey],
|
|
}));
|
|
let lastArrayOfKeys = [...jjCommandsList];
|
|
|
|
let commands = "";
|
|
let preHelp = ``;
|
|
|
|
while (remainingArgsCount > 0) {
|
|
const matchingCommand = lastArrayOfKeys.find((item) => {
|
|
return item?.key == inputArgs[0];
|
|
});
|
|
|
|
if (matchingCommand?.commandForRetrieveKeys) {
|
|
if (remainingArgsCount == 2 && matchingCommand?.commandForRetrieveKeys2) {
|
|
matchingCommand.commandForRetrieveKeys = matchingCommand?.commandForRetrieveKeys2;
|
|
}
|
|
const oldArgOrder = remainingArgsCount;
|
|
remainingArgsCount = 0;
|
|
let command =
|
|
matchingCommand?.commandForRetrieveKeys +
|
|
` ${matchingCommand?.commandForRetrieveKeys?.includes("auto-completion") ? ` auto-completion ${process.argv.join(" ")}` : ""}`;
|
|
command = dynamicStringHandler(command, "", process.env, false);
|
|
// console.log(command);
|
|
const res = await bashRunAndReturnSync({
|
|
command,
|
|
});
|
|
// console.log(res)
|
|
const newCommands = res?.split("\n") || [];
|
|
newCommands.push(String(""));
|
|
lastArrayOfKeys = [];
|
|
|
|
newCommands.forEach((element) => {
|
|
if (element.includes(".") && !element.includes(".com")) {
|
|
return;
|
|
}
|
|
lastArrayOfKeys.push({
|
|
key: element,
|
|
desc: "",
|
|
keys: [],
|
|
});
|
|
});
|
|
}
|
|
|
|
if (matchingCommand?.keysOnly && matchingCommand?.keysOnly?.length > 0) {
|
|
remainingArgsCount = 0;
|
|
lastArrayOfKeys = [];
|
|
|
|
matchingCommand?.keysOnly.forEach((element) => {
|
|
lastArrayOfKeys.push({
|
|
key: element,
|
|
desc: "",
|
|
keys: [],
|
|
});
|
|
});
|
|
}
|
|
|
|
if (matchingCommand?.postCommandsForce && matchingCommand?.postCommandsForce?.length > 0) {
|
|
remainingArgsCount = 0;
|
|
|
|
matchingCommand?.postCommandsForce.forEach((element) => {
|
|
lastArrayOfKeys.push({
|
|
key: element,
|
|
desc: "",
|
|
keys: [],
|
|
});
|
|
});
|
|
}
|
|
|
|
if (matchingCommand?.key && matchingCommand?.keys && remainingArgsCount > 0) {
|
|
preHelp += `${matchingCommand?.desc || ""} `;
|
|
lastArrayOfKeys = matchingCommand?.keys;
|
|
inputArgs.shift();
|
|
remainingArgsCount -= 1;
|
|
} else {
|
|
// lastArrayOfKeys = [];
|
|
remainingArgsCount = 0;
|
|
}
|
|
}
|
|
|
|
if (remainingArgsCount == 0) {
|
|
for (const item of lastArrayOfKeys) {
|
|
if (displayHelp == "1") {
|
|
commands += `'${item?.key}:${preHelp}${item?.desc}' `;
|
|
} else {
|
|
commands += `${item?.key} `;
|
|
}
|
|
if (item?.commandForRetrieveKeys) {
|
|
commands += `-- `;
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`(${commands})`);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error("FATAL ERROR:", err);
|
|
process.exit(1);
|
|
});
|