Skip to content

Commit

Permalink
baby step in JDL to generator types matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcharl committed May 14, 2024
1 parent 9a37e58 commit aa5e2a9
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"entities": [ "Country", "Department", "Employee", "Job", "JobHistory", "Location", "Region", "Task"],
"generator-jhipster": {
"promptValues": {
"packageName": "com.mycompany.myapp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"fluentMethods": false,
"jpaMetamodelFiltering": false,
"relationships": [
{
"relationshipType": "one-to-one",
"relationshipValidateRules": "required",
"relationshipName": "region",
"otherEntityName": "region",
"ownerSide": true,
"otherEntityRelationshipName": "country"
}
],
"fields": [
{
"fieldName": "countryName",
"fieldType": "String"
}
],
"changelogDate": "20190524215046",
"dto": "no",
"pagination": "pager",
"service": "no",
"documentation": "",
"entityTableName": "country"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"fluentMethods": false,
"jpaMetamodelFiltering": false,
"relationships": [
{
"relationshipType": "one-to-many",
"relationshipName": "employee",
"otherEntityName": "employee",
"otherEntityRelationshipName": "department"
},
{
"relationshipType": "one-to-one",
"relationshipValidateRules": "required",
"relationshipName": "location",
"otherEntityName": "location",
"ownerSide": true,
"otherEntityRelationshipName": "department"
}
],
"fields": [
{
"fieldName": "departmentName",
"fieldType": "String"
}
],
"changelogDate": "20190524215041",
"dto": "no",
"pagination": "no",
"service": "no",
"documentation": "",
"entityTableName": "department"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"entities": [ "Country"],
"generator-jhipster": {
"promptValues": {
"packageName": "com.mycompany.myapp"
},
"jhipsterVersion": "6.0.1",
"applicationType": "microservice",
"baseName": "truc",
"packageName": "com.mycompany.myapp",
"packageFolder": "com/mycompany/myapp",
"serverPort": "8081",
"authenticationType": "jwt",
"cacheProvider": "hazelcast",
"enableHibernateCache": true,
"websocket": "no",
"databaseType": "sql",
"devDatabaseType": "h2Disk",
"prodDatabaseType": "mysql",
"searchEngine": "no",
"messageBroker": "no",
"serviceDiscoveryType": "eureka",
"buildTool": "maven",
"enableSwaggerCodegen": false,
"jwtSecretKey": "HIDDEN",
"testFrameworks": [],
"jhiPrefix": "jhi",
"entitySuffix": "",
"dtoSuffix": "DTO",
"enableTranslation": false,
"clientPackageManager": "npm",
"skipClient": true,
"nativeLanguage": "en",
"skipUserManagement": true
}
}
8 changes: 8 additions & 0 deletions jdl/converters/json-to-jdl-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ describe('jdl - JSONToJDLConverter', () => {
`);
});
});
describe('with entities inconsistent informations', () => {
it('should fail with an error', () => {
dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'app_with_inconsistent_information');
jdlFilename = 'app.jdl';
expect(() => convertToJDL(dir)).to.throw();
});
});

describe('with entities', () => {
beforeEach(() => {
dir = path.join(__dirname, '..', '__test-files__', 'json_to_jdl_converter', 'app_with_entities');
Expand Down
27 changes: 16 additions & 11 deletions jdl/converters/json-to-jdl-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { readJSONFile } from '../readers/json-file-reader.js';
import { convertApplicationToJDL } from './json-to-jdl-application-converter.js';
import { convertEntitiesToJDL } from './json-to-jdl-entity-converter.js';
import exportJDLObject from '../exporters/jdl-exporter.js';
import { JSONEntity } from './types.js';
import { JSONEntity, JSONRootObject } from './types.js';
import { removeFieldsWithNullishValues } from '../../generators/base/support/config.js';
import { GENERATOR_JHIPSTER } from '../../generators/generator-constants.js';

Expand All @@ -40,13 +40,13 @@ export default {
* @param directory the directory to find JHipster files.
* @param output the file where the JDL will be written
*/
export function convertToJDL(directory = '.', output: string | false = 'app.jdl') {
export function convertToJDL(directory = '.', output: string | false = 'app.jdl'): JDLObject | undefined {
let jdlObject: JDLObject;
if (doesFileExist(path.join(directory, '.yo-rc.json'))) {
const yoRcFileContent = readJSONFile(path.join(directory, '.yo-rc.json'));
let entities;
const yoRcFileContent: Partial<JSONRootObject> = readJSONFile(path.join(directory, '.yo-rc.json'));
let entities: Map<string, JSONEntity> | undefined;
if (doesDirectoryExist(path.join(directory, '.jhipster'))) {
entities = getJSONEntityFiles(directory);
entities = getJSONEntityFiles(directory, yoRcFileContent);
}
jdlObject = getJDLObjectFromSingleApplication(yoRcFileContent, entities);
} else {
Expand All @@ -63,22 +63,22 @@ export function convertToJDL(directory = '.', output: string | false = 'app.jdl'
return jdlObject;
}

export function convertSingleContentToJDL(yoRcFileContent: Record<string, any>, entities?: Map<string, JSONEntity>) {
export function convertSingleContentToJDL(yoRcFileContent: Record<string, any>, entities?: Map<string, JSONEntity>): string {
return getJDLObjectFromSingleApplication(yoRcFileContent, entities).toString();
}

function getJDLObjectFromMultipleApplications(directory: string) {
function getJDLObjectFromMultipleApplications(directory: string): JDLObject {
const subDirectories = getSubdirectories(directory);
if (subDirectories.length === 0) {
throw new Error('There are no subdirectories.');
}
let jdlObject = new JDLObject();
subDirectories.forEach(subDirectory => {
const applicationDirectory = path.join(directory, subDirectory);
const yoRcFileContent = readJSONFile(path.join(applicationDirectory, '.yo-rc.json'));
const yoRcFileContent: Partial<JSONRootObject> = readJSONFile(path.join(applicationDirectory, '.yo-rc.json'));
let entities: Map<string, JSONEntity> = new Map();
if (doesDirectoryExist(path.join(applicationDirectory, '.jhipster'))) {
entities = getJSONEntityFiles(applicationDirectory);
entities = getJSONEntityFiles(applicationDirectory, yoRcFileContent);
}
jdlObject = getJDLObjectFromSingleApplication(yoRcFileContent, entities, jdlObject);
});
Expand Down Expand Up @@ -116,12 +116,17 @@ function cleanYoRcFileContent(yoRcFileContent: Record<string, any>) {
return yoRcFileContent;
}

function getJSONEntityFiles(applicationDirectory: string) {
function getJSONEntityFiles(applicationDirectory: string, yoRcFileContent: Partial<JSONRootObject>): Map<string, JSONEntity> {
const entities: Map<string, JSONEntity> = new Map();
fs.readdirSync(path.join(applicationDirectory, '.jhipster')).forEach(file => {
const entityName = file.slice(0, file.indexOf('.json'));
if (!yoRcFileContent.entities?.includes(entityName)) {
throw new Error(
`Mismatch identified between the application description amount of entity listed and the entity found in the .jhipster folder for entity name: ${entityName}`,
);
}
const jsonFilePath = path.join(applicationDirectory, '.jhipster', file);
if (fs.statSync(jsonFilePath).isFile() && file.endsWith('.json')) {
const entityName = file.slice(0, file.indexOf('.json'));
entities.set(entityName, readJSONFile(jsonFilePath));
}
});
Expand Down
9 changes: 9 additions & 0 deletions jdl/converters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ export type JSONEntity = {
fields?: JSONField[];
relationships?: JSONRelationship[];
} & Record<string, any>;

export type JSONGeneratorJhipsterContent = {
applicationType: string;
} & Record<string, any>;

export type JSONRootObject = {
entities: string[];
['generator-jhipster']: JSONGeneratorJhipsterContent;
};

0 comments on commit aa5e2a9

Please sign in to comment.