-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
import-jdl-transform.ts
77 lines (69 loc) · 2.88 KB
/
import-jdl-transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Duplex } from 'stream';
import { join } from 'path';
import { loadFile } from 'mem-fs';
import type { MemFsEditorFile } from 'mem-fs-editor';
import { Minimatch } from 'minimatch';
import { upperFirst } from 'lodash-es';
import { GENERATOR_JHIPSTER } from '../../generator-constants.js';
import { createImporterFromContent } from '../../../jdl/jdl-importer.js';
import { mergeYoRcContent } from '../../../jdl/index.js';
export const importJDLTransform = ({ destinationPath, jdlStorePath }: { destinationPath: string; jdlStorePath: string }) =>
Duplex.from(async function* (files: AsyncGenerator<MemFsEditorFile>) {
const yoRcFilePath = join(destinationPath, '.yo-rc.json');
const entitiesFolder = join(destinationPath, '.jhipster');
const entitiesMatcher = new Minimatch(`${entitiesFolder}/*.json`);
const entityFields: Array<MemFsEditorFile> = [];
let jdlStoreFileInMemory: MemFsEditorFile | undefined;
let yoRcFileInMemory: MemFsEditorFile | undefined;
for await (const file of files) {
if (file.path === jdlStorePath) {
jdlStoreFileInMemory = file;
yield jdlStoreFileInMemory;
} else if (file.path === yoRcFilePath) {
yoRcFileInMemory = file;
} else if (entitiesMatcher.match(file.path)) {
entityFields.push(file);
} else {
yield file;
}
}
const jdlStoreContents = jdlStoreFileInMemory?.contents ?? (loadFile(jdlStorePath) as any).contents;
if (!jdlStoreContents) {
if (yoRcFileInMemory) {
yield yoRcFileInMemory;
}
for (const file of entityFields) {
yield file;
}
return;
}
if (entityFields.length > 0) {
throw new Error('Entities configuration files are not supported by jdlStore');
}
const importer = createImporterFromContent(jdlStoreContents.toString());
const importState = importer.import();
const applicationWithEntities = Object.values(importState.exportedApplicationsWithEntities);
if (applicationWithEntities.length !== 1) {
throw new Error(`JDL stores supports only jdls with 1 application, found ${applicationWithEntities.length}`);
}
const { config, namespaceConfigs, entities } = applicationWithEntities[0];
const yoRcFile = loadFile(yoRcFilePath) as MemFsEditorFile;
const yoRcContents = yoRcFileInMemory?.contents ?? yoRcFile.contents;
yoRcFile.contents = Buffer.from(
JSON.stringify(
mergeYoRcContent(yoRcContents ? JSON.parse(yoRcContents.toString()) : {}, {
...namespaceConfigs,
[GENERATOR_JHIPSTER]: config,
}),
null,
2,
),
);
yield yoRcFile;
for (const entity of entities) {
const configFile = join(entitiesFolder, `${upperFirst(entity.name)}.json`);
const file = loadFile(configFile) as MemFsEditorFile;
file.contents = Buffer.from(JSON.stringify(entity, null, 2));
yield file;
}
});