-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathloadAccessConfig.js
104 lines (88 loc) · 3.18 KB
/
loadAccessConfig.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
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
/*
* JS used in `craco.config.js` to load `ACCESS_CONFIG` as a global variable in the frontend
* If you want to use `AccessConfig` in the frontend, use `accessConfig` in `accessConfig.ts`
* */
const fs = require('fs');
const path = require('path');
const ACCESS_TIME_LABELS = 'ACCESS_TIME_LABELS';
const DEFAULT_ACCESS_TIME = 'DEFAULT_ACCESS_TIME';
const FRONTEND = 'FRONTEND';
const NAME_VALIDATION_PATTERN = 'NAME_VALIDATION_PATTERN';
const NAME_VALIDATION_ERROR = 'NAME_VALIDATION_ERROR';
class UndefinedConfigError extends Error {
constructor(key, obj) {
const message = `'${key}' is not a defined config value in AccessConfig`;
super(message);
}
}
class AccessConfigValidationError extends Error {
constructor(message) {
super(message);
}
}
class ConfigFileNotFoundError extends Error {
constructor(filePath) {
const message = `Config override file not found: ${filePath}`;
super(message);
}
}
function getConfig(obj, key) {
if (key in obj) {
return obj[key];
} else {
throw new UndefinedConfigError(String(key), obj);
}
}
function validateConfig(accessConfig) {
if (ACCESS_TIME_LABELS in accessConfig && typeof accessConfig[ACCESS_TIME_LABELS] !== 'object') {
throw new AccessConfigValidationError(`${ACCESS_TIME_LABELS} must be a dictionary`);
}
if (DEFAULT_ACCESS_TIME in accessConfig) {
const defaultAccessTime = parseInt(getConfig(accessConfig, DEFAULT_ACCESS_TIME), 10);
if (isNaN(defaultAccessTime) || !getConfig(accessConfig, ACCESS_TIME_LABELS).hasOwnProperty(defaultAccessTime)) {
throw new AccessConfigValidationError(`${DEFAULT_ACCESS_TIME} must be a valid key in ${ACCESS_TIME_LABELS}`);
}
}
}
function validate_override_config(overrideConfig) {
if (NAME_VALIDATION_PATTERN in overrideConfig && !(NAME_VALIDATION_ERROR in overrideConfig)) {
throw new AccessConfigValidationError(
`If ${NAME_VALIDATION_PATTERN} is present, ${NAME_VALIDATION_ERROR} must also be overridden.`,
);
}
}
function loadOverrideConfig(accessConfig) {
const envConfigPath = process.env.ACCESS_CONFIG_FILE
? path.resolve(__dirname, '../../config', process.env.ACCESS_CONFIG_FILE)
: null;
if (envConfigPath) {
if (fs.existsSync(envConfigPath)) {
const envConfig = JSON.parse(fs.readFileSync(envConfigPath, 'utf8'));
if (FRONTEND in envConfig) {
const frontendConfig = getConfig(envConfig, FRONTEND);
validate_override_config(frontendConfig);
Object.assign(accessConfig, frontendConfig);
}
} else {
throw new ConfigFileNotFoundError(envConfigPath);
}
}
return accessConfig;
}
function loadDefaultConfig() {
const defaultConfigPath = path.resolve(__dirname, '../../config/config.default.json');
const defaultConfig = JSON.parse(fs.readFileSync(defaultConfigPath, 'utf8'));
return getConfig(defaultConfig, FRONTEND);
}
function loadAccessConfig() {
try {
let accessConfig = loadDefaultConfig();
accessConfig = loadOverrideConfig(accessConfig);
validateConfig(accessConfig);
return JSON.stringify(accessConfig);
} catch (error) {
console.error('Error loading access configuration:', error);
throw error;
}
}
module.exports = {loadAccessConfig};