-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathprompt.ts
229 lines (203 loc) · 5.5 KB
/
prompt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { text, confirm, select, multiselect } from "@clack/prompts";
type SelectOption = {
label: string;
value: string;
hint?: string;
};
export const kCancel = Symbol.for("cancel");
export type PromptCommonOptions = {
/**
* Specify how to handle a cancelled prompt (e.g. by pressing Ctrl+C).
*
* Default strategy is `"default"`.
*
* - `"default"` - Resolve the promise with the `default` value or `initial` value.
* - `"undefined`" - Resolve the promise with `undefined`.
* - `"null"` - Resolve the promise with `null`.
* - `"symbol"` - Resolve the promise with a symbol `Symbol.for("cancel")`.
* - `"reject"` - Reject the promise with an error.
*/
cancel?: "reject" | "default" | "undefined" | "null" | "symbol";
};
export type TextPromptOptions = PromptCommonOptions & {
/**
* Specifies the prompt type as text.
* @optional
* @default "text"
*/
type?: "text";
/**
* The default text value.
* @optional
*/
default?: string;
/**
* A placeholder text displayed in the prompt.
* @optional
*/
placeholder?: string;
/**
* The initial text value.
* @optional
*/
initial?: string;
};
export type ConfirmPromptOptions = PromptCommonOptions & {
/**
* Specifies the prompt type as confirm.
*/
type: "confirm";
/**
* The initial value for the confirm prompt.
* @optional
*/
initial?: boolean;
};
export type SelectPromptOptions = PromptCommonOptions & {
/**
* Specifies the prompt type as select.
*/
type: "select";
/**
* The initial value for the select prompt.
* @optional
*/
initial?: string;
/**
* The options to select from. See {@link SelectOption}.
*/
options: (string | SelectOption)[];
};
export type MultiSelectOptions = PromptCommonOptions & {
/**
* Specifies the prompt type as multiselect.
*/
type: "multiselect";
/**
* The options to select from. See {@link SelectOption}.
*/
initial?: string[];
/**
* The options to select from. See {@link SelectOption}.
*/
options: (string | SelectOption)[];
/**
* Whether the prompt requires at least one selection.
*/
required?: boolean;
};
/**
* Defines a combined type for all prompt options.
*/
export type PromptOptions =
| TextPromptOptions
| ConfirmPromptOptions
| SelectPromptOptions
| MultiSelectOptions;
type inferPromptReturnType<T extends PromptOptions> =
T extends TextPromptOptions
? string
: T extends ConfirmPromptOptions
? boolean
: T extends SelectPromptOptions
? T["options"][number] extends SelectOption
? T["options"][number]["value"]
: T["options"][number]
: T extends MultiSelectOptions
? T["options"]
: unknown;
type inferPromptCancalReturnType<T extends PromptOptions> = T extends {
cancel: "reject";
}
? never
: T extends { cancel: "default" }
? inferPromptReturnType<T>
: T extends { cancel: "undefined" }
? undefined
: T extends { cancel: "null" }
? null
: T extends { cancel: "symbol" }
? typeof kCancel
: inferPromptReturnType<T> /* default */;
/**
* Asynchronously prompts the user for input based on specified options.
* Supports text, confirm, select and multi-select prompts.
*
* @param {string} message - The message to display in the prompt.
* @param {PromptOptions} [opts={}] - The prompt options. See {@link PromptOptions}.
* @returns {Promise<inferPromptReturnType<T>>} - A promise that resolves with the user's response, the type of which is inferred from the options. See {@link inferPromptReturnType}.
*/
export async function prompt<
_ = any,
__ = any,
T extends PromptOptions = TextPromptOptions,
>(
message: string,
opts: PromptOptions = {},
): Promise<inferPromptReturnType<T> | inferPromptCancalReturnType<T>> {
const handleCancel = (value: unknown) => {
if (
typeof value !== "symbol" ||
value.toString() !== "Symbol(clack:cancel)"
) {
return value;
}
switch (opts.cancel) {
case "reject": {
const error = new Error("Prompt cancelled.");
error.name = "ConsolaPromptCancelledError";
if (Error.captureStackTrace) {
Error.captureStackTrace(error, prompt);
}
throw error;
}
case "undefined": {
return undefined;
}
case "null": {
return null;
}
case "symbol": {
return kCancel;
}
default:
case "default": {
return (opts as TextPromptOptions).default ?? opts.initial;
}
}
};
if (!opts.type || opts.type === "text") {
return (await text({
message,
defaultValue: opts.default,
placeholder: opts.placeholder,
initialValue: opts.initial as string,
}).then(handleCancel)) as any;
}
if (opts.type === "confirm") {
return (await confirm({
message,
initialValue: opts.initial,
}).then(handleCancel)) as any;
}
if (opts.type === "select") {
return (await select({
message,
options: opts.options.map((o) =>
typeof o === "string" ? { value: o, label: o } : o,
),
initialValue: opts.initial,
}).then(handleCancel)) as any;
}
if (opts.type === "multiselect") {
return (await multiselect({
message,
options: opts.options.map((o) =>
typeof o === "string" ? { value: o, label: o } : o,
),
required: opts.required,
initialValues: opts.initial,
}).then(handleCancel)) as any;
}
throw new Error(`Unknown prompt type: ${opts.type}`);
}