Skip to content

Commit

Permalink
feat: added command help
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 30, 2018
1 parent 1a85a17 commit a65df69
Show file tree
Hide file tree
Showing 18 changed files with 671 additions and 2,497 deletions.
14 changes: 3 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ jobs:
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
- v0-yarn-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
- v0-yarn-{{checksum ".circleci/config.yml"}}-master-
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn test
- run: curl -s https://codecov.io/bash | bash
- run: .circleci/test
- store_test_results: &store_test_results
path: ~/cli/reports
- save_cache: &save_cache
Expand All @@ -31,20 +28,15 @@ jobs:
steps:
- checkout
- restore_cache: *restore_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn test
- run: curl -s https://codecov.io/bash | bash
- run: .circleci/test
- store_test_results: *store_test_results
release:
<<: *test
steps:
- add_ssh_keys
- checkout
- restore_cache: *restore_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn exec nps release
- run: .circleci/release

workflows:
version: 2
Expand Down
11 changes: 11 additions & 0 deletions .circleci/release
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -ex

.circleci/setup_git

PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH

yarn global add @dxcli/semantic-release@1 semantic-release@12
yarn install --frozen-lockfile
semantic-release -e @dxcli/semantic-release
2 changes: 1 addition & 1 deletion .circleci/setup_git
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -ex

if [[ ! -z "$GIT_EMAIL" ]] & [[ ! -z "$GIT_USERNAME" ]]; then
git config --global push.default simple
Expand Down
9 changes: 8 additions & 1 deletion .circleci/yarn → .circleci/test
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env bash

set -e
set -ex

.circleci/setup_git

PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH

Expand All @@ -20,3 +22,8 @@ yarn install $CLI_ENGINE_UTIL_YARN_ARGS
if [[ "$CLI_ENGINE_GREENKEEPER_BRANCH" == 1 ]]; then
greenkeeper-lockfile-upload
fi

yarn add -D nyc @dxcli/nyc-config
yarn test

curl -s https://codecov.io/bash | bash
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
- git submodule update --init --recursive
- git config --global user.email "[email protected]"
- git config --global user.name "dxcli"
- yarn
- yarn add -D nyc @dxcli/nyc-config
test_script:
- yarn test
after_test:
Expand Down
39 changes: 24 additions & 15 deletions package-scripts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable unicorn/filename-case */

const {
setColors,
concurrent,
Expand All @@ -17,28 +19,35 @@ const linters = {
tslint: script('tslint -p test', 'lint ts files'),
}

const scripts = {
...linters,
lint: concurrent(linters),
test: script(concurrent.nps(...Object.keys(linters), 'mocha'), 'lint and run all tests'),
mocha: script('mocha --forbid-only "test/**/*.test.ts"', 'run all mocha tests'),
}

let mocha = 'mocha --forbid-only "test/**/*.test.ts"'
if (process.env.CI) {
if (process.env.CIRCLECI) {
scripts.test.script = series(mkdirp('reports'), scripts.test.script)
// add mocha junit reporter
scripts.mocha.script = crossEnv(`MOCHA_FILE=reports/mocha.xml ${scripts.mocha.script} --reporter mocha-junit-reporter`)
mocha = crossEnv(`MOCHA_FILE=reports/mocha.xml ${mocha} --reporter mocha-junit-reporter`)
// add eslint reporter
scripts.eslint.script = `${scripts.eslint.script} --format junit --output-file reports/eslint.xml`
linters.eslint.script = `${linters.eslint.script} --format junit --output-file reports/eslint.xml`
// add tslint reporter
scripts.tslint.script = `${scripts.tslint.script} --format junit > reports/tslint.xml`
scripts.release = 'semantic-release -e @dxcli/dev-semantic-release'
linters.tslint.script = `${linters.tslint.script} --format junit > reports/tslint.xml`
}
// add code coverage reporting with nyc
const nyc = 'nyc --nycrc-path node_modules/@dxcli/dev-nyc-config/.nycrc'
const nyc = 'nyc --nycrc-path node_modules/@dxcli/nyc-config/.nycrc'
const nycReport = `${nyc} report --reporter text-lcov > coverage.lcov`
scripts.mocha.script = series(`${nyc} ${scripts.mocha.script}`, nycReport)
mocha = series(`${nyc} ${mocha}`, nycReport)
}

module.exports = {scripts}
let test = concurrent({
...linters,
test: series('nps build', mocha),
})

if (process.env.CI) test = series(mkdirp('reports'), test)

module.exports = {
scripts: {
...linters,
build: series('rm -rf lib', 'tsc'),
lint: concurrent(linters),
test,
mocha,
},
}
44 changes: 35 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,47 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/jdxcode/help/issues",
"dependencies": {
"@dxcli/command": "^0.1.19",
"cli-ux": "^3.2.1"
"@dxcli/command": "^0.2.8",
"@dxcli/screen": "^0.0.2",
"chalk": "^2.3.0",
"cli-ux": "^3.3.8",
"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": {
"@dxcli/config": "^0.1.26",
"@dxcli/dev": "^2.0.4",
"@dxcli/dev-test": "^0.9.12",
"@dxcli/engine": "^0.1.12",
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@dxcli/config": "^0.1.39",
"@dxcli/engine": "^0.1.20",
"@dxcli/test": "^0.9.19",
"@dxcli/tslint": "^0.1.3",
"@types/chai": "^4.1.2",
"@types/indent-string": "^3.0.0",
"@types/lodash": "^4.14.98",
"@types/mocha": "^2.2.47",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/node-notifier": "^0.0.28",
"@types/read-pkg": "^3.0.0",
"@types/strip-ansi": "^3.0.0",
"@types/wrap-ansi": "^2.0.14",
"chai": "^4.1.2",
"eslint": "^4.16.0",
"eslint-config-dxcli": "^1.2.1",
"husky": "^0.14.3",
"mocha": "^5.0.0",
"mocha-junit-reporter": "^1.17.0",
"nps": "^5.7.1",
"nps-utils": "^1.5.0"
"nps-utils": "^1.5.0",
"strip-ansi": "^4.0.0",
"ts-node": "^4.1.0",
"typescript": "^2.6.2"
},
"dxcli": {
"bin": "dxcli",
"commands": "./lib/commands"
},
"engines": {
Expand All @@ -35,9 +61,9 @@
"license": "MIT",
"repository": "dxcli/help",
"scripts": {
"commitmsg": "dxcli-dev-commitmsg",
"commitmsg": "commitlint -x @commitlint/config-conventional -e $GIT_PARAMS",
"precommit": "nps lint -l warn",
"prepare": "rm -rf lib && tsc",
"prepublishOnly": "nps build",
"test": "nps test -l warn"
}
}
111 changes: 111 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {ICachedArg, ICachedCommand, ICachedFlag, IConfig} from '@dxcli/config'
import chalk from 'chalk'
import * as _ from 'lodash'

import {Article, Section} from '.'

const {
underline,
dim,
} = chalk

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

command(cmd: ICachedCommand): Article {
const flagDefs = cmd.flags || {}
const flags = Object.keys(flagDefs)
.filter(k => !flagDefs[k].hidden)
.map(k => {
flagDefs[k].name = k
return flagDefs[k]
})
const args = (cmd.args || []).filter(a => !a.hidden)
return {
title: cmd.title,
sections: _([
this.usage(cmd, flags),
this.args(args),
this.flags(flags),
this.aliases(cmd.aliases),
]).compact().value(),
}
}

// commandLine(cmd: ICachedCommand): [string, string | undefined] {
// return [buildUsage(cmd), cmd.description ? dim(cmd.description) : undefined] as [string, string | undefined]
// }

protected usage(cmd: ICachedCommand, flags: ICachedFlag[]): Section {
return {
heading: 'usage',
body: cmd.usage ? _.castArray(cmd.usage) : this.defaultUsage(cmd, flags)
}
}
protected defaultUsage(command: ICachedCommand, flags: ICachedFlag[]): string {
return _([
'$',
this.config.bin,
command.id,
command.args.map(a => this.arg(a)).join(' '),
flags.length && dim('[OPTIONS]'),
])
.compact()
.join(' ')
}

protected description(cmd: ICachedCommand): Section | undefined {
if (!cmd.description) return
return {
heading: 'description',
body: cmd.description.trim(),
}
}

protected aliases(aliases: string[] | undefined): Section | undefined {
if (!aliases || !aliases.length) return
return {
heading: 'aliases',
body: aliases.map(a => ['$', this.config.bin, a].join(' ')),
}
}

protected args(args: ICachedCommand['args']): Section | undefined {
if (!args.find(f => !!f.description)) return
return {
heading: 'arguments',
body: args.map(a => {
return [a.name!.toUpperCase(), a.description ? dim(a.description) : undefined]
})
}
}
protected arg(arg: ICachedArg): string {
let name = arg.name.toUpperCase()
if (arg.required) return `${name}`
return `[${name}]`
}

protected flags(flags: ICachedFlag[]): Section | undefined {
if (!flags.length) return
return {
heading: 'options',
body: _(flags)
.sortBy(f => [!f.char, f.char, f.name])
.map(f => this.flag(f))
.value(),
}
}

protected flag(flag: ICachedFlag): [string, string | undefined] {
const label = []
if (flag.char) label.push(`-${flag.char}`)
if (flag.name) label.push(` --${flag.name}`)
let left = label.join(',').trim()
if (flag.type === 'option') left += `=${underline(flag.name)}`

let right = flag.description || ''
if (flag.required) right = `(required) ${right}`

return [left, dim(right.trim())]
}
}
13 changes: 0 additions & 13 deletions src/commands/hello.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Command from '@dxcli/command'
import cli from 'cli-ux'

export default class HelpCommand extends Command {
async run() {
const name = this.flags.name || 'world'
cli.log(`hello ${name}!`)
}
}
7 changes: 7 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// tslint:disable

declare namespace NodeJS {
interface Global {
'columns': number
}
}
Loading

0 comments on commit a65df69

Please sign in to comment.