-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
export-jdl-transform.ts
84 lines (75 loc) · 3.33 KB
/
export-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
78
79
80
81
82
83
84
import { basename, join } from 'path';
import { Duplex } from 'stream';
import { loadFile } from 'mem-fs';
import type { MemFsEditorFile } from 'mem-fs-editor';
import { Minimatch } from 'minimatch';
import { setModifiedFileState } from 'mem-fs-editor/state';
import { GENERATOR_JHIPSTER } from '../../generator-constants.js';
import { getJDLObjectFromSingleApplication } from '../../../jdl/converters/json-to-jdl-converter.js';
import { Entity } from '../../../jdl/converters/types.js';
export const exportJDLTransform = ({
destinationPath,
jdlStorePath,
throwOnMissingConfig = true,
keepEntitiesConfig,
}: {
destinationPath: string;
jdlStorePath: string;
throwOnMissingConfig?: boolean;
keepEntitiesConfig?: boolean;
}) =>
Duplex.from(async function* (files: AsyncGenerator<MemFsEditorFile>) {
const yoRcFilePath = join(destinationPath, '.yo-rc.json');
const entitiesMatcher = new Minimatch(`${destinationPath}/.jhipster/*.json`);
const entitiesFiles: MemFsEditorFile[] = [];
const entitiesMap: Map<string, Entity> = new Map();
let yoRcFileInMemory: MemFsEditorFile | undefined;
let jdlStoreFileInMemory: MemFsEditorFile | undefined;
for await (const file of files) {
if (file.path === yoRcFilePath) {
yoRcFileInMemory = file;
} else if (file.path === jdlStorePath) {
jdlStoreFileInMemory = file;
} else if (file.contents && entitiesMatcher.match(file.path)) {
entitiesMap.set(basename(file.path).replace('.json', ''), JSON.parse(file.contents.toString()));
entitiesFiles.push(file);
} else {
yield file;
}
}
const yoRcFile = loadFile(yoRcFilePath) as MemFsEditorFile;
const yoRcContents = yoRcFileInMemory?.contents ?? yoRcFile.contents;
if (yoRcContents) {
const contents = JSON.parse(yoRcContents.toString());
if (contents[GENERATOR_JHIPSTER]?.jdlStore) {
const { jdlStore, jwtSecretKey, rememberMeKey, jhipsterVersion, creationTimestamp, incrementalChangelog, ...rest } =
contents[GENERATOR_JHIPSTER];
const jdlObject = getJDLObjectFromSingleApplication(
{ ...contents, [GENERATOR_JHIPSTER]: { ...rest, incrementalChangelog } },
entitiesMap,
);
const jdlContents = jdlObject.toString();
const jdlStoreFile: MemFsEditorFile = jdlStoreFileInMemory ?? (loadFile(jdlStorePath) as any);
jdlStoreFile.contents = Buffer.from(jdlContents);
setModifiedFileState(jdlStoreFile);
(jdlStoreFile as any).conflicter = 'force';
yield jdlStoreFile;
yoRcFile.contents = Buffer.from(
JSON.stringify({ [GENERATOR_JHIPSTER]: { jdlStore, jwtSecretKey, rememberMeKey, jhipsterVersion, creationTimestamp } }, null, 2),
);
setModifiedFileState(yoRcFile);
(yoRcFile as any).conflicter = 'force';
yield yoRcFile;
// Incremental changelog requires entities files to be kept for incremental change at next run
if (keepEntitiesConfig || incrementalChangelog) {
for (const file of entitiesFiles) {
yield file;
}
}
} else if (throwOnMissingConfig) {
throw new Error(`File ${yoRcFilePath} is not a valid JHipster configuration file`);
}
} else if (throwOnMissingConfig) {
throw new Error(`File ${yoRcFilePath} has no contents`);
}
});