Elffuss BraidHub

kikocis/dataspace-portal public

main · 1.6 KiBRawPermalink
1const {writeFileSync, existsSync, readFileSync} = require('fs');
2const dotenv = require('dotenv');
3
4const envName = process.env.ENV || 'local-dev';
5
6// Generate app-config.json from ENV Vars
7// Priority: ENV VAR > .env > .env.local-dev
8// Usage: node ./config-generator.js
9
10// app-config.json in production is not generated by this script
11
12/**
13 * Reads given .env file
14 *
15 * @param path path to .env file
16 * @return vars (Record<string, string>)
17 */
18const readEnvFileSync = (path) => {
19 if (existsSync(path)) {
20 return dotenv.parse(readFileSync(path));
21 }
22 return {};
23};
24
25/**
26 * Filter object properties by applying filter fn to each key.
27 *
28 * @param obj any object
29 * @param fn filter fn (applied to property name)
30 * @return subset of obj
31 */
32const objFilterKeys = (obj, fn) =>
33 Object.fromEntries(Object.entries(obj).filter(([k, _]) => fn(k)));
34
35// Read ENV Vars from .env files as well
36const allProps = {
37 ...readEnvFileSync('.env.' + envName),
38 ...readEnvFileSync('.env'),
39 ...process.env,
40};
41
42// Collect ENV Vars with prefix AUTHORITY_PORTAL_FRONTEND_
43const prefix = 'AUTHORITY_PORTAL_FRONTEND_';
44const filteredProps = objFilterKeys(allProps, (k) => k.startsWith(prefix));
45if (!Object.keys(filteredProps).length) {
46 console.warn(
47 `No ${prefix} configuration properties are set in ENV, application might not be configured properly.`,
48 );
49}
50
51// Write app-config.json
52const output = './src/assets/config/app-configuration.json';
53const json = JSON.stringify(filteredProps);
54writeFileSync(output, json);
55
56// It is ok to log this config as the data will be available in all client browsers
57console.log(`Writing app.config.json to ${output}: ${json}`);