From 219500ca7929324b509a70ea2766cc7fc5471802 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Tue, 24 Sep 2024 11:27:08 -0300 Subject: [PATCH] generate-blueprint: adjusts (#27406) --- .../app/__snapshots__/generator.spec.ts.snap | 6 ++++ generators/generate-blueprint/files.ts | 13 +++----- generators/generate-blueprint/generator.ts | 33 +++++++++++++++---- .../.github/workflows/samples.yml.ejs | 14 ++++---- .../generator/generator.mjs.jhi.ejs | 2 +- .../generators/generator/index.mjs.ejs | 4 +-- .../generators/bootstrap/generator.ts | 10 +++--- generators/javascript/types.d.ts | 2 ++ lib/command/load.ts | 1 + 9 files changed, 55 insertions(+), 30 deletions(-) diff --git a/generators/app/__snapshots__/generator.spec.ts.snap b/generators/app/__snapshots__/generator.spec.ts.snap index aa4c41b7f4a2..a677a28062b3 100644 --- a/generators/app/__snapshots__/generator.spec.ts.snap +++ b/generators/app/__snapshots__/generator.spec.ts.snap @@ -252,6 +252,7 @@ exports[`generator - app with default config should match snapshot 1`] = ` "cacheProviderRedis": false, "camelizedBaseName": "jhipster", "capitalizedBaseName": "Jhipster", + "cjsExtension": ".cjs", "clientDistDir": "target/classes/static/", "clientFramework": "angular", "clientFrameworkAngular": true, @@ -609,6 +610,7 @@ exports[`generator - app with default config should match snapshot 1`] = ` "messageBrokerPulsar": false, "microfrontend": false, "microfrontends": undefined, + "mjsExtension": ".mjs", "monorepository": undefined, "nativeLanguage": "en", "nativeLanguageDefinition": { @@ -889,6 +891,7 @@ exports[`generator - app with gateway should match snapshot 1`] = ` "cacheProviderRedis": false, "camelizedBaseName": "jhipster", "capitalizedBaseName": "Jhipster", + "cjsExtension": ".cjs", "clientDistDir": "target/classes/static/", "clientFramework": "angular", "clientFrameworkAngular": true, @@ -1242,6 +1245,7 @@ exports[`generator - app with gateway should match snapshot 1`] = ` "messageBrokerPulsar": false, "microfrontend": undefined, "microfrontends": undefined, + "mjsExtension": ".mjs", "monorepository": undefined, "nativeLanguage": "en", "nativeLanguageDefinition": { @@ -1521,6 +1525,7 @@ exports[`generator - app with microservice should match snapshot 1`] = ` "cacheProviderRedis": false, "camelizedBaseName": "jhipster", "capitalizedBaseName": "Jhipster", + "cjsExtension": ".cjs", "clientDistDir": "target/classes/static/", "clientFramework": "no", "clientFrameworkAngular": false, @@ -1879,6 +1884,7 @@ exports[`generator - app with microservice should match snapshot 1`] = ` "messageBrokerPulsar": false, "microfrontend": false, "microfrontends": undefined, + "mjsExtension": ".mjs", "monorepository": undefined, "nativeLanguage": "en", "nativeLanguageDefinition": { diff --git a/generators/generate-blueprint/files.ts b/generators/generate-blueprint/files.ts index 62737bba1fa0..a4e1b967e482 100644 --- a/generators/generate-blueprint/files.ts +++ b/generators/generate-blueprint/files.ts @@ -27,10 +27,7 @@ export const files = asWriteFilesSection({ '.github/workflows/generator.yml', '.prettierignore.jhi.blueprint', { sourceFile: 'eslint.config.js.jhi.blueprint', destinationFile: ctx => `${ctx.eslintConfigFile}.jhi.blueprint` }, - { - sourceFile: 'README.md', - override: data => !data.ignoreExistingGenerators, - }, + 'README.md', 'tsconfig.json', 'vitest.config.ts', '.blueprint/cli/commands.mjs', @@ -68,21 +65,21 @@ export const generatorFiles = asWriteFilesSection({ path: 'generators/generator', to: ctx => `${ctx.application.blueprintsPath}${ctx.generator}`, templates: [ - { sourceFile: 'index.mjs', destinationFile: ctx => (ctx.js ? 'index.js' : 'index.mjs') }, + { sourceFile: 'index.mjs', destinationFile: ctx => `index.${ctx.blueprintMjsExtension}` }, { sourceFile: 'command.mjs', - destinationFile: ctx => (ctx.js ? 'command.js' : 'command.mjs'), + destinationFile: ctx => `command.${ctx.blueprintMjsExtension}`, override: data => !data.ignoreExistingGenerators, }, { sourceFile: 'generator.mjs.jhi', - destinationFile: ctx => (ctx.js ? 'generator.js.jhi' : 'generator.mjs.jhi'), + destinationFile: ctx => `generator.${ctx.blueprintMjsExtension}.jhi`, override: data => !data.ignoreExistingGenerators, }, { condition: data => !data.generator.startsWith('entity') && !data.application[LOCAL_BLUEPRINT_OPTION], sourceFile: 'generator.spec.mjs', - destinationFile: data => (data.js ? 'generator.spec.js' : 'generator.spec.mjs'), + destinationFile: data => `generator.spec.${data.blueprintMjsExtension}`, override: data => !data.ignoreExistingGenerators, }, ], diff --git a/generators/generate-blueprint/generator.ts b/generators/generate-blueprint/generator.ts index ebfce2e531f6..5d38eacb50f4 100644 --- a/generators/generate-blueprint/generator.ts +++ b/generators/generate-blueprint/generator.ts @@ -193,9 +193,10 @@ export default class extends BaseGenerator { preparePath() { this.application.blueprintsPath = this.application[LOCAL_BLUEPRINT_OPTION] ? '.blueprint/' : 'generators/'; }, - prepare() { + prepare({ application }) { const { cli, cliName, baseName } = this.application; this.application.githubRepository = this.jhipsterConfig.githubRepository ?? `jhipster/generator-jhipster-${baseName}`; + application.blueprintMjsExtension = this.application.js ? 'js' : 'mjs'; if (cli) { this.application.cliName = cliName ?? `jhipster-${baseName}`; } @@ -212,7 +213,7 @@ export default class extends BaseGenerator { async cleanup({ control }) { await control.cleanupFiles({ '8.5.1': ['.eslintrc.json'], - '8.7.2': ['vitest.test-setup.ts'], + '8.7.2': ['.eslintignore', 'vitest.test-setup.ts'], }); }, async writing({ application }) { @@ -229,7 +230,7 @@ export default class extends BaseGenerator { }); this.jhipsterConfig.sampleWritten = true; }, - async writingGenerators() { + async writingGenerators({ application }) { if (!this.application[GENERATORS]) return; const { skipWorkflows, ignoreExistingGenerators } = this; for (const generator of Object.keys(this.application[GENERATORS])) { @@ -243,9 +244,9 @@ export default class extends BaseGenerator { const customGenerator = !Object.values(GENERATOR_LIST).includes(generator); const jhipsterGenerator = customGenerator || subGeneratorConfig.sbs ? 'base-application' : generator; const subTemplateData = { + ...application, skipWorkflows, ignoreExistingGenerators, - js: this.application.js, application: this.application, ...defaultSubGeneratorConfig(), ...subGeneratorConfig, @@ -272,12 +273,30 @@ export default class extends BaseGenerator { get postWriting() { return this.asPostWritingTaskGroup({ - upgrade() { + upgrade({ application }) { if (!this.application[GENERATORS]) return; if (!this.isJhipsterVersionLessThan('8.7.2')) return; for (const generator of Object.keys(this.application[GENERATORS])) { - const extension = this.application.js ? 'js' : 'mjs'; - const generatorSpec = `${this.application.blueprintsPath}${generator}/generator.spec.${extension}`; + const commandFile = `${this.application.blueprintsPath}${generator}/command.${application.blueprintMjsExtension}`; + this.editFile(commandFile, content => + content + .replace( + `/** + * @type {import('generator-jhipster').JHipsterCommandDefinition} + */`, + `import { asCommand } from 'generator-jhipster'; +`, + ) + .replace('const command = ', 'export default asCommand(') + .replace( + ` +};`, + '});', + ) + .replace('export default command;', ''), + ); + + const generatorSpec = `${this.application.blueprintsPath}${generator}/generator.spec.${application.blueprintMjsExtension}`; this.editFile(generatorSpec, content => content.replaceAll(/blueprint: '([\w-]*)'/g, "blueprint: ['$1']")); } }, diff --git a/generators/generate-blueprint/templates/.github/workflows/samples.yml.ejs b/generators/generate-blueprint/templates/.github/workflows/samples.yml.ejs index 97c8c71d0618..a64c3a74b8b2 100644 --- a/generators/generate-blueprint/templates/.github/workflows/samples.yml.ejs +++ b/generators/generate-blueprint/templates/.github/workflows/samples.yml.ejs @@ -26,7 +26,7 @@ jobs: npm install ./cli/cli.cjs github-build-matrix samples: - name: ${{ matrix.sample-name }} + name: ${{ matrix.job-name || matrix.sample-name }} runs-on: ubuntu-latest needs: build-matrix defaults: @@ -48,11 +48,9 @@ jobs: maven-cache: true gradle-cache: true binary-dir: ${{ github.workspace }}/generator-jhipster-<%= baseName %>/cli/ - - name: 'Install blueprint' - run: npm install + - run: npm install working-directory: ${{ github.workspace }}/generator-jhipster-<%= baseName %> - - name: 'Generate Project' - run: cli.cjs generate-sample ${{ matrix.sample-name }} --skip-jhipster-dependencies --force + - run: cli.cjs generate-sample ${{ matrix.sample-name }} --skip-jhipster-dependencies ${{ matrix.extra-args }} - uses: jhipster/actions/compare-sample@v0 id: compare if: >- @@ -60,7 +58,7 @@ jobs: !contains(github.event.pull_request.labels.*.name, 'pr: disable-compare') with: generator-path: generator-jhipster-<%= baseName %> - cmd: cli.cjs generate-sample ${{ matrix.sample-name }} --skip-jhipster-dependencies --force --skip-install + cmd: cli.cjs generate-sample ${{ matrix.sample-name }} --skip-jhipster-dependencies --skip-install ${{ matrix.extra-args }} - run: npm run ci:backend:test if: steps.compare.outputs.equals != 'true' id: backend @@ -73,7 +71,7 @@ jobs: - run: npm run ci:e2e:run --if-present if: steps.compare.outputs.equals != 'true' id: e2e - - name: 'BACKEND: Store failure logs' + - name: Store backend test failure logs uses: actions/upload-artifact@v4 if: always() && steps.backend.outcome == 'failure' with: @@ -81,7 +79,7 @@ jobs: path: | ${{ github.workspace }}/app/build/test-results/**/*.xml ${{ github.workspace }}/app/target/surefire-reports - - name: 'E2E: Store failure screenshots' + - name: Store cypress screenshots uses: actions/upload-artifact@v4 if: always() && steps.e2e.outcome == 'failure' with: diff --git a/generators/generate-blueprint/templates/generators/generator/generator.mjs.jhi.ejs b/generators/generate-blueprint/templates/generators/generator/generator.mjs.jhi.ejs index 7f44f41d6fc7..8b394fb3025e 100644 --- a/generators/generate-blueprint/templates/generators/generator/generator.mjs.jhi.ejs +++ b/generators/generate-blueprint/templates/generators/generator/generator.mjs.jhi.ejs @@ -28,7 +28,7 @@ import <%= generatorClass %>Generator from 'generator-jhipster/generators/<%= jhipsterGenerator %>'; <%_ } _%> <%_ if (priorities.find(priority => priority.name === 'initializing')) { _%> -import command from './command.<%- js ? '' : 'm' %>js'; +import command from './command.<%- blueprintMjsExtension %>'; <%_ } _%> <%_ if (application.dynamic) { _%> diff --git a/generators/generate-blueprint/templates/generators/generator/index.mjs.ejs b/generators/generate-blueprint/templates/generators/generator/index.mjs.ejs index dcdc7a41add3..6652acd673b4 100644 --- a/generators/generate-blueprint/templates/generators/generator/index.mjs.ejs +++ b/generators/generate-blueprint/templates/generators/generator/index.mjs.ejs @@ -16,5 +16,5 @@ See the License for the specific language governing permissions and limitations under the License. -%> -export { <%- application.dynamic ? 'createGenerator' : 'default' %> } from './generator.<%- js ? '' : 'm' %>js'; -export { default as command } from './command.<%- js ? '' : 'm' %>js'; +export { <%- application.dynamic ? 'createGenerator' : 'default' %> } from './generator.<%- blueprintMjsExtension %>'; +export { default as command } from './command.<%- blueprintMjsExtension %>'; diff --git a/generators/javascript/generators/bootstrap/generator.ts b/generators/javascript/generators/bootstrap/generator.ts index 33fe1dfc037a..f0de65d17c26 100644 --- a/generators/javascript/generators/bootstrap/generator.ts +++ b/generators/javascript/generators/bootstrap/generator.ts @@ -21,10 +21,6 @@ import BaseApplicationGenerator from '../../../base-application/index.js'; import { GENERATOR_PROJECT_NAME } from '../../../generator-list.js'; export default class BootstrapGenerator extends BaseApplicationGenerator { - constructor(args, options, features) { - super(args, options, { queueCommandTasks: true, ...features }); - } - async beforeQueue() { if (!this.fromBlueprint) { await this.composeWithBlueprints(); @@ -43,6 +39,12 @@ export default class BootstrapGenerator extends BaseApplicationGenerator { this.fetchFromInstalledJHipster('javascript', 'resources', 'package.json'), ); }, + jsExtensions({ applicationDefaults, application }) { + applicationDefaults({ + cjsExtension: application.packageJsonTypeCommonjs ? '.js' : '.cjs', + mjsExtension: application.packageJsonTypeCommonjs ? '.js' : '.mjs', + }); + }, }); } diff --git a/generators/javascript/types.d.ts b/generators/javascript/types.d.ts index 190c9ca53965..dfb2b0e7ea4f 100644 --- a/generators/javascript/types.d.ts +++ b/generators/javascript/types.d.ts @@ -14,6 +14,8 @@ export type JavaScriptSourceType = { export type JavaScriptApplication = JavascriptBootstrapProperties & { packageJsonNodeEngine?: boolean | string; eslintConfigFile?: string; + cjsExtension?: string; + mjsExtension?: string; addPrettierExtensions?: (extensions: string[]) => void; }; diff --git a/lib/command/load.ts b/lib/command/load.ts index c9b349c0e05c..809996554986 100644 --- a/lib/command/load.ts +++ b/lib/command/load.ts @@ -4,6 +4,7 @@ import type { CommandConfigScope, JHipsterConfigs, JHispterChoices } from './typ const prepareChoices = (key: string, choices: JHispterChoices) => choices .map(choice => (typeof choice === 'string' ? { value: choice } : choice)) + .filter(choice => choice.value != null) .map(choice => ({ ...choice, choiceKey: `${key}${upperFirst(choice.value)}` })); const filteredScopeEntries = (commandsConfigs: JHipsterConfigs, scopes: CommandConfigScope[]) =>