This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: this is a big refactor that consolidates the code from @anycli/config and @anycli/engine
- Loading branch information
Showing
18 changed files
with
740 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.