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

base-core: fix varargs cli argument handling #27408

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions cli/jhipster-command.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
25 changes: 25 additions & 0 deletions generators/ci-cd/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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",
]
`);
});
});
});
});
9 changes: 6 additions & 3 deletions lib/command/converter.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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[] =>
Expand Down
Loading