index.ts
3.08 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
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
115
116
117
118
119
import type { GlobEnvConfig } from '/#/external/config';
// import pkg from '../../../../package.json';
import { getGlobalConfigName } from '../../../../build/external/vite/plugins/globConfig/getGlobConfigName';
import { ProjectRuntimeEnvEnum } from '@/enums/external/envEnum';
const stringToJSONParse = (string: string) => {
try {
return JSON.parse(string);
} catch (error) {
return string;
}
};
export function parseEnv(env: Record<string, string>) {
const res: Record<string, string> = {};
Object.keys(env).forEach((key) => {
try {
const value = env[key];
res[key] = stringToJSONParse(value);
} catch (err) {
// eslint-disable-next-line no-console
console.log(`env variable ${key} can't serialization!`);
}
});
return res;
}
export function getCommonStoragePrefix() {
// const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
const VITE_GLOB_APP_SHORT_NAME = 'undefined'
const ENV = [ProjectRuntimeEnvEnum.DEV, ProjectRuntimeEnvEnum.DEV_ALONE].includes(import.meta.env.MODE as ProjectRuntimeEnvEnum) ? ProjectRuntimeEnvEnum.DEV : ProjectRuntimeEnvEnum.PROD
return `${VITE_GLOB_APP_SHORT_NAME}__${ENV.toUpperCase()}`.toUpperCase();
}
// Generate cache key according to version
export function getStorageShortName() {
const pkg = { version: '2.7.1' }
return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}
export function getAppEnvConfig() {
const ENV_NAME = getGlobalConfigName(import.meta.env);
const ENV = (import.meta.env.DEV
? // Get the global configuration (the configuration will be extracted independently when packaging)
(parseEnv(import.meta.env) as unknown as GlobEnvConfig)
: window[ENV_NAME as any]) as unknown as GlobEnvConfig;
const {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_APP_SHORT_NAME,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
VITE_GLOB_WEB_SOCKET,
VITE_GLOB_ALARM_NOTIFY_DURATION,
VITE_GLOB_CONTENT_SECURITY_POLICY,
VITE_GLOB_PUBLIC_PATH,
} = ENV;
if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
console.warn(
`VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`
);
}
return {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_APP_SHORT_NAME,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
VITE_GLOB_WEB_SOCKET,
VITE_GLOB_ALARM_NOTIFY_DURATION,
VITE_GLOB_CONTENT_SECURITY_POLICY,
VITE_GLOB_PUBLIC_PATH,
};
}
/**
* @description: Development mode
*/
export const devMode = 'development';
/**
* @description: Production mode
*/
export const prodMode = 'production';
/**
* @description: Get environment variables
* @returns:
* @example:
*/
export function getEnv(): string {
return import.meta.env.MODE;
}
/**
* @description: Is it a development mode
* @returns:
* @example:
*/
export function isDevMode(): boolean {
return import.meta.env.DEV;
}
/**
* @description: Is it a production mode
* @returns:
* @example:
*/
export function isProdMode(): boolean {
return import.meta.env.PROD;
}