Simplify when logic for prompts.

We now no longer need to check the command line options and previous
prompt responses separately.

I did not manage to get the when functionality from Yeoman (/Inquirer.js)
to work.
This commit is contained in:
Tom Page
2019-06-18 19:11:11 +01:00
parent ff6ca45b1d
commit ae04ba8a07

View File

@@ -42,8 +42,8 @@ module.exports = class extends Generator {
default: 'http' default: 'http'
}, },
{ {
when: function (response) { whenFunction: function (response) {
return response.httpMode == 'http' || commandProps['httpMode'] == 'http'; return response.httpMode == 'http';
}, },
type: 'confirm', type: 'confirm',
name: 'replication', name: 'replication',
@@ -52,9 +52,8 @@ module.exports = class extends Generator {
}, },
// Enterprise only options // Enterprise only options
{ {
when: function (response) { whenFunction: function (response) {
return (response.alfrescoVersion == 'enterprise' || commandProps['alfrescoVersion'] == 'enterprise') && return response.alfrescoVersion == 'enterprise' && !response.replication;
(!response.replication && !commandProps['replication']);
}, },
type: 'confirm', type: 'confirm',
name: 'sharding', name: 'sharding',
@@ -62,9 +61,8 @@ module.exports = class extends Generator {
default: false default: false
}, },
{ {
when: function (response) { whenFunction: function (response) {
return (response.alfrescoVersion == 'enterprise' || commandProps['alfrescoVersion'] == 'enterprise') && return response.alfrescoVersion == 'enterprise' && response.sharding;
(response.sharding || commandProps['sharding']);
}, },
type: 'confirm', type: 'confirm',
name: 'explicitRouting', name: 'explicitRouting',
@@ -72,8 +70,8 @@ module.exports = class extends Generator {
default: false default: false
}, },
{ {
when: function (response) { whenFunction: function (response) {
return response.alfrescoVersion == 'enterprise' || commandProps['alfrescoVersion'] == 'enterprise'; return response.alfrescoVersion == 'enterprise';
}, },
type: 'confirm', type: 'confirm',
name: 'insightEngine', name: 'insightEngine',
@@ -81,9 +79,8 @@ module.exports = class extends Generator {
default: false default: false
}, },
{ {
when: function (response) { whenFunction: function (response) {
return (response.alfrescoVersion == 'enterprise' || commandProps['alfrescoVersion'] == 'enterprise') && return response.alfrescoVersion == 'enterprise' && response.insightEngine;
(response.insightEngine || commandProps['insightEngine']);
}, },
type: 'confirm', type: 'confirm',
name: 'zeppelin', name: 'zeppelin',
@@ -92,24 +89,26 @@ module.exports = class extends Generator {
} }
]; ];
// Read options from command line parameters // Create a chain of promises containing the prompts.
const filteredPrompts = []; this.promise = Promise.resolve();
const commandProps = new Map(); this.props = {};
prompts.forEach(function prompts(prompt) { prompts.forEach(prompt => {
// Check if we can answer the prompt via a command line argument.
const option = this.options[prompt.name]; const option = this.options[prompt.name];
if (option === undefined) { if (option === undefined) {
filteredPrompts.push(prompt); this.promise = this.promise.then(_ => {
// Check if the prompt is valid given the existing settings.
if (!prompt.whenFunction || prompt.whenFunction(this.props)) {
// Display the prompt and update this.props with the response.
return this.prompt(prompt).then(props => Object.assign(this.props, props));
}
});
} else { } else {
commandProps[prompt.name] = normalize(option, prompt); this.props[prompt.name] = option;
} }
}, this);
// Prompt only for parameters not passed by command line
return this.prompt(filteredPrompts).then(props => {
this.props = props;
Object.assign(props, commandProps);
}); });
// Provide Yeoman with the chain of promises so it will wait for answers.
return this.promise;
} }
// Generate boilerplate from "templates" folder // Generate boilerplate from "templates" folder