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

Commit

Permalink
feat: move engine logic into config
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
this is a big refactor that consolidates the code from @anycli/config
and @anycli/engine
  • Loading branch information
jdx committed Feb 3, 2018
1 parent 6083c8e commit 003c74f
Show file tree
Hide file tree
Showing 18 changed files with 740 additions and 428 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/anycli/config/issues",
"dependencies": {
"cli-ux": "^3.3.13",
"debug": "^3.1.0",
"fs-extra": "^5.0.0",
"fs-extra-debug": "^1.0.4",
"globby": "^7.1.1",
"load-json-file": "^4.0.0",
"lodash": "^4.17.4",
"read-pkg": "^3.0.0"
"read-pkg": "^3.0.0",
"tslib": "^1.9.0"
},
"devDependencies": {
"@anycli/parser": "^3.0.4",
"@anycli/tslint": "^0.2.3",
"@types/chai": "^4.1.2",
"@types/fs-extra": "^5.0.0",
"@types/globby": "^6.1.0",
"@types/load-json-file": "^2.0.7",
"@types/lodash": "^4.14.100",
"@types/mocha": "^2.2.48",
"@types/nock": "^9.1.2",
"@types/node": "^9.4.0",
"@types/node-notifier": "^0.0.28",
"@types/read-pkg": "^3.0.0",
"chai": "^4.1.2",
"concurrently": "^3.5.1",
Expand Down
150 changes: 94 additions & 56 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,112 @@
import * as Parser from '@anycli/parser'
import * as _ from 'lodash'

import {IConfig} from './config'
import {IPlugin} from './plugin'
import * as Config from '.'

export interface ICommandBase {
_base: string
export interface Command {
id: string
hidden: boolean
aliases: string[]
description?: string
title?: string
usage?: string | string[]
examples?: string[]
pluginName?: string
type?: string
flags: {[name: string]: Command.Flag.Boolean | Command.Flag.Option}
args: {
name: string
description?: string
required?: boolean
hidden?: boolean
default?: string
options?: string[]
}[]
}

export interface ICachedCommand extends ICommandBase {
flags: {[name: string]: ICachedFlag}
args: ICachedArg[]
load(): Promise<ICommand>
}

export interface IConvertToCachedOptions {
id?: string
pluginName?: string
}
export namespace Command {
export namespace Flag {
export interface Boolean {
type: 'boolean'
name: string
required?: boolean
char?: string
hidden?: boolean
description?: string
}
export interface Option {
type: 'option'
name: string
required?: boolean
char?: string
hidden?: boolean
description?: string
helpValue?: string
default?: string
options?: string[]
}
}

export interface ICommand<T = any> extends ICommandBase {
plugin?: IPlugin
flags?: Parser.flags.Input<any>
args?: Parser.args.Input
new (argv: string[], opts: ICommandOptions): T
run(this: ICommand<T>, argv: string[], opts?: Partial<ICommandOptions>): Promise<void>
convertToCached(opts?: IConvertToCachedOptions): ICachedCommand
}

export interface ICommandOptions {
root?: string
config: IConfig
}
export interface Base {
_base: string
id: string
hidden: boolean
aliases: string[]
description?: string
title?: string
usage?: string | string[]
examples?: string[]
}

export interface ICachedArg {
name: string
description?: string
required?: boolean
hidden?: boolean
default?: string
options?: string[]
}
export interface Full extends Base {
plugin?: Config.IPlugin
flags?: Parser.flags.Input<any>
args?: Parser.args.Input
run(argv: string[], config?: Config.Options): Promise<any>
}

export interface ICachedBooleanFlag {
type: 'boolean'
name: string
required?: boolean
char?: string
hidden?: boolean
description?: string
}
export interface Plugin extends Command {
load(): Full
}

export interface ICachedOptionFlag {
type: 'option'
name: string
required?: boolean
char?: string
hidden?: boolean
description?: string
helpValue?: string
default?: string
options?: string[]
export function toCached(c: Full): Command {
return {
title: c.title,
id: c.id,
description: c.description,
usage: c.usage,
hidden: c.hidden,
aliases: c.aliases || [],
flags: _.mapValues(c.flags || {}, (flag, name) => {
if (flag.type === 'boolean') {
return {
name,
type: flag.type,
char: flag.char,
description: flag.description,
hidden: flag.hidden,
required: flag.required,
}
}
return {
name,
type: flag.type,
char: flag.char,
description: flag.description,
hidden: flag.hidden,
required: flag.required,
helpValue: flag.helpValue,
options: flag.options,
default: _.isFunction(flag.default) ? flag.default({options: {}, flags: {}}) : flag.default,
}
}),
args: c.args ? c.args.map(a => ({
name: a.name,
description: a.description,
required: a.required,
options: a.options,
default: _.isFunction(a.default) ? a.default({}) : a.default,
hidden: a.hidden,
})) : {} as Command['args'],
}
}
}

export type ICachedFlag = ICachedBooleanFlag | ICachedOptionFlag
Loading

0 comments on commit 003c74f

Please sign in to comment.