utils.ts 4.4 KB

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