Skip to content

Commit

Permalink
fix: fixed topic list
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 6, 2018
1 parent 9651a8c commit b42fde4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"wrap-ansi": "^3.0.1"
},
"devDependencies": {
"@anycli/config": "^1.3.15",
"@anycli/config": "^1.3.16",
"@anycli/dev-cli": "^0.2.2",
"@anycli/errors": "^0.2.1",
"@anycli/plugin-legacy": "^0.0.3",
Expand Down
7 changes: 5 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Config from '@anycli/config'
import chalk from 'chalk'
import indent = require('indent-string')
import stripAnsi = require('strip-ansi')

import {HelpOptions} from '.'
import {renderList} from './list'
Expand Down Expand Up @@ -28,14 +29,16 @@ export default class CommandHelp {
return v
}), f => [!f.char, f.char, f.name])
const args = (cmd.args || []).filter(a => !a.hidden)
return compact([
let output = compact([
cmd.description && this.render(cmd.description).split('\n')[0],
this.usage(cmd, flags),
this.args(args),
this.flags(flags),
this.description(cmd),
this.aliases(cmd.aliases),
]).join('\n\n')
if (this.opts.stripAnsi) output = stripAnsi(output)
return output
}

protected usage(cmd: Config.Command, flags: Config.Command.Flag[]): string {
Expand Down Expand Up @@ -75,7 +78,7 @@ export default class CommandHelp {
}

protected args(args: Config.Command['args']): string | undefined {
if (!args.length) return
if (!args.filter(a => a.description).length) return
let body = renderList(args.map(a => {
const name = a.name.toUpperCase()
let description = a.description || ''
Expand Down
25 changes: 21 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import * as Config from '@anycli/config'
import {error} from '@anycli/errors'
import chalk from 'chalk'
import indent = require('indent-string')
import stripAnsi = require('strip-ansi')

import CommandHelp from './command'
import {renderList} from './list'
import RootHelp from './root'
import {stdtermwidth} from './screen'
import {sortBy, template, uniqBy} from './util'
import {compact, sortBy, template, uniqBy} from './util'

const wrap = require('wrap-ansi')
const {
bold,
} = chalk
Expand Down Expand Up @@ -47,7 +49,7 @@ export default class Help {
commands = uniqBy(commands, c => c.id)
let subject = getHelpSubject()
let command: Config.Command | undefined
let topic
let topic: Config.Topic | undefined
if (!subject) {
if (!this.opts.all) commands = commands.filter(c => !c.id.includes(':'))
console.log(this.root())
Expand All @@ -66,7 +68,7 @@ export default class Help {
}
} else if (topic = this.config.findTopic(subject)) {
console.log(this.topic(topic))
console.log()
commands = commands.filter(c => c.id.startsWith(topic!.name))
if (commands.length) {
console.log(this.commands(commands))
console.log()
Expand All @@ -82,7 +84,22 @@ export default class Help {
}

topic(topic: Config.Topic): string {
return topic.name
let description = this.render(topic.description || '')
let title = description.split('\n')[0]
description = description.split('\n').slice(1).join('\n')
let output = compact([
title,
[
bold('USAGE'),
indent(wrap(`$ ${this.config.bin} ${topic.name}:COMMAND`, this.opts.maxWidth - 2, {trim: false, hard: true}), 2),
].join('\n'),
description && ([
bold('DESCRIPTION'),
indent(wrap(description, this.opts.maxWidth - 2, {trim: false, hard: true}), 2)
].join('\n'))
]).join('\n\n')
if (this.opts.stripAnsi) output = stripAnsi(output)
return output + '\n'
}

command(command: Config.Command): string {
Expand Down
5 changes: 4 additions & 1 deletion src/root.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Config from '@anycli/config'
import chalk from 'chalk'
import indent = require('indent-string')
import stripAnsi = require('strip-ansi')

import {HelpOptions} from '.'
import {compact, template} from './util'
Expand All @@ -21,11 +22,13 @@ export default class RootHelp {
let description = this.config.pjson.anycli.description || this.config.pjson.description || ''
description = this.render(description)
description = description.split('\n')[0]
return compact([
let output = compact([
description,
this.usage(),
this.description(),
]).join('\n\n')
if (this.opts.stripAnsi) output = stripAnsi(output)
return output
}

protected usage(): string {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
version "1.3.10"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-1.3.10.tgz#8d821fe8a60ec400dd49d9b1a12f7062e590dd4f"

"@anycli/config@^1.3.15":
version "1.3.15"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-1.3.15.tgz#5c3c85929af5b6b8d19ada8c15c13dc96523c40f"
"@anycli/config@^1.3.16":
version "1.3.16"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-1.3.16.tgz#7157649932c7c90450e32cf685ce295f2faa069a"

"@anycli/dev-cli@^0.2.2":
version "0.2.2"
Expand Down

0 comments on commit b42fde4

Please sign in to comment.