Skip to content

Commit f5c698a

Browse files
authoredFeb 19, 2024··
fix: clear timeout when prompt times out (#961)
1 parent e2bbb89 commit f5c698a

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed
 

‎src/cli-ux/prompt.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,31 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
3535
input: process.stdin,
3636
output: process.stdout,
3737
})
38+
let timeout: NodeJS.Timeout
39+
if (options.timeout) {
40+
timeout = setTimeout(() => ac.abort(), options.timeout)
41+
signal.addEventListener(
42+
'abort',
43+
() => {
44+
rl.close()
45+
clearTimeout(timeout)
46+
reject(new Error('Prompt timeout'))
47+
},
48+
{once: true},
49+
)
50+
}
3851

3952
rl.question(options.prompt, {signal}, (answer) => {
4053
rl.close()
4154
const data = answer.trim()
4255
if (!options.default && options.required && data === '') {
56+
clearTimeout(timeout)
4357
resolve(normal(options, retries - 1))
4458
} else {
59+
clearTimeout(timeout)
4560
resolve(data || (options.default as string))
4661
}
4762
})
48-
49-
if (options.timeout) {
50-
signal.addEventListener(
51-
'abort',
52-
() => {
53-
reject(new Error('Prompt timeout'))
54-
},
55-
{once: true},
56-
)
57-
58-
setTimeout(() => ac.abort(), options.timeout)
59-
}
6063
})
6164
}
6265

0 commit comments

Comments
 (0)
Please sign in to comment.