diff --git a/.blueprint/cli/commands.mjs b/.blueprint/cli/commands.mjs index baec01a..30ba54a 100644 --- a/.blueprint/cli/commands.mjs +++ b/.blueprint/cli/commands.mjs @@ -22,6 +22,10 @@ const defaultCommands = { desc: 'Generate a test sample', blueprint: '@jhipster/jhipster-dev', }, + 'github-build-matrix': { + desc: 'Build a matrix of jobs for github actions', + blueprint: '@jhipster/jhipster-dev', + }, }; export default defaultCommands; diff --git a/.blueprint/generate-sample/command.mjs b/.blueprint/generate-sample/command.mjs index 4fd9e69..f1c986d 100644 --- a/.blueprint/generate-sample/command.mjs +++ b/.blueprint/generate-sample/command.mjs @@ -16,7 +16,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { readdir } from 'node:fs/promises'; import { GENERATOR_APP } from 'generator-jhipster/generators'; + /** * @type {import('generator-jhipster').JHipsterCommandDefinition} */ @@ -26,6 +28,24 @@ const command = { type: String, }, }, + configs: { + sampleName: { + prompt: gen => ({ + when: !gen.all, + type: 'list', + message: 'which sample do you want to generate?', + choices: async () => readdir(gen.templatePath('samples')), + }), + scope: 'generator', + }, + all: { + description: 'Generate every sample in a workspace', + cli: { + type: Boolean, + }, + scope: 'generator', + }, + }, options: {}, import: [GENERATOR_APP], }; diff --git a/.blueprint/generate-sample/generator.mjs b/.blueprint/generate-sample/generator.mjs index 385c8c2..37b7227 100644 --- a/.blueprint/generate-sample/generator.mjs +++ b/.blueprint/generate-sample/generator.mjs @@ -1,18 +1,24 @@ import { readdir } from 'node:fs/promises'; +import { readFileSync } from 'node:fs'; import BaseGenerator from 'generator-jhipster/generators/base'; -import command from './command.mjs'; export default class extends BaseGenerator { sampleName; + all; + + constructor(args, opts, features) { + super(args, opts, { ...features, jhipsterBootstrap: false }); + } get [BaseGenerator.INITIALIZING]() { return this.asInitializingTaskGroup({ + async parseCommand() { + await this.parseCurrentJHipsterCommand(); + }, async initializeOptions() { - this.parseJHipsterArguments(command.arguments); if (this.sampleName && !this.sampleName.endsWith('.jdl')) { this.sampleName += '.jdl'; } - this.parseJHipsterOptions(command.options); }, }); } @@ -20,15 +26,7 @@ export default class extends BaseGenerator { get [BaseGenerator.PROMPTING]() { return this.asPromptingTaskGroup({ async askForSample() { - if (!this.sampleName) { - const answers = await this.prompt({ - type: 'list', - name: 'sampleName', - message: 'which sample do you want to generate?', - choices: async () => readdir(this.templatePath('samples')), - }); - this.sampleName = answers.sampleName; - } + await this.promptCurrentJHipsterCommand(); }, }); } @@ -36,7 +34,11 @@ export default class extends BaseGenerator { get [BaseGenerator.WRITING]() { return this.asWritingTaskGroup({ async copySample() { - this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true }); + if (this.all) { + this.copyTemplate('samples/*.jdl', ''); + } else { + this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true }); + } }, }); } @@ -44,13 +46,17 @@ export default class extends BaseGenerator { get [BaseGenerator.END]() { return this.asEndTaskGroup({ async generateSample() { + const packageJson = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url))); + const projectVersion = `${packageJson.version}-git`; + await this.composeWithJHipster('jdl', { - generatorArgs: [this.sampleName], + generatorArgs: this.all ? await readdir(this.templatePath('samples')) : [this.sampleName], generatorOptions: { skipJhipsterDependencies: true, insight: false, skipChecks: true, - skipInstall: true, + projectVersion, + ...(this.all ? { workspaces: true, monorepository: true } : { skipInstall: true }), }, }); }, diff --git a/.blueprint/github-build-matrix/build-matrix.mjs b/.blueprint/github-build-matrix/build-matrix.mjs new file mode 100644 index 0000000..d60e02c --- /dev/null +++ b/.blueprint/github-build-matrix/build-matrix.mjs @@ -0,0 +1,22 @@ +import { readdir } from 'fs/promises'; +import { RECOMMENDED_JAVA_VERSION, RECOMMENDED_NODE_VERSION } from 'generator-jhipster'; +import { fromMatrix } from 'generator-jhipster/testing'; + +const defaultMatrix = { + os: ['ubuntu-latest'], + 'node-version': [RECOMMENDED_NODE_VERSION], + 'java-version': [RECOMMENDED_JAVA_VERSION], + 'default-environment': ['prod'], +}; + +export const buildMatrix = async samplesFolder => { + const samples = await readdir(samplesFolder); + return { + include: Object.values( + fromMatrix({ + ...defaultMatrix, + 'sample-name': samples.filter(sample => !sample.includes('disabled')), + }), + ), + }; +}; diff --git a/.blueprint/github-build-matrix/generator.mjs b/.blueprint/github-build-matrix/generator.mjs new file mode 100644 index 0000000..2535ccc --- /dev/null +++ b/.blueprint/github-build-matrix/generator.mjs @@ -0,0 +1,24 @@ +import { existsSync, appendFileSync } from 'node:fs'; +import os from 'node:os'; +import BaseGenerator from 'generator-jhipster/generators/base'; +import { buildMatrix } from './build-matrix.mjs'; + +export default class extends BaseGenerator { + constructor(args, opts, features) { + super(args, opts, { ...features, jhipsterBootstrap: false }); + } + + get [BaseGenerator.WRITING]() { + return this.asWritingTaskGroup({ + async buildMatrix() { + const matrix = await buildMatrix(this.templatePath('../../generate-sample/templates/samples')); + const matrixoutput = `matrix< { diff --git a/package-lock.json b/package-lock.json index aeee3c0..4b19f8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2781,9 +2781,9 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "node_modules/bare-events": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.3.1.tgz", - "integrity": "sha512-sJnSOTVESURZ61XgEleqmP255T6zTYwHPwE4r6SssIh0U9/uDvfpdoJYpVUerJJZH2fueO+CdT8ZT+OC/7aZDA==", + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.4.2.tgz", + "integrity": "sha512-qMKFd2qG/36aA4GwvKq8MxnPgCQAmBWmSyLWsJcbn8v03wvIPQ/hG1Ms8bPzndZxMDoHpxez5VOS+gC9Yi24/Q==", "optional": true }, "node_modules/base64-js": { @@ -4676,9 +4676,9 @@ } }, "node_modules/foreground-child": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.1.1.tgz", - "integrity": "sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.2.0.tgz", + "integrity": "sha512-CrWQNaEl1/6WeZoarcM9LHupTo3RpZO2Pdk1vktwzPiQTsJnAKJmm3TACKeG5UZbWDfaH2AbvYxzP96y0MT7fA==", "dependencies": { "cross-spawn": "^7.0.0", "signal-exit": "^4.0.1" diff --git a/vitest.config.ts b/vitest.config.ts index d46556a..d1fa83c 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -5,5 +5,6 @@ export default defineConfig({ pool: 'forks', hookTimeout: 20000, exclude: [...defaultExclude.filter(val => val !== '**/cypress/**'), '**/templates/**', '**/resources/**'], + setupFiles: ['./vitest.test-setup.ts'], }, }); diff --git a/vitest.test-setup.ts b/vitest.test-setup.ts new file mode 100644 index 0000000..b94ccb0 --- /dev/null +++ b/vitest.test-setup.ts @@ -0,0 +1,6 @@ +import { vi } from 'vitest'; +import { defineDefaults } from 'generator-jhipster/testing'; + +defineDefaults({ + mockFactory: () => vi.fn(), +});