Blame view

build/utils.ts 3.56 KB
陈文彬 authored
1
import fs from 'fs';
2
import path from 'path';
陈文彬 authored
3
4
import { networkInterfaces } from 'os';
import dotenv from 'dotenv';
5
import chalk from 'chalk';
6
// import execa from 'execa';
7
陈文彬 authored
8
9
export const isFunction = (arg: unknown): arg is (...args: any[]) => any =>
  typeof arg === 'function';
vben authored
10
陈文彬 authored
11
12
13
export const isRegExp = (arg: unknown): arg is RegExp =>
  Object.prototype.toString.call(arg) === '[object RegExp]';
vben authored
14
15
16
/**
 * get client ip address
 */
陈文彬 authored
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
export function getIPAddress() {
  let interfaces = networkInterfaces();
  for (let devName in interfaces) {
    let iFace = interfaces[devName];
    if (!iFace) return;
    for (let i = 0; i < iFace.length; i++) {
      let alias = iFace[i];
      if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
        return alias.address;
      }
    }
  }

  return '';
}
vben authored
33
export function isDevFn(mode: string): boolean {
34
  return mode === 'development';
陈文彬 authored
35
36
}
vben authored
37
export function isProdFn(mode: string): boolean {
38
  return mode === 'production';
陈文彬 authored
39
40
}
vben authored
41
42
43
/**
 * Whether to generate package preview
 */
陈文彬 authored
44
45
46
export function isReportMode(): boolean {
  return process.env.REPORT === 'true';
}
vben authored
47
48
49
50

/**
 * Whether to generate gzip for packaging
 */
vben authored
51
52
53
export function isBuildGzip(): boolean {
  return process.env.VITE_BUILD_GZIP === 'true';
}
vben authored
54
55
56
57
export interface ViteEnv {
  VITE_PORT: number;
  VITE_USE_MOCK: boolean;
vben authored
58
  VITE_USE_PWA: boolean;
59
60
  VITE_PUBLIC_PATH: string;
  VITE_PROXY: [string, string][];
61
  VITE_GLOB_APP_TITLE: string;
vben authored
62
  VITE_GLOB_APP_SHORT_NAME: string;
63
  VITE_USE_CDN: boolean;
vben authored
64
65
  VITE_DROP_CONSOLE: boolean;
  VITE_BUILD_GZIP: boolean;
vben authored
66
  VITE_DYNAMIC_IMPORT: boolean;
vben authored
67
  VITE_LEGACY: boolean;
68
69
}
vben authored
70
// Read all environment variable configuration files to process.env
71
export function wrapperEnv(envConf: any): ViteEnv {
陈文彬 authored
72
73
  const ret: any = {};
74
75
  for (const envName of Object.keys(envConf)) {
    let realName = envConf[envName].replace(/\\n/g, '\n');
76
77
78
79
80
81
82
83
84
    realName = realName === 'true' ? true : realName === 'false' ? false : realName;
    if (envName === 'VITE_PORT') {
      realName = Number(realName);
    }
    if (envName === 'VITE_PROXY') {
      try {
        realName = JSON.parse(realName);
      } catch (error) {}
    }
陈文彬 authored
85
86
87
88
89
    ret[envName] = realName;
    process.env[envName] = realName;
  }
  return ret;
}
90
vben authored
91
92
93
94
95
/**
 * Get the environment variables starting with the specified prefix
 * @param match prefix
 * @param confFiles ext
 */
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.production']) {
  let envConfig = {};
  confFiles.forEach((item) => {
    try {
      const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)));

      envConfig = { ...envConfig, ...env };
    } catch (error) {}
  });
  Object.keys(envConfig).forEach((key) => {
    const reg = new RegExp(`^(${match})`);
    if (!reg.test(key)) {
      Reflect.deleteProperty(envConfig, key);
    }
  });
  return envConfig;
}
vben authored
114
function consoleFn(color: string, message: any) {
115
116
  console.log(
    chalk.blue.bold('****************  ') +
vben authored
117
      (chalk as any)[color].bold(message) +
118
119
120
121
      chalk.blue.bold('  ****************')
  );
}
vben authored
122
123
124
125
/**
 * warnConsole
 * @param message
 */
vben authored
126
127
128
129
export function successConsole(message: any) {
  consoleFn('green', '✨ ' + message);
}
vben authored
130
131
132
133
/**
 * warnConsole
 * @param message
 */
134
export function errorConsole(message: any) {
vben authored
135
  consoleFn('red', '✨ ' + message);
136
137
}
vben authored
138
139
140
141
/**
 * warnConsole
 * @param message message
 */
142
export function warnConsole(message: any) {
vben authored
143
  consoleFn('yellow', '✨ ' + message);
144
145
}
vben authored
146
147
148
149
/**
 * Get user root directory
 * @param dir file path
 */
150
151
152
export function getCwdPath(...dir: string[]) {
  return path.resolve(process.cwd(), ...dir);
}