forked from sovity/edc-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-generator.js
55 lines (47 loc) · 1.54 KB
/
config-generator.js
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
const {writeFileSync, existsSync, readFileSync} = require('fs');
const dotenv = require('dotenv');
// Generate app-config.json from ENV Vars
// Priority: ENV VAR > .env > .env.local-dev
// Usage: node ./config-generator.js
// app-config.json in production is not generated by this script
/**
* Reads given .env file
*
* @param path path to .env file
* @return vars (Record<string, string>)
*/
const readEnvFileSync = (path) => {
if (existsSync(path)) {
return dotenv.parse(readFileSync(path));
}
return {};
};
/**
* Filter object properties by applying filter fn to each key.
*
* @param obj any object
* @param fn filter fn (applied to property name)
* @return subset of obj
*/
const objFilterKeys = (obj, fn) =>
Object.fromEntries(Object.entries(obj).filter(([k, _]) => fn(k)));
// Read ENV Vars from .env files as well
const allProps = {
...readEnvFileSync('.env.local-dev'),
...readEnvFileSync('.env'),
...process.env,
};
// Collect ENV Vars with prefix EDC_UI_
const prefix = 'EDC_UI_';
const filteredProps = objFilterKeys(allProps, (k) => k.startsWith(prefix));
if (!Object.keys(filteredProps).length) {
console.warn(
`No ${prefix} configuration properties are set in ENV, application might not be configured properly.`,
);
}
// Write app-config.json
const output = './src/assets/config/app-config.json';
const json = JSON.stringify(filteredProps);
writeFileSync(output, json);
// It is ok to log this config as the data will be available in all client browsers
console.log(`Writing app.config.json to ${output}: ${json}`);