Skip to content

Commit 82f0e22

Browse files
authored
ENG-813 repair and improvements to dbDotEnv (#400)
* eng-813 repair dbDotEnv * eng-813: Repair dbDotEnv Replace __filename with urlFromPath, as it seems to sometimes be called as ejs. Use SUPABASE variables when defined. (It means that the database _will_ be used if those are defined in a `.env` file.) Give warnings to the developer as appropriate. nextjs should use dbDotEnv. * coderabbit suggestions
1 parent 8072bca commit 82f0e22

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

apps/website/next.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import type { NextConfig } from "next";
2+
import { config } from "@repo/database/dbDotEnv";
23

3-
const nextConfig = {
4+
config();
5+
6+
const nextConfig: NextConfig = {
47
reactStrictMode: true,
58
serverRuntimeConfig: {
69
maxDuration: 300,

packages/database/src/dbDotEnv.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,55 @@
1+
import { readFileSync, existsSync } from "node:fs";
2+
import { join, dirname, basename } from "node:path";
3+
import { fileURLToPath } from "node:url";
14
import dotenv from "dotenv";
2-
import { readFileSync, existsSync } from "fs";
3-
import { join, dirname, basename } from "path";
45

56
const findRoot = () => {
6-
let dir = __filename;
7+
let dir = fileURLToPath(import.meta.url);
78
while (basename(dir) !== "database") {
89
dir = dirname(dir);
910
}
1011
return dir;
1112
};
1213

1314
export const getVariant = () => {
14-
if (process.env.HOME === "/vercel" || process.env.GITHUB_ACTIONS === "true")
15-
return "implicit";
16-
const useDbArgPos = process.argv.indexOf("--use-db");
17-
const variant =
18-
(useDbArgPos > 0
15+
const processHasVars =
16+
!!process.env["SUPABASE_URL"] && !!process.env["SUPABASE_ANON_KEY"];
17+
const useDbArgPos = (process.argv || []).indexOf("--use-db");
18+
let variant =
19+
useDbArgPos > 0
1920
? process.argv[useDbArgPos + 1]
20-
: process.env["SUPABASE_USE_DB"]) || "none";
21+
: process.env["SUPABASE_USE_DB"];
2122

22-
if (["local", "branch", "production", "none"].indexOf(variant) === -1) {
23+
if (
24+
["local", "branch", "production", "none", "implicit", undefined].indexOf(
25+
variant,
26+
) === -1
27+
) {
2328
throw new Error("Invalid variant: " + variant);
2429
}
30+
31+
if (process.env.HOME === "/vercel" || process.env.GITHUB_ACTIONS === "true") {
32+
// deployment should have variables
33+
if (!processHasVars) {
34+
console.error("Missing SUPABASE variables in deployment");
35+
variant = "none";
36+
} else {
37+
variant = "implicit";
38+
}
39+
}
40+
if (variant === undefined) {
41+
if (processHasVars) {
42+
console.warn(
43+
"please define explicitly which database to use (set SUPABASE_USE_DB)",
44+
);
45+
variant = "implicit";
46+
} else {
47+
console.warn("Not using the database");
48+
variant = "none";
49+
}
50+
}
51+
// avoid repeating warnings
52+
process.env["SUPABASE_USE_DB"] = variant;
2553
return variant;
2654
};
2755

@@ -34,13 +62,15 @@ export const envFilePath = () => {
3462

3563
export const envContents = () => {
3664
const path = envFilePath();
37-
if (!path)
65+
if (!path) {
3866
// Fallback to process.env when running in production environments
39-
return {
67+
const raw = {
4068
SUPABASE_URL: process.env.SUPABASE_URL,
4169
SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY,
4270
NEXT_API_ROOT: process.env.NEXT_API_ROOT,
4371
};
72+
return Object.fromEntries(Object.entries(raw).filter(([, v]) => !!v));
73+
}
4474
const data = readFileSync(path, "utf8");
4575
return dotenv.parse(data);
4676
};

packages/database/types/dbDotEnv.d.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,8 @@ export type Variant =
33
| "local"
44
| "branch"
55
| "production"
6-
| "all"
76
| "implicit";
87
export declare const getVariant: () => Variant;
98
export declare const envFilePath: () => string | null;
10-
export declare const envContents: () =>
11-
| EnvMap
12-
| {
13-
SUPABASE_URL: string | undefined;
14-
SUPABASE_ANON_KEY: string | undefined;
15-
NEXT_API_ROOT: string | undefined;
16-
};
9+
export declare const envContents: () => Partial<Record<string, string>>;
1710
export declare const config: () => void;

0 commit comments

Comments
 (0)