Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Fixed #180
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuAA committed Feb 15, 2018
1 parent 546b6fb commit a4bfdf4
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 172 deletions.
75 changes: 55 additions & 20 deletions lib/export/jhipster_entity_exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

const fs = require('fs');
const ApplicationTypes = require('../core/jhipster/application_types');
const readEntityJSON = require('../reader/json_file_reader').readEntityJSON;
const toFilePath = require('../reader/json_file_reader').toFilePath;
const FileUtils = require('../utils/file_utils');
Expand All @@ -26,6 +27,8 @@ const BuildException = require('../exceptions/exception_factory').BuildException
const exceptions = require('../exceptions/exception_factory').exceptions;
const DeprecationUtils = require('../utils/deprecation_utils');

let configuration = {};

module.exports = {
exportToJSON,
exportEntities,
Expand All @@ -37,52 +40,75 @@ module.exports = {
* Exports the passed entities to JSON.
* @param entities the entities to export.
* @param forceNoFiltering whether to filter out unchanged entities.
* @param applicationName
* @param applicationType
* @returns The exported entities.
* @deprecated Use ::exportEntities instead.
*/
function exportToJSON(entities, forceNoFiltering) {
function exportToJSON(entities, forceNoFiltering, applicationName, applicationType) {
DeprecationUtils.displayMethodDeprecationMessage({
deprecatedMethod: 'JHipsterEntityExporter::exportToJSON',
preferredMethod: 'JHipsterEntityExporter::exportEntities'
});
return exportEntities({
entities,
forceNoFiltering
forceNoFiltering,
applicationName,
applicationType
});
}

/**
* Exports the passed entities to JSON.
* @param configuration the object having the keys:
* @param passedConfiguration the object having the keys:
* - entities: the entities to export,
* - forceNoFiltering: whether to filter out unchanged entities
* - applicationName: the application name,
* - applicationType: the application type
* @returns {*} The exported entities.
*/
function exportEntities(configuration) {
if (!configuration.entities) {
throw new BuildException(
exceptions.NullPointer,
'Entities have to be passed to be exported.');
}
function exportEntities(passedConfiguration) {
init(passedConfiguration);
createJHipsterJSONFolder();
if (!configuration.forceNoFiltering) {
configuration.entities = filterOutUnchangedEntities(configuration.entities);
filterOutUnchangedEntities();
}
for (let i = 0, entityNames = Object.keys(configuration.entities); i < entityNames.length; i++) {
const filePath = toFilePath(entityNames[i]);
const entity = updateChangelogDate(filePath, configuration.entities[entityNames[i]]);
fs.writeFileSync(filePath, JSON.stringify(entity, null, 4));
if (shouldFilterOutEntitiesBasedOnMicroservice()) {
filterOutEntitiesByMicroservice();
}
writeEntities();
return configuration.entities;
}

function init(passedConfiguration) {
if (!passedConfiguration.entities) {
throw new BuildException(
exceptions.NullPointer,
'Entities have to be passed to be exported.');
}
configuration = passedConfiguration;
}

function shouldFilterOutEntitiesBasedOnMicroservice() {
return configuration.applicationType && configuration.applicationType === ApplicationTypes.MICROSERVICE
&& configuration.applicationName;
}

/**
* Creates the JHipster entity folder, if possible.
*/
function createJHipsterJSONFolder() {
FileUtils.createDirectory('.jhipster');
}

function writeEntities() {
for (let i = 0, entityNames = Object.keys(configuration.entities); i < entityNames.length; i++) {
const filePath = toFilePath(entityNames[i]);
const entity = updateChangelogDate(filePath, configuration.entities[entityNames[i]]);
fs.writeFileSync(filePath, JSON.stringify(entity, null, 4));
}
}

function updateChangelogDate(filePath, entity) {
if (FileUtils.doesFileExist(filePath)) {
const fileOnDisk = readEntityJSON(filePath);
Expand All @@ -93,14 +119,23 @@ function updateChangelogDate(filePath, entity) {
return entity;
}

function filterOutUnchangedEntities(entities) {
const filtered = {};
for (let i = 0, entityNames = Object.keys(entities); i < entityNames.length; i++) {
function filterOutUnchangedEntities() {
for (let i = 0, entityNames = Object.keys(configuration.entities); i < entityNames.length; i++) {
const entityName = entityNames[i];
const filePath = toFilePath(entityName);
if (!(FileUtils.doesFileExist(filePath) && areJHipsterEntitiesEqual(readEntityJSON(filePath), entities[entityName]))) {
filtered[entityName] = (entities[entityName]);
if (FileUtils.doesFileExist(filePath)
&& areJHipsterEntitiesEqual(readEntityJSON(filePath), configuration.entities[entityName])) {
delete configuration.entities[entityName];
}
}
}

function filterOutEntitiesByMicroservice() {
for (let i = 0, entityNames = Object.keys(configuration.entities); i < entityNames.length; i++) {
const entity = configuration.entities[entityNames[i]];
if (entity.microserviceName
&& entity.microserviceName.toLowerCase() !== configuration.applicationName.toLowerCase()) {
delete configuration.entities[entityNames[i]];
}
}
return filtered;
}
Loading

0 comments on commit a4bfdf4

Please sign in to comment.