From ab8a7015b74979a17a1b27b54ef1f7927ca9b8f7 Mon Sep 17 00:00:00 2001 From: dellamina Date: Thu, 4 Aug 2022 08:38:10 +0200 Subject: [PATCH] feat(editor): add flag waitUserInput --- README.md | 3 ++- packages/inquirer/examples/editor.js | 1 + packages/inquirer/lib/prompts/editor.js | 6 ++++++ packages/inquirer/test/specs/prompts/editor.js | 9 +++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9002c776f..8f8a37128 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,7 @@ A question object is a `hash` containing question related values: - **suffix**: (String) Change the default _suffix_ message. - **askAnswered**: (Boolean) Force to prompt the question if the answer already exists. - **loop**: (Boolean) Enable list looping. Defaults: `true` +- **waitUserInput**: (Boolean) Flag to enable/disable wait for user input before opening system editor - Defaults: `true` `default`, `choices`(if defined as functions), `validate`, `filter` and `when` functions can be called asynchronously. Either return a promise or use `this.async()` to get a callback you'll call with the final value. @@ -300,7 +301,7 @@ Note that `mask` is required to hide the actual user input. #### Editor - `{type: 'editor'}` -Take `type`, `name`, `message`[, `default`, `filter`, `validate`, `postfix`] properties +Take `type`, `name`, `message`[, `default`, `filter`, `validate`, `postfix`, `waitUserInput`] properties Launches an instance of the users preferred editor on a temporary file. Once the user exits their editor, the contents of the temporary file are read in as the result. The editor to use is determined by reading the $VISUAL or $EDITOR environment variables. If neither of those are present, notepad (on Windows) or vim (Linux or Mac) is used. diff --git a/packages/inquirer/examples/editor.js b/packages/inquirer/examples/editor.js index 3f75cf2ae..1e527ec6f 100644 --- a/packages/inquirer/examples/editor.js +++ b/packages/inquirer/examples/editor.js @@ -16,6 +16,7 @@ const questions = [ return true; }, + waitUserInput: true, }, ]; diff --git a/packages/inquirer/lib/prompts/editor.js b/packages/inquirer/lib/prompts/editor.js index 84030d9f1..c370ecde6 100644 --- a/packages/inquirer/lib/prompts/editor.js +++ b/packages/inquirer/lib/prompts/editor.js @@ -23,6 +23,12 @@ export default class EditorPrompt extends Base { // Open Editor on "line" (Enter Key) const events = observe(this.rl); this.lineSubscription = events.line.subscribe(this.startExternalEditor.bind(this)); + const waitUserInput = + this.opt.waitUserInput === undefined ? true : this.opt.waitUserInput; + + if (!waitUserInput) { + this.startExternalEditor(); + } // Trigger Validation when editor closes const validation = this.handleSubmitEvents(this.editorResult); diff --git a/packages/inquirer/test/specs/prompts/editor.js b/packages/inquirer/test/specs/prompts/editor.js index 363ee9a10..0422644f5 100644 --- a/packages/inquirer/test/specs/prompts/editor.js +++ b/packages/inquirer/test/specs/prompts/editor.js @@ -25,4 +25,13 @@ describe('`editor` prompt', () => { return promise.then((answer) => expect(answer).to.equal('testing')); }); + + it('should open editor without waiting for the user to press enter', function () { + this.fixture.waitUserInput = false; + const prompt = new Editor(this.fixture, this.rl); + + const promise = prompt.run(); + + return promise.then((answer) => expect(answer).to.equal('testing')); + }); });