Skip to content

internal: add types to base-core #29513

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

Merged
merged 4 commits into from
May 20, 2025
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
5 changes: 3 additions & 2 deletions .blueprint/generate-sample/generator.mts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import assert from 'assert';
import { basename, extname, resolve } from 'path';
import { transform } from '@yeoman/transform';
import type { Config } from '../../generators/base-core/types.js';
import BaseGenerator from '../../generators/base-core/index.js';
import { packageJson } from '../../lib/index.js';
import { promptSamplesFolder } from '../support.mjs';
import { GENERATOR_APP, GENERATOR_INFO, GENERATOR_JDL } from '../../generators/generator-list.js';
import { entitiesByType, generateSample } from './support/index.js';
import assert from 'assert';

export default class extends BaseGenerator {
export default class extends BaseGenerator<Config & { entities: string[] }> {
sampleName;
global;
projectFolder;
Expand Down
2 changes: 1 addition & 1 deletion generators/angular/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import type { AngularApplication, AngularEntity } from './types.js';

const { ANGULAR } = clientFrameworkTypes;

export default class AngularGenerator extends BaseApplicationGenerator<DefaultTaskTypes<AngularEntity, AngularApplication>> {
export default class AngularGenerator extends BaseApplicationGenerator<unknown, DefaultTaskTypes<AngularEntity, AngularApplication>> {
async beforeQueue() {
if (!this.fromBlueprint) {
await this.composeWithBlueprints();
Expand Down
4 changes: 2 additions & 2 deletions generators/angular/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export interface AngularEntity extends Entity {
* @param fields returns the import of enums that are referenced by the fields
* @returns {typeImports:Map} the fields that potentially contains some enum types
*/
generateEntityClientEnumImports: (fields: any) => Map<any, any>;
generateEntityClientEnumImports?: (fields: any) => Map<any, any>;
}

export type AngularApplication = {
/** @experimental to be replaced with needles */
angularEntities: AngularEntity[];
angularEntities?: AngularEntity[];
} & ApplicationType<AngularEntity>;
7 changes: 4 additions & 3 deletions generators/base-application/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ const PRIORITY_WITH_APPLICATION: string[] = [
* This is the base class for a generator that generates entities.
*/
export default class BaseApplicationGenerator<
ConfigType = unknown,
TaskTypes extends DefaultTaskTypes<any, any> = DefaultTaskTypes,
> extends BaseGenerator<TaskTypes> {
Options = unknown,
Features = unknown,
> extends BaseGenerator<ConfigType & ApplicationConfiguration, TaskTypes, Options, Features> {
static CONFIGURING_EACH_ENTITY = asPriority(CONFIGURING_EACH_ENTITY);

static LOADING_ENTITIES = asPriority(LOADING_ENTITIES);
Expand All @@ -133,8 +136,6 @@ export default class BaseApplicationGenerator<

static POST_WRITING_ENTITIES = asPriority(POST_WRITING_ENTITIES);

declare jhipsterConfig: ApplicationConfiguration & Record<string, any>;

constructor(args: string | string[], options: JHipsterGeneratorOptions, features: JHipsterGeneratorFeatures) {
super(args, options, features);

Expand Down
31 changes: 19 additions & 12 deletions generators/base-application/support/task-type-inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,52 @@
import type { ApplicationType } from '../../../lib/types/application/application.js';
import type { TaskTypes } from '../../../lib/types/application/tasks.js';
import type CoreGenerator from '../../base-core/generator.js';
import type BaseApplicationGenerator from '../../base-application/generator.js';
import type { WriteFileSection } from '../../base/api.js';
import type { Entity } from '../../../lib/types/application/entity.js';

export function asWriteFilesSection<Data = ApplicationType<Entity>>(section: WriteFileSection<Data>) {
return section;
}

export function asPromptingTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = CoreGenerator>(
export function asPromptingTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = BaseApplicationGenerator>(
task: (this: G, params: TaskTypes<E, A>['PromptingTaskParam']) => void,
) {
return task;
}

export function asPostPreparingEachEntityTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = CoreGenerator>(
task: (this: G, params: TaskTypes<E, A>['PostPreparingEachEntityTaskParam']) => void,
) {
export function asPostPreparingEachEntityTask<
E = Entity,
A = ApplicationType<E>,
const G extends BaseApplicationGenerator = BaseApplicationGenerator,
>(task: (this: G, params: TaskTypes<E, A>['PostPreparingEachEntityTaskParam']) => void) {
return task;
}

export function asWritingTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = CoreGenerator>(
export function asWritingTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = BaseApplicationGenerator>(
task: (this: G, params: TaskTypes<E, A>['WritingTaskParam']) => void,
) {
return task;
}

export function asWritingEntitiesTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = CoreGenerator>(
task: (this: G, params: TaskTypes<E, A>['WritingEntitiesTaskParam']) => void,
) {
export function asWritingEntitiesTask<
E = Entity,
A = ApplicationType<E>,
const G extends BaseApplicationGenerator = BaseApplicationGenerator,
>(task: (this: G, params: TaskTypes<E, A>['WritingEntitiesTaskParam']) => void) {
return task;
}

export function asPostWritingTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = CoreGenerator>(
export function asPostWritingTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = BaseApplicationGenerator>(
task: (this: G, params: TaskTypes<E, A>['PostWritingTaskParam']) => void,
) {
return task;
}

export function asPostWritingEntitiesTask<E = Entity, A = ApplicationType<E>, const G extends CoreGenerator = CoreGenerator>(
task: (this: G, params: TaskTypes<E, A>['PostWritingEntitiesTaskParam']) => void,
) {
export function asPostWritingEntitiesTask<
E = Entity,
A = ApplicationType<E>,
const G extends BaseApplicationGenerator = BaseApplicationGenerator,
>(task: (this: G, params: TaskTypes<E, A>['PostWritingEntitiesTaskParam']) => void) {
return task;
}
23 changes: 13 additions & 10 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ import { dockerPlaceholderGenerator } from '../docker/utils.js';
import { getConfigWithDefaults } from '../../lib/jhipster/index.js';
import { extractArgumentsFromConfigs } from '../../lib/command/index.js';
import type BaseApplicationGenerator from '../base-application/generator.js';
import type { ApplicationConfiguration } from '../../lib/types/application/yo-rc.js';
import type { CleanupArgumentType, Control } from '../base/types.js';
import type { Config } from '../base-core/types.js';
import type { GenericTaskGroup } from '../../lib/types/base/tasks.js';
import { convertWriteFileSectionsToBlocks } from './internal/index.js';

Expand Down Expand Up @@ -113,7 +113,10 @@ const deepMerge = (source1: any, source2: any) => mergeWith({}, source1, source2
/**
* This is the base class for a generator for every generator.
*/
export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOptions, JHipsterGeneratorFeatures> {
export default class CoreGenerator<ConfigType extends Config = Config, Options = unknown, Features = unknown> extends YeomanGenerator<
JHipsterGeneratorOptions & Options,
JHipsterGeneratorFeatures & Features
> {
static asPriority = asPriority;

static INITIALIZING = asPriority(INITIALIZING);
Expand Down Expand Up @@ -154,7 +157,7 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
relative = posixRelative;

readonly logger: Logger;
jhipsterConfig!: Record<string, any>;
jhipsterConfig!: ConfigType;
/**
* @deprecated
*/
Expand Down Expand Up @@ -190,7 +193,7 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
this._config = this._getStorage('generator-jhipster');

/* JHipster config using proxy mode used as a plain object instead of using get/set. */
this.jhipsterConfig = this.config.createProxy();
this.jhipsterConfig = this.config.createProxy() as ConfigType;

/* Options parsing must be executed after forcing jhipster storage namespace and after sharedData have been populated */
this.parseJHipsterOptions(baseCommand.options);
Expand Down Expand Up @@ -341,7 +344,7 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
/**
* JHipster config with default values fallback
*/
get jhipsterConfigWithDefaults(): Readonly<ApplicationConfiguration & Record<string, any>> {
get jhipsterConfigWithDefaults(): Readonly<ConfigType> {
const configWithDefaults = getConfigWithDefaults(removeFieldsWithNullishValues(this.config.getAll()));
defaults(configWithDefaults, {
skipFakeData: false,
Expand All @@ -350,7 +353,7 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
autoCrlf: false,
pages: [],
});
return configWithDefaults as ApplicationConfiguration;
return configWithDefaults as Readonly<ConfigType>;
}

/**
Expand Down Expand Up @@ -768,11 +771,11 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
}
} else {
// Get and store lastLiquibaseTimestamp, a future timestamp can be used
let lastLiquibaseTimestamp = this.jhipsterConfig.lastLiquibaseTimestamp;
const lastLiquibaseTimestamp = this.jhipsterConfig.lastLiquibaseTimestamp;
if (lastLiquibaseTimestamp) {
lastLiquibaseTimestamp = new Date(lastLiquibaseTimestamp);
if (lastLiquibaseTimestamp >= now) {
now = lastLiquibaseTimestamp;
const lastTimestampDate = new Date(lastLiquibaseTimestamp);
if (lastTimestampDate >= now) {
now = lastTimestampDate;
now.setSeconds(now.getSeconds() + 1);
now.setMilliseconds(0);
}
Expand Down
24 changes: 24 additions & 0 deletions generators/base-core/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2013-2025 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.
*/
export type Config = {
jhipsterVersion?: string;
autoCrlf?: boolean;
lastLiquibaseTimestamp?: number;
blueprints?: { name: string; version?: string }[];
};
2 changes: 1 addition & 1 deletion generators/base-entity-changes/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type TaskTypes = ApplicationTaskTypes & {
/**
* This is the base class for a generator for every generator.
*/
export default abstract class GeneratorBaseEntityChanges extends GeneratorBaseApplication<TaskTypes> {
export default abstract class GeneratorBaseEntityChanges extends GeneratorBaseApplication<unknown, TaskTypes> {
recreateInitialChangelog!: boolean;
private entityChanges!: any[];

Expand Down
12 changes: 11 additions & 1 deletion generators/base-workspaces/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ type WorkspacesTypes<E extends Entity = Entity, A extends ApplicationType<E> = A
/**
* This is the base class for a generator that generates entities.
*/
export default abstract class BaseWorkspacesGenerator extends BaseGenerator<WorkspacesTypes> {
export default abstract class BaseWorkspacesGenerator<Config = unknown> extends BaseGenerator<
Config & {
appsFolders: string[];
directoryPath: string;
deploymentType: string;
jwtSecretKey: string;
adminPassword: string;
serviceDiscoveryType: string;
},
WorkspacesTypes
> {
static PROMPTING_WORKSPACES = BaseGenerator.asPriority(PROMPTING_WORKSPACES);

static CONFIGURING_WORKSPACES = BaseGenerator.asPriority(CONFIGURING_WORKSPACES);
Expand Down
4 changes: 2 additions & 2 deletions generators/base-workspaces/internal/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import { defaults } from 'lodash-es';
import { applicationOptions, deploymentOptions } from '../../../lib/jhipster/index.js';
import { loadDerivedPlatformConfig, loadDerivedServerAndPlatformProperties, loadPlatformConfig } from '../../server/support/index.js';
import type { GeneratorBaseCore } from '../../index.js';
import type BaseWorkspacesGenerator from '../index.js';

const { OptionNames } = applicationOptions;
const { Options: DeploymentOptions } = deploymentOptions;

const { JWT_SECRET_KEY } = OptionNames;

export function loadDeploymentConfig(
this: GeneratorBaseCore,
this: BaseWorkspacesGenerator,
{
config = defaults({}, this.jhipsterConfig, DeploymentOptions.defaults(this.jhipsterConfig.deploymentType)),
deployment = this,
Expand Down
8 changes: 7 additions & 1 deletion generators/base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { union } from 'lodash-es';
import { packageJson } from '../../lib/index.js';
import CoreGenerator from '../base-core/index.js';
import type { TaskTypes as BaseTaskTypes, GenericTaskGroup } from '../../lib/types/base/tasks.js';
import type { Config } from '../base-core/types.js';
import { packageNameToNamespace } from './support/index.js';
import { loadBlueprintsFromConfiguration, mergeBlueprints, normalizeBlueprintName, parseBluePrints } from './internal/index.js';
import { PRIORITY_NAMES } from './priorities.js';
Expand All @@ -35,7 +36,12 @@ import { LOCAL_BLUEPRINT_PACKAGE_NAMESPACE } from './support/constants.js';
/**
* Base class that contains blueprints support.
*/
export default class JHipsterBaseBlueprintGenerator<TaskTypes extends BaseTaskTypes = BaseTaskTypes> extends CoreGenerator {
export default class JHipsterBaseBlueprintGenerator<
ConfigType = unknown,
TaskTypes extends BaseTaskTypes = BaseTaskTypes,
Options = unknown,
Features = unknown,
> extends CoreGenerator<ConfigType & Config, Options, Features> {
fromBlueprint!: boolean;
sbsBlueprint?: boolean;
delegateToBlueprint?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion generators/bootstrap/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default class BootstrapGenerator extends BaseGenerator {
await createPrettierTransform.call(this, {
ignoreErrors,
prettierPackageJson: true,
prettierJava: !this.jhipsterConfig.skipServer,
prettierJava: !(this.jhipsterConfig as any).skipServer,
extensions: this.prettierExtensions.join(','),
prettierOptions: this.prettierOptions,
skipForks: this.skipForks,
Expand Down
2 changes: 1 addition & 1 deletion generators/client/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class JHipsterClientGenerator extends BaseApplicationGenerator {
if (this.jhipsterConfig.devServerPort !== undefined || this.jhipsterConfig.applicationIndex === undefined) return;

const { applicationIndex, devServerPort } = this.jhipsterConfigWithDefaults;
this.jhipsterConfig.devServerPort = devServerPort + applicationIndex;
this.jhipsterConfig.devServerPort = devServerPort! + applicationIndex!;
},
});
}
Expand Down
8 changes: 2 additions & 6 deletions generators/git/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ import type { QueuedAdapter } from '@yeoman/types';
import BaseGenerator from '../base/index.js';
import { files } from './files.js';

/**
* @class
* @extends {BaseGenerator}
*/
export default class GitGenerator extends BaseGenerator {
export default class GitGenerator extends BaseGenerator<{ baseName?: string; monorepository?: boolean }> {
gitInitialized!: boolean;
skipGit!: boolean;
forceGit!: boolean;
Expand Down Expand Up @@ -68,7 +64,7 @@ export default class GitGenerator extends BaseGenerator {
async preparing() {
if (!this.skipGit) {
// Force write .yo-rc.json to disk, it's used to check if the application is regenerated
this.jhipsterConfig.skipGit = undefined;
this.jhipsterConfig.monorepository ??= undefined;
}
},
});
Expand Down
28 changes: 13 additions & 15 deletions generators/info/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@
*/
import chalk from 'chalk';

import BaseApplicationGenerator from '../base-application/index.js';
import BaseCoreGenerator from '../base-core/index.js';
import JSONToJDLEntityConverter from '../../lib/jdl/converters/json-to-jdl-entity-converter.js';
import JSONToJDLOptionConverter from '../../lib/jdl/converters/json-to-jdl-option-converter.js';
import type { JHipsterGeneratorFeatures, JHipsterGeneratorOptions } from '../base/api.js';
import { YO_RC_FILE } from '../generator-constants.js';
import { JHIPSTER_CONFIG_DIR, YO_RC_FILE } from '../generator-constants.js';
import { applicationsLookup } from '../workspaces/support/applications-lookup.js';
import type { Entity } from '../../lib/types/base/entity.js';
import { convertFieldBlobType } from '../../lib/application/field-types.js';
import { getEntitiesFromDir } from '../base-application/support/index.js';
import type { Config } from '../base-core/types.js';
import { replaceSensitiveConfig } from './support/utils.js';

const isInfoCommand = commandName => commandName === 'info' || undefined;

export default class InfoGenerator extends BaseApplicationGenerator {
export default class InfoGenerator extends BaseCoreGenerator<Config & { appsFolders?: string[]; baseName?: string; packages?: string[] }> {
constructor(args: string | string[], options: JHipsterGeneratorOptions, features: JHipsterGeneratorFeatures) {
super(args, options, {
jhipsterBootstrap: false,
Expand All @@ -44,8 +45,8 @@ export default class InfoGenerator extends BaseApplicationGenerator {
});
}

get [BaseApplicationGenerator.INITIALIZING]() {
return this.asInitializingTaskGroup({
get [BaseCoreGenerator.INITIALIZING]() {
return this.asAnyTaskGroup({
sayHello() {
this.log.log(chalk.white('Welcome to the JHipster Information Sub-Generator\n'));
},
Expand Down Expand Up @@ -143,16 +144,13 @@ export default class InfoGenerator extends BaseApplicationGenerator {
let jdlObject;
const entities = new Map<string, Entity>();
try {
this.getExistingEntities().forEach(({ name, definition: entity }) => {
if (entity.fields) {
for (const field of entity.fields) {
if (field.fieldType === 'byte[]') {
convertFieldBlobType(field);
}
}
const foundEntities = getEntitiesFromDir(this.destinationPath(JHIPSTER_CONFIG_DIR));
for (const entity of foundEntities) {
const entityJson = this.readDestinationJSON(this.destinationPath(JHIPSTER_CONFIG_DIR, `${entity}.json`));
if (entityJson) {
entities.set(entity, entityJson);
}
entities.set(name, entity);
});
}
jdlObject = JSONToJDLEntityConverter.convertEntitiesToJDL(entities);
JSONToJDLOptionConverter.convertServerOptionsToJDL({ 'generator-jhipster': this.config.getAll() }, jdlObject);
} catch (error) {
Expand Down
Loading
Loading