2022-01-08 17:20:46 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
|
2022-06-25 14:45:23 +08:00
|
|
|
const httpsReg = /^https:\/\//
|
2022-05-03 22:37:51 +08:00
|
|
|
|
2022-01-08 17:20:46 +08:00
|
|
|
export function wrapperEnv(envOptions) {
|
|
|
|
if (!envOptions) return {}
|
|
|
|
const ret = {}
|
|
|
|
|
|
|
|
for (const key in envOptions) {
|
|
|
|
let val = envOptions[key]
|
|
|
|
if (['true', 'false'].includes(val)) {
|
|
|
|
val = val === 'true'
|
|
|
|
}
|
|
|
|
if (['VITE_PORT'].includes(key)) {
|
|
|
|
val = +val
|
|
|
|
}
|
2022-05-19 12:02:01 +08:00
|
|
|
if (key === 'VITE_PROXY' && val && typeof val === 'string') {
|
2022-01-08 17:20:46 +08:00
|
|
|
try {
|
|
|
|
val = JSON.parse(val.replace(/'/g, '"'))
|
|
|
|
} catch (error) {
|
|
|
|
val = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret[key] = val
|
2022-05-19 12:02:01 +08:00
|
|
|
if (typeof val === 'string') {
|
2022-01-08 17:20:46 +08:00
|
|
|
process.env[key] = val
|
2022-05-19 12:02:01 +08:00
|
|
|
} else if (typeof val === 'object') {
|
2022-01-08 17:20:46 +08:00
|
|
|
process.env[key] = JSON.stringify(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-05-03 22:37:51 +08:00
|
|
|
export function createProxy(list = []) {
|
|
|
|
const ret = {}
|
|
|
|
for (const [prefix, target] of list) {
|
2022-06-25 14:45:23 +08:00
|
|
|
const isHttps = httpsReg.test(target)
|
2022-05-03 22:37:51 +08:00
|
|
|
|
|
|
|
// https://github.com/http-party/node-http-proxy#options
|
|
|
|
ret[prefix] = {
|
|
|
|
target: target,
|
|
|
|
changeOrigin: true,
|
|
|
|
ws: true,
|
|
|
|
rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
|
|
|
|
// https is require secure=false
|
|
|
|
...(isHttps ? { secure: false } : {}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-01-08 17:20:46 +08:00
|
|
|
/**
|
|
|
|
* 获取当前环境下生效的配置文件名
|
|
|
|
*/
|
|
|
|
function getConfFiles() {
|
|
|
|
const script = process.env.npm_lifecycle_script
|
|
|
|
const reg = new RegExp('--mode ([a-z_\\d]+)')
|
|
|
|
const result = reg.exec(script)
|
|
|
|
if (result) {
|
|
|
|
const mode = result[1]
|
2022-01-25 15:25:29 +08:00
|
|
|
return ['.env', '.env.local', `.env.${mode}`]
|
2022-01-08 17:20:46 +08:00
|
|
|
}
|
2022-01-25 15:25:29 +08:00
|
|
|
return ['.env', '.env.local', '.env.production']
|
2022-01-08 17:20:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getEnvConfig(match = 'VITE_APP_GLOB_', confFiles = getConfFiles()) {
|
|
|
|
let envConfig = {}
|
|
|
|
confFiles.forEach((item) => {
|
|
|
|
try {
|
2022-01-25 15:25:29 +08:00
|
|
|
if (fs.existsSync(path.resolve(process.cwd(), item))) {
|
|
|
|
const env = dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), item)))
|
|
|
|
envConfig = { ...envConfig, ...env }
|
|
|
|
}
|
2022-01-08 17:20:46 +08:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error in parsing ${item}`, e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const reg = new RegExp(`^(${match})`)
|
|
|
|
Object.keys(envConfig).forEach((key) => {
|
|
|
|
if (!reg.test(key)) {
|
|
|
|
Reflect.deleteProperty(envConfig, key)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return envConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getRootPath(...dir) {
|
|
|
|
return path.resolve(process.cwd(), ...dir)
|
|
|
|
}
|