Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a tree view for commands #292

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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