Skip to content

Commit

Permalink
fix: clear timeout when prompt times out (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Feb 19, 2024
1 parent e2bbb89 commit f5c698a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/cli-ux/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,31 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
input: process.stdin,
output: process.stdout,
})
let timeout: NodeJS.Timeout
if (options.timeout) {
timeout = setTimeout(() => ac.abort(), options.timeout)
signal.addEventListener(
'abort',
() => {
rl.close()
clearTimeout(timeout)
reject(new Error('Prompt timeout'))
},
{once: true},
)
}

rl.question(options.prompt, {signal}, (answer) => {
rl.close()
const data = answer.trim()
if (!options.default && options.required && data === '') {
clearTimeout(timeout)
resolve(normal(options, retries - 1))
} else {
clearTimeout(timeout)
resolve(data || (options.default as string))
}
})

if (options.timeout) {
signal.addEventListener(
'abort',
() => {
reject(new Error('Prompt timeout'))
},
{once: true},
)

setTimeout(() => ac.abort(), options.timeout)
}
})
}

Expand Down

0 comments on commit f5c698a

Please sign in to comment.