Skip to content
Open
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
774 changes: 387 additions & 387 deletions generators/generate-blueprint/__snapshots__/generator.spec.ts.snap

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions generators/generate-blueprint/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
LINK_JHIPSTER_DEPENDENCY,
LOCAL_BLUEPRINT_OPTION,
SUB_GENERATORS,
TS,
} from './constants.ts';

const command = {
Expand Down Expand Up @@ -137,6 +138,13 @@ const command = {
},
scope: 'storage',
},
[TS]: {
description: 'Use typescript extension',
cli: {
type: Boolean,
},
scope: 'storage',
},
[LOCAL_BLUEPRINT_OPTION]: {
description: 'Generate a local blueprint',
cli: {
Expand Down
34 changes: 24 additions & 10 deletions generators/generate-blueprint/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const SUB_GENERATORS = 'subGenerators';
export const ADDITIONAL_SUB_GENERATORS = 'additionalSubGenerators';
export const DYNAMIC = 'dynamic';
export const JS = 'js';
export const TS = 'ts';
export const LOCAL_BLUEPRINT_OPTION = 'localBlueprint';
export const CLI_OPTION = 'cli';

Expand All @@ -47,15 +48,20 @@ export const requiredConfig = () => ({});
/**
* Default config that will be used for templates
*/
export const defaultConfig = ({ config = {} }: { config?: any } = {}) => ({
...requiredConfig,
[DYNAMIC]: false,
[JS]: !config[LOCAL_BLUEPRINT_OPTION],
[LOCAL_BLUEPRINT_OPTION]: false,
[CLI_OPTION]: !config[LOCAL_BLUEPRINT_OPTION],
[SUB_GENERATORS]: [] as string[],
[ADDITIONAL_SUB_GENERATORS]: '',
});
export const defaultConfig = ({ config = {} }: { config?: any } = {}) => {
const tsEnabled = config[TS] ?? true;

return {
...requiredConfig,
[DYNAMIC]: false,
[JS]: !config[LOCAL_BLUEPRINT_OPTION] && !tsEnabled,
[TS]: tsEnabled,
[LOCAL_BLUEPRINT_OPTION]: false,
[CLI_OPTION]: !config[LOCAL_BLUEPRINT_OPTION],
[SUB_GENERATORS]: [] as string[],
[ADDITIONAL_SUB_GENERATORS]: '',
};
};

export const defaultSubGeneratorConfig = () => ({
[SBS]: true,
Expand All @@ -75,7 +81,8 @@ export const allGeneratorsConfig = () => ({
[SUB_GENERATORS]: lookupGeneratorsNamespaces(),
[ADDITIONAL_SUB_GENERATORS]: '',
[DYNAMIC]: false,
[JS]: true,
[JS]: false,
[TS]: true,
generators: Object.fromEntries(lookupGeneratorsNamespaces().map(subGenerator => [subGenerator, allSubGeneratorConfig(subGenerator)])),
});

Expand Down Expand Up @@ -110,6 +117,13 @@ export const prompts = () => {
{
when: (answers: any) => !answers[LOCAL_BLUEPRINT_OPTION],
type: 'confirm',
name: TS,
message: 'Do you want to use TypeScript for the blueprint?',
default: true,
},
{
when: (answers: any) => !answers[LOCAL_BLUEPRINT_OPTION] && !answers[TS],
type: 'confirm',
name: CLI_OPTION,
message: 'Add a cli?',
default: CLI_OPTION_DEFAULT_VALUE,
Expand Down
53 changes: 40 additions & 13 deletions generators/generate-blueprint/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import { asWriteFilesSection } from '../base-application/support/index.ts';

import { LOCAL_BLUEPRINT_OPTION } from './constants.ts';
import { LOCAL_BLUEPRINT_OPTION, TS } from './constants.ts';
import type { Application as GenerateBlueprintApplication, TemplateData } from './types.ts';

export const files = asWriteFilesSection<GenerateBlueprintApplication>({
Expand All @@ -34,10 +34,22 @@ export const files = asWriteFilesSection<GenerateBlueprintApplication>({
'tsconfig.json',
'vitest.config.ts',
'vitest.test-setup.ts',
'.blueprint/cli/commands.mjs',
'.blueprint/generate-sample/command.mjs',
'.blueprint/generate-sample/generator.mjs',
'.blueprint/generate-sample/index.mjs',
{
sourceFile: '.blueprint/cli/commands.mjs',
destinationFile: ctx => `.blueprint/cli/commands.${ctx[TS] ? 'ts' : 'mjs'}`,
},
{
sourceFile: '.blueprint/generate-sample/command.mjs',
destinationFile: ctx => `.blueprint/generate-sample/command.${ctx[TS] ? 'ts' : 'mjs'}`,
},
{
sourceFile: '.blueprint/generate-sample/generator.mjs',
destinationFile: ctx => `.blueprint/generate-sample/generator.${ctx[TS] ? 'ts' : 'mjs'}`,
},
{
sourceFile: '.blueprint/generate-sample/index.mjs',
destinationFile: ctx => `.blueprint/generate-sample/index.${ctx[TS] ? 'ts' : 'mjs'}`,
},
// Always write cli for devBlueprint usage
'cli/cli.cjs',
{ sourceFile: 'cli/cli-customizations.cjs', override: false },
Expand All @@ -46,10 +58,22 @@ export const files = asWriteFilesSection<GenerateBlueprintApplication>({
{
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && ctx.githubWorkflows,
templates: [
'.blueprint/github-build-matrix/command.mjs',
'.blueprint/github-build-matrix/generator.mjs',
'.blueprint/github-build-matrix/generator.spec.mjs',
'.blueprint/github-build-matrix/index.mjs',
{
sourceFile: '.blueprint/github-build-matrix/command.mjs',
destinationFile: ctx => `.blueprint/github-build-matrix/command.${ctx[TS] ? 'ts' : 'mjs'}`,
},
{
sourceFile: '.blueprint/github-build-matrix/generator.mjs',
destinationFile: ctx => `.blueprint/github-build-matrix/generator.${ctx[TS] ? 'ts' : 'mjs'}`,
},
{
sourceFile: '.blueprint/github-build-matrix/generator.spec.mjs',
destinationFile: ctx => `.blueprint/github-build-matrix/generator.spec.${ctx[TS] ? 'ts' : 'mjs'}`,
},
{
sourceFile: '.blueprint/github-build-matrix/index.mjs',
destinationFile: ctx => `.blueprint/github-build-matrix/index.${ctx[TS] ? 'ts' : 'mjs'}`,
},
],
},
{
Expand All @@ -73,20 +97,23 @@ export const generatorFiles = asWriteFilesSection<TemplateData>({
path: 'generators/generator',
to: ctx => `${ctx.application.blueprintsPath}${ctx.generator.replaceAll(':', '/generators/')}`,
templates: [
{ sourceFile: 'index.mjs', destinationFile: ctx => `index.${ctx.blueprintMjsExtension}` },
{
sourceFile: 'command.mjs',
sourceFile: ctx => (ctx[TS] ? 'index.mts' : 'index.mjs'),
destinationFile: ctx => `index.${ctx.blueprintMjsExtension}`,
},
{
sourceFile: ctx => (ctx[TS] ? 'command.mts' : 'command.mjs'),
destinationFile: ctx => `command.${ctx.blueprintMjsExtension}`,
override: data => !data.ignoreExistingGenerators,
},
{
sourceFile: 'generator.mjs.jhi',
sourceFile: ctx => (ctx[TS] ? 'generator.mts.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',
sourceFile: ctx => (ctx[TS] ? 'generator.spec.mts' : 'generator.spec.mjs'),
destinationFile: data => `generator.spec.${data.blueprintMjsExtension}`,
override: data => !data.ignoreExistingGenerators,
},
Expand Down
6 changes: 3 additions & 3 deletions generators/generate-blueprint/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ describe(`generator - ${generator}`, () => {
it('should write java files with gradle build tool and match snapshot', () => {
expect(runResult.getStateSnapshot()).toMatchInlineSnapshot(`
{
".blueprint/app/command.mjs": {
".blueprint/app/command.ts": {
"stateCleared": "modified",
},
".blueprint/app/generator.mjs": {
".blueprint/app/generator.ts": {
"stateCleared": "modified",
},
".blueprint/app/index.mjs": {
".blueprint/app/index.ts": {
"stateCleared": "modified",
},
".blueprint/app/templates/template-file-app.ejs": {
Expand Down
49 changes: 32 additions & 17 deletions generators/generate-blueprint/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import { BLUEPRINT_API_VERSION } from '../generator-constants.ts';
import {
DYNAMIC,
GENERATE_SNAPSHOTS,
JS,
LOCAL_BLUEPRINT_OPTION,
PRIORITIES,
TS,
WRITTEN,
allGeneratorsConfig,
defaultConfig,
Expand All @@ -48,7 +50,7 @@ import type {
Options as GenerateBlueprintOptions,
} from './types.ts';

const defaultPublishedFiles = ['generators', '!**/__*', '!**/*.snap', '!**/*.spec.?(c|m)js'];
const defaultPublishedFiles = ['generators', '!**/__*', '!**/*.snap', '!**/*.spec.?(c|m)?(j|t)s'];

export default class extends BaseSimpleApplicationGenerator<
GenerateBlueprintApplication,
Expand Down Expand Up @@ -139,9 +141,14 @@ export default class extends BaseSimpleApplicationGenerator<
if (this.jhipsterConfig[LOCAL_BLUEPRINT_OPTION]) {
this.config.defaults({
[DYNAMIC]: true,
js: false,
[JS]: false,
[TS]: false,
});
}
// Ensure only one of JS or TS is true
if (this.jhipsterConfig[TS] && this.jhipsterConfig[JS]) {
this.config.set(JS, false);
}
},
});
}
Expand Down Expand Up @@ -196,7 +203,11 @@ export default class extends BaseSimpleApplicationGenerator<
prepare({ application }) {
const { cli, cliName, baseName } = application;
application.githubRepository = this.jhipsterConfig.githubRepository ?? `jhipster/generator-jhipster-${baseName}`;
application.blueprintMjsExtension = application.js ? 'js' : 'mjs';
if (application[TS]) {
application.blueprintMjsExtension = 'ts';
} else {
application.blueprintMjsExtension = application[JS] ? 'js' : 'mjs';
}
if (cli) {
application.cliName = cliName ?? `jhipster-${baseName}`;
}
Expand Down Expand Up @@ -315,6 +326,22 @@ export default class extends BaseSimpleApplicationGenerator<
mainDependencies,
this.fetchFromInstalledJHipster('generate-blueprint/resources/package.json'),
);
const devDependencies: Record<string, string> = {
'ejs-lint': mainDependencies['ejs-lint'],
eslint: mainDependencies.eslint,
jiti: mainDependencies.jiti,
globals: mainDependencies.globals,
vitest: mainDependencies.vitest,
prettier: mainDependencies.prettier,
/*
* yeoman-test version is loaded through generator-jhipster peer dependency.
* generator-jhipster uses a fixed version, blueprints must set a compatible range.
*/
'yeoman-test': '>=10',
};
if (application[TS]) {
devDependencies.typescript = mainDependencies.typescript;
}
this.packageJson.merge({
name: `generator-jhipster-${application.baseName}`,
keywords: ['yeoman-generator', 'jhipster-blueprint', BLUEPRINT_API_VERSION],
Expand All @@ -323,24 +350,12 @@ export default class extends BaseSimpleApplicationGenerator<
ejslint: 'ejslint generators/**/*.ejs',
lint: 'eslint .',
'lint-fix': 'npm run ejslint && npm run lint -- --fix',
pretest: 'npm run prettier-check && npm run lint',
pretest: `npm run prettier-check && npm run lint${application[TS] ? ' && tsc --noEmit' : ''}`,
test: 'vitest run',
'update-snapshot': 'vitest run --update',
vitest: 'vitest',
},
devDependencies: {
'ejs-lint': mainDependencies['ejs-lint'],
eslint: mainDependencies.eslint,
jiti: mainDependencies.jiti,
globals: mainDependencies.globals,
vitest: mainDependencies.vitest,
prettier: mainDependencies.prettier,
/*
* yeoman-test version is loaded through generator-jhipster peer dependency.
* generator-jhipster uses a fixed version, blueprints must set a compatible range.
*/
'yeoman-test': '>=10',
},
devDependencies,
engines: {
node: jhipsterPackageJson.engines.node,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default } from './generator.mjs';
export { default as command } from './command.mjs';
export { default } from './generator.<%- ts ? 'ts' : 'mjs' %>';
export { default as command } from './command.<%- ts ? 'ts' : 'mjs' %>';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ describe(`generator - ${generator}`, async () => {
for (const workflow of groups.map(sample => sample.split('.')[0])) {
describe(`with ${workflow}`, () => {
beforeAll(async () => {
await helpers.runJHipster(join(import.meta.dirname, 'index.mjs'), { prepareEnvironment: true }).withArguments(workflow);
await helpers
.runJHipster(join(import.meta.dirname, 'index.<%- ts ? 'ts' : 'mjs' %>'), { prepareEnvironment: true })
.withArguments(workflow);
});

it('should match matrix value', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default } from './generator.mjs';
export { default as command } from './command.mjs';
export { default } from './generator.<%- ts ? 'ts' : 'mjs' %>';
export { default as command } from './command.<%- ts ? 'ts' : 'mjs' %>';
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<%#
Copyright 2013-2026 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
import type { JHipsterCommandDefinition } from 'generator-jhipster';
import { asCommand } from 'generator-jhipster';
<%_ if (!sbs && !customGenerator) { _%>
import { command as jhipsterCommand } from 'generator-jhipster/generators/<%- subGenerator %>';
<%_ } _%>

const command: JHipsterCommandDefinition = {
configs: {
<%_ if (!sbs && !customGenerator) { _%>
...jhipsterCommand.configs,
<%_ } _%>
},
<%_ if (!sbs && !customGenerator) { _%>
arguments: {
...jhipsterCommand.arguments,
},
<%_ } _%>
};

export default asCommand(command);
Loading
Loading