Skip to content

Commit 7d8d583

Browse files
authored
internal: omit value from prompts if value is not defined (#28228)
1 parent 2a3a51f commit 7d8d583

File tree

1 file changed

+45
-51
lines changed

1 file changed

+45
-51
lines changed

generators/spring-boot/prompts.ts

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import chalk from 'chalk';
21-
import { includes, intersection } from 'lodash-es';
21+
import { intersection } from 'lodash-es';
2222

2323
import {
2424
applicationOptions,
@@ -30,35 +30,18 @@ import {
3030
} from '../../lib/jhipster/index.js';
3131
import { R2DBC_DB_OPTIONS, SQL_DB_OPTIONS } from '../server/support/database.js';
3232
import type CoreGenerator from '../base-core/generator.js';
33+
import { asPromptingTask } from '../base-application/support/task-type-inference.js';
3334

3435
const { OptionNames } = applicationOptions;
3536
const { GATEWAY, MONOLITH } = applicationTypes;
3637
const { CAFFEINE, EHCACHE, HAZELCAST, INFINISPAN, MEMCACHED, REDIS } = cacheTypes;
3738
const { OAUTH2 } = authenticationTypes;
3839
const { CASSANDRA, H2_DISK, H2_MEMORY, MONGODB, NEO4J, SQL, COUCHBASE } = databaseTypes;
39-
const { SERVICE_DISCOVERY_TYPE, WEBSOCKET, SEARCH_ENGINE, ENABLE_SWAGGER_CODEGEN } = OptionNames;
40+
const { WEBSOCKET, SEARCH_ENGINE, ENABLE_SWAGGER_CODEGEN } = OptionNames;
4041
const NO_DATABASE = databaseTypes.NO;
4142
const NO_CACHE_PROVIDER = cacheTypes.NO;
4243
const { GATLING, CUCUMBER } = testFrameworkTypes;
4344

44-
/**
45-
* Get Option From Array
46-
*
47-
* @param {Array} array - array
48-
* @param {any} option - options
49-
* @returns {boolean} true if option is in array and is set to 'true'
50-
*/
51-
const getOptionFromArray = (array, option) => {
52-
let optionValue: any = false;
53-
array.forEach(value => {
54-
if (includes(value, option)) {
55-
optionValue = value.split(':')[1];
56-
}
57-
});
58-
optionValue = optionValue === 'true' ? true : optionValue;
59-
return optionValue;
60-
};
61-
6245
export async function askForServerSideOpts(this: CoreGenerator, { control }) {
6346
if (control.existingProject && !this.options.askAnswered) return;
6447

@@ -187,13 +170,12 @@ export async function askForServerSideOpts(this: CoreGenerator, { control }) {
187170
);
188171
}
189172

190-
export async function askForOptionalItems(this: CoreGenerator, { control }) {
173+
export const askForOptionalItems = asPromptingTask(async function askForOptionalItems(this: CoreGenerator, { control }) {
191174
if (control.existingProject && !this.options.askAnswered) return;
192175

193176
const { applicationType, reactive, databaseType } = this.jhipsterConfigWithDefaults;
194177

195178
const choices: any[] = [];
196-
const defaultChoice = [];
197179
if ([SQL, MONGODB, NEO4J].includes(databaseType)) {
198180
choices.push({
199181
name: 'Elasticsearch as search engine',
@@ -214,54 +196,66 @@ export async function askForOptionalItems(this: CoreGenerator, { control }) {
214196
});
215197
}
216198
}
217-
choices.push({
218-
name: 'Apache Kafka as asynchronous messages broker',
219-
value: 'messageBroker:kafka',
220-
});
221-
choices.push({
222-
name: 'Apache Pulsar as asynchronous messages broker',
223-
value: 'messageBroker:pulsar',
224-
});
225-
choices.push({
226-
name: 'API first development using OpenAPI-generator',
227-
value: 'enableSwaggerCodegen:true',
228-
});
199+
choices.push(
200+
{
201+
name: 'Apache Kafka as asynchronous messages broker',
202+
value: 'messageBroker:kafka',
203+
},
204+
{
205+
name: 'Apache Pulsar as asynchronous messages broker',
206+
value: 'messageBroker:pulsar',
207+
},
208+
{
209+
name: 'API first development using OpenAPI-generator',
210+
value: 'enableSwaggerCodegen:true',
211+
},
212+
);
229213

230214
if (choices.length > 0) {
231-
await this.prompt({
215+
const selectedChoices: string[] = [WEBSOCKET, SEARCH_ENGINE, 'messageBroker', ENABLE_SWAGGER_CODEGEN]
216+
.map(property => [property, this.jhipsterConfig[property]])
217+
.filter(([, value]) => value !== undefined)
218+
.map(([key, value]) => `${key}:${value}`)
219+
.filter(Boolean) as string[];
220+
221+
choices.forEach(choice => {
222+
choice.checked = selectedChoices.includes(choice.value);
223+
});
224+
225+
const answers = await this.prompt({
232226
type: 'checkbox',
233227
name: 'serverSideOptions',
234228
message: 'Which other technologies would you like to use?',
235229
choices,
236-
default: defaultChoice,
237-
}).then(answers => {
238-
this.jhipsterConfig.serverSideOptions = answers.serverSideOptions;
239-
this.jhipsterConfig.websocket = getOptionFromArray(answers.serverSideOptions, WEBSOCKET);
240-
this.jhipsterConfig.searchEngine = getOptionFromArray(answers.serverSideOptions, SEARCH_ENGINE);
241-
this.jhipsterConfig.messageBroker = getOptionFromArray(answers.serverSideOptions, 'messageBroker');
242-
this.jhipsterConfig.enableSwaggerCodegen = getOptionFromArray(answers.serverSideOptions, ENABLE_SWAGGER_CODEGEN);
243-
// Only set this option if it hasn't been set in a previous question, as it's only optional for monoliths
244-
if (!this.jhipsterConfig.serviceDiscoveryType) {
245-
this.jhipsterConfig.serviceDiscoveryType = getOptionFromArray(answers.serverSideOptions, SERVICE_DISCOVERY_TYPE);
246-
}
230+
default: selectedChoices,
247231
});
232+
233+
Object.assign(
234+
this.jhipsterConfig,
235+
Object.fromEntries(
236+
answers.serverSideOptions
237+
.map(it => it.split(':'))
238+
.map(([key, value]) => [key, ['true', 'false'].includes(value) ? value === 'true' : value]),
239+
),
240+
);
248241
}
249-
}
242+
});
250243

251-
export async function askForServerTestOpts(this: CoreGenerator, { control }) {
244+
export const askForServerTestOpts = asPromptingTask(async function (this: CoreGenerator, { control }) {
252245
if (control.existingProject && this.options.askAnswered !== true) return;
253246

247+
const testFrameworks = this.jhipsterConfigWithDefaults.testFrameworks ?? [];
254248
const answers = await this.prompt([
255249
{
256250
type: 'checkbox',
257251
name: 'serverTestFrameworks',
258252
message: 'Besides JUnit, which testing frameworks would you like to use?',
259253
choices: [
260-
{ name: 'Gatling', value: GATLING },
261-
{ name: 'Cucumber', value: CUCUMBER },
254+
{ name: 'Gatling', value: GATLING, checked: testFrameworks.includes(GATLING) },
255+
{ name: 'Cucumber', value: CUCUMBER, checked: testFrameworks.includes(CUCUMBER) },
262256
],
263257
default: intersection([GATLING, CUCUMBER], this.jhipsterConfigWithDefaults.testFrameworks),
264258
},
265259
]);
266260
this.jhipsterConfig.testFrameworks = [...new Set([...(this.jhipsterConfig.testFrameworks ?? []), ...answers.serverTestFrameworks])];
267-
}
261+
});

0 commit comments

Comments
 (0)