Skip to content

Commit

Permalink
fix: inquirer.prompt is not called when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
NickTolhurst26 committed Sep 17, 2020
1 parent 0b2b328 commit beeca29
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
60 changes: 36 additions & 24 deletions lib/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,30 @@ class StencilInit {
* @returns {Promise<void>}
*/
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}
Expand Down Expand Up @@ -82,17 +97,13 @@ class StencilInit {

/**
* @param {{port: (number), normalStoreUrl: (string), accessToken: (string)}} defaultAnswers
* @param {{port: (number), url: (string), token: (string)}} cliOptions
* @returns {Promise<object>}
* @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',
Expand All @@ -102,10 +113,7 @@ class StencilInit {
});
}

if(cliOptions.token){
cliAnswers.accessToken = cliOptions.token;
}
else{
if(!cliConfig.accessToken){
prompts.push({
type: 'input',
name: 'accessToken',
Expand All @@ -115,10 +123,7 @@ class StencilInit {
});
}

if(cliOptions.port){
cliAnswers.port = cliOptions.port;
}
else{
if(!cliConfig.port){
prompts.push({
type: 'input',
name: 'port',
Expand All @@ -136,21 +141,28 @@ 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<object>}
*/
async askQuestions (questions) {
return await this.inquirer.prompt(questions);
}

/**
* @param {object} stencilConfig
* @param {object} answers
* @returns {object}
*/
applyAnswers (stencilConfig, answers) {
applyAnswers (stencilConfig, answers, cliOptions) {
return {
customLayouts: DEFAULT_CUSTOM_LAYOUTS_CONFIG,
...stencilConfig,
...cliOptions,
...answers,
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/stencil-init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit beeca29

Please sign in to comment.