fix(pw): 🐛 disable emulate and skip .env.local against prod db

bunfig preloads scripts/preload-env.ts which loaded dw .env.local files
BEFORE pw could rename them, so pw inherited the dev DB URL and dev.ts
auto-set EMULATE_GOOGLE_URL. The previous stash-rename approach raced
the preload and lost. Now: preload-env.ts honours PW_MODE=1 and skips
.env.local loading; pw sets EMULATE_GOOGLE_URL='' so dev.ts's ??
fallback can't reinstate emulate; the stash dance is gone. A 'bun pw
restore' command remains for one-time cleanup of pre-fix .pw-stash
files.
This commit is contained in:
amianthus
2026-05-20 14:33:23 +01:00
parent 2d0cf2611d
commit 0512d58303
3 changed files with 47 additions and 90 deletions

View File

@@ -8,27 +8,25 @@ import { loadLocalEnv } from "@server/utils/envUtils.js";
loadLocalEnv();
// Worktree-aware: `bun dw` writes per-worktree `.env.local` files to each
// workspace dir. infisical run + server/.env stop short of these, so things
// like AUTUMN_TEST_BASE_URL and DATABASE_URL never get the worktree value.
// Loading here covers `bun t`, `bun cm`, `bun setup-test`, and any direct
// `bun test` invocation from the repo root. Missing files = no-op (canonical
// repo has none of these).
const __preloadRoot = resolve(
fileURLToPath(new URL(".", import.meta.url)),
"..",
);
for (const rel of [
"server/.env.local",
"vite/.env.local",
"apps/checkout/.env.local",
]) {
const abs = join(__preloadRoot, rel);
if (!existsSync(abs)) continue;
const contents = readFileSync(abs, "utf-8");
for (const line of contents.split(/\r?\n/)) {
const m = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
if (!m) continue;
// Worktree-local overrides win over infisical + server/.env.
process.env[m[1]] = m[2];
// workspace dir. PW_MODE=1 (set by `bun pw`) skips this so prod Infisical
// secrets aren't overridden by dev DB URLs.
if (process.env.PW_MODE !== "1") {
const __preloadRoot = resolve(
fileURLToPath(new URL(".", import.meta.url)),
"..",
);
for (const rel of [
"server/.env.local",
"vite/.env.local",
"apps/checkout/.env.local",
]) {
const abs = join(__preloadRoot, rel);
if (!existsSync(abs)) continue;
const contents = readFileSync(abs, "utf-8");
for (const line of contents.split(/\r?\n/)) {
const m = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
if (!m) continue;
process.env[m[1]] = m[2];
}
}
}