Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: clear prompt timeout (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Oct 29, 2018
1 parent d2cbb9f commit 5cc36aa
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,19 @@ async function single(options: IPromptConfig): Promise<string> {
function normal(options: IPromptConfig, retries = 100): Promise<string> {
if (retries < 0) throw new Error('no input')
return new Promise((resolve, reject) => {
let timer: NodeJS.Timer
if (options.timeout) {
timer = setTimeout(() => {
process.stdin.pause()
reject(new Error('Prompt timeout'))
}, options.timeout)
timer.unref()
}
process.stdin.setEncoding('utf8')
process.stderr.write(options.prompt)
process.stdin.resume()
process.stdin.once('data', data => {
if (timer) clearTimeout(timer)
process.stdin.pause()
data = data.trim()
if (!options.default && options.required && data === '') {
Expand All @@ -114,11 +123,5 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
resolve(data || options.default)
}
})
if (options.timeout) {
setTimeout(() => {
process.stdin.pause()
reject(new Error('Prompt timeout'))
}, options.timeout).unref()
}
})
}

0 comments on commit 5cc36aa

Please sign in to comment.