forked from ls1intum/Artemis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild.mjs
114 lines (90 loc) · 3.72 KB
/
prebuild.mjs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* This script is executed before the Angular CLI's build script is executed.
* It adds environment variables to the environment file and merges the i18n files.
* This way, we don't need a webpack configuration file: It replaces
* - webpack.DefinePlugin and
* - MergeJsonWebpackPlugin
*/
import fs from "fs";
import path from "path";
import { hashElement } from "folder-hash";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const languagesHash = await hashElement(path.resolve(__dirname, 'src', 'main', 'webapp', 'i18n'), {
algo: 'md5',
encoding: 'hex',
files: { include: ['*.json'] },
});
// =====================
// Environment variables
// =====================
/*
* Needed for client compilations with docker compose, where the 'APP_VERSION' property isn't injected by gradle.
*
* Returns the inferred APP_VERSION from 'build.gradle', or 'DEV' if this couldn't be retrieved
*/
function inferVersion() {
let version = 'DEV';
try {
let data = fs.readFileSync('build.gradle', 'UTF-8');
version = data.match(/\nversion\s=\s"(.*)"/);
version = version[1] ?? 'DEV';
} catch (error) {
console.log("Error while retrieving 'APP_VERSION' property");
}
return version;
}
// --develop flag is used to enable debug mode
const args = process.argv.slice(2);
const developFlag = args.includes('--develop');
const environmentConfig = `// Don't change this file manually, it will be overwritten by the build process!
export const __DEBUG_INFO_ENABLED__ = ${developFlag};
export const __VERSION__ = '${process.env.APP_VERSION || inferVersion()}';
// The root URL for API calls, ending with a '/' - for example: \`"https://www.jhipster.tech:8081/myservice/"\`.
// If you use an API server, in \`prod\` mode, you will need to enable CORS
// (see the \`jhipster.cors\` common JHipster property in the \`application-*.yml\` configurations)
export const I18N_HASH = '${languagesHash.hash}';
`;
fs.writeFileSync(path.resolve(__dirname, 'src', 'main', 'webapp', 'app', 'environments', 'environment.override.ts'), environmentConfig);
// =====================
// i18n merging
// =====================
const groups = [
{ folder: './src/main/webapp/i18n/en', output: './src/main/webapp/i18n/en.json' },
{ folder: './src/main/webapp/i18n/de', output: './src/main/webapp/i18n/de.json' },
];
const isObject = (obj) => obj && typeof obj === 'object';
function deepMerge(target, source) {
if (!isObject(target) || !isObject(source)) {
return source;
}
for (const key in source) {
// prevent prototype pollution
if (!source.hasOwnProperty(key)) continue;
if (key === "__proto__" || key === "constructor") continue;
const targetValue = target[key];
const sourceValue = source[key];
if (isObject(sourceValue)) {
target[key] = deepMerge(targetValue || {}, sourceValue);
} else {
target[key] = sourceValue;
}
}
return target;
}
for (const group of groups) {
try {
// create output folder if it doesn't exist
fs.mkdirSync(path.dirname(group.output), { recursive: true });
const files = fs.readdirSync(group.folder).filter(file => file.endsWith('.json'));
const mergedContent = files.reduce((acc, file) => {
const content = JSON.parse(fs.readFileSync(path.resolve(group.folder, file)).toString());
return deepMerge(acc, content);
}, {});
await fs.promises.writeFile(group.output, JSON.stringify(mergedContent));
} catch (error) {
console.error(`Error merging JSON files for ${group.output}:`, error);
}
}
console.log("Pre-Build complete!");