chore(debug): envs in api functions
This commit is contained in:
48
update-dotenv.mjs
Normal file
48
update-dotenv.mjs
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* We define SCANDIC_ENV_URL for stable environments on Netlify:
|
||||
* production, stage and test. Avoid using SCANDIC_ENV_URL locally.
|
||||
* For deployments to those branches we have SCANDIC_ENV_URL defined.
|
||||
* Otherwise we fallback to DEPLOY_PRIME_URL from Netlify built-in variables.
|
||||
* Locally we set DEPLOY_PRIME_URL
|
||||
*
|
||||
* We set NEXTAUTH_URL here because build time environment variables are not
|
||||
* available runtime in Netlify functions. This leads to use being unable to
|
||||
* login on preview deployments.
|
||||
*
|
||||
* This approach updates the .env file in the root of the repo and tells
|
||||
* Netlify to include this file in the bundled function output
|
||||
* (see netlify.toml). This way Next.js will read .env runtime and update the
|
||||
* environment accordingly making the value available at runtime.
|
||||
*/
|
||||
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
|
||||
|
||||
// This is set for stable environments, e.g. test, stage, production
|
||||
let NEXTAUTH_URL = process.env.NEXTAUTH_URL
|
||||
|
||||
// This is used for preview deployments
|
||||
const DEPLOY_PRIME_URL = process.env.DEPLOY_PRIME_URL
|
||||
|
||||
const URL = SCANDIC_ENV_URL || DEPLOY_PRIME_URL
|
||||
|
||||
if (!NEXTAUTH_URL) {
|
||||
NEXTAUTH_URL = URL + "/api/web/auth"
|
||||
}
|
||||
|
||||
let contents = fs.readFileSync("./.env", { encoding: "utf-8" })
|
||||
|
||||
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}"`
|
||||
)
|
||||
|
||||
fs.writeFileSync("./.env", contents, { encoding: "utf-8" })
|
||||
Reference in New Issue
Block a user