Skip to content

Commit

Permalink
Fixed enum-default logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed Jun 27, 2024
1 parent 7418c97 commit d87aa36
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ Extensions:

- `.enum(values: [string, ...string[]])` - limits values of string to one of specified here
- `values` - allowed enum values
:warning: - does not test default value, will add it to the output type instead

- `.int()` - ensures that number is an integer

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@drizzle-team/brocli",
"type": "module",
"author": "Drizzle Team",
"version": "0.2.3",
"version": "0.2.4",
"description": "Typed CLI command runner",
"license": "Apache-2.0",
"sideEffects": false,
Expand Down
29 changes: 22 additions & 7 deletions src/option-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class OptionBuilderBase<
TBuilderConfig extends BuilderConfig = BuilderConfig,
TOutput extends OutputType = string,
TOmit extends string = '',
TDefault extends OutputType = undefined,
TEnums extends string | undefined = undefined,
> {
public _: {
config: TBuilderConfig;
Expand Down Expand Up @@ -188,14 +188,22 @@ export class OptionBuilderBase<
return this as any;
}

public default<TDefVal extends TOutput extends string ? string : Exclude<TOutput, undefined>>(value: TDefVal): Omit<
public default<TDefVal extends TEnums extends undefined ? Exclude<TOutput, undefined> : TEnums>(value: TDefVal): Omit<
OptionBuilderBase<
BuilderConfig,
Exclude<TOutput | TDefVal, undefined>,
TOmit | 'required' | 'default'
Exclude<TOutput, undefined>,
TOmit | 'enum' | 'required' | 'default',
TEnums
>,
TOmit | 'required' | 'default'
TOmit | 'enum' | 'required' | 'default'
> {
const enums = this.config().enumVals;
if (enums && !enums.find((v) => value === v)) {
throw new Error(
`Option enums [ ${enums.join(', ')} ] are incompatible with default value ${value}`,
);
}

this.config().default = value;

return this as any;
Expand All @@ -204,11 +212,18 @@ export class OptionBuilderBase<
public enum<TValues extends [string, ...string[]]>(...values: TValues): Omit<
OptionBuilderBase<
BuilderConfig,
Exclude<TOutput | TDefault, string> | TValues[number],
TOmit | 'enum'
TValues[number],
TOmit | 'enum',
TValues[number]
>,
TOmit | 'enum'
> {
const defaultVal = this.config().default;
if (defaultVal !== undefined && !values.find((v) => defaultVal === v)) {
throw new Error(
`Option enums [ ${values.join(', ')} ] are incompatible with default value ${defaultVal}`,
);
}
this.config().enumVals = values;

return this as any;
Expand Down

0 comments on commit d87aa36

Please sign in to comment.