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

added tree

parent c1d910ad
No related branches found
No related tags found
1 merge request!8Draft: Resolve "Add bash completion helper function"
Pipeline #29315 failed
import { Options } from 'boxen';
import { Command } from 'commander';
import { writeFileSync } from 'fs';
// #/usr/bin/env bash
......@@ -39,15 +39,17 @@ import { writeFileSync } from 'fs';
// }
// complete -F _dojo_completions dojo
// class NodeTree {
// parent: NodeTree | null;
// name: string;
class Tree {
name: string;
parent?: string;
children: Array<Tree>;
// constructor(parent: NodeTree | null, name: string) {
// this.parent = parent;
// this.name = name;
// }
// }
constructor(name: string, children: Array<Tree>, parent?: string) {
this.name = name;
this.children = children;
this.parent = parent;
}
}
class AutoCompletionHelper {
......@@ -104,6 +106,66 @@ class AutoCompletionHelper {
console.log("complete -F _dojo_completions dojo")
}
private isLeaf(tree: Tree): boolean {
return tree.children === undefined || tree.children.length == 0
}
private getOptions(cmd: Command): Array<Tree> {
return cmd.options.map(o => new Tree("--" + o.name(), [], cmd.name()))
}
private getSubTree(cmd: Command, parent: Command): Tree {
// let words = []
if (cmd.commands.length > 0) {
return new Tree(cmd.name(), cmd.commands.map((sc) => this.getSubTree(sc, cmd)).concat(this.getOptions(cmd)), parent.name())
} else {
return new Tree(cmd.name(), this.getOptions(cmd), parent.name())
}
}
private printTree(tree: Tree, i: number) {
if (!this.isLeaf(tree)) {
tree.children.map((c) => this.printTree(c, i + 4))
console.log(" ".repeat(i) + tree.name)
} else {
console.log(" ".repeat(i) + tree.name)
}
}
private search(tree: Tree, nodes: Array<Tree>, cmdName: string): Array<Tree> {
if (this.isLeaf(tree)) {
if (tree.name != cmdName) {
return nodes
} else {
return nodes.concat(tree)
}
} else if (tree.name == cmdName) {
return tree.children.flatMap(st => this.search(st, nodes, cmdName)).concat(tree)
} else {
return tree.children.flatMap(st => this.search(st, nodes, cmdName))
}
}
flatten(tree: Tree): string[] {
if (this.isLeaf(tree)) {
return [tree.name]
} else {
return tree.children
.map(c =>
this.flatten(c)
)
.reduce((acc, n) => acc.concat(n), [tree.name])
}
}
buildTree(root: Command): Tree {
let tree = new Tree(root.name(), root.commands.map((sc) => this.getSubTree(sc, root)))
this.printTree(tree, 0)
console.log(this.flatten(tree))
console.log(this.search(tree, [], "login"))
return tree
}
}
export default new AutoCompletionHelper();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment