Skip to content

Commit

Permalink
fix: updated engine
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 1, 2018
1 parent 0c50c69 commit 8046612
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 213 deletions.
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/jdxcode/help/issues",
"dependencies": {
"@anycli/command": "^0.2.8",
"@anycli/command": "^0.2.22",
"@anycli/screen": "^0.0.3",
"chalk": "^2.3.0",
"cli-ux": "^3.3.8",
"cli-ux": "^3.3.10",
"indent-string": "^3.2.0",
"lodash": "^4.17.4",
"string-width": "^2.1.1",
"widest-line": "^2.0.0",
"wrap-ansi": "^3.0.1"
},
"devDependencies": {
"@anycli/config": "^0.2.1",
"@anycli/engine": "^0.1.29",
"@anycli/plugins": "^0.1.7",
"@anycli/test": "^0.10.0",
"@anycli/tslint": "^0.1.3",
"@anycli/version": "^0.1.15",
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@anycli/config": "^0.2.6",
"@anycli/engine": "^0.1.38",
"@anycli/plugins": "^0.2.0",
"@anycli/test": "^0.10.1",
"@anycli/tslint": "^0.2.1",
"@anycli/version": "^0.1.19",
"@commitlint/cli": "^6.0.5",
"@commitlint/config-conventional": "^6.0.4",
"@types/chai": "^4.1.2",
"@types/indent-string": "^3.0.0",
"@types/lodash": "^4.14.98",
"@types/mocha": "^2.2.47",
"@types/lodash": "^4.14.99",
"@types/mocha": "^2.2.48",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/node-notifier": "^0.0.28",
Expand All @@ -44,7 +44,7 @@
"nps-utils": "^1.5.0",
"strip-ansi": "^4.0.0",
"ts-node": "^4.1.0",
"typescript": "^2.6.2"
"typescript": "^2.7.1"
},
"anycli": {
"bin": "anycli",
Expand Down
6 changes: 3 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ICachedArg, ICachedCommand, ICachedFlag, IConfig} from '@anycli/config'
import chalk from 'chalk'
import * as _ from 'lodash'

import {Article, Section} from '.'
import {Article, HelpOptions, Section} from '.'

const {
underline,
Expand All @@ -11,7 +11,7 @@ const {
} = chalk

export default class CommandHelp {
constructor(public config: IConfig) {}
constructor(public config: IConfig, public opts: HelpOptions) {}

command(cmd: ICachedCommand): Article {
const flagDefs = cmd.flags || {}
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class CommandHelp {
if (flag.char) label.push(blueBright(`-${flag.char[0]}`))
if (flag.name) label.push(blueBright(`--${flag.name.trim()}`))
let left = label.join(', ')
if (flag.type === 'option') left += `=${underline(flag.name)}`
if (flag.type === 'option') left += `=${underline(flag.helpValue || flag.name)}`

let right = flag.description || ''
if (flag.required) right = `(required) ${right}`
Expand Down
15 changes: 7 additions & 8 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Command, {flags} from '@anycli/command'
import {Command, flags, parse} from '@anycli/command'
import {IConfig} from '@anycli/config'
import cli from 'cli-ux'

Expand All @@ -8,20 +8,19 @@ const config: IConfig = global.anycli.config

export default class HelpCommand extends Command {
static title = `display help for ${config.bin}`
static flags: flags.Input<HelpCommand['flags']> = {
static flags = {
all: flags.boolean({description: 'see all commands in CLI'}),
// format: flags.enum({description: 'output in a different format'}),
format: flags.enum({description: 'output in a different format', options: ['markdown', 'man']}),
}
static args = [
{name: 'command', required: false}
]
flags: {
all?: boolean
}

options = parse(this.argv, HelpCommand)

async run() {
let id = this.args.command as string
let help = new Help(this.config)
let id = this.options.args.command
let help = new Help(this.config, {format: this.options.flags.format as any})
if (!id) {
let rootHelp = help.root()
cli.info(rootHelp)
Expand Down
43 changes: 25 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const widestLine = require('widest-line')
// }

export interface HelpOptions {
markdown?: boolean
format?: 'markdown' | 'screen' | 'man'
all?: boolean
}

function renderList(input: (string | undefined)[][], opts: {maxWidth: number, multiline?: boolean, stripAnsi?: boolean}): string {
Expand Down Expand Up @@ -100,51 +101,57 @@ function renderList(input: (string | undefined)[][], opts: {maxWidth: number, mu
}

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

root(opts: HelpOptions = {}): string {
const help = new RootHelp(this.config)
root(): string {
const help = new RootHelp(this.config, this.opts)
const article = help.root()
return this.render(article, opts)
return this.render(article)
}

command(command: ICachedCommand, opts: HelpOptions = {}): string {
const help = new CommandHelp(this.config)
command(command: ICachedCommand): string {
const help = new CommandHelp(this.config, this.opts)
const article = help.command(command)
return this.render(article, opts)
return this.render(article)
}

protected render(article: Article, opts: HelpOptions): string {
if (opts.markdown) return this.renderMarkdown(article)
return this.renderScreen(article)
protected render(article: Article): string {
switch (this.opts.format) {
case 'markdown': return this.renderMarkdown(article)
case 'man':
case 'screen':
default: return this.renderScreen(article)
}
}

protected renderMarkdown(article: Article): string {
const maxWidth = 120
const maxWidth = 100
return _([
stripAnsi(article.title || ''),
'='.repeat(width(article.title)),
'-'.repeat(width(article.title)),
'',
...article.sections
.map(s => {
let body = '\n'
if (s.body.length === 0) {
body += ''
} else if (_.isArray(s.body[0])) {
body += '```\n'
body += renderList(s.body as any, {maxWidth: maxWidth - 2, stripAnsi: true})
body += '\n```'
} else {
body += _.castArray(s.body as string).join('\n')
body += wrap(stripAnsi(body), maxWidth - 2, {trim: false, hard: true})
let output = _.castArray(s.body as string).join('\n')
body += wrap(stripAnsi(output), maxWidth - 2, {trim: false, hard: true})
}
if (s.type === 'code') {
body = `\n\`\`\`sh-session${body}\n\`\`\``
}
return _([
`${_.capitalize(s.heading)}\n${'-'.repeat(width(s.heading))}`,
indent(body, 2),
`**${_.capitalize(s.heading)}**`,
body,
]).compact().join('\n') + '\n'
})
]).join('\n')
]).join('\n').trim()
}

protected renderScreen(article: Article): string {
Expand Down
13 changes: 3 additions & 10 deletions src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@ import {IConfig} from '@anycli/config'
// import chalk from 'chalk'
import * as _ from 'lodash'

import {Article, Section} from '.'
import {Article, HelpOptions, Section} from '.'

// const {
// underline,
// dim,
// blueBright,
// } = chalk

export interface RootOptions {
all?: boolean
}

export default class RootHelp {
opts: RootOptions

constructor(public config: IConfig) {}
constructor(public config: IConfig, public opts: HelpOptions = {}) {}

root(opts: RootOptions = {}): Article {
this.opts = opts
root(): Article {
return {
title: this.config.pjson.anycli.title || this.config.pjson.description,
sections: _([
Expand Down
12 changes: 12 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ ALIASES
$ anycli app:init
$ anycli create`))

test
.commandHelp(class extends Command {
static id = 'apps:create'
static flags = {
myenum: flags.enum({options: ['a', 'b', 'c']}),
}})
.it('outputs with title', ctx => expect(ctx.commandHelp).to.equal(`USAGE
$ anycli apps:create [OPTIONS]
OPTIONS
--myenum=(a|b|c)`))

// class AppsCreate3 extends Command {
// static id = 'apps:create'
// static flags = {
Expand Down
3 changes: 2 additions & 1 deletion test/commands/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ USAGE
$ anycli help [COMMAND] [OPTIONS]
OPTIONS
--all see all commands in CLI
--all see all commands in CLI
--format=(markdown|man) output in a different format
`)
})
})
70 changes: 31 additions & 39 deletions test/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Command extends Base {

const test = base
.loadConfig()
.add('help', ctx => new Help(ctx.config))
.add('help', ctx => new Help(ctx.config, {format: 'markdown'}))
.register('commandHelp', (command?: ICommand) => ({
run(ctx: {help: Help, commandHelp: string, expectation: string}) {
const cached = command!.convertToCached()
let help = ctx.help.command(cached, {markdown: true})
let help = ctx.help.command(cached)
ctx.commandHelp = stripAnsi(help).split('\n').map(s => s.trimRight()).join('\n')
ctx.expectation = 'has commandHelp'
}
Expand All @@ -41,55 +41,47 @@ describe('markdown', () => {
remote: flags.string({char: 'r'}),
}})
.it(ctx => expect(ctx.commandHelp).to.equal(`the title
=========
Usage
-----
---------
\`\`\`sh-session
$ anycli apps:create [APP_NAME] [OPTIONS]
$ anycli apps:create [APP_NAME] [OPTIONS]
\`\`\`
**Usage**
Arguments
---------
\`\`\`sh-session
$ anycli apps:create [APP_NAME] [OPTIONS]
\`\`\`
APP_NAME app to use
**Arguments**
Options
-------
\`\`\`
APP_NAME app to use
\`\`\`
-f, --foo=foo foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarf
oobarfoobar
**Options**
-r, --remote=remote
\`\`\`
-f, --foo=foo foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfooba
rfoobarfoobarfoobarfoobarfoobar
--force force it force it force it force it force it force it force it force it force it force
it force it force it force it force it force it
-r, --remote=remote
--ss newliney
newliney
newliney
newliney
--force force it force it force it force it force it force it force it force
it force it force it force it force it force it force it force it
Description
-----------
--ss newliney
newliney
newliney
newliney
\`\`\`
some
**Description**
multiline help
some
some
multiline help
multiline help
Aliases
-------
**Aliases**
\`\`\`sh-session
$ anycli app:init
$ anycli create
$ anycli app:init
$ anycli create
\`\`\`
`))
\`\`\`sh-session
$ anycli app:init
$ anycli create
\`\`\``))
})
Loading

0 comments on commit 8046612

Please sign in to comment.