diff --git a/generators/app/__snapshots__/generator.spec.ts.snap b/generators/app/__snapshots__/generator.spec.ts.snap index a3f19d927be3..a1144f5b66d5 100644 --- a/generators/app/__snapshots__/generator.spec.ts.snap +++ b/generators/app/__snapshots__/generator.spec.ts.snap @@ -27,7 +27,7 @@ Options: --skip-jhipster-dependencies Don't write jhipster dependencies to package.json. (env: JHI_SKIP_JHIPSTER_DEPENDENCIES) --creation-timestamp Project creation timestamp (used for reproducible builds) --jdl-store JDL store - --prettier-tab-width Default tab width for prettier + --prettier-tab-width Default tab width for prettier (default: 2) --monorepository Use monorepository --skip-commit-hook Skip adding husky commit hooks --db Provide DB name for the application when skipping server side generation @@ -47,14 +47,14 @@ Options: --reactive Generate a reactive backend --service-discovery-type Service discovery type (choices: "consul", "eureka", "no") --auth Provide authentication type for the application when skipping server side generation (choices: "jwt", "oauth2", "session") - --feign-client Generate a feign client + --feign-client Generate a feign client (default: false) --sync-user-with-idp Allow relationships with User for oauth2 applications --message-broker message broker (choices: "kafka", "pulsar", "no") --database-migration Database migration (choices: "liquibase") --with-generated-flag Add a GeneratedByJHipster annotation to all generated java classes and interfaces --package-name The package name for the generated application - --build Provide build tool for the application when skipping server side generation (choices: "maven", "gradle") - --enable-gradle-enterprise Enable Gradle Enterprise integration + --build Provide build tool for the application when skipping server side generation (default: maven) (choices: "maven", "gradle") + --enable-gradle-enterprise Enable Gradle Enterprise integration (default: false) --gradle-enterprise-host Gradle Enterprise Host --incremental-changelog Creates incremental database changelogs --dev-database-type Development database diff --git a/generators/base-core/generator.ts b/generators/base-core/generator.ts index 813ed5978177..27b5d5d27f54 100644 --- a/generators/base-core/generator.ts +++ b/generators/base-core/generator.ts @@ -553,7 +553,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`); } else if (optionDesc.scope !== 'none') { throw new Error(`Scope ${optionDesc.scope} not supported`); } - } else if (optionDesc.default && optionDesc.scope === 'generator' && this[optionName] === undefined) { + } else if (optionDesc.default !== undefined && optionDesc.scope === 'generator' && this[optionName] === undefined) { this[optionName] = optionDesc.default; } }); diff --git a/generators/init/__snapshots__/generator.spec.ts.snap b/generators/init/__snapshots__/generator.spec.ts.snap index 207df51de565..bfca045fdd95 100644 --- a/generators/init/__snapshots__/generator.spec.ts.snap +++ b/generators/init/__snapshots__/generator.spec.ts.snap @@ -16,7 +16,7 @@ Options: --commit-msg Commit changes (implies forceGit) --monorepository Use monorepository --base-name Application base name - --prettier-tab-width Default tab width for prettier + --prettier-tab-width Default tab width for prettier (default: 2) --skip-commit-hook Skip adding husky commit hooks -h, --help display help for command " diff --git a/generators/jdl/__snapshots__/generator.spec.ts.snap b/generators/jdl/__snapshots__/generator.spec.ts.snap index 8fb64cbfa892..990c812e0235 100644 --- a/generators/jdl/__snapshots__/generator.spec.ts.snap +++ b/generators/jdl/__snapshots__/generator.spec.ts.snap @@ -192,7 +192,7 @@ Options: --skip-jhipster-dependencies Don't write jhipster dependencies to package.json. (env: JHI_SKIP_JHIPSTER_DEPENDENCIES) --creation-timestamp Project creation timestamp (used for reproducible builds) --jdl-store JDL store - --prettier-tab-width Default tab width for prettier + --prettier-tab-width Default tab width for prettier (default: 2) --skip-commit-hook Skip adding husky commit hooks --db Provide DB name for the application when skipping server side generation --recreate-initial-changelog Recreate the initial database changelog based on the current config @@ -210,14 +210,14 @@ Options: --reactive Generate a reactive backend --service-discovery-type Service discovery type (choices: "consul", "eureka", "no") --auth Provide authentication type for the application when skipping server side generation (choices: "jwt", "oauth2", "session") - --feign-client Generate a feign client + --feign-client Generate a feign client (default: false) --sync-user-with-idp Allow relationships with User for oauth2 applications --message-broker message broker (choices: "kafka", "pulsar", "no") --database-migration Database migration (choices: "liquibase") --with-generated-flag Add a GeneratedByJHipster annotation to all generated java classes and interfaces --package-name The package name for the generated application - --build Provide build tool for the application when skipping server side generation (choices: "maven", "gradle") - --enable-gradle-enterprise Enable Gradle Enterprise integration + --build Provide build tool for the application when skipping server side generation (default: maven) (choices: "maven", "gradle") + --enable-gradle-enterprise Enable Gradle Enterprise integration (default: false) --gradle-enterprise-host Gradle Enterprise Host --incremental-changelog Creates incremental database changelogs --dev-database-type Development database diff --git a/lib/command/converter.ts b/lib/command/converter.ts index cde5fd2313db..403af9c0bca4 100644 --- a/lib/command/converter.ts +++ b/lib/command/converter.ts @@ -35,6 +35,7 @@ export const convertConfigToOption = (name: string, config?: ConfigSpec): J const choices = config.choices?.map(choice => (typeof choice === 'string' ? choice : choice.value)) as any; return { name, + default: config.default, description: config.description, choices, scope: config.scope ?? 'storage', diff --git a/lib/command/generator-command.spec.ts b/lib/command/generator-command.spec.ts index 95b14e741947..229ae73bca0a 100644 --- a/lib/command/generator-command.spec.ts +++ b/lib/command/generator-command.spec.ts @@ -70,8 +70,13 @@ const expectApplicationTestOption = () => expect(runResult.generator.sharedData. describe('generator commands', () => { for (const scope of ['generator', 'context', 'storage', 'blueprint', 'none'] as const) { describe(`${scope} scoped`, () => { - const checkOptions = (value: any, argument = false) => { - if (argument) { + const checkOptions = ( + value: any, + { argument = false, expectUndefinedOption = false }: { argument?: boolean; expectUndefinedOption?: boolean } = {}, + ) => { + if (expectUndefinedOption) { + expectGeneratorOptionsTestOption().toBe(undefined); + } else if (argument) { // Argument is passed through positionalArguments option. expectGeneratorOptionsTestOption().toBeUndefined(); } else if (typeof value === 'number') { @@ -107,7 +112,7 @@ describe('generator commands', () => { expectBlueprintConfigTestOption().toBe(value); } - if (!['application', 'storage', 'blueprint'].includes(scope)) { + if (!['application', 'context', 'storage', 'blueprint'].includes(scope)) { expectApplicationTestOption().toBeUndefined(); } else if (Array.isArray(value)) { expectApplicationTestOption().toEqual(value); @@ -206,6 +211,95 @@ describe('generator commands', () => { }); }); }); + + if (scope === 'generator') { + describe('cli option with default value', () => { + describe('boolean', () => { + const config: JHipsterConfig = { + cli: { + type: Boolean, + }, + default: true, + scope, + }; + + it('without options', async () => { + await runDummyCli('', config); + checkOptions(true, { expectUndefinedOption: true }); + }); + it('with true option', async () => { + await runDummyCli('--test-option', config); + checkOptions(true); + }); + it('with false option', async () => { + await runDummyCli('--no-test-option', config); + checkOptions(false); + }); + }); + + describe('string', () => { + const config: JHipsterConfig = { + cli: { + type: String, + }, + default: 'foo', + scope, + }; + + it('without options', async () => { + await runDummyCli('', config); + checkOptions('foo', { expectUndefinedOption: true }); + }); + it('with option value', async () => { + await runDummyCli('--test-option 1', config); + checkOptions('1'); + }); + }); + + describe('number', () => { + const config: JHipsterConfig = { + cli: { + type: Number, + }, + default: 1, + scope, + }; + + it('without options', async () => { + await runDummyCli('', config); + checkOptions(1, { expectUndefinedOption: true }); + }); + it('with option value', async () => { + await runDummyCli('--test-option 1', config); + checkOptions(1); + }); + }); + + describe('array', () => { + const config: JHipsterConfig = { + cli: { + type: Array, + }, + default: ['1'], + scope, + }; + + it('without options', async () => { + await runDummyCli('', config); + checkOptions(['1'], { expectUndefinedOption: true }); + }); + it('with option value', async () => { + await runDummyCli('--test-option 1', config); + checkOptions(['1']); + }); + it('with option values', async () => { + await runDummyCli('--test-option 1 2', config); + checkOptions(['1', '2']); + }); + }); + }); + } + describe('cli argument', () => { describe('string', () => { const config: JHipsterConfig = { @@ -217,11 +311,11 @@ describe('generator commands', () => { it('without argument', async () => { await runDummyCli('', config); - checkOptions(undefined, true); + checkOptions(undefined, { argument: true }); }); it('with argument value', async () => { await runDummyCli('1', config); - checkOptions('1', true); + checkOptions('1', { argument: true }); }); }); @@ -235,15 +329,15 @@ describe('generator commands', () => { it('without arguments', async () => { await runDummyCli('', config); - checkOptions(undefined, true); + checkOptions(undefined, { argument: true }); }); it('with argument value', async () => { await runDummyCli('1', config); - checkOptions(['1'], true); + checkOptions(['1'], { argument: true }); }); it('with arguments values', async () => { await runDummyCli('1 2', config); - checkOptions(['1', '2'], true); + checkOptions(['1', '2'], { argument: true }); }); }); });