Skip to content
Snippets Groups Projects
Verified Commit 042edc8d authored by orestis.malaspin's avatar orestis.malaspin
Browse files

convert to string instead of console

parent 4a879744
No related branches found
No related tags found
1 merge request!8Draft: Resolve "Add bash completion helper function"
Pipeline #29348 passed
......@@ -44,7 +44,7 @@ class CommanderApp {
this.registerCommands();
getBashCompletion(this.program)
getBashCompletion(this.program, "bash_completion.sh")
this.program.parse();
}
......
import { Command } from 'commander';
import { writeFileSync } from 'fs';
interface Node {
readonly name: string;
......@@ -19,20 +19,6 @@ function isLeaf(tree: Node): boolean {
return tree.children === undefined || tree.children.length == 0
}
function getOptions(cmd: Command): Array<Node> {
return cmd.options.filter(o => !o.hidden).map(o =>
createNode(o.flags.replace(/<.*?>/, '').replace(/\[.*?\]/, '').replace(',', '').trimEnd(), [], cmd.name())
)
}
function getSubNode(cmd: Command, parent: Command): Node {
if (cmd.commands.length > 0) {
return createNode(cmd.name(), cmd.commands.map((sc) => getSubNode(sc, cmd)).concat(getOptions(cmd)), parent.name())
} else {
return createNode(cmd.name(), getOptions(cmd), parent.name())
}
}
function search(tree: Node, nodes: Array<Node>, cmdName: string): Array<Node> {
if (isLeaf(tree)) {
if (tree.name != cmdName) {
......@@ -59,25 +45,39 @@ function flatten(tree: Node): Node[] {
}
}
function addWords(cmd: Node, prefix: string): string {
return prefix + " words=\"" + cmd.children.map(c => c.name).join(" ") + " --help -h" + "\""
return prefix + " words=\"" + cmd.children.map(c => c.name).join(" ") + " --help -h" + "\"\n"
}
function printWordsGrandParents(cmd: Node) {
function printWordsGrandParents(node: Node): string {
let parentWords = ""
let prefix = ""
if (cmd.parent !== undefined) {
console.log(" case \"${grand_parent}\" in")
console.log(" " + cmd.parent + ")")
if (node.parent !== undefined) {
parentWords += " case \"${grand_parent}\" in\n"
parentWords += " " + node.parent + ")\n"
prefix = " "
}
const words = addWords(cmd, prefix)
console.log(words)
if (cmd.parent !== undefined) {
console.log(" ;;")
console.log(" *)")
console.log(" ;;")
console.log(" esac")
parentWords += addWords(node, prefix)
if (node.parent !== undefined) {
parentWords += " ;;\n"
parentWords += " *)\n"
parentWords += " ;;\n"
parentWords += " esac\n"
}
return parentWords
}
function getOptions(cmd: Command): Array<Node> {
return cmd.options.filter(o => !o.hidden).map(o =>
createNode(o.flags.replace(/<.*?>/, '').replace(/\[.*?\]/, '').replace(',', '').trimEnd(), [], cmd.name())
)
}
function getSubNode(cmd: Command, parent: Command): Node {
if (cmd.commands.length > 0) {
return createNode(cmd.name(), cmd.commands.map((sc) => getSubNode(sc, cmd)).concat(getOptions(cmd)), parent.name())
} else {
return createNode(cmd.name(), getOptions(cmd), parent.name())
}
}
......@@ -85,34 +85,34 @@ function buildCommandNode(root: Command): Node {
return createNode(root.name(), root.commands.map((sc) => getSubNode(sc, root)).concat(getOptions(root)))
}
export function getBashCompletion(root: Command) {
export function getBashCompletion(root: Command, filename: string) {
const tree = buildCommandNode(root)
console.log("#/usr/bin/env bash")
console.log("function _dojo_completions()")
console.log("{")
console.log(" latest=\"${COMP_WORDS[$COMP_CWORD]}\"")
console.log(" parent=\"${COMP_WORDS[$COMP_CWORD - 1]}\"")
console.log(" grand_parent=\"${COMP_WORDS[$COMP_CWORD - 2]}\"")
console.log(" words=\"\"")
console.log(" case \"${parent}\" in")
let data = "#/usr/bin/env bash\nfunction _dojo_completions()\n";
data += "{\n"
data += " latest=\"${COMP_WORDS[$COMP_CWORD]}\"\n"
data += " parent=\"${COMP_WORDS[$COMP_CWORD - 1]}\"\n"
data += " grand_parent=\"${COMP_WORDS[$COMP_CWORD - 2]}\"\n"
data += " words=\"\"\n"
data += " case \"${parent}\" in\n"
const commands = Array.from(new Set(flatten(tree).filter(t => !isLeaf(t)).map(n => n.name))).map(s => search(tree, [], s))
for (const node of commands) {
const cmd = node[0]
console.log(" " + cmd.name + ")")
data += " " + cmd.name + ")\n"
if (cmd !== undefined && node.length == 1) {
const words = addWords(cmd, "")
console.log(words)
data += addWords(cmd, "")
} else if (cmd !== undefined && node.length > 1) {
node.forEach(n => printWordsGrandParents(n))
node.forEach(n => data += printWordsGrandParents(n))
}
console.log(" ;;")
data += " ;;\n"
}
console.log(" *)")
console.log(" ;;")
console.log(" esac")
console.log(" COMPREPLY=($(compgen -W \"$words\" -- $latest))")
console.log(" return 0")
console.log("}")
console.log("complete -F _dojo_completions dojo")
data += " *)\n"
data += " ;;\n"
data += " esac\n"
data += " COMPREPLY=($(compgen -W \"$words\" -- $latest))\n"
data += " return 0\n"
data += "}\n"
data += "complete -F _dojo_completions dojo\n"
writeFileSync(filename, data);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment