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

Commit

Permalink
feat: add timeout to prompt (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Jun 1, 2018
1 parent 3a292a3 commit 77b6118
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import deps from './deps'
export interface IPromptOptions {
prompt?: string
type?: 'normal' | 'mask' | 'hide'
timeout?: number
/**
* Requires user input if true, otherwise allows empty input
*/
Expand All @@ -20,6 +21,7 @@ interface IPromptConfig {
isTTY: boolean
required: boolean
default?: string
timeout?: number
}

export default {
Expand Down Expand Up @@ -66,7 +68,7 @@ function _prompt(name: string, inputOptions: Partial<IPromptOptions> = {}): Prom

function normal(options: IPromptConfig, retries = 100): Promise<string> {
if (retries < 0) throw new Error('no input')
return new Promise(resolve => {
return new Promise((resolve, reject) => {
process.stdin.setEncoding('utf8')
process.stderr.write(options.prompt)
process.stdin.resume()
Expand All @@ -79,5 +81,6 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
resolve(data || options.default)
}
})
setTimeout(() => reject(), options.timeout || 10000)
})
}
8 changes: 8 additions & 0 deletions test/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ describe('prompt', () => {
await cli.done()
expect(answer).to.equal(undefined)
})

fancy
.stdout()
.stderr()
.end('timeouts with no input', async () => {
const response = await cli.prompt('Require input?', {timeout: 100})
expect(response).to.not.exist
})
})

0 comments on commit 77b6118

Please sign in to comment.