feat(env): handle dynamic env

This commit is contained in:
Michael Zetterberg
2024-05-19 11:00:00 +02:00
parent f27e105898
commit 89cc410b1d
2 changed files with 28 additions and 38 deletions

6
.env
View File

@@ -1,5 +1,3 @@
# See update-dotenv.mjs
ENVTEST="value from .env.api"
PUBLIC_URL="REPLACEME-ON-BUILD-ON-NETLIFY"
NEXTAUTH_URL="REPLACEME-ON-BUILD-ON-NETLIFY"
NEXTAUTH_URL2="REPLACEME-ON-BUILD-ON-NETLIFY"
PUBLIC_URL="https://www.google.com"
NEXTAUTH_URL="https://www.google.com/api/web/auth"

View File

@@ -16,42 +16,34 @@
*/
import fs from "node:fs"
// This is set for stable environments, e.g. test, stage, production
const SCANDIC_ENV_URL = process.env.SCANDIC_ENV_URL
if (process.env.NETLIFY) {
const PUBLIC_URLS = {
production: "https://www.scandichotels.com",
stage: "https://stage.scandichotels.com",
test: "https://test2.scandichotels.com",
}
// This is set for stable environments, e.g. test, stage, production
let NEXTAUTH_URL = process.env.NEXTAUTH_URL
let PUBLIC_URL
if (PUBLIC_URLS[process.env.CONTEXT]) {
PUBLIC_URL = PUBLIC_URLS[process.env.CONTEXT]
} else if (PUBLIC_URLS[process.env.BRANCH]) {
PUBLIC_URL = PUBLIC_URLS[process.env.BRANCH]
} else {
PUBLIC_URL = process.env.DEPLOY_PRIME_URL
}
// This is used for preview deployments
const DEPLOY_PRIME_URL = process.env.DEPLOY_PRIME_URL
const NEXTAUTH_URL = `${PUBLIC_URL}/api/web/auth`
const URL = SCANDIC_ENV_URL || DEPLOY_PRIME_URL
let contents = fs.readFileSync("./.env", { encoding: "utf-8" })
if (!NEXTAUTH_URL) {
NEXTAUTH_URL = URL + "/api/web/auth"
console.log({ pre_contents: contents })
console.log({ process_env: process.env })
contents = contents.replace(/PUBLIC_URL=.*/, `PUBLIC_URL="${PUBLIC_URL}"`)
contents = contents.replace(
/NEXTAUTH_URL=.*/,
`NEXTAUTH_URL="${NEXTAUTH_URL}"`
)
console.log({ post_contents: contents })
fs.writeFileSync("./.env", contents, { encoding: "utf-8" })
}
let contents = fs.readFileSync("./.env", { encoding: "utf-8" })
console.log({ pre_contents: contents })
console.log({
SCANDIC_ENV_URL,
NEXTAUTH_URL,
DEPLOY_PRIME_URL,
URL,
})
contents = contents.replace(
// 'URL="REPLACEME-ON-BUILD-ON-NETLIFY"',
/URL="[^"]+"/,
`URL="${URL}"`
)
contents = contents.replace(
// 'NEXTAUTH_URL="REPLACEME-ON-BUILD-ON-NETLIFY"',
/NEXTAUTH_URL="[^"]+"/,
`NEXTAUTH_URL="${NEXTAUTH_URL}"`
)
console.log({ post_contents: contents })
fs.writeFileSync("./.env", contents, { encoding: "utf-8" })