diff --git a/cli/jhipster-command.mjs b/cli/jhipster-command.mjs index 57e9ddeeeb29..4fb8d7cee6a3 100644 --- a/cli/jhipster-command.mjs +++ b/cli/jhipster-command.mjs @@ -18,7 +18,7 @@ */ import chalk from 'chalk'; -import { Command, Option } from 'commander'; +import { Argument, Command, Option } from 'commander'; import { kebabCase } from 'lodash-es'; import { convertConfigToOption } from '../lib/command/index.js'; @@ -167,7 +167,11 @@ export default class JHipsterCommand extends Command { Object.entries(jhipsterArguments ?? {}).forEach(([key, value]) => { let argName = value.type === Array ? `${key}...` : key; argName = value.required ? `<${argName}>` : `[${argName}]`; - this.argument(argName, value.description); + const argument = new Argument(argName, value.description); + if (value.choices) { + argument.choices(value.choices.map(choice => (typeof choice === 'string' ? choice : choice.value))); + } + this.addArgument(argument); }); return this; } diff --git a/generators/base-core/generator.ts b/generators/base-core/generator.ts index 1bb66c8d4a23..4d80c30e92b3 100644 --- a/generators/base-core/generator.ts +++ b/generators/base-core/generator.ts @@ -578,6 +578,8 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`); argument = positionalArguments; positionalArguments = []; } + // Replace varargs empty array with undefined. + argument = Array.isArray(argument) && argument.length === 0 ? undefined : argument; if (argument !== undefined) { const convertedValue = !argumentDef.type || argumentDef.type === Array ? argument : argumentDef.type(argument); if (argumentDef.scope === undefined || argumentDef.scope === 'generator') { diff --git a/generators/ci-cd/generator.spec.ts b/generators/ci-cd/generator.spec.ts index eac371cd8f9f..6b35cb44b424 100644 --- a/generators/ci-cd/generator.spec.ts +++ b/generators/ci-cd/generator.spec.ts @@ -22,6 +22,7 @@ import { describe, expect, it } from 'esmocha'; import { snakeCase } from 'lodash-es'; import { shouldSupportFeatures, testBlueprintSupport } from '../../test/support/tests.js'; +import { defaultHelpers as helpers, runResult } from '../../lib/testing/helpers.js'; import Generator from './index.js'; const __filename = fileURLToPath(import.meta.url); @@ -35,4 +36,28 @@ describe(`generator - ${generator}`, () => { }); shouldSupportFeatures(Generator); describe('blueprint support', () => testBlueprintSupport(generator)); + + describe('questions', () => { + describe('without answers', () => { + before(async () => { + await helpers + .runJHipster('ci-cd') + .withJHipsterConfig() + .withOptions({ + // ciCd argument is a varargs, pass an empty array to check if ciCd prompt is asked + positionalArguments: [[]], + }) + .withSkipWritingPriorities(); + }); + + it('should match order', () => { + expect(runResult.askedQuestions.map(({ name }) => name)).toMatchInlineSnapshot(` +[ + "ciCd", + "ciCdIntegrations", +] +`); + }); + }); + }); }); diff --git a/lib/command/converter.ts b/lib/command/converter.ts index 72ae0666db8c..7ff50a3de5d0 100644 --- a/lib/command/converter.ts +++ b/lib/command/converter.ts @@ -1,7 +1,9 @@ import type { JHipsterOptionDefinition } from '../jdl/core/types/parsing.js'; -import type { ConfigSpec, JHipsterArguments, JHipsterConfigs, JHipsterOption } from './types.js'; +import type { ConfigSpec, JHipsterArguments, JHipsterConfigs, JHipsterOption, JHispterChoices } from './types.js'; -export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined): JHipsterArguments => { +type JHipsterArgumentsWithChoices = JHipsterArguments & { choices?: JHispterChoices }; + +export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined): JHipsterArgumentsWithChoices => { if (!configs) return {}; return Object.fromEntries( Object.entries(configs) @@ -11,10 +13,11 @@ export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined { description: def.description, scope: def.scope, + choices: def.choices, ...def.argument, }, ]), - ) as JHipsterArguments; + ) as JHipsterArgumentsWithChoices; }; export const extractJdlDefinitionFromCommandConfig = (configs: JHipsterConfigs = {}): JHipsterOptionDefinition[] =>