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: allow templates in usage and provide command context #35

Merged
merged 1 commit into from
Jan 30, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const wrap = require('wrap-ansi')

export default class CommandHelp {
render: (input: string) => string
constructor(public config: Config.IConfig, public opts: HelpOptions) {
constructor(public command: Config.Command, public config: Config.IConfig, public opts: HelpOptions) {
this.render = template(this)
}

command(cmd: Config.Command): string {
generate(): string {
const cmd = this.command
const flags = sortBy(Object.entries(cmd.flags || {})
.filter(([, v]) => !v.hidden)
.map(([k, v]) => {
Expand All @@ -34,36 +35,38 @@ export default class CommandHelp {
}), f => [!f.char, f.char, f.name])
const args = (cmd.args || []).filter(a => !a.hidden)
let output = compact([
this.usage(cmd, flags),
this.usage(flags),
this.args(args),
this.flags(flags),
this.description(cmd),
this.description(),
this.aliases(cmd.aliases),
this.examples(cmd.examples || (cmd as any).example),
]).join('\n\n')
if (this.opts.stripAnsi) output = stripAnsi(output)
return output
}

protected usage(cmd: Config.Command, flags: Config.Command.Flag[]): string {
let body = (cmd.usage ? castArray(cmd.usage) : [this.defaultUsage(cmd, flags)])
protected usage(flags: Config.Command.Flag[]): string {
const usage = this.command.usage
let body = (usage ? castArray(usage) : [this.defaultUsage(flags)])
.map(u => `$ ${this.config.bin} ${u}`.trim())
.join('\n')
return [
bold('USAGE'),
indent(wrap(body, this.opts.maxWidth - 2, {trim: false, hard: true}), 2),
indent(wrap(this.render(body), this.opts.maxWidth - 2, {trim: false, hard: true}), 2),
].join('\n')
}

protected defaultUsage(command: Config.Command, _: Config.Command.Flag[]): string {
protected defaultUsage(_: Config.Command.Flag[]): string {
return compact([
command.id,
command.args.filter(a => !a.hidden).map(a => this.arg(a)).join(' '),
this.command.id,
this.command.args.filter(a => !a.hidden).map(a => this.arg(a)).join(' '),
// flags.length && '[OPTIONS]',
]).join(' ')
}

protected description(cmd: Config.Command): string | undefined {
protected description(): string | undefined {
const cmd = this.command
let description = cmd.description && this.render(cmd.description).split('\n').slice(1).join('\n')
if (!description) return
return [
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export default class Help {
}

command(command: Config.Command): string {
const help = new CommandHelp(this.config, this.opts)
return help.command(command)
const help = new CommandHelp(command, this.config, this.opts)
return help.generate()
}

topics(topics: Config.Topic[]): string | undefined {
Expand Down
17 changes: 17 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ ARGUMENTS
OPTIONS
--[no-]opt`))

test
.commandHelp(class extends Command {
static id = 'apps:create'
static usage = '<%= config.bin %> <%= command.id %> usage'
})
.it('outputs usage with templates', ctx => expect(ctx.commandHelp).to.equal(`USAGE
$ oclif oclif apps:create usage`))

test
.commandHelp(class extends Command {
static id = 'apps:create'
static usage = ['<%= config.bin %>', '<%= command.id %> usage']
})
.it('outputs usage arrays with templates', ctx => expect(ctx.commandHelp).to.equal(`USAGE
$ oclif oclif
$ oclif apps:create usage`))

// class AppsCreate3 extends Command {
// static id = 'apps:create'
// static flags = {
Expand Down