From 7e31a79919bc2c844a03038b119fc46464675e50 Mon Sep 17 00:00:00 2001 From: Philipe Navarro Date: Thu, 31 May 2018 17:08:54 -0700 Subject: [PATCH 1/2] add timeout to prompt --- src/prompt.ts | 5 ++++- test/prompt.test.ts | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/prompt.ts b/src/prompt.ts index a24c949b..fbffe2f9 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -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 */ @@ -20,6 +21,7 @@ interface IPromptConfig { isTTY: boolean required: boolean default?: string + timeout?: number } export default { @@ -66,7 +68,7 @@ function _prompt(name: string, inputOptions: Partial = {}): Prom function normal(options: IPromptConfig, retries = 100): Promise { 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() @@ -79,5 +81,6 @@ function normal(options: IPromptConfig, retries = 100): Promise { resolve(data || options.default) } }) + setTimeout(() => reject(), options.timeout || 10000) }) } diff --git a/test/prompt.test.ts b/test/prompt.test.ts index d6b439b1..26d35331 100644 --- a/test/prompt.test.ts +++ b/test/prompt.test.ts @@ -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: 3000}) + expect(response).to.not.exist + }) }) From 64b9d157988d15e8d47bc2e0ae41f662a0a4c63c Mon Sep 17 00:00:00 2001 From: Philipe Navarro Date: Thu, 31 May 2018 17:12:15 -0700 Subject: [PATCH 2/2] test fixes --- test/prompt.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/prompt.test.ts b/test/prompt.test.ts index 26d35331..7603540d 100644 --- a/test/prompt.test.ts +++ b/test/prompt.test.ts @@ -32,7 +32,7 @@ describe('prompt', () => { .stdout() .stderr() .end('timeouts with no input', async () => { - const response = await cli.prompt('Require input?', {timeout: 3000}) + const response = await cli.prompt('Require input?', {timeout: 100}) expect(response).to.not.exist }) })