Skip to content

Commit 8e9aace

Browse files
authored
Move some checks and asserts where it makes sense (#28382)
* move some checks and asserts where it makes sense * align types * use pick instead of Omit in base-application types * adjust some types * clean type * moved primary key field processing to bootstrap-app instead of bootstrap-app-base * moved primary key field processing to bootstrap-app instead of bootstrap-app-base * duplicate code to avoid breaking API change * dedupe derived primary key array * rolledback move of derived primary key actions
1 parent 0b26382 commit 8e9aace

File tree

15 files changed

+49
-35
lines changed

15 files changed

+49
-35
lines changed

generators/base-application/generator.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ import {
3232
import type {
3333
ConfiguringEachEntityTaskParam,
3434
TaskTypes as DefaultTaskTypes,
35-
EntityTaskParam,
3635
EntityToLoad,
3736
LoadingEntitiesTaskParam,
3837
PostWritingEntitiesTaskParam,
38+
PreparingEachEntityFieldTaskParam,
39+
PreparingEachEntityRelationshipTaskParam,
40+
PreparingEachEntityTaskParam,
3941
PreparingTaskParam,
4042
TaskParamWithApplication,
4143
TaskParamWithEntities,
@@ -482,7 +484,7 @@ export default class BaseApplicationGenerator<
482484
* Get entities to prepare.
483485
* @returns {object[]}
484486
*/
485-
getEntitiesDataToPrepare(): EntityTaskParam[] {
487+
getEntitiesDataToPrepare(): Pick<PreparingEachEntityTaskParam, 'entity' | 'entityName' | 'description'>[] {
486488
return this.sharedData.getEntities().map(({ entityName, ...data }) => ({
487489
description: entityName,
488490
entityName,
@@ -495,7 +497,10 @@ export default class BaseApplicationGenerator<
495497
* Get entities and fields to prepare.
496498
* @returns {object[]}
497499
*/
498-
getEntitiesFieldsDataToPrepare() {
500+
getEntitiesFieldsDataToPrepare(): Pick<
501+
PreparingEachEntityFieldTaskParam,
502+
'entity' | 'entityName' | 'field' | 'fieldName' | 'description'
503+
>[] {
499504
return this.getEntitiesDataToPrepare()
500505
.map(({ entity, entityName, ...data }) => {
501506
if (!entity.fields) return [];
@@ -517,7 +522,10 @@ export default class BaseApplicationGenerator<
517522
* Get entities and relationships to prepare.
518523
* @returns {object[]}
519524
*/
520-
getEntitiesRelationshipsDataToPrepare() {
525+
getEntitiesRelationshipsDataToPrepare(): Pick<
526+
PreparingEachEntityRelationshipTaskParam,
527+
'entity' | 'entityName' | 'relationship' | 'relationshipName' | 'description'
528+
>[] {
521529
return this.getEntitiesDataToPrepare()
522530
.map(({ entity, entityName, ...data }) => {
523531
if (!entity.relationships) return [];

generators/base-application/support/relationship.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const loadEntitiesOtherSide = (entities: Entity[], { application }: { app
8686
relationship.otherEntity = otherEntity;
8787
const otherRelationship = findOtherRelationshipInRelationships(entity.name, relationship, otherEntity.relationships ?? []);
8888
if (otherRelationship) {
89-
relationship.otherRelationship = otherRelationship as Relationship;
89+
relationship.otherRelationship = otherRelationship;
9090
otherRelationship.otherEntityRelationshipName = otherRelationship.otherEntityRelationshipName ?? relationship.relationshipName;
9191
relationship.otherEntityRelationshipName = relationship.otherEntityRelationshipName ?? otherRelationship.relationshipName;
9292
if (

generators/base-core/generator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
280280
*
281281
* @param {string} version - A valid semver version string
282282
*/
283-
isJhipsterVersionLessThan(version) {
283+
isJhipsterVersionLessThan(version: string): boolean {
284284
const jhipsterOldVersion = this.sharedData.getControl().jhipsterOldVersion;
285285
return this.isVersionLessThan(jhipsterOldVersion, version);
286286
}
@@ -289,7 +289,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
289289
* Wrapper for `semver.lt` to check if the oldVersion exists and is less than the newVersion.
290290
* Can be used by blueprints.
291291
*/
292-
isVersionLessThan(oldVersion: string | null, newVersion: string) {
292+
isVersionLessThan(oldVersion: string | null, newVersion: string): boolean {
293293
return oldVersion ? semverLessThan(oldVersion, newVersion) : false;
294294
}
295295

@@ -647,7 +647,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
647647
* Set false to create a changelog date incrementing the last one.
648648
* @return {String} Changelog date.
649649
*/
650-
dateFormatForLiquibase(reproducible?: boolean) {
650+
dateFormatForLiquibase(reproducible?: boolean): string {
651651
const control = this.sharedData.getControl();
652652
reproducible = reproducible ?? Boolean(control.reproducible);
653653
// Use started counter or use stored creationTimestamp if creationTimestamp option is passed
@@ -693,7 +693,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
693693
/**
694694
* Alternative templatePath that fetches from the blueprinted generator, instead of the blueprint.
695695
*/
696-
jhipsterTemplatePath(...path: string[]) {
696+
jhipsterTemplatePath(...path: string[]): string {
697697
let existingGenerator: string;
698698
try {
699699
existingGenerator = this._jhipsterGenerator ?? requireNamespace(this.options.namespace).generator;
@@ -751,7 +751,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
751751
/**
752752
* Remove File
753753
*/
754-
removeFile(...path: string[]) {
754+
removeFile(...path: string[]): string {
755755
const destinationFile = this.destinationPath(...path);
756756
const relativePath = relative((this.env as any).logCwd, destinationFile);
757757
// Delete from memory fs to keep updated.
@@ -771,7 +771,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
771771
* Remove Folder
772772
* @param path
773773
*/
774-
removeFolder(...path: string[]) {
774+
removeFolder(...path: string[]): void {
775775
const destinationFolder = this.destinationPath(...path);
776776
const relativePath = relative((this.env as any).logCwd, destinationFolder);
777777
// Delete from memory fs to keep updated.
@@ -789,7 +789,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
789789
/**
790790
* Fetch files from the generator-jhipster instance installed
791791
*/
792-
fetchFromInstalledJHipster(...path: string[]) {
792+
fetchFromInstalledJHipster(...path: string[]): string {
793793
if (path) {
794794
return joinPath(__dirname, '..', ...path);
795795
}

generators/base-entity-changes/generator.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* limitations under the License.
1818
*/
1919
import { existsSync, readFileSync } from 'fs';
20+
import type { Field } from '../base-application/index.js';
2021
import GeneratorBaseApplication from '../base-application/index.js';
2122
import { PRIORITY_NAMES } from '../base-application/priorities.js';
2223
import { loadEntitiesAnnotations, loadEntitiesOtherSide } from '../base-application/support/index.js';
@@ -219,11 +220,11 @@ export default abstract class GeneratorBaseEntityChanges extends GeneratorBaseAp
219220
});
220221
}
221222

222-
private hasAnyDefaultValue(field) {
223-
return field.defaultValue !== undefined || field.defaultValueComputed;
223+
private hasAnyDefaultValue(field: Field): boolean {
224+
return field.defaultValue !== undefined || field.defaultValueComputed !== undefined;
224225
}
225226

226-
private doDefaultValuesDiffer(field1, field2) {
227+
private doDefaultValuesDiffer(field1: Field, field2: Field): boolean {
227228
return field1.defaultValue !== field2.defaultValue || field1.defaultValueComputed !== field2.defaultValueComputed;
228229
}
229230
}

generators/base-workspaces/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export default abstract class BaseWorkspacesGenerator extends BaseGenerator<Work
165165
);
166166
}
167167

168-
getArgsForPriority(priorityName): any {
168+
getArgsForPriority(priorityName: string): any {
169169
const args = super.getArgsForPriority(priorityName);
170170
if (
171171
![

generators/bootstrap-application-base/generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { loadCommandConfigsIntoApplication, loadCommandConfigsKeysIntoTemplatesC
4343
import { getConfigWithDefaults } from '../../lib/jhipster/default-application-options.js';
4444
import { removeFieldsWithNullishValues } from '../base/support/index.js';
4545
import { convertFieldBlobType, getBlobContentType, isFieldBinaryType, isFieldBlobType } from '../../lib/application/field-types.js';
46+
import type { Entity } from '../../lib/types/application/entity.js';
4647
import { createAuthorityEntity, createUserEntity, createUserManagementEntity } from './utils.js';
4748
import { exportJDLTransform, importJDLTransform } from './support/index.js';
4849

@@ -443,7 +444,7 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator {
443444
entity.anyRelationshipIsRequired = entity.relationships.some(rel => rel.relationshipRequired || rel.id);
444445
},
445446
checkForCircularRelationships({ entity }) {
446-
const detectCyclicRequiredRelationship = (entity, relatedEntities) => {
447+
const detectCyclicRequiredRelationship = (entity: Entity, relatedEntities: Set<Entity>) => {
447448
if (relatedEntities.has(entity)) return true;
448449
relatedEntities.add(entity);
449450
return entity.relationships

generators/bootstrap-application-client/generator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import BaseApplicationGenerator from '../base-application/index.js';
2727
import { loadStoredAppOptions } from '../app/support/index.js';
2828
import clientCommand from '../client/command.js';
2929
import { loadConfig, loadDerivedConfig } from '../../lib/internal/index.js';
30+
import { getFrontendAppName } from '../base/support/index.js';
3031

3132
export default class BootStrapApplicationClient extends BaseApplicationGenerator {
3233
constructor(args: any, options: any, features: any) {
@@ -68,6 +69,9 @@ export default class BootStrapApplicationClient extends BaseApplicationGenerator
6869
loadDerivedConfig(clientCommand.configs, { application });
6970
loadDerivedClientConfig({ application });
7071
},
72+
prepareForTemplates({ application }) {
73+
application.frontendAppName = getFrontendAppName({ baseName: application.baseName });
74+
},
7175
});
7276
}
7377

generators/bootstrap-application-server/generator.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import { getPomVersionProperties } from '../maven/support/index.js';
4444
import { prepareField as prepareFieldForLiquibaseTemplates } from '../liquibase/support/index.js';
4545
import { getDockerfileContainers } from '../docker/utils.js';
4646
import { normalizePathEnd } from '../base/support/path.js';
47-
import { getFrontendAppName } from '../base/support/index.js';
4847
import { getMainClassName } from '../java/support/index.js';
4948
import { loadConfig, loadDerivedConfig } from '../../lib/internal/index.js';
5049
import serverCommand from '../server/command.js';
@@ -126,11 +125,8 @@ export default class BoostrapApplicationServer extends BaseApplicationGenerator
126125
prepareForTemplates({ application: app }) {
127126
const application: any = app;
128127
// Application name modified, using each technology's conventions
129-
application.frontendAppName = getFrontendAppName({ baseName: application.baseName });
130128
application.mainClass = getMainClassName({ baseName: application.baseName });
131-
132129
application.jhiTablePrefix = hibernateSnakeCase(application.jhiPrefix);
133-
134130
application.mainJavaDir = SERVER_MAIN_SRC_DIR;
135131
application.mainJavaPackageDir = normalizePathEnd(`${SERVER_MAIN_SRC_DIR}${application.packageFolder}`);
136132
application.mainJavaResourceDir = SERVER_MAIN_RES_DIR;

generators/bootstrap-application/generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
import { preparePostEntityServerDerivedProperties } from '../server/support/index.js';
3131
import { loadStoredAppOptions } from '../app/support/index.js';
3232
import { JHIPSTER_DOCUMENTATION_ARCHIVE_PATH, JHIPSTER_DOCUMENTATION_URL } from '../generator-constants.js';
33+
import type { Field } from '../../lib/types/base/field.js';
3334

3435
const {
3536
Validations: { MAX, MIN, MAXLENGTH, MINLENGTH, MAXBYTES, MINBYTES, PATTERN },
@@ -102,7 +103,7 @@ export default class BootstrapApplicationGenerator extends BaseApplicationGenera
102103
entityConfig.name = entityName;
103104
}
104105

105-
entityConfig.fields!.forEach((field: any) => {
106+
entityConfig.fields!.forEach((field: Field) => {
106107
const { fieldName, fieldType, fieldValidateRules } = field;
107108

108109
assert(fieldName, `fieldName is missing in .jhipster/${entityName}.json for field ${stringifyApplicationData(field)}`);

generators/client/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type FrontendApplication = ApplicationClientProperties &
1616
clientWebappDir?: string;
1717
webappEnumerationsDir?: string;
1818
clientFrameworkBuiltIn?: boolean;
19+
frontendAppName?: string;
1920
};
2021

2122
/**

generators/server/support/prepare-relationship.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Relationship } from '../../../lib/types/application/relationship.js';
22
import { mutateData } from '../../../lib/utils/object.js';
33
import { formatDocAsApiDescription, formatDocAsJavaDoc } from '../../java/support/doc.js';
4+
import type { Entity } from '../../../lib/types/application/index.js';
45

5-
export function prepareRelationship({ relationship }: { relationship: Relationship; entity: any }) {
6+
export function prepareRelationship({ relationship }: { relationship: Relationship; entity: Entity }) {
67
if (relationship.documentation) {
78
mutateData(relationship, {
89
__override__: false,

lib/types/application/entity.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export interface Entity<F extends BaseField = Field, R extends BaseRelationship
4949
changelogDateForRecent: any;
5050
/** @experimental */
5151
auditableEntity?: boolean;
52-
relationships: (IsNever<R> extends true ? Relationship<Omit<Entity, 'relationships'>> : R)[];
53-
otherRelationships: (IsNever<R> extends true ? Relationship<Omit<Entity, 'relationships'>> : R)[];
52+
relationships: (IsNever<R> extends true ? Relationship : R)[];
53+
otherRelationships: (IsNever<R> extends true ? Relationship : R)[];
5454

5555
primaryKey?: PrimaryKey<F>;
5656

lib/types/application/field.d.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ export interface Field extends Property, BaseField {
3232
// Validation
3333
fieldValidate?: boolean;
3434
unique?: boolean;
35-
fieldValidateRules?: string[];
36-
fieldValidateRulesPattern?: string | RegExp;
37-
fieldValidateRulesMaxlength?: number;
38-
fieldValidateRulesMax?: number;
39-
fieldValidateRulesMin?: number;
40-
fieldValidateRulesMinlength?: number;
41-
fieldValidationRequired?: boolean;
4235
maxlength?: number;
4336

4437
// Temporary fields for Faker

lib/types/application/relationship.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import type { Entity } from '../base/entity.js';
1+
import type { Entity as BaseEntity } from '../base/entity.js';
22
import type { Relationship as BaseRelationship } from '../base/relationship.js';
33
import type { DerivedPropertiesOnlyOf } from '../utils/derived-properties.js';
4+
import type { Entity } from './entity.js';
45
import type { Property } from './property.js';
56

67
type RelationshipProperties = DerivedPropertiesOnlyOf<
78
'relationship',
89
'LeftSide' | 'RightSide' | 'ManyToOne' | 'OneToMany' | 'OneToOne' | 'ManyToMany'
910
>;
1011

11-
export interface Relationship<E extends Entity = Entity> extends BaseRelationship, Property, RelationshipProperties {
12+
export interface Relationship<E extends BaseEntity = Entity> extends BaseRelationship, Property, RelationshipProperties {
1213
propertyName: string;
1314
relationshipNameCapitalized: string;
1415

lib/types/base/field.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ export type Field = Partial<FieldEnum> &
3636
fieldType: FieldType | string;
3737
options?: Record<string, boolean | string | number>;
3838
fieldValidateRules?: string[];
39-
39+
fieldValidateRulesPattern?: string | RegExp;
40+
fieldValidateRulesMaxlength?: number;
41+
fieldValidateRulesMax?: number;
42+
fieldValidateRulesMin?: number;
43+
fieldValidateRulesMinlength?: number;
44+
fieldValidationRequired?: boolean;
45+
fieldValidateRulesMaxbytes?: number;
46+
fieldValidateRulesMinbytes?: number;
4047
/** @deprecated */
4148
fieldTypeJavadoc?: string;
4249
};

0 commit comments

Comments
 (0)