utils.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { networkInterfaces } from 'os';
  4. import dotenv from 'dotenv';
  5. import chalk from 'chalk';
  6. // import execa from 'execa';
  7. export const isFunction = (arg: unknown): arg is (...args: any[]) => any =>
  8. typeof arg === 'function';
  9. export const isRegExp = (arg: unknown): arg is RegExp =>
  10. Object.prototype.toString.call(arg) === '[object RegExp]';
  11. /**
  12. * get client ip address
  13. */
  14. export function getIPAddress() {
  15. let interfaces = networkInterfaces();
  16. for (let devName in interfaces) {
  17. let iFace = interfaces[devName];
  18. if (!iFace) return;
  19. for (let i = 0; i < iFace.length; i++) {
  20. let alias = iFace[i];
  21. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  22. return alias.address;
  23. }
  24. }
  25. }
  26. return '';
  27. }
  28. export function isDevFn(mode: string): boolean {
  29. return mode === 'development';
  30. }
  31. export function isProdFn(mode: string): boolean {
  32. return mode === 'production';
  33. }
  34. /**
  35. * Whether to generate package preview
  36. */
  37. export function isReportMode(): boolean {
  38. return process.env.REPORT === 'true';
  39. }
  40. /**
  41. * Whether to generate gzip for packaging
  42. */
  43. export function isBuildGzip(): boolean {
  44. return process.env.VITE_BUILD_GZIP === 'true';
  45. }
  46. export interface ViteEnv {
  47. VITE_PORT: number;
  48. VITE_USE_MOCK: boolean;
  49. VITE_USE_PWA: boolean;
  50. VITE_PUBLIC_PATH: string;
  51. VITE_PROXY: [string, string][];
  52. VITE_GLOB_APP_TITLE: string;
  53. VITE_GLOB_APP_SHORT_NAME: string;
  54. VITE_USE_CDN: boolean;
  55. VITE_DROP_CONSOLE: boolean;
  56. VITE_BUILD_GZIP: boolean;
  57. VITE_DYNAMIC_IMPORT: boolean;
  58. VITE_LEGACY: boolean;
  59. }
  60. // Read all environment variable configuration files to process.env
  61. export function wrapperEnv(envConf: any): ViteEnv {
  62. const ret: any = {};
  63. for (const envName of Object.keys(envConf)) {
  64. let realName = envConf[envName].replace(/\\n/g, '\n');
  65. realName = realName === 'true' ? true : realName === 'false' ? false : realName;
  66. if (envName === 'VITE_PORT') {
  67. realName = Number(realName);
  68. }
  69. if (envName === 'VITE_PROXY') {
  70. try {
  71. realName = JSON.parse(realName);
  72. } catch (error) {}
  73. }
  74. ret[envName] = realName;
  75. process.env[envName] = realName;
  76. }
  77. return ret;
  78. }
  79. /**
  80. * Get the environment variables starting with the specified prefix
  81. * @param match prefix
  82. * @param confFiles ext
  83. */
  84. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  85. let envConfig = {};
  86. confFiles.forEach((item) => {
  87. try {
  88. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  89. envConfig = { ...envConfig, ...env };
  90. } catch (error) {}
  91. });
  92. Object.keys(envConfig).forEach((key) => {
  93. const reg = new RegExp(`^(${match})`);
  94. if (!reg.test(key)) {
  95. Reflect.deleteProperty(envConfig, key);
  96. }
  97. });
  98. return envConfig;
  99. }
  100. function consoleFn(color: string, message: any) {
  101. console.log(
  102. chalk.blue.bold('**************** ') +
  103. (chalk as any)[color].bold(message) +
  104. chalk.blue.bold(' ****************')
  105. );
  106. }
  107. /**
  108. * warnConsole
  109. * @param message
  110. */
  111. export function successConsole(message: any) {
  112. consoleFn('green', '✨ ' + message);
  113. }
  114. /**
  115. * warnConsole
  116. * @param message
  117. */
  118. export function errorConsole(message: any) {
  119. consoleFn('red', '✨ ' + message);
  120. }
  121. /**
  122. * warnConsole
  123. * @param message message
  124. */
  125. export function warnConsole(message: any) {
  126. consoleFn('yellow', '✨ ' + message);
  127. }
  128. /**
  129. * Get user root directory
  130. * @param dir file path
  131. */
  132. export function getCwdPath(...dir: string[]) {
  133. return path.resolve(process.cwd(), ...dir);
  134. }