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

Commit b75d738

Browse files
committed
fix: added optionType
1 parent 643aea7 commit b75d738

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

package-scripts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ setColors(['dim'])
1313
const script = (script, description) => description ? {script, description} : {script}
1414

1515
const linters = {
16-
eslint: script('eslint .', 'lint js files'),
16+
eslint: script(series('nps build', 'eslint .'), 'lint js files'),
1717
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
1818
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
1919
tslint: script('tslint -p test', 'lint ts files'),

src/flags.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface IBooleanFlag<T> extends IFlagBase<T, boolean> {
1818

1919
export interface IOptionFlag<T> extends IFlagBase<T, string> {
2020
type: 'option'
21+
optionType: string
2122
default?: T | ((context: DefaultContext<T>) => T | undefined)
2223
multiple: boolean
2324
input: string[]
@@ -29,6 +30,12 @@ export interface Definition<T> {
2930
(options?: Partial<IOptionFlag<T>>): IOptionFlag<T | undefined>
3031
}
3132

33+
export interface EnumFlagOptions<T> extends Partial<IOptionFlag<T>> {
34+
options: string[]
35+
}
36+
37+
export type IFlag<T> = IBooleanFlag<T> | IOptionFlag<T>
38+
3239
export function build<T>(defaults: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>): Definition<T>
3340
export function build(defaults: Partial<IOptionFlag<string>>): Definition<string>
3441
export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
@@ -44,8 +51,6 @@ export function build<T>(defaults: Partial<IOptionFlag<T>>): Definition<T> {
4451
}
4552
}
4653

47-
export type IFlag<T> = IBooleanFlag<T> | IOptionFlag<T>
48-
4954
export function boolean<T = boolean>(options: Partial<IBooleanFlag<T>> = {}): IBooleanFlag<T> {
5055
return {
5156
parse: b => b,
@@ -56,30 +61,28 @@ export function boolean<T = boolean>(options: Partial<IBooleanFlag<T>> = {}): IB
5661
}
5762

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

65-
export interface EnumFlagOptions<T> extends Partial<IOptionFlag<T>> {
66-
options: string[]
67-
}
68-
6971
const _enum = <T = string>(opts: EnumFlagOptions<T>) => build<T>({
7072
parse(input) {
7173
if (!opts.options.includes(input)) throw new Error(`Expected --${this.name}=${input} to be one of: ${opts.options.join(', ')}`)
7274
return input
7375
},
7476
...opts as any,
77+
optionType: 'enum',
7578
})
7679
export {_enum as enum}
7780

7881
export function option<T>(options: {parse: IOptionFlag<T>['parse']} & Partial<IOptionFlag<T>>) {
79-
return build<T>(options)()
82+
return build<T>({optionType: 'custom', ...options})()
8083
}
8184

82-
const stringFlag = build({})
85+
const stringFlag = build({optionType: 'string'})
8386
export {stringFlag as string}
8487

8588
export const defaultFlags = {

0 commit comments

Comments
 (0)