Skip to content

Commit

Permalink
move entity to base-application
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 29, 2023
1 parent ab599e0 commit 244c602
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 78 deletions.
66 changes: 66 additions & 0 deletions generators/base-application/generator-ts.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { existsSync, mkdirSync, opendirSync } from 'fs';
import _ from 'lodash';
import { basename, extname } from 'path';
import type Storage from 'yeoman-generator/lib/util/storage.js';

import BaseGenerator from '../base/index.mjs';
import { JHIPSTER_CONFIG_DIR } from '../generator-constants.mjs';

const { upperFirst } = _;

// Temporary Generator with Typescript implementations
export default class BaseApplicationTsGenerator extends BaseGenerator {
/**
* Get all the generator configuration from the .yo-rc.json file
* @param entityName - Name of the entity to load.
* @param create - Create storage if doesn't exists.
*/
getEntityConfig(entityName: string, create = false): Storage | undefined {
const entityPath = this.destinationPath(JHIPSTER_CONFIG_DIR, `${upperFirst(entityName)}.json`);
if (!create && !this.fs.exists(entityPath)) return undefined;
return this.createStorage(entityPath, { sorted: true } as any);
}

/**
* get sorted list of entitiy names according to changelog date (i.e. the order in which they were added)
*/
getExistingEntityNames(): string[] {
return this.getExistingEntities().map(entity => entity.name);
}

/**
* get sorted list of entities according to changelog date (i.e. the order in which they were added)
*/
getExistingEntities(): { name: string; definition: Record<string, any> }[] {
function isBefore(e1, e2) {
return e1.definition.changelogDate - e2.definition.changelogDate;
}

const configDir = this.destinationPath(JHIPSTER_CONFIG_DIR);
if (!existsSync(configDir)) {
mkdirSync(configDir);
}
const dir = opendirSync(configDir);
const entityNames: string[] = [];
let dirent = dir.readSync();
while (dirent !== null) {
const extension = extname(dirent.name);
if (dirent.isFile() && extension === '.json') {
entityNames.push(basename(dirent.name, extension));
}
dirent = dir.readSync();
}
dir.closeSync();

const entities: { name: string; definition: Record<string, any> }[] = [];
for (const entityName of [...new Set(((this.jhipsterConfig.entities as string[]) || []).concat(entityNames))]) {
const definition = this.getEntityConfig(entityName)?.getAll();
if (definition) {
entities.push({ name: entityName, definition });
}
}
entities.sort(isBefore);
this.jhipsterConfig.entities = entities.map(({ name }) => name);
return entities;
}
}
8 changes: 4 additions & 4 deletions generators/base-application/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import _ from 'lodash';

import BaseGenerator from '../base/index.mjs';
import BaseApplicationTsGenerator from './generator-ts.mjs';
import { CUSTOM_PRIORITIES, PRIORITY_NAMES, QUEUES } from './priorities.mjs';
import { JHIPSTER_CONFIG_DIR } from '../generator-constants.mjs';

Expand Down Expand Up @@ -52,16 +52,16 @@ const {
POST_WRITING_ENTITIES_QUEUE,
} = QUEUES;

const asPriority = BaseGenerator.asPriority;
const asPriority = BaseApplicationTsGenerator.asPriority;

/**
* This is the base class for a generator that generates entities.
*
* @class
* @template ApplicationType
* @extends {BaseGenerator}
* @extends {BaseApplicationTsGenerator}
*/
export default class BaseApplicationGenerator extends BaseGenerator {
export default class BaseApplicationGenerator extends BaseApplicationTsGenerator {
static CONFIGURING_EACH_ENTITY = asPriority(CONFIGURING_EACH_ENTITY);

static LOADING_ENTITIES = asPriority(LOADING_ENTITIES);
Expand Down
25 changes: 0 additions & 25 deletions generators/base/generator-base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
} from '../../jdl/jhipster/index.mjs';
import { databaseData, getJdbcUrl, getR2dbcUrl, prepareSqlApplicationProperties } from '../sql/support/index.mjs';
import {
JHIPSTER_CONFIG_DIR,
SERVER_MAIN_SRC_DIR,
SERVER_TEST_SRC_DIR,
SERVER_MAIN_RES_DIR,
Expand Down Expand Up @@ -688,30 +687,6 @@ export default class JHipsterBaseGenerator extends PrivateBase {
return _.camelCase(microserviceName) + (microserviceName.endsWith('App') ? '' : 'App');
}

/**
* get sorted list of entitiy names according to changelog date (i.e. the order in which they were added)
*/
getExistingEntityNames() {
return this.getExistingEntities().map(entity => entity.name);
}

/**
* @private
* Read entity json from config folder.
* @param {string} entityName - Entity name
* @return {object} entity definition
*/
readEntityJson(entityName) {
const file = path.join(path.dirname(this.config.path), JHIPSTER_CONFIG_DIR, `${entityName}.json`);
try {
return this.fs.readJSON(file);
} catch (error) {
this.logger.warn(`Unable to parse ${file}, is the entity file malformed or invalid?`);
this.logger.debug('Error:', error);
return undefined;
}
}

/**
* @private
* get a table name in JHipster preferred style.
Expand Down
47 changes: 1 addition & 46 deletions generators/base/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { existsSync, mkdirSync, opendirSync } from 'fs';
import { basename, extname, join as joinPath, dirname } from 'path';
import { basename, join as joinPath, dirname } from 'path';
import { createHash } from 'crypto';
import { fileURLToPath } from 'url';
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
Expand All @@ -44,7 +43,6 @@ import type {
CheckResult,
} from './api.mjs';
import type { BaseTaskGroup } from './tasks.mjs';
import { JHIPSTER_CONFIG_DIR } from '../generator-constants.mjs';
import { packageJson } from '../../lib/index.mjs';
import { type BaseApplication } from '../base-application/types.mjs';
import { GENERATOR_BOOTSTRAP } from '../generator-list.mjs';
Expand Down Expand Up @@ -529,49 +527,6 @@ export default class BaseGenerator extends JHipsterBaseBlueprintGenerator {
});
}

/**
* Get all the generator configuration from the .yo-rc.json file
* @param entityName - Name of the entity to load.
* @param {boolean} create - Create storage if doesn't exists.
*/
getEntityConfig(entityName: string, create = false): Storage | undefined {
const entityPath = this.destinationPath(JHIPSTER_CONFIG_DIR, `${_.upperFirst(entityName)}.json`);
if (!create && !this.fs.exists(entityPath)) return undefined;
return this.createStorage(entityPath, { sorted: true } as any);
}

/**
* get sorted list of entities according to changelog date (i.e. the order in which they were added)
*/
getExistingEntities() {
function isBefore(e1, e2) {
return e1.definition.changelogDate - e2.definition.changelogDate;
}

const configDir = this.destinationPath(JHIPSTER_CONFIG_DIR);
if (!existsSync(configDir)) {
mkdirSync(configDir);
}
const dir = opendirSync(configDir);
const entityNames: string[] = [];
let dirent = dir.readSync();
while (dirent !== null) {
const extension = extname(dirent.name);
if (dirent.isFile() && extension === '.json') {
entityNames.push(basename(dirent.name, extension));
}
dirent = dir.readSync();
}
dir.closeSync();

const entities = [...new Set(((this.jhipsterConfig.entities as string[]) || []).concat(entityNames))]
.map(entityName => ({ name: entityName, definition: this.getEntityConfig(entityName)?.getAll() }))
.filter(entity => entity && entity.definition)
.sort(isBefore);
this.jhipsterConfig.entities = entities.map(({ name }) => name);
return entities;
}

private createSharedData(jhipsterOldVersion: string | null): SharedData<BaseApplication> {
const destinationPath = this.destinationPath();
const dirname = basename(destinationPath);
Expand Down
2 changes: 1 addition & 1 deletion generators/bootstrap-application-base/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const { STRING: TYPE_STRING } = CommonDBTypes;

// eslint-disable-next-line import/prefer-default-export
export function createUserEntity(customUserData = {}, application) {
const userEntityDefinition = this.readEntityJson('User');
const userEntityDefinition = this.getEntityConfig('User')?.getAll();
if (userEntityDefinition) {
if (userEntityDefinition.relationships && userEntityDefinition.relationships.length > 0) {
this.logger.warn('Relationships on the User entity side will be disregarded');
Expand Down
4 changes: 2 additions & 2 deletions generators/entities/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import BaseGenerator from '../base/index.mjs';
import BaseApplicationGenerator from '../base-application/index.mjs';
import { JHIPSTER_CONFIG_DIR } from '../generator-constants.mjs';
import { GENERATOR_ENTITIES, GENERATOR_APP } from '../generator-list.mjs';
import { getDefaultAppName } from '../project-name/support/index.mjs';

export default class EntitiesGenerator extends BaseGenerator {
export default class EntitiesGenerator extends BaseApplicationGenerator {
constructor(args, options, features) {
super(args, options, { unique: 'namespace', ...features });

Expand Down

0 comments on commit 244c602

Please sign in to comment.