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

Commit

Permalink
fix: added optionType
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 30, 2018
1 parent 643aea7 commit b75d738
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ setColors(['dim'])
const script = (script, description) => description ? {script, description} : {script}

const linters = {
eslint: script('eslint .', 'lint js files'),
eslint: script(series('nps build', 'eslint .'), 'lint js files'),
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
tslint: script('tslint -p test', 'lint ts files'),
Expand Down
19 changes: 11 additions & 8 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IBooleanFlag<T> extends IFlagBase<T, boolean> {

export interface IOptionFlag<T> extends IFlagBase<T, string> {
type: 'option'
optionType: string
default?: T | ((context: DefaultContext<T>) => T | undefined)
multiple: boolean
input: string[]
Expand All @@ -29,6 +30,12 @@ export interface Definition<T> {
(options?: Partial<IOptionFlag<T>>): IOptionFlag<T | undefined>
}

export interface EnumFlagOptions<T> extends Partial<IOptionFlag<T>> {
options: string[]
}

export type IFlag<T> = IBooleanFlag<T> | IOptionFlag<T>

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> {
Expand All @@ -44,8 +51,6 @@ export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
}
}

export type IFlag<T> = IBooleanFlag<T> | IOptionFlag<T>

export function boolean<T = boolean>(options: Partial<IBooleanFlag<T>> = {}): IBooleanFlag<T> {
return {
parse: b => b,
Expand All @@ -56,30 +61,28 @@ export function boolean<T = boolean>(options: Partial<IBooleanFlag<T>> = {}): IB
}

export const integer = build({
optionType: 'integer',
parse: input => {
if (!/^[0-9]+$/.test(input)) throw new Error(`Expected an integer but received: ${input}`)
return parseInt(input, 10)
},
})

export interface EnumFlagOptions<T> extends Partial<IOptionFlag<T>> {
options: string[]
}

const _enum = <T = string>(opts: EnumFlagOptions<T>) => build<T>({
parse(input) {
if (!opts.options.includes(input)) throw new Error(`Expected --${this.name}=${input} to be one of: ${opts.options.join(', ')}`)
return input
},
...opts as any,
optionType: 'enum',
})
export {_enum as enum}

export function option<T>(options: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>) {
return build<T>(options)()
return build<T>({optionType: 'custom', ...options})()
}

const stringFlag = build({})
const stringFlag = build({optionType: 'string'})
export {stringFlag as string}

export const defaultFlags = {
Expand Down

0 comments on commit b75d738

Please sign in to comment.