Skip to content

Commit

Permalink
feat: init no longer prompts when cli option is present
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
NickTolhurst26 committed Sep 11, 2020
1 parent fc62b19 commit a9fee29
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions lib/stencil-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<object>}
*/
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?',
Expand All @@ -115,8 +133,13 @@ class StencilInit {
return true;
}
},
},
]);
});
}

return {
...await this.inquirer.prompt(prompts),
...cliAnswers
}
}

/**
Expand Down

0 comments on commit a9fee29

Please sign in to comment.