utils.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. export const isFunction = (arg: unknown): arg is (...args: any[]) => any =>
  7. typeof arg === 'function';
  8. export const isRegExp = (arg: unknown): arg is RegExp =>
  9. Object.prototype.toString.call(arg) === '[object RegExp]';
  10. /*
  11. * Read all files in the specified folder, filter through regular rules, and return file path array
  12. * @param root Specify the folder path
  13. * [@param] reg Regular expression for filtering files, optional parameters
  14. * 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.
  15. */
  16. export function readAllFile(root: string, reg: RegExp) {
  17. let resultArr: string[] = [];
  18. try {
  19. if (fs.existsSync(root)) {
  20. const stat = fs.lstatSync(root);
  21. if (stat.isDirectory()) {
  22. // dir
  23. const files = fs.readdirSync(root);
  24. files.forEach(function (file) {
  25. const t = readAllFile(root + '/' + file, reg);
  26. resultArr = resultArr.concat(t);
  27. });
  28. } else {
  29. if (reg !== undefined) {
  30. if (isFunction(reg.test) && reg.test(root)) {
  31. resultArr.push(root);
  32. }
  33. } else {
  34. resultArr.push(root);
  35. }
  36. }
  37. }
  38. } catch (error) {}
  39. return resultArr;
  40. }
  41. export function getIPAddress() {
  42. let interfaces = networkInterfaces();
  43. for (let devName in interfaces) {
  44. let iFace = interfaces[devName];
  45. if (!iFace) return;
  46. for (let i = 0; i < iFace.length; i++) {
  47. let alias = iFace[i];
  48. if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
  49. return alias.address;
  50. }
  51. }
  52. }
  53. return '';
  54. }
  55. export function isDevFn(): boolean {
  56. return process.env.NODE_ENV === 'development';
  57. }
  58. export function isProdFn(): boolean {
  59. return process.env.NODE_ENV === 'production';
  60. }
  61. export function isReportMode(): boolean {
  62. return process.env.REPORT === 'true';
  63. }
  64. export interface ViteEnv {
  65. VITE_PORT: number;
  66. VITE_USE_MOCK: boolean;
  67. VITE_PUBLIC_PATH: string;
  68. VITE_PROXY: [string, string][];
  69. VITE_GLOB_APP_TITLE: string;
  70. VITE_USE_CDN: boolean;
  71. }
  72. export function loadEnv(): ViteEnv {
  73. const env = process.env.NODE_ENV;
  74. const ret: any = {};
  75. const envList = [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env', ,];
  76. envList.forEach((e) => {
  77. dotenv.config({
  78. path: e,
  79. });
  80. });
  81. for (const envName of Object.keys(process.env)) {
  82. let realName = (process.env as any)[envName].replace(/\\n/g, '\n');
  83. realName = realName === 'true' ? true : realName === 'false' ? false : realName;
  84. if (envName === 'VITE_PORT') {
  85. realName = Number(realName);
  86. }
  87. if (envName === 'VITE_PROXY') {
  88. try {
  89. realName = JSON.parse(realName);
  90. } catch (error) {}
  91. }
  92. ret[envName] = realName;
  93. process.env[envName] = realName;
  94. }
  95. return ret;
  96. }
  97. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  98. let envConfig = {};
  99. confFiles.forEach((item) => {
  100. try {
  101. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  102. envConfig = { ...envConfig, ...env };
  103. } catch (error) {}
  104. });
  105. Object.keys(envConfig).forEach((key) => {
  106. const reg = new RegExp(`^(${match})`);
  107. if (!reg.test(key)) {
  108. Reflect.deleteProperty(envConfig, key);
  109. }
  110. });
  111. return envConfig;
  112. }
  113. function consoleFn(color: string, message: any) {
  114. console.log(
  115. chalk.blue.bold('**************** ') +
  116. (chalk as any)[color].bold(message) +
  117. chalk.blue.bold(' ****************')
  118. );
  119. }
  120. export function successConsole(message: any) {
  121. consoleFn('green', '✨ ' + message);
  122. }
  123. export function errorConsole(message: any) {
  124. consoleFn('red', '✨ ' + message);
  125. }
  126. export function warnConsole(message: any) {
  127. consoleFn('yellow', '✨ ' + message);
  128. }
  129. export function getCwdPath(...dir: string[]) {
  130. return path.resolve(process.cwd(), ...dir);
  131. }