fix: env in dev:worker

This commit is contained in:
Jean Piere Rodriguez
2025-07-08 10:28:33 -05:00
parent 9e141524fc
commit 3ba5f98eae
2 changed files with 17 additions and 4 deletions
+1 -1
View File
@@ -19,7 +19,7 @@
"cypress:verify": "cypress verify",
"test:e2e": "start-server-and-test dev http://localhost:4321 cypress:run",
"test:e2e:ci": "NODE_TLS_REJECT_UNAUTHORIZED=0 start-server-and-test preview:ci http://localhost:4321 cypress:run",
"dev:worker": "npx wrangler dev --port 4321 --ip 0.0.0.0"
"dev:worker": "npx astro build && npx wrangler dev --port 4321 --ip 0.0.0.0"
},
"dependencies": {
"@astrojs/cloudflare": "^12.6.0",
+16 -3
View File
@@ -1,12 +1,25 @@
import dotenv from "dotenv";
let dotenvLoaded = false;
function loadEnv() {
if (typeof process !== "undefined" && !dotenvLoaded) {
dotenv.config();
dotenvLoaded = true;
}
}
export function getEnvVariable(key: string): string {
loadEnv();
const envVar = import.meta.env?.[key] ?? process.env?.[key];
if (envVar === undefined) {
throw new Error(`Missing env variable: ${key} is not defined`);
throw new Error(`Missing env variable: ${key}`);
}
if (envVar === "") {
throw new Error(`Env variable: ${key} is defined but empty`);
if (envVar === null || envVar === "") {
throw new Error(`Env variable ${key} is empty`);
}
return envVar;