Skip to content

Commit

Permalink
feat: added topics to help
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 6, 2018
1 parent 7daf127 commit 1d8133b
Show file tree
Hide file tree
Showing 6 changed files with 805 additions and 40 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
"anycli": {
"commands": "./lib/commands",
"bin": "anycli",
"topics": {
"fob": {
"description": "foo"
}
},
"devPlugins": [
"@anycli/plugin-plugins"
"@anycli/plugin-plugins",
"@anycli/plugin-legacy",
"@heroku-cli/plugin-apps"
]
},
"bugs": "https://github.com/anycli/plugin-help/issues",
Expand All @@ -24,9 +31,11 @@
"@anycli/config": "^1.3.11",
"@anycli/dev-cli": "^0.2.2",
"@anycli/errors": "^0.2.1",
"@anycli/plugin-legacy": "^0.0.2",
"@anycli/plugin-plugins": "^0.2.13",
"@anycli/test": "^0.10.11",
"@anycli/tslint": "^0.2.6",
"@heroku-cli/plugin-apps": "^2.4.23",
"@types/chai": "^4.1.2",
"@types/indent-string": "^3.0.0",
"@types/lodash.template": "^4.4.3",
Expand Down
23 changes: 22 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Config from '@anycli/config'
import chalk from 'chalk'

import {Article, HelpOptions, Section} from '.'
import {castArray, compact, sortBy} from './util'
import {castArray, compact, sortBy, uniqBy} from './util'

const {
underline,
Expand Down Expand Up @@ -30,6 +30,7 @@ export default class CommandHelp {
this.flags(flags),
this.description(cmd),
this.aliases(cmd.aliases),
this.subcommands(cmd),
]),
}
}
Expand Down Expand Up @@ -119,4 +120,24 @@ export default class CommandHelp {

return [left, dim(right.trim())]
}

protected subcommands(command: Config.Command) {
let commands = this.config.commands
commands = commands.filter(c => this.opts.all || !c.hidden)
commands = commands.filter(c => c.id !== command.id && c.id.startsWith(command.id))
commands = sortBy(commands, c => c.id)
commands = uniqBy(commands, c => c.id)
if (!commands.length) return
if (this.opts.format === 'markdown') {
return {
heading: 'commands',
body: commands.map(c => compact([`* [${c.id}](#${c.id})`, c.title]).join(' - '))
}
} else {
return {
heading: 'commands',
body: commands.map(c => [c.id, c.title]),
}
}
}
}
5 changes: 3 additions & 2 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default class HelpCommand extends Command {
static title = 'display help for <%= config.bin %>'
static flags = {
all: flags.boolean({description: 'see all commands in CLI'}),
format: flags.enum({description: 'output in a different format', options: ['markdown', 'man']}),
format: flags.enum({description: 'output in a different format', options: ['markdown', 'screen']}),
// format: flags.enum({description: 'output in a different format', options: ['markdown', 'man']}),
}
static args = [
{name: 'command', required: false, description: 'command to show help for'}
Expand All @@ -16,7 +17,7 @@ export default class HelpCommand extends Command {
async run() {
const {flags, argv} = this.parse(HelpCommand)
const format = flags.format as any || 'screen'
let help = new Help(this.config, {format})
let help = new Help(this.config, {format, all: flags.all})
help.showHelp(argv)
}
}
41 changes: 32 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Config from '@anycli/config'
import {error} from '@anycli/errors'
import chalk from 'chalk'
import indent = require('indent-string')
import template = require('lodash.template')
Expand All @@ -7,7 +8,7 @@ import stripAnsi = require('strip-ansi')
import CommandHelp from './command'
import RootHelp from './root'
import {stdtermwidth} from './screen'
import {castArray, compact} from './util'
import {castArray, compact, sortBy, uniqBy} from './util'

const width = require('string-width')
const wrap = require('wrap-ansi')
Expand Down Expand Up @@ -60,26 +61,49 @@ export default class Help {
return arg
}
}
const subject = getHelpSubject()
let subject = getHelpSubject()
let command
let topic
if (!subject) {
console.log(this.root())
} else {
// TODO: topic help
let command = this.config.findCommand(subject, {must: true})
let commands = this.config.commands
commands = commands.filter(c => this.opts.all || !c.hidden)
if (!this.opts.all) commands = commands.filter(c => !c.id.includes(':'))
commands = sortBy(commands, c => c.id)
commands = uniqBy(commands, c => c.id)
console.log(this.root(commands))
console.log()
if (this.opts.format === 'markdown') {
for (let command of commands) {
console.log(this.command(command))
console.log()
}
}
} else if (command = this.config.findCommand(subject)) {
console.log(this.command(command))
} else if (topic = this.config.findTopic(subject)) {
console.log(this.topic(topic))
} else {
error(`command ${subject} not found`)
}
if (this.opts.format === 'screen') console.log()
}

root(): string {
root(commands: Config.Command[]): string {
const help = new RootHelp(this.config, this.opts)
const article = help.root()
const article = help.root(commands)
return this.render(article)
}

topic(topic: Config.Topic): string {
return topic.name
}

command(command: Config.Command): string {
const help = new CommandHelp(this.config, this.opts)
const article = help.command(command)
console.log(command.id)
console.log('-'.repeat(width(command.id)))
console.log()
return this.render(article)
}

Expand All @@ -96,7 +120,6 @@ export default class Help {
const maxWidth = 100
return [
stripAnsi(this.renderTemplate(article.title)),
'-'.repeat(width(article.title)),
'',
...article.sections
.map(s => {
Expand Down
31 changes: 17 additions & 14 deletions src/root.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {IConfig} from '@anycli/config'
import * as Config from '@anycli/config'
// import chalk from 'chalk'

import {Article, HelpOptions, Section} from '.'
import {compact, sortBy, uniqBy} from './util'
import {compact} from './util'

// const {
// underline,
Expand All @@ -11,15 +11,15 @@ import {compact, sortBy, uniqBy} from './util'
// } = chalk

export default class RootHelp {
constructor(public config: IConfig, public opts: HelpOptions = {}) {}
constructor(public config: Config.IConfig, public opts: HelpOptions = {}) {}

root(): Article {
root(commands: Config.Command[]): Article {
return {
title: this.config.pjson.anycli.title || this.config.pjson.description,
sections: compact([
this.usage(),
this.description(),
this.commands(),
this.commands(commands),
])
}
}
Expand All @@ -39,15 +39,18 @@ export default class RootHelp {
}
}

protected commands(): Section | undefined {
let commands = this.config.commands
commands = commands.filter(c => this.opts.all || !c.hidden)
commands = sortBy(commands, c => c.id)
commands = uniqBy(commands, c => c.id)

return {
heading: 'commands',
body: commands.map(c => [c.id, c.title]),
protected commands(commands: Config.Command[]): Section | undefined {
if (commands.length === 0) return
if (this.opts.format === 'markdown') {
return {
heading: 'commands',
body: commands.map(c => compact([`* [${c.id}](#${c.id})`, c.title]).join(' - '))
}
} else {
return {
heading: 'commands',
body: commands.map(c => [c.id, c.title]),
}
}
}
}
Loading

0 comments on commit 1d8133b

Please sign in to comment.