Blame view

src/utils/env.ts 1.76 KB
Vben authored
1
import type { GlobEnvConfig } from '/#/config';
2
Vben authored
3
import pkg from '../../package.json';
4
5

const getVariableName = (title: string) => {
vben authored
6
7
8
  return `__PRODUCTION__${title.replace(/\s/g, '_') || '__APP'}__CONF__`
    .toUpperCase()
    .replace(/\s/g, '');
9
};
Vben authored
10
11
export function getCommonStoragePrefix() {
vben authored
12
13
  const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();
  return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase();
14
}
15
Vben authored
16
17
// Generate cache key according to version
export function getStorageShortName() {
18
19
20
21
  return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}

export function getAppEnvConfig() {
vben authored
22
  const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE);
23
Vben authored
24
  const ENV = (import.meta.env.DEV
25
    ? // Get the global configuration (the configuration will be extracted independently when packaging)
Vben authored
26
27
      (import.meta.env as unknown as GlobEnvConfig)
    : window[ENV_NAME as any]) as unknown as GlobEnvConfig;
28
vben authored
29
30
  const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } =
    ENV;
Vben authored
31
32
33
34
35
36
37
  return {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
  };
Vben authored
38
39
}
陈文彬 authored
40
/**
41
 * @description: Development mode
陈文彬 authored
42
43
44
45
 */
export const devMode = 'development';

/**
vben authored
46
 * @description: Production mode
陈文彬 authored
47
48
49
50
 */
export const prodMode = 'production';

/**
vben authored
51
 * @description: Get environment variables
陈文彬 authored
52
53
54
 * @returns:
 * @example:
 */
55
56
57
export function getEnv(): string {
  return import.meta.env.MODE;
}
陈文彬 authored
58
59

/**
vben authored
60
 * @description: Is it a development mode
陈文彬 authored
61
62
63
 * @returns:
 * @example:
 */
64
65
66
export function isDevMode(): boolean {
  return import.meta.env.DEV;
}
陈文彬 authored
67
68

/**
vben authored
69
 * @description: Is it a production mode
陈文彬 authored
70
71
72
 * @returns:
 * @example:
 */
73
74
75
export function isProdMode(): boolean {
  return import.meta.env.PROD;
}