utils.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. realName = '';
  30. }
  31. }
  32. ret[envName] = realName;
  33. if (typeof realName === 'string') {
  34. process.env[envName] = realName;
  35. } else if (typeof realName === 'object') {
  36. process.env[envName] = JSON.stringify(realName);
  37. }
  38. }
  39. return ret;
  40. }
  41. /**
  42. * 获取当前环境下生效的配置文件名
  43. */
  44. function getConfFiles() {
  45. const script = process.env.npm_lifecycle_script;
  46. // update-begin--author:liaozhiyang---date:20240326---for:【QQYUN-8690】修正获取当前环境下的文件名
  47. const reg = new RegExp('NODE_ENV=([a-z_\\d]+)');
  48. // update-end--author:liaozhiyang---date:20240326---for:【QQYUN-8690】修正获取当前环境下的文件名
  49. const result = reg.exec(script as string) as any;
  50. if (result) {
  51. const mode = result[1] as string;
  52. return ['.env', `.env.${mode}`];
  53. }
  54. return ['.env', '.env.production'];
  55. }
  56. /**
  57. * Get the environment variables starting with the specified prefix
  58. * @param match prefix
  59. * @param confFiles ext
  60. */
  61. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = getConfFiles()) {
  62. let envConfig = {};
  63. confFiles.forEach((item) => {
  64. try {
  65. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  66. envConfig = { ...envConfig, ...env };
  67. } catch (e) {
  68. console.error(`Error in parsing ${item}`, e);
  69. }
  70. });
  71. const reg = new RegExp(`^(${match})`);
  72. Object.keys(envConfig).forEach((key) => {
  73. if (!reg.test(key)) {
  74. Reflect.deleteProperty(envConfig, key);
  75. }
  76. });
  77. return envConfig;
  78. }
  79. /**
  80. * Get user root directory
  81. * @param dir file path
  82. */
  83. export function getRootPath(...dir: string[]) {
  84. return path.resolve(process.cwd(), ...dir);
  85. }