Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: add new config help vars
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 30, 2018
1 parent 1327fc3 commit f075793
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1,814 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
- restore_cache: *restore_cache
- run: .circleci/setup_git
- run: .circleci/yarn
- run: yarn add -D @dxcli/semantic-release@1
- run: yarn exec nps release

workflows:
Expand Down
13 changes: 7 additions & 6 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const linters = {
tslint: script('tslint -p test', 'lint ts files'),
}

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

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

if (process.env.CI) test = series(mkdirp('reports'), test)
Expand All @@ -48,6 +48,7 @@ module.exports = {
build: series('rm -rf lib', 'tsc'),
lint: concurrent(linters),
test,
release: 'semantic-release -e @dxcli/semantic-release',
mocha,
release: 'dxcli-semantic-release -e @dxcli/semantic-release',
},
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/dxcli/command/issues",
"dependencies": {
"@dxcli/parser": "^0.0.5",
"@dxcli/parser": "^0.0.6",
"debug": "^3.1.0",
"lodash": "^4.17.4",
"tslib": "^1.9.0"
},
"devDependencies": {
"@dxcli/config": "^0.1.37",
"@commitlint/cli": "^6.0.2",
"@commitlint/config-conventional": "^6.0.2",
"@dxcli/config": "^0.1.39",
"@dxcli/dev": "^2.0.16",
"@dxcli/dev-semantic-release": "^0.2.0",
"@dxcli/semantic-release": "^0.3.3",
"@dxcli/nyc-config": "^0.0.4",
"@dxcli/tslint": "^0.1.3",
"chai": "^4.1.2",
"cli-ux": "^3.3.8",
"eslint": "^4.16.0",
"fancy-test": "^0.6.4",
"fancy-test": "^0.6.5",
"http-call": "^5.0.2",
"husky": "^0.14.3",
"mocha": "^5.0.0",
Expand Down Expand Up @@ -48,7 +49,7 @@
},
"repository": "dxcli/command",
"scripts": {
"commitmsg": "dxcli-commitlint",
"commitmsg": "commitlint -x @commitlint/config-conventional -e $GIT_PARAMS",
"precommit": "nps lint -l warn",
"prepublishOnly": "nps build",
"test": "nps test -l warn"
Expand Down
48 changes: 48 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as Config from '@dxcli/config'
import * as _ from 'lodash'

export interface ConvertToCachedOptions {
id?: string
plugin?: Config.IPlugin
}

export function convertToCached(c: Config.ICommand, opts: ConvertToCachedOptions = {}): Config.ICachedCommand {
return {
_base: c._base,
id: c.id || opts.id!,
description: c.description,
usage: c.usage,
pluginName: opts.plugin && opts.plugin.name,
hidden: c.hidden,
aliases: c.aliases || [],
// help: c.help,
flags: _.mapValues(c.flags, flag => {
if (flag.type === 'boolean') {
return {
type: flag.type,
char: flag.char,
description: flag.description,
hidden: flag.hidden,
name: flag.name,
required: flag.required,
}
}
return {
type: flag.type,
char: flag.char,
description: flag.description,
hidden: flag.hidden,
name: flag.name,
required: flag.required,
}
}),
args: c.args.map(a => ({
name: a.name,
description: a.description,
required: a.required,
// default: a.default && a.default(),
hidden: a.hidden,
})),
load: async () => c,
}
}
65 changes: 14 additions & 51 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {args} from '@dxcli/parser'
import cli from 'cli-ux'
import * as _ from 'lodash'

import {convertToCached, ConvertToCachedOptions} from './cache'
import deps from './deps'
import * as flags from './flags'

Expand All @@ -18,64 +19,20 @@ const g = global as any

const parentModule = module.parent && module.parent.parent && module.parent.parent.filename

export interface ConvertToCachedOptions {
id?: string
plugin?: Config.IPlugin
}

export function convertToCached(c: Config.ICommand, opts: ConvertToCachedOptions = {}): Config.ICachedCommand {
return {
_base: c._base,
id: c.id || opts.id!,
description: c.description,
usage: c.usage,
pluginName: opts.plugin && opts.plugin.name,
hidden: c.hidden,
aliases: c.aliases || [],
help: c.help,
flags: _.mapValues(c.flags, flag => {
if (flag.type === 'boolean') {
return {
type: flag.type,
char: flag.char,
description: flag.description,
hidden: flag.hidden,
name: flag.name,
required: flag.required,
}
}
return {
type: flag.type,
char: flag.char,
description: flag.description,
hidden: flag.hidden,
name: flag.name,
required: flag.required,
}
}),
args: c.args.map(a => ({
name: a.name,
description: a.description,
required: a.required,
// default: a.default && a.default(),
hidden: a.hidden,
})),
load: async () => c,
}
}

export default abstract class Command {
static _base = `${pjson.name}@${pjson.version}`
static id: string
static title: string | undefined
static description: string | undefined
static hidden: boolean
static usage: string | undefined
static usage: string | string[] | undefined
static help: string | undefined
static aliases: string[] = []
static variableArgs = false
static flags: flags.Input
static flags: flags.Input<any>
static args: args.IArg[] = []
static plugin: Config.IPlugin | undefined
static examples: string[] | undefined

/**
* instantiate and run the command
Expand All @@ -84,16 +41,17 @@ export default abstract class Command {
const cmd = new this()
return cmd._run(argv, opts)
}

static async load() { return this }

static convertToCached(opts: ConvertToCachedOptions = {}): Config.ICachedCommand {
return convertToCached(this, opts)
}

config: Config.IConfig
flags: { [name: string]: any } = {}
argv: string[]
args: { [name: string]: string } = {}
flags: flags.Output
args: args.Output

// prevent setting things that need to be static
topic: null
Expand Down Expand Up @@ -147,7 +105,7 @@ export default abstract class Command {
flags: this.ctor.flags || {},
strict: !this.ctor.variableArgs,
})
this.flags = parse.flags
this.flags = parse.flags as any
this.argv = parse.argv
this.args = parse.args
} catch (err) {
Expand Down Expand Up @@ -175,3 +133,8 @@ export default abstract class Command {
}
}
}

export {
convertToCached,
ConvertToCachedOptions,
}
25 changes: 14 additions & 11 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@ export interface ICompletion {
options(ctx: ICompletionContext): Promise<string[]>
}

export interface IOptionFlag<T = string> extends flags.IOptionFlag<T> {
export interface IOptionFlag<T> extends flags.IOptionFlag<T> {
completion?: ICompletion
}

export type IFlag<T = string> = flags.IBooleanFlag | IOptionFlag<T>
export type IFlag<T> = flags.IBooleanFlag<T> | IOptionFlag<T>

export interface Input {
[k: string]: IFlag<any>
}

export type Definition<T = string> = (options?: Partial<IOptionFlag<T>>) => IOptionFlag<T>
export type Output = flags.Output
export type Input<T extends flags.Output> = { [P in keyof T]: IFlag<T[P]> }

export function option<T = string>(defaults: Partial<IOptionFlag<T>> = {}): Definition<T> {
return deps.Parser.flags.option<T>(defaults)
export interface Definition<T> {
(options: {multiple: true} & Partial<IOptionFlag<T>>): IOptionFlag<T[]>
(options: {required: true} & Partial<IOptionFlag<T>>): IOptionFlag<T>
(options?: Partial<IOptionFlag<T>>): IOptionFlag<T | undefined>
}

export function string(defaults: Partial<IOptionFlag> = {}): IOptionFlag {
return deps.Parser.flags.string(defaults)
export function build<T>(defaults: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>): Definition<T>
export function build(defaults: Partial<IOptionFlag<string>>): Definition<string>
export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
return deps.Parser.flags.build<T>(defaults as any)
}

const stringFlag = build({})
export {stringFlag as string}
export {boolean} from '@dxcli/parser/lib/flags'
Loading

0 comments on commit f075793

Please sign in to comment.