getEnv.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import path from "path";
  2. export function isDevFn(mode: string): boolean {
  3. return mode === "development";
  4. }
  5. export function isProdFn(mode: string): boolean {
  6. return mode === "production";
  7. }
  8. export function isTestFn(mode: string): boolean {
  9. return mode === "test";
  10. }
  11. /**
  12. * Whether to generate package preview
  13. */
  14. export function isReportMode(): boolean {
  15. return process.env.VITE_REPORT === "true";
  16. }
  17. // Read all environment variable configuration files to process.env
  18. export function wrapperEnv(envConf: Recordable): ViteEnv {
  19. const ret: any = {};
  20. for (const envName of Object.keys(envConf)) {
  21. let realName = envConf[envName].replace(/\\n/g, "\n");
  22. realName = realName === "true" ? true : realName === "false" ? false : realName;
  23. if (envName === "VITE_PORT") realName = Number(realName);
  24. if (envName === "VITE_PROXY") {
  25. try {
  26. realName = JSON.parse(realName);
  27. } catch (error) {}
  28. }
  29. ret[envName] = realName;
  30. }
  31. return ret;
  32. }
  33. /**
  34. * Get user root directory
  35. * @param dir file path
  36. */
  37. export function getRootPath(...dir: string[]) {
  38. return path.resolve(process.cwd(), ...dir);
  39. }