Skip to content

Commit

Permalink
feat: add topics
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 17, 2018
1 parent daf5044 commit 67d1cba
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"wrap-ansi": "^3.0.1"
},
"devDependencies": {
"@oclif/config": "^1.3.55",
"@oclif/dev-cli": "^1.2.12",
"@oclif/config": "^1.3.56",
"@oclif/dev-cli": "^1.2.13",
"@oclif/errors": "^1.0.2",
"@oclif/plugin-legacy": "^1.0.2",
"@oclif/plugin-plugins": "^1.0.0",
"@oclif/plugin-plugins": "^1.0.1",
"@oclif/test": "^1.0.1",
"@oclif/tslint": "^1.0.2",
"@types/chai": "^4.1.2",
Expand Down Expand Up @@ -53,11 +53,6 @@
"oclif": {
"commands": "./lib/commands",
"bin": "oclif",
"topics": {
"fob": {
"description": "foo"
}
},
"devPlugins": [
"@oclif/plugin-plugins",
"@oclif/plugin-legacy"
Expand Down
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class Help {
return arg
}
}
let topics = this.config.topics
let commands = this.config.commands
commands = commands.filter(c => this.opts.all || !c.hidden)
commands = sortBy(commands, c => c.id)
Expand All @@ -51,15 +52,19 @@ export default class Help {
let command: Config.Command | undefined
let topic: Config.Topic | undefined
if (!subject) {
if (!this.opts.all) commands = commands.filter(c => !c.id.includes(':'))
console.log(this.root())
console.log()
if (commands.length) {
console.log(this.commands(commands))
console.log()
if (!this.opts.all) {
commands = commands.filter(c => !c.id.includes(':'))
topics = topics.filter(t => !t.name.includes(':'))
}
console.log(this.commands([...topics, ...commands]))
console.log()
} else if (command = this.config.findCommand(subject)) {
commands = commands.filter(c => c.id !== command!.id && c.id.startsWith(command!.id))
const id = command.id
const depth = id.split(':').length
commands = commands.filter(c => c.id.startsWith(id) && c.id.split(':').length === depth + 1)
topics = topics.filter(t => t.name.startsWith(id) && t.name.split(':').length === depth + 1)
let title = command.description && this.render(command.description).split('\n')[0]
if (title) console.log(title + '\n')
console.log(this.command(command))
Expand Down Expand Up @@ -109,10 +114,11 @@ export default class Help {
return help.command(command)
}

commands(commands: Config.Command[]): string | undefined {
commands(commands: (Config.Command | Config.Topic)[]): string | undefined {
if (!commands.length) return
commands = uniqBy(commands, c => id(c))
let body = renderList(commands.map(c => [
c.id,
id(c),
c.description && this.render(c.description.split('\n')[0])
]), {stripAnsi: this.opts.stripAnsi, maxWidth: this.opts.maxWidth - 2})
return [
Expand All @@ -121,3 +127,7 @@ export default class Help {
].join('\n')
}
}

function id(c: Config.Command | Config.Topic): string {
return (c as any).id || (c as any).name
}
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Template = require('lodash.template')
export function uniqBy<T>(arr: T[], fn: (cur: T) => any): T[] {
return arr.filter((a, i) => {
let aVal = fn(a)
return !arr.find((b, j) => j !== i && fn(b) === aVal)
return !arr.find((b, j) => j > i && fn(b) === aVal)
})
}

Expand Down
26 changes: 15 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,24 @@
call-me-maybe "^1.0.1"
glob-to-regexp "^0.3.0"

"@oclif/command@^1.2.21", "@oclif/command@^1.3.0", "@oclif/command@^1.3.1":
"@oclif/command@^1.3.0", "@oclif/command@^1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@oclif/command/-/command-1.3.1.tgz#b2e063034f2fd39d89729bb2fe8f9cb070c4b4e1"
dependencies:
"@oclif/parser" "^3.2.9"
semver "^5.5.0"

"@oclif/config@^1.3.52", "@oclif/config@^1.3.55":
"@oclif/config@^1.3.52":
version "1.3.55"
resolved "https://registry.yarnpkg.com/@oclif/config/-/config-1.3.55.tgz#f7417eb5c0eaea7c738c4fa65361b7c12935b615"

"@oclif/dev-cli@^1.2.12":
version "1.2.12"
resolved "https://registry.yarnpkg.com/@oclif/dev-cli/-/dev-cli-1.2.12.tgz#20accc2784b1ea0fad842b0639cdf6b260c59148"
"@oclif/config@^1.3.56":
version "1.3.56"
resolved "https://registry.yarnpkg.com/@oclif/config/-/config-1.3.56.tgz#daa92087dda5fd649d4aeea9cc44aef92b854e7f"

"@oclif/dev-cli@^1.2.13":
version "1.2.13"
resolved "https://registry.yarnpkg.com/@oclif/dev-cli/-/dev-cli-1.2.13.tgz#769d651e2ea2fb7df646d4a376402a5521d35221"
dependencies:
"@oclif/command" "^1.3.0"
"@oclif/config" "^1.3.52"
Expand Down Expand Up @@ -95,14 +99,14 @@
debug "^3.1.0"
semver "^5.5.0"

"@oclif/plugin-plugins@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@oclif/plugin-plugins/-/plugin-plugins-1.0.0.tgz#2ae58b5bc3b9ada8138c298c8e7299fbba62666e"
"@oclif/plugin-plugins@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@oclif/plugin-plugins/-/plugin-plugins-1.0.1.tgz#9e9478fea9f153162b0530f62bb552703cbcbfb5"
dependencies:
"@heroku-cli/color" "^1.1.3"
"@oclif/command" "^1.2.21"
"@oclif/command" "^1.3.1"
chalk "^2.3.1"
cli-ux "^3.3.19"
cli-ux "^3.3.23"
debug "^3.1.0"
fs-extra "^5.0.0"
http-call "^5.0.2"
Expand Down Expand Up @@ -388,7 +392,7 @@ clean-stack@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-1.3.0.tgz#9e821501ae979986c46b1d66d2d432db2fd4ae31"

cli-ux@^3.3.18, cli-ux@^3.3.19:
cli-ux@^3.3.18, cli-ux@^3.3.23:
version "3.3.23"
resolved "https://registry.yarnpkg.com/cli-ux/-/cli-ux-3.3.23.tgz#5b9462aabb27acd91bb7abb6f91518b085167d99"
dependencies:
Expand Down

0 comments on commit 67d1cba

Please sign in to comment.