import type { GlobEnvConfig, GlobConfig } from "../types/config"; import { warn } from "./log"; import pkg from "../../package.json"; export const getConfigFileName = (env: Record) => { return `__PRODUCTION__${env.VITE_GLOB_APP_SHORT_NAME || "__APP"}__CONF__` .toUpperCase() .replace(/\s/g, ""); }; export function getCommonStoragePrefix() { const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig(); return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase(); } // Generate cache key according to version /** * @description: 根据版本生成缓存密钥 * @return {*} * @author: xiezhongbin */ export function getStorageShortName() { return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase(); } export function getAppEnvConfig() { const ENV_NAME = getConfigFileName(import.meta.env); const ENV = (import.meta.env.DEV ? // Get the global configuration (the configuration will be extracted independently when packaging) (import.meta.env as unknown as GlobEnvConfig) : window[ENV_NAME as any]) as unknown as GlobEnvConfig; const { VITE_GLOB_APP_TITLE, VITE_APP_BASE_API, VITE_GLOB_APP_SHORT_NAME, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, } = ENV; if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) { 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_APP_BASE_API, VITE_GLOB_APP_SHORT_NAME, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, }; } /** * @description: 获取环境配置参数 * @return {*} 所有环境配置 * @author: xiezhongbin */ export function useGlobSetting() { const ENV_NAME = getConfigFileName(import.meta.env); const ENV = (import.meta.env.DEV ? // Get the global configuration (the configuration will be extracted independently when packaging) (import.meta.env as unknown as GlobEnvConfig) : import.meta.env) as unknown as GlobEnvConfig; const { VITE_GLOB_APP_TITLE, VITE_APP_BASE_API, VITE_GLOB_APP_SHORT_NAME, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, } = ENV; if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) { warn( `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.` ); } const glob: Readonly = { title: VITE_GLOB_APP_TITLE, apiUrl: VITE_APP_BASE_API, shortName: VITE_GLOB_APP_SHORT_NAME, urlPrefix: VITE_GLOB_API_URL_PREFIX, uploadUrl: VITE_GLOB_UPLOAD_URL, }; return glob as Readonly; } // 系统配置常量 export const globSettingData = useGlobSetting(); /** * @description: Get environment variables * @returns: * @example: */ export function getEnv(): string { return import.meta.env.MODE; }