vue-vben-admin/src/utils/env.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-02-26 20:09:24 +08:00
import type { GlobEnvConfig } from '/#/config';
2021-02-25 20:17:08 +08:00
import pkg from '../../package.json';
2023-04-05 00:20:48 +08:00
const getVariableName = (title: string) => {
return `__PRODUCTION__${title.replace(/\s/g, '_').replace(/-/g, '_') || '__APP'}__CONF__`
2023-04-05 15:58:03 +08:00
.toUpperCase()
.replace(/\s/g, '');
2023-04-05 00:20:48 +08:00
};
2021-02-25 20:17:08 +08:00
export function getCommonStoragePrefix() {
2023-04-05 15:58:03 +08:00
const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();
return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase();
2021-01-06 00:08:45 +08:00
}
2021-02-25 20:17:08 +08:00
// Generate cache key according to version
export function getStorageShortName() {
return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}
export function getAppEnvConfig() {
2023-04-05 15:58:03 +08:00
const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE);
2021-06-09 22:36:30 +08:00
const ENV = (import.meta.env.DEV
? // Get the global configuration (the configuration will be extracted independently when packaging)
2021-06-09 22:36:30 +08:00
(import.meta.env as unknown as GlobEnvConfig)
: window[ENV_NAME as any]) as unknown as GlobEnvConfig;
2023-04-05 15:58:03 +08:00
const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } =
ENV;
2021-04-10 19:25:49 +08:00
return {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
};
2021-02-25 20:17:08 +08:00
}
2020-09-28 20:19:10 +08:00
/**
2021-07-29 22:43:04 +08:00
* @description: Development mode
2020-09-28 20:19:10 +08:00
*/
export const devMode = 'development';
/**
2021-02-09 23:06:00 +08:00
* @description: Production mode
2020-09-28 20:19:10 +08:00
*/
export const prodMode = 'production';
/**
2021-02-09 23:06:00 +08:00
* @description: Get environment variables
2020-09-28 20:19:10 +08:00
* @returns:
* @example:
*/
2021-01-06 00:08:45 +08:00
export function getEnv(): string {
return import.meta.env.MODE;
}
2020-09-28 20:19:10 +08:00
/**
2021-02-09 23:06:00 +08:00
* @description: Is it a development mode
2020-09-28 20:19:10 +08:00
* @returns:
* @example:
*/
2021-01-06 00:08:45 +08:00
export function isDevMode(): boolean {
return import.meta.env.DEV;
}
2020-09-28 20:19:10 +08:00
/**
2021-02-09 23:06:00 +08:00
* @description: Is it a production mode
2020-09-28 20:19:10 +08:00
* @returns:
* @example:
*/
2021-01-06 00:08:45 +08:00
export function isProdMode(): boolean {
return import.meta.env.PROD;
}