开始多平台 多店铺整改
This commit is contained in:
@@ -7,13 +7,17 @@ import { fileURLToPath } from 'node:url'
|
||||
const require = createRequire(import.meta.url)
|
||||
const CURRENT_DIR = path.dirname(fileURLToPath(import.meta.url))
|
||||
export const PROJECT_ROOT = path.resolve(CURRENT_DIR, '../..')
|
||||
const WORKSPACE_ROOT = path.resolve(PROJECT_ROOT, '../..')
|
||||
const CONFIG_ROOT = path.join(PROJECT_ROOT, 'config')
|
||||
|
||||
const defaultConfig = loadConfig(path.join(CONFIG_ROOT, 'default.cjs'))
|
||||
const localConfig = loadConfig(path.join(CONFIG_ROOT, 'local.cjs'))
|
||||
const mergedConfig = deepMerge(defaultConfig, localConfig)
|
||||
loadEnvFiles([
|
||||
path.join(WORKSPACE_ROOT, '.env'),
|
||||
path.join(PROJECT_ROOT, '.env'),
|
||||
])
|
||||
|
||||
export const runtimeConfig = applyEnvOverrides(mergedConfig)
|
||||
const defaultConfig = loadConfig(path.join(CONFIG_ROOT, 'default.cjs'))
|
||||
|
||||
export const runtimeConfig = applyEnvOverrides(defaultConfig)
|
||||
|
||||
function loadConfig(configPath) {
|
||||
if (!fs.existsSync(configPath)) {
|
||||
@@ -24,6 +28,57 @@ function loadConfig(configPath) {
|
||||
return isPlainObject(loaded) ? loaded : {}
|
||||
}
|
||||
|
||||
function loadEnvFiles(filePaths) {
|
||||
for (const filePath of filePaths) {
|
||||
loadEnvFile(filePath)
|
||||
}
|
||||
}
|
||||
|
||||
function loadEnvFile(filePath) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return
|
||||
}
|
||||
|
||||
const rawText = fs.readFileSync(filePath, 'utf8')
|
||||
const lines = rawText.split(/\r?\n/)
|
||||
|
||||
for (const rawLine of lines) {
|
||||
const line = rawLine.trim()
|
||||
|
||||
if (!line || line.startsWith('#')) {
|
||||
continue
|
||||
}
|
||||
|
||||
const separatorIndex = line.indexOf('=')
|
||||
if (separatorIndex <= 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const key = line.slice(0, separatorIndex).trim()
|
||||
if (!key || key in process.env) {
|
||||
continue
|
||||
}
|
||||
|
||||
const value = parseEnvValue(line.slice(separatorIndex + 1))
|
||||
process.env[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
function parseEnvValue(rawValue) {
|
||||
const value = String(rawValue || '').trim()
|
||||
|
||||
if (!value) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const quote = value[0]
|
||||
if ((quote === '"' || quote === "'") && value.endsWith(quote)) {
|
||||
return value.slice(1, -1)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
function applyEnvOverrides(baseConfig) {
|
||||
const nextConfig = deepMerge(baseConfig, {})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user