Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: omit value from prompts if value is not defined #28228

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 45 additions & 51 deletions generators/spring-boot/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import chalk from 'chalk';
import { includes, intersection } from 'lodash-es';
import { intersection } from 'lodash-es';

import {
applicationOptions,
Expand All @@ -30,35 +30,18 @@ import {
} from '../../lib/jhipster/index.js';
import { R2DBC_DB_OPTIONS, SQL_DB_OPTIONS } from '../server/support/database.js';
import type CoreGenerator from '../base-core/generator.js';
import { asPromptingTask } from '../base-application/support/task-type-inference.js';

const { OptionNames } = applicationOptions;
const { GATEWAY, MONOLITH } = applicationTypes;
const { CAFFEINE, EHCACHE, HAZELCAST, INFINISPAN, MEMCACHED, REDIS } = cacheTypes;
const { OAUTH2 } = authenticationTypes;
const { CASSANDRA, H2_DISK, H2_MEMORY, MONGODB, NEO4J, SQL, COUCHBASE } = databaseTypes;
const { SERVICE_DISCOVERY_TYPE, WEBSOCKET, SEARCH_ENGINE, ENABLE_SWAGGER_CODEGEN } = OptionNames;
const { WEBSOCKET, SEARCH_ENGINE, ENABLE_SWAGGER_CODEGEN } = OptionNames;
const NO_DATABASE = databaseTypes.NO;
const NO_CACHE_PROVIDER = cacheTypes.NO;
const { GATLING, CUCUMBER } = testFrameworkTypes;

/**
* Get Option From Array
*
* @param {Array} array - array
* @param {any} option - options
* @returns {boolean} true if option is in array and is set to 'true'
*/
const getOptionFromArray = (array, option) => {
let optionValue: any = false;
array.forEach(value => {
if (includes(value, option)) {
optionValue = value.split(':')[1];
}
});
optionValue = optionValue === 'true' ? true : optionValue;
return optionValue;
};

export async function askForServerSideOpts(this: CoreGenerator, { control }) {
if (control.existingProject && !this.options.askAnswered) return;

Expand Down Expand Up @@ -187,13 +170,12 @@ export async function askForServerSideOpts(this: CoreGenerator, { control }) {
);
}

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

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

const choices: any[] = [];
const defaultChoice = [];
if ([SQL, MONGODB, NEO4J].includes(databaseType)) {
choices.push({
name: 'Elasticsearch as search engine',
Expand All @@ -214,54 +196,66 @@ export async function askForOptionalItems(this: CoreGenerator, { control }) {
});
}
}
choices.push({
name: 'Apache Kafka as asynchronous messages broker',
value: 'messageBroker:kafka',
});
choices.push({
name: 'Apache Pulsar as asynchronous messages broker',
value: 'messageBroker:pulsar',
});
choices.push({
name: 'API first development using OpenAPI-generator',
value: 'enableSwaggerCodegen:true',
});
choices.push(
{
name: 'Apache Kafka as asynchronous messages broker',
value: 'messageBroker:kafka',
},
{
name: 'Apache Pulsar as asynchronous messages broker',
value: 'messageBroker:pulsar',
},
{
name: 'API first development using OpenAPI-generator',
value: 'enableSwaggerCodegen:true',
},
);

if (choices.length > 0) {
await this.prompt({
const selectedChoices: string[] = [WEBSOCKET, SEARCH_ENGINE, 'messageBroker', ENABLE_SWAGGER_CODEGEN]
.map(property => [property, this.jhipsterConfig[property]])
.filter(([, value]) => value !== undefined)
.map(([key, value]) => `${key}:${value}`)
.filter(Boolean) as string[];

choices.forEach(choice => {
choice.checked = selectedChoices.includes(choice.value);
});

const answers = await this.prompt({
type: 'checkbox',
name: 'serverSideOptions',
message: 'Which other technologies would you like to use?',
choices,
default: defaultChoice,
}).then(answers => {
this.jhipsterConfig.serverSideOptions = answers.serverSideOptions;
this.jhipsterConfig.websocket = getOptionFromArray(answers.serverSideOptions, WEBSOCKET);
this.jhipsterConfig.searchEngine = getOptionFromArray(answers.serverSideOptions, SEARCH_ENGINE);
this.jhipsterConfig.messageBroker = getOptionFromArray(answers.serverSideOptions, 'messageBroker');
this.jhipsterConfig.enableSwaggerCodegen = getOptionFromArray(answers.serverSideOptions, ENABLE_SWAGGER_CODEGEN);
// Only set this option if it hasn't been set in a previous question, as it's only optional for monoliths
if (!this.jhipsterConfig.serviceDiscoveryType) {
this.jhipsterConfig.serviceDiscoveryType = getOptionFromArray(answers.serverSideOptions, SERVICE_DISCOVERY_TYPE);
}
default: selectedChoices,
});

Object.assign(
this.jhipsterConfig,
Object.fromEntries(
answers.serverSideOptions
.map(it => it.split(':'))
.map(([key, value]) => [key, ['true', 'false'].includes(value) ? value === 'true' : value]),
),
);
}
}
});

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

const testFrameworks = this.jhipsterConfigWithDefaults.testFrameworks ?? [];
const answers = await this.prompt([
{
type: 'checkbox',
name: 'serverTestFrameworks',
message: 'Besides JUnit, which testing frameworks would you like to use?',
choices: [
{ name: 'Gatling', value: GATLING },
{ name: 'Cucumber', value: CUCUMBER },
{ name: 'Gatling', value: GATLING, checked: testFrameworks.includes(GATLING) },
{ name: 'Cucumber', value: CUCUMBER, checked: testFrameworks.includes(CUCUMBER) },
],
default: intersection([GATLING, CUCUMBER], this.jhipsterConfigWithDefaults.testFrameworks),
},
]);
this.jhipsterConfig.testFrameworks = [...new Set([...(this.jhipsterConfig.testFrameworks ?? []), ...answers.serverTestFrameworks])];
}
});
Loading