utils.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import fs from 'fs';
  2. import path from 'path';
  3. import dotenv from 'dotenv';
  4. export function isDevFn(mode: string): boolean {
  5. return mode === 'development';
  6. }
  7. export function isProdFn(mode: string): boolean {
  8. return mode === 'production';
  9. }
  10. /**
  11. * Whether to generate package preview
  12. */
  13. export function isReportMode(): boolean {
  14. return process.env.REPORT === 'true';
  15. }
  16. // Read all environment variable configuration files to process.env
  17. export function wrapperEnv(envConf: Recordable): ViteEnv {
  18. const ret: any = {};
  19. for (const envName of Object.keys(envConf)) {
  20. let realName = envConf[envName].replace(/\\n/g, '\n');
  21. realName = realName === 'true' ? true : realName === 'false' ? false : realName;
  22. if (envName === 'VITE_PORT') {
  23. realName = Number(realName);
  24. }
  25. if (envName === 'VITE_PROXY' && realName) {
  26. try {
  27. realName = JSON.parse(realName.replace(/'/g, '"'));
  28. } catch (error) {
  29. console.log("PARSE VITE PROXY ERROR:", error);
  30. realName = '';
  31. }
  32. }
  33. ret[envName] = realName;
  34. if (typeof realName === 'string') {
  35. process.env[envName] = realName;
  36. } else if (typeof realName === 'object') {
  37. process.env[envName] = JSON.stringify(realName);
  38. }
  39. }
  40. return ret;
  41. }
  42. /**
  43. * 获取当前环境下生效的配置文件名
  44. */
  45. function getConfFiles() {
  46. // 代码逻辑说明: 【QQYUN-9685】构建 electron 桌面应用
  47. const {VITE_GLOB_RUN_PLATFORM} = process.env
  48. if (VITE_GLOB_RUN_PLATFORM === 'electron') {
  49. return ['.env', '.env.prod_electron'];
  50. }
  51. const script = process.env.npm_lifecycle_script;
  52. // 代码逻辑说明: 【QQYUN-8690】修正获取当前环境下的文件名
  53. const reg = new RegExp('NODE_ENV=([a-z_\\d]+)');
  54. const result = reg.exec(script as string) as any;
  55. if (result) {
  56. const mode = result[1] as string;
  57. return ['.env', `.env.${mode}`];
  58. }
  59. return ['.env', '.env.production'];
  60. }
  61. /**
  62. * Get the environment variables starting with the specified prefix
  63. * @param match prefix
  64. * @param confFiles ext
  65. */
  66. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = getConfFiles()) {
  67. let envConfig = {};
  68. confFiles.forEach((item) => {
  69. try {
  70. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  71. envConfig = { ...envConfig, ...env };
  72. } catch (e) {
  73. console.error(`Error in parsing ${item}`, e);
  74. }
  75. });
  76. const reg = new RegExp(`^(${match})`);
  77. Object.keys(envConfig).forEach((key) => {
  78. if (!reg.test(key)) {
  79. Reflect.deleteProperty(envConfig, key);
  80. }
  81. });
  82. return envConfig;
  83. }
  84. /**
  85. * Get user root directory
  86. * @param dir file path
  87. */
  88. export function getRootPath(...dir: string[]) {
  89. return path.resolve(process.cwd(), ...dir);
  90. }