Vben
authored
|
1
|
import type { GlobEnvConfig } from '/#/config';
|
Vben
authored
|
2
|
import pkg from '../../package.json';
|
vben
authored
|
3
4
|
const getVariableName = (title: string) => {
|
|
5
6
7
8
9
10
11
12
13
14
|
function strToHex(str: string) {
const result: string[] = [];
for (let i = 0; i < str.length; ++i) {
const hex = str.charCodeAt(i).toString(16);
result.push(('000' + hex).slice(-4));
}
return result.join('').toUpperCase();
}
return `__PRODUCTION__${strToHex(title) || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
|
vben
authored
|
15
|
};
|
Vben
authored
|
16
|
|
Vben
authored
|
17
|
export function getCommonStoragePrefix() {
|
vben
authored
|
18
19
|
const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();
return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase();
|
vben
authored
|
20
|
}
|
nebv
authored
|
21
|
|
Vben
authored
|
22
23
|
// Generate cache key according to version
export function getStorageShortName() {
|
Vben
authored
|
24
25
26
27
|
return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}
export function getAppEnvConfig() {
|
vben
authored
|
28
|
const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE);
|
Vben
authored
|
29
|
|
Vben
authored
|
30
|
const ENV = (import.meta.env.DEV
|
Vben
authored
|
31
|
? // Get the global configuration (the configuration will be extracted independently when packaging)
|
Vben
authored
|
32
33
|
(import.meta.env as unknown as GlobEnvConfig)
: window[ENV_NAME as any]) as unknown as GlobEnvConfig;
|
Vben
authored
|
34
|
|
vben
authored
|
35
36
|
const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } =
ENV;
|
Vben
authored
|
37
|
|
Vben
authored
|
38
39
40
41
42
43
|
return {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
};
|
Vben
authored
|
44
45
|
}
|
|
46
|
/**
|
|
47
|
* @description: Development mode
|
|
48
49
50
51
|
*/
export const devMode = 'development';
/**
|
vben
authored
|
52
|
* @description: Production mode
|
|
53
54
55
56
|
*/
export const prodMode = 'production';
/**
|
vben
authored
|
57
|
* @description: Get environment variables
|
|
58
59
60
|
* @returns:
* @example:
*/
|
vben
authored
|
61
62
63
|
export function getEnv(): string {
return import.meta.env.MODE;
}
|
|
64
65
|
/**
|
vben
authored
|
66
|
* @description: Is it a development mode
|
|
67
68
69
|
* @returns:
* @example:
*/
|
vben
authored
|
70
71
72
|
export function isDevMode(): boolean {
return import.meta.env.DEV;
}
|
|
73
74
|
/**
|
vben
authored
|
75
|
* @description: Is it a production mode
|
|
76
77
78
|
* @returns:
* @example:
*/
|
vben
authored
|
79
80
81
|
export function isProdMode(): boolean {
return import.meta.env.PROD;
}
|