utils.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 interface ViteEnv {
  66. VITE_PORT: number;
  67. VITE_USE_MOCK: boolean;
  68. VITE_PUBLIC_PATH: string;
  69. VITE_PROXY: [string, string][];
  70. VITE_GLOB_APP_TITLE: string;
  71. VITE_USE_CDN: boolean;
  72. }
  73. export function loadEnv(): ViteEnv {
  74. const env = process.env.NODE_ENV;
  75. const ret: any = {};
  76. const envList = [`.env.${env}.local`, `.env.${env}`, '.env.local', '.env', ,];
  77. envList.forEach((e) => {
  78. dotenv.config({
  79. path: e,
  80. });
  81. });
  82. for (const envName of Object.keys(process.env)) {
  83. let realName = (process.env as any)[envName].replace(/\\n/g, '\n');
  84. realName = realName === 'true' ? true : realName === 'false' ? false : realName;
  85. if (envName === 'VITE_PORT') {
  86. realName = Number(realName);
  87. }
  88. if (envName === 'VITE_PROXY') {
  89. try {
  90. realName = JSON.parse(realName);
  91. } catch (error) {}
  92. }
  93. ret[envName] = realName;
  94. process.env[envName] = realName;
  95. }
  96. return ret;
  97. }
  98. export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  99. let envConfig = {};
  100. confFiles.forEach((item) => {
  101. try {
  102. const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));
  103. envConfig = { ...envConfig, ...env };
  104. } catch (error) {}
  105. });
  106. Object.keys(envConfig).forEach((key) => {
  107. const reg = new RegExp(`^(${match})`);
  108. if (!reg.test(key)) {
  109. Reflect.deleteProperty(envConfig, key);
  110. }
  111. });
  112. return envConfig;
  113. }
  114. function consoleFn(color: string, message: any) {
  115. console.log(
  116. chalk.blue.bold('**************** ') +
  117. (chalk as any)[color].bold(message) +
  118. chalk.blue.bold(' ****************')
  119. );
  120. }
  121. export function successConsole(message: any) {
  122. consoleFn('green', '✨ ' + message);
  123. }
  124. export function errorConsole(message: any) {
  125. consoleFn('red', '✨ ' + message);
  126. }
  127. export function warnConsole(message: any) {
  128. consoleFn('yellow', '✨ ' + message);
  129. }
  130. export function getCwdPath(...dir: string[]) {
  131. return path.resolve(process.cwd(), ...dir);
  132. }
  133. export const run = (bin: string, args: any, opts = {}) =>
  134. execa(bin, args, { stdio: 'inherit', ...opts });