Skip to content

Commit 2cb1f23

Browse files
committed
Add support for select prompts for number options
1 parent 9ad321d commit 2cb1f23

5 files changed

Lines changed: 27 additions & 6 deletions

File tree

.changeset/khaki-doodles-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@gud/cli": minor
3+
---
4+
5+
Added support for select prompts when the option type is "number" and added type inference to the choice values based on the option type.

packages/cli/src/core/client.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Console } from 'node:console';
22
import process from 'node:process';
3-
import prompts, { type PromptObject, type PromptType } from 'prompts';
3+
import prompts, {
4+
type Choice,
5+
type PromptObject,
6+
type PromptType,
7+
} from 'prompts';
48
import { CliError, type CliErrorOptions } from 'src/core/errors';
9+
import type { OptionPrimitiveType, OptionType } from 'src/core/options/options';
510
import type { KeyMap, Replace } from 'src/utils/types';
611

712
// Errors //
@@ -58,14 +63,23 @@ export type PromptPrimitiveType<T extends PromptType = PromptType> =
5863
* @see [GitHub - terkelg/prompts - Prompt Objects](https://github.com/terkelg/prompts#-prompt-objects)
5964
* @group Client
6065
*/
61-
export type PromptParams<T extends PromptType = PromptType> = Replace<
66+
export type PromptParams<
67+
T extends PromptType = PromptType,
68+
TOptionType extends OptionType = OptionType,
69+
> = Replace<
6270
Omit<PromptObject, 'name' | 'separator'>,
6371
{
6472
// make the message property required since prompts throws an error if it's
6573
// not defined.
6674
message: NonNullable<prompts.PromptObject['message']>;
6775
// make the type property optional since we'll default to 'text'
6876
type?: T;
77+
choices?: Replace<
78+
Choice,
79+
{
80+
value?: OptionPrimitiveType<TOptionType>;
81+
}
82+
>[];
6983
}
7084
>;
7185

packages/cli/src/core/options/option-prompt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type OptionPromptTypeMap = KeyMap<
1515
{
1616
array: 'autocompleteMultiselect' | 'list' | 'multiselect';
1717
boolean: 'confirm' | 'toggle';
18-
number: 'number';
18+
number: 'number' | 'select';
1919
secret: 'invisible' | 'password';
2020
string:
2121
| 'autocomplete'
@@ -32,7 +32,7 @@ export type OptionPromptType<T extends OptionType> = OptionPromptTypeMap[T];
3232
// Functions + Function Types //
3333

3434
export type OptionPromptParams<T extends OptionConfig = OptionConfig> = Replace<
35-
PromptParams,
35+
PromptParams<OptionPromptType<T['type']>, T['type']>,
3636
{
3737
/**
3838
* The name of the option.

packages/cli/src/core/options/options-getter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ type OptionGetterParams<T extends OptionConfig = OptionConfig> = Pick<
3434
/**
3535
* The prompt to show the user if no value is provided (optional).
3636
*/
37-
prompt?: string | Omit<PromptParams<OptionPromptType<T['type']>>, 'validate'>;
37+
prompt?:
38+
| string
39+
| Omit<PromptParams<OptionPromptType<T['type']>, T['type']>, 'validate'>;
3840
};
3941

4042
/**

packages/cli/src/utils/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type Eval<T> = { [K in keyof T]: T[K] } & unknown;
2929
/**
3030
* Replace properties in `T` with properties in `U`.
3131
*/
32-
export type Replace<T, U> = Omit<T, keyof U> & U;
32+
export type Replace<T, U extends Partial<T> | {}> = Omit<T, keyof U> & U;
3333

3434
/**
3535
* The opposite of {@linkcode Readonly<T>}. Make all properties in `T` mutable.

0 commit comments

Comments
 (0)