diff --git a/bin/stencil-init.js b/bin/stencil-init.js index f64dee3b..eaca9c86 100755 --- a/bin/stencil-init.js +++ b/bin/stencil-init.js @@ -22,4 +22,9 @@ if (!versionCheck()) { const dotStencilFilePath = './.stencil'; const cliOptions = _.pick(Program, ['url', 'token', 'port']); -new StencilInit().run(dotStencilFilePath, cliOptions); +new StencilInit().run(dotStencilFilePath, + { + normalStoreUrl: cliOptions.url, + accessToken: cliOptions.token, + port: cliOptions.port, + }); diff --git a/lib/stencil-init.js b/lib/stencil-init.js index 91082126..af207ffa 100644 --- a/lib/stencil-init.js +++ b/lib/stencil-init.js @@ -31,36 +31,23 @@ class StencilInit { /** * @param {string} dotStencilFilePath * @param {object} cliOptions - * @param {string} cliOptions.url - * @param {string} cliOptions.token + * @param {string} cliOptions.normalStoreUrl + * @param {string} cliOptions.accessToken * @param {number} cliOptions.port * @returns {Promise} */ async run (dotStencilFilePath, cliOptions = {}) { - const cliConfig = this.getCliConfig(cliOptions); const oldStencilConfig = this.readStencilConfig(dotStencilFilePath); const defaultAnswers = this.getDefaultAnswers(oldStencilConfig); - const questions = this.getQuestions(defaultAnswers, cliConfig); - const answers = questions ? await this.askQuestions(questions) : {}; - const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers, cliConfig); + const questions = this.getQuestions(defaultAnswers, cliOptions); + const answers = await this.askQuestions(questions); + const updatedStencilConfig = this.applyAnswers(oldStencilConfig, answers, cliOptions); 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} @@ -97,13 +84,13 @@ class StencilInit { /** * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers - * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliConfig - * @returns {{port: (number), normalStoreUrl: (string), accessToken: (string)}} + * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} cliOptions + * @returns {{object[]}} */ - getQuestions (defaultAnswers, cliConfig) { + getQuestions (defaultAnswers, cliOptions) { const prompts = []; - if(!cliConfig.normalStoreUrl){ + if(!cliOptions.normalStoreUrl){ prompts.push({ type: 'input', name: 'normalStoreUrl', @@ -113,7 +100,7 @@ class StencilInit { }); } - if(!cliConfig.accessToken){ + if(!cliOptions.accessToken){ prompts.push({ type: 'input', name: 'accessToken', @@ -123,7 +110,7 @@ class StencilInit { }); } - if(!cliConfig.port){ + if(!cliOptions.port){ prompts.push({ type: 'input', name: 'port', @@ -141,21 +128,21 @@ class StencilInit { }); } - return prompts.length > 0 ? prompts : null; + return prompts; } /** - * @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers - * @param {{port: (number), url: (string), token: (string)}} cliOptions + * @param {{object[]}} cliOptions * @returns {Promise} */ async askQuestions (questions) { - return await this.inquirer.prompt(questions); + return questions.length ? this.inquirer.prompt(questions) : {}; } /** * @param {object} stencilConfig * @param {object} answers + * @param {{port: (number), url: (string), token: (string)}} cliOptions * @returns {object} */ applyAnswers (stencilConfig, answers, cliOptions) { diff --git a/lib/stencil-init.spec.js b/lib/stencil-init.spec.js index bc4d60d0..83803393 100644 --- a/lib/stencil-init.spec.js +++ b/lib/stencil-init.spec.js @@ -32,9 +32,9 @@ const getAnswers = () => ({ accessToken: "accessToken_from_answers", }); const getCliOptions = () => ({ - url: "https://url-from-cli-options.mybigcommerce.com", + normalStoreUrl: "https://url-from-cli-options.mybigcommerce.com", port: 3002, - token: "accessToken_from_CLI_options", + accessToken: "accessToken_from_CLI_options", }); const getQuestions = () => ([ { @@ -102,12 +102,7 @@ describe('StencilInit integration tests', () => { it('using cli options, should perform all the actions, save the result and inform the user about the successful finish', async () => { const dotStencilFilePath = './test/_mocks/bin/dotStencilFile.json'; const cliOptions = getCliOptions(); - const cliOptionsAsAnswers = { - "normalStoreUrl": cliOptions.url, - "port": cliOptions.port, - "accessToken": cliOptions.token, - }; - const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...cliOptionsAsAnswers }, null, 2); + const expectedResult = JSON.stringify({ customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG, ...cliOptions }, null, 2); const fsWriteFileSyncStub = jest.spyOn(fs, "writeFileSync").mockImplementation(jest.fn()); const inquirerPromptStub = jest.spyOn(inquirer, 'prompt').mockReturnValue({}); const consoleErrorStub = jest.spyOn(console, 'error').mockImplementation(jest.fn()); @@ -189,19 +184,6 @@ describe('StencilInit unit tests', () => { }); }); - describe('getCliConfig ', () => { - it('should return config object with cli option values', async () => { - const instance = getStencilInitInstance(); - const cliOptions = getCliOptions(); - - const res = instance.getCliConfig(cliOptions); - - expect(res.normalStoreUrl).toEqual(cliOptions.url); - expect(res.accessToken).toEqual(cliOptions.token); - expect(res.port).toEqual(cliOptions.port); - }); - }); - describe('readStencilConfig ', () => { it('should return an empty config if the file doesn\'t exist', async () => { const instance = getStencilInitInstance();