feat(env): handle dynamic env
This commit is contained in:
6
.env
6
.env
@@ -1,5 +1,3 @@
|
|||||||
# See update-dotenv.mjs
|
# See update-dotenv.mjs
|
||||||
ENVTEST="value from .env.api"
|
PUBLIC_URL="https://www.google.com"
|
||||||
PUBLIC_URL="REPLACEME-ON-BUILD-ON-NETLIFY"
|
NEXTAUTH_URL="https://www.google.com/api/web/auth"
|
||||||
NEXTAUTH_URL="REPLACEME-ON-BUILD-ON-NETLIFY"
|
|
||||||
NEXTAUTH_URL2="REPLACEME-ON-BUILD-ON-NETLIFY"
|
|
||||||
|
|||||||
@@ -16,42 +16,34 @@
|
|||||||
*/
|
*/
|
||||||
import fs from "node:fs"
|
import fs from "node:fs"
|
||||||
|
|
||||||
// This is set for stable environments, e.g. test, stage, production
|
if (process.env.NETLIFY) {
|
||||||
const SCANDIC_ENV_URL = process.env.SCANDIC_ENV_URL
|
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 PUBLIC_URL
|
||||||
let NEXTAUTH_URL = process.env.NEXTAUTH_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 NEXTAUTH_URL = `${PUBLIC_URL}/api/web/auth`
|
||||||
const DEPLOY_PRIME_URL = process.env.DEPLOY_PRIME_URL
|
|
||||||
|
|
||||||
const URL = SCANDIC_ENV_URL || DEPLOY_PRIME_URL
|
let contents = fs.readFileSync("./.env", { encoding: "utf-8" })
|
||||||
|
|
||||||
if (!NEXTAUTH_URL) {
|
console.log({ pre_contents: contents })
|
||||||
NEXTAUTH_URL = URL + "/api/web/auth"
|
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" })
|
|
||||||
|
|||||||
Reference in New Issue
Block a user