Skip to content

Commit

Permalink
write jdl blueprint's configs to the right namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 23, 2023
1 parent 29b3fc1 commit bf49ba5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
24 changes: 24 additions & 0 deletions generators/jdl/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ exports[`generator - jdl --json-only option for two applications and entity jdl
}
`;

exports[`generator - jdl for application jdl with blueprint jdl with blueprint config should write expected files 1`] = `
{
".yo-rc.json": {
"contents": "{
"generator-jhipster": {
"applicationIndex": 0,
"baseName": "jhipster",
"blueprints": [
{
"name": "generator-jhipster-foo"
}
],
"entities": []
},
"generator-jhipster-foo": {
"config": "bar"
}
}
",
"stateCleared": "modified",
},
}
`;

exports[`generator - jdl for application jdl with valid jdl should write expected files 1`] = `
{
".yo-rc.json": {
Expand Down
7 changes: 3 additions & 4 deletions generators/jdl/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ApplicationWithEntities, createImporterFromContent } from '../../jdl/jd
import { GENERATOR_JHIPSTER, JHIPSTER_CONFIG_DIR } from '../generator-constants.mjs';
import statistics from '../statistics.mjs';
import { addApplicationIndex, allNewApplications, customizeForMicroservices } from './internal/index.mjs';
import { mergeYoRcContent, splitBlueprintConfigs } from '../../jdl/index.js';

const { upperFirst } = _;

Expand Down Expand Up @@ -307,10 +308,8 @@ export default class JdlGenerator extends BaseGenerator {
if (config) {
const configFile = this.destinationPath(`${appPath}.yo-rc.json`);
const oldConfig: any = fs.readJSON(configFile, {});
fs.writeJSON(configFile, {
...oldConfig,
[GENERATOR_JHIPSTER]: { ...oldConfig[GENERATOR_JHIPSTER], ...config },
});

fs.writeJSON(configFile, mergeYoRcContent(oldConfig, splitBlueprintConfigs(config)));
}
for (const entity of entities) {
const configFile = this.destinationPath(`${appPath}${JHIPSTER_CONFIG_DIR}/${upperFirst(entity.name)}.json`);
Expand Down
15 changes: 15 additions & 0 deletions generators/jdl/generator.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ describe(`generator - ${generator}`, () => {
expect(runResult.getSnapshot()).toMatchSnapshot();
});
});

describe('with blueprint jdl with blueprint config', () => {
let runResult: RunResult;

before(async () => {
runResult = await helpers.runJHipster(GENERATOR_JDL).withOptions({
jsonOnly: true,
inline: 'application { config { blueprints [foo] foo:config bar } }',
});
});

it('should write expected files', () => {
expect(runResult.getSnapshot()).toMatchSnapshot();
});
});
});

describe('for one application and entity jdl', () => {
Expand Down
28 changes: 28 additions & 0 deletions jdl/exporters/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export const GENERATOR_NAME = 'generator-jhipster';

export const splitBlueprintConfigs = (config: Record<string, any>) => {
const byNamespaces: Record<string, Record<string, any>> = { [GENERATOR_NAME]: {} };
Object.entries(config).forEach(([name, value]: [string, any]) => {
if (name.includes(':')) {
const [ns, configName] = name.split(':');
if (!byNamespaces[ns]) {
byNamespaces[ns] = {};
}
byNamespaces[ns][configName] = value;
} else {
byNamespaces[GENERATOR_NAME][name] = value;
}
});
return byNamespaces;
};

export const mergeYoRcContent = (oldConfig: Record<string, Record<string, any>>, newConfig: Record<string, Record<string, any>>) => {
const merged: Record<string, Record<string, any>> = { [GENERATOR_NAME]: {} };
for (const ns of new Set([...Object.keys(oldConfig), ...Object.keys(newConfig)])) {
merged[ns] = { ...oldConfig[ns], ...newConfig[ns] };
}
if (oldConfig[GENERATOR_NAME]?.creationTimestamp) {
merged[GENERATOR_NAME].creationTimestamp = oldConfig[GENERATOR_NAME].creationTimestamp;
}
return merged;
};
15 changes: 2 additions & 13 deletions jdl/exporters/export-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import fs from 'fs';
import { doesFileExist } from '../utils/file-utils.js';
import { mergeYoRcContent } from './config.js';

export const GENERATOR_NAME = 'generator-jhipster';

Expand All @@ -31,19 +32,7 @@ export function writeConfigFile(config, yoRcPath = '.yo-rc.json') {
let newYoRc = { ...config };
if (doesFileExist(yoRcPath)) {
const yoRc = JSON.parse(fs.readFileSync(yoRcPath, { encoding: 'utf-8' }));
let creationTimestamp = config[GENERATOR_NAME].creationTimestamp;
if (yoRc[GENERATOR_NAME] && yoRc[GENERATOR_NAME].creationTimestamp) {
creationTimestamp = yoRc[GENERATOR_NAME].creationTimestamp;
}
newYoRc = {
...yoRc,
...config,
[GENERATOR_NAME]: {
...yoRc[GENERATOR_NAME],
...config[GENERATOR_NAME],
creationTimestamp,
},
};
newYoRc = mergeYoRcContent(yoRc, config);
}
fs.writeFileSync(yoRcPath, JSON.stringify(newYoRc, null, 2).concat('\n'));
}
1 change: 1 addition & 0 deletions jdl/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './jdl-importer.js';
export * from './parsing/api.js';
export * from './jhipster/index.mjs';
export * from './exporters/config.js';

0 comments on commit bf49ba5

Please sign in to comment.