Skip to content

Commit 9e5bc6c

Browse files
authored
feat: add support for signals to prompts (#340)
1 parent df4eea1 commit 9e5bc6c

27 files changed

+407
-0
lines changed

.changeset/mean-years-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/prompts": minor
3+
---
4+
5+
Add support for signals in prompts, allowing them to be aborted.

packages/prompts/src/autocomplete.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
8383
filter: (search: string, opt: Option<Value>) => {
8484
return getFilteredOption(search, opt);
8585
},
86+
signal: opts.signal,
8687
input: opts.input,
8788
output: opts.output,
8889
validate: opts.validate,
@@ -230,6 +231,7 @@ export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOpti
230231
return undefined;
231232
},
232233
initialValue: opts.initialValues,
234+
signal: opts.signal,
233235
input: opts.input,
234236
output: opts.output,
235237
render() {

packages/prompts/src/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ export const symbol = (state: State) => {
4949
export interface CommonOptions {
5050
input?: Readable;
5151
output?: Writable;
52+
signal?: AbortSignal;
5253
}

packages/prompts/src/confirm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const confirm = (opts: ConfirmOptions) => {
2121
return new ConfirmPrompt({
2222
active,
2323
inactive,
24+
signal: opts.signal,
2425
input: opts.input,
2526
output: opts.output,
2627
initialValue: opts.initialValue ?? true,

packages/prompts/src/group-multi-select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) =>
7878

7979
return new GroupMultiSelectPrompt({
8080
options: opts.options,
81+
signal: opts.signal,
8182
input: opts.input,
8283
output: opts.output,
8384
initialValues: opts.initialValues,

packages/prompts/src/multi-select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export const multiselect = <Value>(opts: MultiSelectOptions<Value>) => {
5353

5454
return new MultiSelectPrompt({
5555
options: opts.options,
56+
signal: opts.signal,
5657
input: opts.input,
5758
output: opts.output,
5859
initialValues: opts.initialValues,

packages/prompts/src/password.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const password = (opts: PasswordOptions) => {
1111
return new PasswordPrompt({
1212
validate: opts.validate,
1313
mask: opts.mask ?? S_PASSWORD_MASK,
14+
signal: opts.signal,
1415
input: opts.input,
1516
output: opts.output,
1617
render() {

packages/prompts/src/select-key.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const selectKey = <Value extends string>(opts: SelectOptions<Value>) => {
2727

2828
return new SelectKeyPrompt({
2929
options: opts.options,
30+
signal: opts.signal,
3031
input: opts.input,
3132
output: opts.output,
3233
initialValue: opts.initialValue,

packages/prompts/src/select.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const select = <Value>(opts: SelectOptions<Value>) => {
7676

7777
return new SelectPrompt({
7878
options: opts.options,
79+
signal: opts.signal,
7980
input: opts.input,
8081
output: opts.output,
8182
initialValue: opts.initialValue,

packages/prompts/src/spinner.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const spinner = ({
3535
errorMessage,
3636
frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'],
3737
delay = unicode ? 80 : 120,
38+
signal,
3839
}: SpinnerOptions = {}): SpinnerResult => {
3940
const isCI = isCIFn();
4041

@@ -72,6 +73,10 @@ export const spinner = ({
7273
process.on('SIGINT', signalEventHandler);
7374
process.on('SIGTERM', signalEventHandler);
7475
process.on('exit', handleExit);
76+
77+
if (signal) {
78+
signal.addEventListener('abort', signalEventHandler);
79+
}
7580
};
7681

7782
const clearHooks = () => {
@@ -80,6 +85,10 @@ export const spinner = ({
8085
process.removeListener('SIGINT', signalEventHandler);
8186
process.removeListener('SIGTERM', signalEventHandler);
8287
process.removeListener('exit', handleExit);
88+
89+
if (signal) {
90+
signal.removeEventListener('abort', signalEventHandler);
91+
}
8392
};
8493

8594
const clearPrevMessage = () => {

0 commit comments

Comments
 (0)