From a9fee29848877fc054d62a379d7d69fccdd949c9 Mon Sep 17 00:00:00 2001 From: "Nick.Tolhurst" Date: Fri, 11 Sep 2020 14:01:45 +0100 Subject: [PATCH] feat: init no longer prompts when cli option is present Prompts depend on the absence of cli options. The prompt answers and cli options are then merged. Provided cli options do not count as 'defaultAnswers'. --- lib/stencil-init.js | 55 ++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/stencil-init.js b/lib/stencil-init.js index face3ad5..2fedbe0a 100644 --- a/lib/stencil-init.js +++ b/lib/stencil-init.js @@ -38,8 +38,8 @@ class StencilInit { */ async run (dotStencilFilePath, cliOptions = {}) { const oldStencilConfig = this.readStencilConfig(dotStencilFilePath); - const defaultAnswers = this.getDefaultAnswers(oldStencilConfig, cliOptions); - const answers = await this.askQuestions(defaultAnswers); + const defaultAnswers = this.getDefaultAnswers(oldStencilConfig); + const answers = await this.askQuestions(defaultAnswers, cliOptions); const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers); this.saveStencilConfig(updatedStencilConfig, dotStencilFilePath); @@ -70,38 +70,56 @@ class StencilInit { /** * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} stencilConfig - * @param {{port: (number), url: (string), token: (string)}} cliOptions * @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}} */ - getDefaultAnswers (stencilConfig, cliOptions) { + getDefaultAnswers (stencilConfig) { return { - normalStoreUrl: cliOptions.url || stencilConfig.normalStoreUrl, - accessToken: cliOptions.token || stencilConfig.accessToken, - port: cliOptions.port || stencilConfig.port || this.serverConfig.get('/server/port'), + normalStoreUrl: stencilConfig.normalStoreUrl, + accessToken: stencilConfig.accessToken, + port: stencilConfig.port || this.serverConfig.get('/server/port'), }; } /** * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers + * @param {{port: (number), url: (string), token: (string)}} cliOptions * @returns {Promise} */ - async askQuestions (defaultAnswers) { - return await this.inquirer.prompt([ - { + async askQuestions (defaultAnswers, cliOptions) { + var prompts = []; + var cliAnswers = {}; + + if(cliOptions.url){ + cliAnswers.normalStoreUrl = cliOptions.url; + } + else{ + prompts.push({ type: 'input', name: 'normalStoreUrl', message: 'What is the URL of your store\'s home page?', validate: val => /^https?:\/\//.test(val) || 'You must enter a URL', default: defaultAnswers.normalStoreUrl, - }, - { + }); + } + + if(cliOptions.token){ + cliAnswers.accessToken = cliOptions.token; + } + else{ + prompts.push({ type: 'input', name: 'accessToken', message: 'What is your Stencil OAuth Access Token?', default: defaultAnswers.accessToken, filter: val => val.trim(), - }, - { + }); + } + + if(cliOptions.port){ + cliAnswers.port = cliOptions.port; + } + else{ + prompts.push({ type: 'input', name: 'port', message: 'What port would you like to run the server on?', @@ -115,8 +133,13 @@ class StencilInit { return true; } }, - }, - ]); + }); + } + + return { + ...await this.inquirer.prompt(prompts), + ...cliAnswers + } } /**