Skip to content
Source
Sign inSign up
samwillis/hackernews-realtime
Open History to see the last commit

hydrate-build-env.mjs

26 lines886 Bytes

1import { chmod, readFile, writeFile } from "node:fs/promises";
2import { parseEnv } from "node:util";
3const environment = process.argv[2] ?? "preview";
4if (!["preview", "production"].includes(environment))
5 throw new Error("Choose preview or production");
6const local = parseEnv(await readFile(".env.local", "utf8"));
7const path = `.vercel/.env.${environment}.local`;
8const pulled = parseEnv(await readFile(path, "utf8"));
9for (const key of [
10 "DATABASE_URL",
11 "DATABASE_URL_UNPOOLED",
12 "NEON_REALTIME_SECRET",
13 "NEXT_PUBLIC_NEON_REALTIME_URL",
14]) {
15 if (!local[key]) throw new Error(`Missing ${key}`);
16 pulled[key] = local[key];
18await writeFile(
19 path,
20 Object.entries(pulled)
21 .map(([key, value]) => `${key}=${JSON.stringify(value)}`)
22 .join("\n") + "\n",
23 { mode: 0o600 },
24);
25await chmod(path, 0o600);
26console.log(`Prepared local ${environment} build environment`);