feat: update env script
This commit is contained in:
parent
e108617182
commit
ea3f54f28f
@ -9,7 +9,7 @@
|
|||||||
"lint": "next lint",
|
"lint": "next lint",
|
||||||
"fix": "next lint --fix",
|
"fix": "next lint --fix",
|
||||||
"eslint-fix": "eslint --fix",
|
"eslint-fix": "eslint --fix",
|
||||||
"prepare": "cp .env.example .env && cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install ./web/.husky",
|
"prepare": "node ./scripts/update-env.mjs && cd ../ && node -e \"if (process.env.NODE_ENV !== 'production'){process.exit(1)} \" || husky install ./web/.husky",
|
||||||
"gen-icons": "node ./app/components/base/icons/script.js",
|
"gen-icons": "node ./app/components/base/icons/script.js",
|
||||||
"uglify-embed": "node ./bin/uglify-embed",
|
"uglify-embed": "node ./bin/uglify-embed",
|
||||||
"check-i18n": "node ./i18n/script.js"
|
"check-i18n": "node ./i18n/script.js"
|
||||||
|
|||||||
37
web/scripts/update-env.mjs
Normal file
37
web/scripts/update-env.mjs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import * as fs from 'node:fs'
|
||||||
|
|
||||||
|
function parseEnvFile(contents) {
|
||||||
|
return contents.split('\n').reduce((acc, line) => {
|
||||||
|
const [key, value] = line.split('=')
|
||||||
|
if (key)
|
||||||
|
acc.set(key.trim(), value === undefined ? '' : value.trim())
|
||||||
|
|
||||||
|
return acc
|
||||||
|
}, new Map())
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateEnvironmentFile() {
|
||||||
|
const examplePath = '.env.example'
|
||||||
|
const envPath = '.env'
|
||||||
|
|
||||||
|
const exampleContents = fs.readFileSync(examplePath, 'utf-8')
|
||||||
|
const envContents = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf-8') : ''
|
||||||
|
|
||||||
|
const exampleConfig = parseEnvFile(exampleContents)
|
||||||
|
const envConfig = parseEnvFile(envContents)
|
||||||
|
|
||||||
|
exampleConfig.forEach((value, key) => {
|
||||||
|
if (!envConfig.has(key))
|
||||||
|
envConfig.set(key, value)
|
||||||
|
})
|
||||||
|
|
||||||
|
// 生成新的 .env 内容
|
||||||
|
const newEnvContents = Array.from(envConfig.entries())
|
||||||
|
.map(([key, value]) => `${key}=${value}`)
|
||||||
|
.join('\n')
|
||||||
|
|
||||||
|
fs.writeFileSync(envPath, newEnvContents)
|
||||||
|
console.log('.env file has been updated')
|
||||||
|
}
|
||||||
|
|
||||||
|
updateEnvironmentFile()
|
||||||
Loading…
x
Reference in New Issue
Block a user