WIP
This commit is contained in:
Executable
+110
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { bashRunAndReturnSync } from "@helpers/bashUtils"
|
||||
import { getJJConfigByKeyChain } from "@helpers/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
|
||||
const command =
|
||||
matchingCommand?.commandForRetrieveKeys + ` ${matchingCommand?.commandForRetrieveKeys?.includes("auto-completion") ? ` auto-completion ${process.argv.join(" ")}` : ""}`
|
||||
const res = await bashRunAndReturnSync(command)
|
||||
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)
|
||||
})
|
||||
Reference in New Issue
Block a user