Blame view

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

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

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

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

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

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

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