runtimeConfig.js
1.8 KB
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
const environmentConfigs = require("./baseConfig.environments.json");
const ENVIRONMENT_TARGETS = Object.freeze([
"local",
"develop",
"test",
"production",
"preRelease",
"cdptest",
]);
const CONFIG_SOURCE = Object.freeze({
local: "develop",
develop: "develop",
test: "test",
production: "production",
preRelease: "beta",
cdptest: "cdptest",
});
// 手动修改该值切换环境:0 本地、1 开发、2 测试、3 生产、4 预发、5 CDP 测试。
// eslint-disable-next-line prefer-const
let index = 1;
function cloneConfig(value) {
return JSON.parse(JSON.stringify(value));
}
function assertEnvironmentIndex(targetIndex) {
if (!Number.isInteger(targetIndex) || targetIndex < 0 || targetIndex >= ENVIRONMENT_TARGETS.length) {
throw new Error(`环境索引必须是 0-5 的整数,当前值:${targetIndex}`);
}
}
function assertRequiredConfig(environmentName, selectedConfig) {
["axios", "axios2", "axios8", "common", "gsc"].forEach((configKey) => {
if (!selectedConfig?.[configKey]?.baseURL) {
throw new Error(`${environmentName} 环境缺少 ${configKey}.baseURL`);
}
});
}
function getRuntimeConfig(targetIndex = index) {
assertEnvironmentIndex(targetIndex);
const environmentName = ENVIRONMENT_TARGETS[targetIndex];
const selectedConfig = cloneConfig(environmentConfigs[CONFIG_SOURCE[environmentName]]);
if (environmentName === "local") {
selectedConfig.env = "local";
}
selectedConfig.axios2CarlifeRouter = selectedConfig.axios2.baseURL.replace(
"/rop-scrm-shopmall/router",
"/rop-scrm-carlife/router",
);
assertRequiredConfig(environmentName, selectedConfig);
return selectedConfig;
}
const config = getRuntimeConfig();
const currentEnvironment = ENVIRONMENT_TARGETS[index];
module.exports = {
ENVIRONMENT_TARGETS,
config,
currentEnvironment,
envIndex: index,
getRuntimeConfig,
};