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

feat: add timeout to prompt #34

Merged
merged 2 commits into from Jun 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/prompt.ts
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
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
})
})