utils.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. * Read all files in the specified folder, filter through regular rules, and return file path array
  13. * @param root Specify the folder path
  14. * [@param] reg Regular expression for filtering files, optional parameters
  15. * Note: It can also be deformed to check whether the file path conforms to regular rules. The path can be a folder or a file. The path that does not exist is also fault-tolerant.
  16. */
  17. export function readAllFile(root: string, reg: RegExp) {
  18. let resultArr: string[] = [];
  19. try {
  20. if (fs.existsSync(root)) {
  21. const stat = fs.lstatSync(root);
  22. if (stat.isDirectory()) {
  23. // dir
  24. const files = fs.readdirSync(root);
  25. files.forEach(function (file) {
  26. const t = readAllFile(root + '/' + file, reg);
  27. resultArr = resultArr.concat(t);
  28. });
  29. } else {
  30. if (reg !== undefined) {
  31. if (isFunction(reg.test) && reg.test(root)) {
  32. resultArr.push(root);
  33. }
  34. } else {
  35. resultArr.push(root);
  36. }
  37. }
  38. }
  39. } catch (error) {}
  40. return resultArr;
  41. }
  42. /**
  43. * get client ip address
  44. */
  45. export function getIPAddress() {
  46. let interfaces = networkInterfaces();
  47. for (let devName in interfaces) {
  48. let iFace = interfaces[devName];
  49. if (!iFace) return;
  50. for (let i = 0; i < iFace.length; i++) {
  51. let alias = iFace[i];
  52. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  53. return alias.address;
  54. }
  55. }
  56. }
  57. return '';
  58. }
  59. export function isDevFn(mode: string): boolean {
  60. return mode === 'development';
  61. }
  62. export function isProdFn(mode: string): boolean {
  63. return mode === 'production';
  64. }
  65. /**
  66. * Whether to generate package preview
  67. */
  68. export function isReportMode(): boolean {
  69. return process.env.REPORT === 'true';
  70. }
  71. /**
  72. * Whether to generate gzip for packaging
  73. */
  74. export function isBuildGzip(): boolean {
  75. return process.env.VITE_BUILD_GZIP === 'true';
  76. }
  77. export interface ViteEnv {
  78. VITE_PORT: number;
  79. VITE_USE_MOCK: boolean;
  80. VITE_USE_PWA: boolean;
  81. VITE_PUBLIC_PATH: string;
  82. VITE_PROXY: [string, string][];
  83. VITE_GLOB_APP_TITLE: string;
  84. VITE_GLOB_APP_SHORT_NAME: string;
  85. VITE_USE_CDN: boolean;
  86. VITE_DROP_CONSOLE: boolean;
  87. VITE_BUILD_GZIP: boolean;
  88. VITE_DYNAMIC_IMPORT: boolean;
  89. VITE_LEGACY: boolean;
  90. }
  91. // Read all environment variable configuration files to process.env
  92. export function wrapperEnv(envConf: any): ViteEnv {
  93. const ret: any = {};
  94. for (const envName of Object.keys(envConf)) {
  95. let realName = envConf[envName].replace(/\\n/g, '\n');
  96. realName = realName === 'true' ? true : realName === 'false' ? false : realName;
  97. if (envName === 'VITE_PORT') {
  98. realName = Number(realName);
  99. }
  100. if (envName === 'VITE_PROXY') {
  101. try {
  102. realName = JSON.parse(realName);
  103. } catch (error) {}
  104. }
  105. ret[envName] = realName;
  106. process.env[envName] = realName;
  107. }
  108. return ret;
  109. }
  110. /**
  111. * Get the environment variables starting with the specified prefix
  112. * @param match prefix
  113. * @param confFiles ext
  114. */
  115. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  116. let envConfig = {};
  117. confFiles.forEach((item) => {
  118. try {
  119. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  120. envConfig = { ...envConfig, ...env };
  121. } catch (error) {}
  122. });
  123. Object.keys(envConfig).forEach((key) => {
  124. const reg = new RegExp(`^(${match})`);
  125. if (!reg.test(key)) {
  126. Reflect.deleteProperty(envConfig, key);
  127. }
  128. });
  129. return envConfig;
  130. }
  131. function consoleFn(color: string, message: any) {
  132. console.log(
  133. chalk.blue.bold('**************** ') +
  134. (chalk as any)[color].bold(message) +
  135. chalk.blue.bold(' ****************')
  136. );
  137. }
  138. /**
  139. * warnConsole
  140. * @param message
  141. */
  142. export function successConsole(message: any) {
  143. consoleFn('green', '✨ ' + message);
  144. }
  145. /**
  146. * warnConsole
  147. * @param message
  148. */
  149. export function errorConsole(message: any) {
  150. consoleFn('red', '✨ ' + message);
  151. }
  152. /**
  153. * warnConsole
  154. * @param message message
  155. */
  156. export function warnConsole(message: any) {
  157. consoleFn('yellow', '✨ ' + message);
  158. }
  159. /**
  160. * Get user root directory
  161. * @param dir file path
  162. */
  163. export function getCwdPath(...dir: string[]) {
  164. return path.resolve(process.cwd(), ...dir);
  165. }
  166. // export const run = (bin: string, args: any, opts = {}) =>
  167. // execa(bin, args, { stdio: 'inherit', ...opts });