From beeca29c8e25927498b38fcc68a9562627491ef9 Mon Sep 17 00:00:00 2001 From: "Nick.Tolhurst" Date: Thu, 17 Sep 2020 13:38:04 +0100 Subject: [PATCH] fix: inquirer.prompt is not called when not necessary --- lib/stencil-init.js | 60 ++++++++++++++++++++++++---------------- lib/stencil-init.spec.js | 2 +- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/stencil-init.js b/lib/stencil-init.js index 4edc60e4..91082126 100644 --- a/lib/stencil-init.js +++ b/lib/stencil-init.js @@ -37,15 +37,30 @@ class StencilInit { * @returns {Promise} */ async run (dotStencilFilePath, cliOptions = {}) { + + const cliConfig = this.getCliConfig(cliOptions); const oldStencilConfig = this.readStencilConfig(dotStencilFilePath); const defaultAnswers = this.getDefaultAnswers(oldStencilConfig); - const answers = await this.askQuestions(defaultAnswers, cliOptions); - const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers); + const questions = this.getQuestions(defaultAnswers, cliConfig); + const answers = questions ? await this.askQuestions(questions) : {}; + const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers, cliConfig); this.saveStencilConfig(updatedStencilConfig, dotStencilFilePath); this.logger.log('You are now ready to go! To start developing, run $ ' + 'stencil start'.cyan); } + /** + * @param {{port: (number), url: (string), token: (string)}} cliOptions + * @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}} + */ + getCliConfig (cliOptions) { + return { + normalStoreUrl: cliOptions.url, + accessToken: cliOptions.token, + port: cliOptions.port, + }; + } + /** * @param {string} dotStencilFilePath * @returns {object} @@ -82,17 +97,13 @@ class StencilInit { /** * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers - * @param {{port: (number), url: (string), token: (string)}} cliOptions - * @returns {Promise} + * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliConfig + * @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}} */ - async askQuestions (defaultAnswers, cliOptions) { - var prompts = []; - var cliAnswers = {}; + getQuestions (defaultAnswers, cliConfig) { + const prompts = []; - if(cliOptions.url){ - cliAnswers.normalStoreUrl = cliOptions.url; - } - else{ + if(!cliConfig.normalStoreUrl){ prompts.push({ type: 'input', name: 'normalStoreUrl', @@ -102,10 +113,7 @@ class StencilInit { }); } - if(cliOptions.token){ - cliAnswers.accessToken = cliOptions.token; - } - else{ + if(!cliConfig.accessToken){ prompts.push({ type: 'input', name: 'accessToken', @@ -115,10 +123,7 @@ class StencilInit { }); } - if(cliOptions.port){ - cliAnswers.port = cliOptions.port; - } - else{ + if(!cliConfig.port){ prompts.push({ type: 'input', name: 'port', @@ -136,10 +141,16 @@ class StencilInit { }); } - return { - ...await this.inquirer.prompt(prompts), - ...cliAnswers, - }; + return prompts.length > 0 ? prompts : null; + } + + /** + * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers + * @param {{port: (number), url: (string), token: (string)}} cliOptions + * @returns {Promise} + */ + async askQuestions (questions) { + return await this.inquirer.prompt(questions); } /** @@ -147,10 +158,11 @@ class StencilInit { * @param {object} answers * @returns {object} */ - applyAnswers (stencilConfig, answers) { + applyAnswers (stencilConfig, answers, cliOptions) { return { customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...stencilConfig, + ...cliOptions, ...answers, }; } diff --git a/lib/stencil-init.spec.js b/lib/stencil-init.spec.js index cf9de099..7d8bddd0 100644 --- a/lib/stencil-init.spec.js +++ b/lib/stencil-init.spec.js @@ -93,7 +93,7 @@ describe('StencilInit integration tests', () => { await instance.run(dotStencilFilePath, cliOptions); expect(fsWriteFileSyncStub).toHaveBeenCalledTimes(1); - expect(inquirerPromptStub).toHaveBeenCalledTimes(1); + expect(inquirerPromptStub).toHaveBeenCalledTimes(0); expect(consoleErrorStub).toHaveBeenCalledTimes(0); expect(consoleLogStub).toHaveBeenCalledTimes(1);