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 c604e8b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 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 './internal/config.mjs';

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
25 changes: 25 additions & 0 deletions generators/jdl/internal/config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GENERATOR_JHIPSTER } from '../../generator-constants.mjs';

export const splitBlueprintConfigs = (config: Record<string, any>) => {
const byNamespaces: Record<string, Record<string, any>> = { [GENERATOR_JHIPSTER]: {} };
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_JHIPSTER][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>> = {};
for (const ns of new Set([...Object.keys(oldConfig), ...Object.keys(newConfig)])) {
merged[ns] = { ...oldConfig[ns], ...newConfig[ns] };
}
return merged;
};

0 comments on commit c604e8b

Please sign in to comment.