admin-vben/build/utils.ts

101 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-10-30 02:36:06 +00:00
import path, { join } from 'node:path'
2023-03-18 13:10:54 +00:00
import dotenv from 'dotenv'
2023-10-30 02:36:06 +00:00
import { readFile } from 'fs-extra'
2023-03-18 13:10:54 +00:00
export function isDevFn(mode: string): boolean {
return mode === 'development'
}
export function isProdFn(mode: string): boolean {
return mode === 'production'
}
/**
* Whether to generate package preview
*/
export function isReportMode(): boolean {
return process.env.REPORT === 'true'
}
// Read all environment variable configuration files to process.env
export function wrapperEnv(envConf: Recordable): ViteEnv {
const ret: any = {}
for (const envName of Object.keys(envConf)) {
let realName = envConf[envName].replace(/\\n/g, '\n')
realName = realName === 'true' ? true : realName === 'false' ? false : realName
2023-07-29 10:46:43 +00:00
if (envName === 'VITE_PORT')
2023-03-18 13:10:54 +00:00
realName = Number(realName)
2023-07-29 10:46:43 +00:00
2023-03-18 13:10:54 +00:00
if (envName === 'VITE_PROXY' && realName) {
try {
realName = JSON.parse(realName.replace(/'/g, '"'))
2023-07-29 10:46:43 +00:00
}
catch (error) {
2023-03-18 13:10:54 +00:00
realName = ''
}
}
ret[envName] = realName
// if (typeof realName === 'string') {
// process.env[envName] = realName;
// } else if (typeof realName === 'object') {
// process.env[envName] = JSON.stringify(realName);
// }
}
return ret
}
/**
*
*/
function getConfFiles() {
2023-10-30 02:36:06 +00:00
const script = process.env.npm_lifecycle_script as string
2023-07-29 12:20:40 +00:00
const reg = /--mode ([a-z_\d]+)/
2023-10-30 02:36:06 +00:00
const result = reg.exec(script)
2023-03-18 13:10:54 +00:00
if (result) {
2023-10-30 02:36:06 +00:00
const mode = result[1]
2023-03-18 13:10:54 +00:00
return ['.env', `.env.${mode}`]
}
return ['.env', '.env.production']
}
/**
* Get the environment variables starting with the specified prefix
* @param match prefix
* @param confFiles ext
*/
2023-10-30 02:36:06 +00:00
export async function getEnvConfig(
2023-10-10 03:56:35 +00:00
match = 'VITE_GLOB_',
confFiles = getConfFiles(),
): Promise<{
2023-10-30 02:36:06 +00:00
[key: string]: string
2023-10-10 03:56:35 +00:00
}> {
2023-03-18 13:10:54 +00:00
let envConfig = {}
2023-10-30 02:36:06 +00:00
for (const confFile of confFiles) {
2023-03-18 13:10:54 +00:00
try {
2023-10-30 02:36:06 +00:00
const envPath = await readFile(join(process.cwd(), confFile), { encoding: 'utf8' })
const env = dotenv.parse(envPath)
2023-03-18 13:10:54 +00:00
envConfig = { ...envConfig, ...env }
2023-07-29 10:46:43 +00:00
}
catch (e) {
2023-10-30 02:36:06 +00:00
console.error(`Error in parsing ${confFile}`, e)
2023-03-18 13:10:54 +00:00
}
2023-10-30 02:36:06 +00:00
}
2023-03-18 13:10:54 +00:00
const reg = new RegExp(`^(${match})`)
Object.keys(envConfig).forEach((key) => {
2023-07-29 10:46:43 +00:00
if (!reg.test(key))
2023-03-18 13:10:54 +00:00
Reflect.deleteProperty(envConfig, key)
})
return envConfig
}
/**
* Get user root directory
* @param dir file path
*/
export function getRootPath(...dir: string[]) {
return path.resolve(process.cwd(), ...dir)
}