Skip to content

Commit

Permalink
feat: add a tree view for commands (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodEsp committed Jun 17, 2022
1 parent 86dd58b commit 0ea4028
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Suspended",
"type": "node",
"request": "attach",
"port": 9229
},
]
}
14 changes: 13 additions & 1 deletion src/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {CliUx, Command, Flags, toConfiguredId} from '@oclif/core'
import * as _ from 'lodash'
import {EOL} from 'os'
import createCommandTree from '../utils/tree'

type Dictionary = {[index: string]: object}
export default class Commands extends Command {
Expand All @@ -11,6 +12,7 @@ export default class Commands extends Command {
static flags: any = {
help: Flags.help({char: 'h'}),
hidden: Flags.boolean({description: 'show hidden commands'}),
tree: Flags.boolean({description: 'show tree of commands'}),
...CliUx.ux.table.flags(),
}

Expand All @@ -31,7 +33,7 @@ export default class Commands extends Command {
return command
})

if (this.jsonEnabled()) {
if (this.jsonEnabled() && !flags.tree) {
const formatted = await Promise.all(commands.map(async cmd => {
let commandClass = await cmd.load()
const obj = {...cmd, ...commandClass}
Expand All @@ -51,6 +53,16 @@ export default class Commands extends Command {
return formatted
}

if (flags.tree) {
const tree = createCommandTree(commands, this.config.topicSeparator)

if (!this.jsonEnabled()) {
tree.display()
}

return tree
}

CliUx.ux.table(commands.map(command => {
// Massage some fields so it looks good in the table
command.description = (command.description || '').split(EOL)[0]
Expand Down
32 changes: 32 additions & 0 deletions src/utils/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {CliUx, Interfaces} from '@oclif/core'
import {Tree} from '@oclif/core/lib/cli-ux/styled/tree'

const addNodes = (tree: Tree, commandParts: string[]) => {
const existingNode = tree.search(commandParts[0])

// If the node exists and there's another part, add it to the node
if (existingNode && commandParts[1]) {
addNodes(existingNode as Tree, commandParts.slice(1))
} else {
// The node doesn't exist, create it
tree.insert(commandParts[0])

// If there are more parts, add them to the node
if (commandParts.length > 1) {
addNodes(tree.search(commandParts[0]) as Tree, commandParts.slice(1))
}
}
}

const createCommandTree = (commands: Interfaces.Command[], topicSeparator = ':') => {
const tree = CliUx.ux.tree()

commands.forEach(command => {
const commandParts = command.id.split(topicSeparator)
addNodes(tree, commandParts)
})

return tree
}

export default createCommandTree

0 comments on commit 0ea4028

Please sign in to comment.