utils.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. export function getIPAddress() {
  43. let interfaces = networkInterfaces();
  44. for (let devName in interfaces) {
  45. let iFace = interfaces[devName];
  46. if (!iFace) return;
  47. for (let i = 0; i < iFace.length; i++) {
  48. let alias = iFace[i];
  49. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  50. return alias.address;
  51. }
  52. }
  53. }
  54. return '';
  55. }
  56. export function isDevFn(): boolean {
  57. return process.env.NODE_ENV === 'development';
  58. }
  59. export function isProdFn(): boolean {
  60. return process.env.NODE_ENV === 'production';
  61. }
  62. export function isReportMode(): boolean {
  63. return process.env.REPORT === 'true';
  64. }
  65. export function isBuildGzip(): boolean {
  66. return process.env.VITE_BUILD_GZIP === 'true';
  67. }
  68. export function isSiteMode(): boolean {
  69. return process.env.SITE === 'true';
  70. }
  71. export interface ViteEnv {
  72. VITE_PORT: number;
  73. VITE_USE_MOCK: boolean;
  74. VITE_USE_PWA: boolean;
  75. VITE_PUBLIC_PATH: string;
  76. VITE_PROXY: [string, string][];
  77. VITE_GLOB_APP_TITLE: string;
  78. VITE_USE_CDN: boolean;
  79. VITE_DROP_CONSOLE: boolean;
  80. VITE_BUILD_GZIP: boolean;
  81. }
  82. export function loadEnv(): ViteEnv {
  83. const env = process.env.NODE_ENV;
  84. const ret: any = {};
  85. const envList = [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env', ,];
  86. envList.forEach((e) => {
  87. dotenv.config({
  88. path: e,
  89. });
  90. });
  91. for (const envName of Object.keys(process.env)) {
  92. let realName = (process.env as any)[envName].replace(/\\n/g, '\n');
  93. realName = realName === 'true' ? true : realName === 'false' ? false : realName;
  94. if (envName === 'VITE_PORT') {
  95. realName = Number(realName);
  96. }
  97. if (envName === 'VITE_PROXY') {
  98. try {
  99. realName = JSON.parse(realName);
  100. } catch (error) {}
  101. }
  102. ret[envName] = realName;
  103. process.env[envName] = realName;
  104. }
  105. return ret;
  106. }
  107. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  108. let envConfig = {};
  109. confFiles.forEach((item) => {
  110. try {
  111. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  112. envConfig = { ...envConfig, ...env };
  113. } catch (error) {}
  114. });
  115. Object.keys(envConfig).forEach((key) => {
  116. const reg = new RegExp(`^(${match})`);
  117. if (!reg.test(key)) {
  118. Reflect.deleteProperty(envConfig, key);
  119. }
  120. });
  121. return envConfig;
  122. }
  123. function consoleFn(color: string, message: any) {
  124. console.log(
  125. chalk.blue.bold('**************** ') +
  126. (chalk as any)[color].bold(message) +
  127. chalk.blue.bold(' ****************')
  128. );
  129. }
  130. export function successConsole(message: any) {
  131. consoleFn('green', '✨ ' + message);
  132. }
  133. export function errorConsole(message: any) {
  134. consoleFn('red', '✨ ' + message);
  135. }
  136. export function warnConsole(message: any) {
  137. consoleFn('yellow', '✨ ' + message);
  138. }
  139. export function getCwdPath(...dir: string[]) {
  140. return path.resolve(process.cwd(), ...dir);
  141. }
  142. // export const run = (bin: string, args: any, opts = {}) =>
  143. // execa(bin, args, { stdio: 'inherit', ...opts });