diff --git a/NodeApp/src/commander/CommanderApp.ts b/NodeApp/src/commander/CommanderApp.ts index 1d7ddae2f31d7254eec95071958f69a4f6c3afa9..17d435aa912cca9aacb35893db6252ac97ad2a8b 100644 --- a/NodeApp/src/commander/CommanderApp.ts +++ b/NodeApp/src/commander/CommanderApp.ts @@ -9,7 +9,7 @@ import { stateConfigFile } from '../config/ConfigFiles'; import semver from 'semver/preload'; import { version } from '../config/Version'; import Config from '../config/Config'; -import AutoCompletionHelper from '../helpers/AutoCompletionHelper'; +import {getBashCompletion} from '../helpers/AutoCompletionHelper'; class CommanderApp { @@ -44,7 +44,7 @@ class CommanderApp { this.registerCommands(); - AutoCompletionHelper.getBashCompletion(this.program) + getBashCompletion(this.program) // AutoCompletionHelper.getCommands(this.program) this.program.parse(); diff --git a/NodeApp/src/helpers/AutoCompletionHelper.ts b/NodeApp/src/helpers/AutoCompletionHelper.ts index 9856c2169bcd2599414e4a00624d35e45d99bc8f..740e80f9a45fdc4be700bb8937fa0760f4e81bf8 100644 --- a/NodeApp/src/helpers/AutoCompletionHelper.ts +++ b/NodeApp/src/helpers/AutoCompletionHelper.ts @@ -1,5 +1,6 @@ import { Command } from 'commander'; + interface Node { readonly name: string; readonly children: Array<Node>; @@ -59,60 +60,54 @@ function flatten(tree: Node): Node[] { } -class AutoCompletionHelper { - private printWordsGrandParents(cmd: Node) { - let prefix = "" - if (cmd.parent !== undefined) { - console.log(" case \"${grand_parent}\" in") - console.log(" " + cmd.parent + ")") - prefix = " " - } - const words = prefix + " words=\"" + cmd.children.map(c => c.name).join(" ") + "\"" - console.log(words) - if (cmd.parent !== undefined) { - console.log(" ;;") - console.log(" *)") - console.log(" ;;") - console.log(" esac") - } +function printWordsGrandParents(cmd: Node) { + let prefix = "" + if (cmd.parent !== undefined) { + console.log(" case \"${grand_parent}\" in") + console.log(" " + cmd.parent + ")") + prefix = " " } - - private buildCommandNode(root: Command): Node { - return createNode(root.name(), root.commands.map((sc) => getSubNode(sc, root))) + const words = prefix + " words=\"" + cmd.children.map(c => c.name).join(" ") + "\"" + console.log(words) + if (cmd.parent !== undefined) { + console.log(" ;;") + console.log(" *)") + console.log(" ;;") + console.log(" esac") } +} - getBashCompletion(root: Command) { - const tree = this.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") - 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 + ")") - if (cmd != undefined && node.length == 1) { - const words = " words=\"" + cmd.children.map(c => c.name).join(" ") + "\"" - console.log(words) - } else if (cmd != undefined && node.length > 1) { - node.forEach(n => this.printWordsGrandParents(n)) - } - console.log(" ;;") +function buildCommandNode(root: Command): Node { + return createNode(root.name(), root.commands.map((sc) => getSubNode(sc, root))) +} + +export function getBashCompletion(root: Command) { + 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") + 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 + ")") + if (cmd != undefined && node.length == 1) { + const words = " words=\"" + cmd.children.map(c => c.name).join(" ") + "\"" + console.log(words) + } else if (cmd != undefined && node.length > 1) { + node.forEach(n => printWordsGrandParents(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") } - - + 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") } - -export default new AutoCompletionHelper(); \ No newline at end of file