-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdbDotEnv.mjs
More file actions
102 lines (94 loc) · 3 KB
/
dbDotEnv.mjs
File metadata and controls
102 lines (94 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { readFileSync, existsSync } from "node:fs";
import { join, dirname, basename } from "node:path";
import process from "node:process";
import console from "node:console";
import { fileURLToPath } from "node:url";
import dotenv from "dotenv";
// Note: This file is written as mjs so it can be used before typescript compilation.
// This means the corresponding .d.ts file is currently maintained by hand.
// Remember to update it as needed.
const findRoot = () => {
let dir = fileURLToPath(import.meta.url);
while (basename(dir) !== "database") {
dir = dirname(dir);
}
return dir;
};
export const getVariant = () => {
const useDbArgPos = (process.argv || []).indexOf("--use-db");
let variant =
useDbArgPos > 0
? process.argv[useDbArgPos + 1]
: process.env["SUPABASE_USE_DB"];
if (variant === undefined) {
dotenv.config();
const dbGlobalEnv = join(findRoot(), ".env");
if (existsSync(dbGlobalEnv)) dotenv.config({ path: dbGlobalEnv });
variant = process.env["SUPABASE_USE_DB"];
}
const processHasVars =
!!process.env["SUPABASE_URL"] && !!process.env["SUPABASE_PUBLISHABLE_KEY"];
if (
["local", "branch", "production", "none", "implicit", undefined].indexOf(
variant,
) === -1
) {
throw new Error("Invalid variant: " + variant);
}
if (
process.env.HOME === "/vercel" ||
(process.env.GITHUB_ACTIONS === "true" &&
process.env.GITHUB_TEST !== "test")
) {
// deployment should have variables
if (!processHasVars) {
console.error("Missing SUPABASE variables in deployment");
variant = "none";
} else {
variant = "implicit";
}
}
if (variant === undefined) {
if (processHasVars) {
console.warn(
"please define explicitly which database to use (set SUPABASE_USE_DB)",
);
variant = "implicit";
} else {
console.warn("Not using the database");
variant = "none";
}
}
// avoid repeating warnings
process.env["SUPABASE_USE_DB"] = variant;
return variant;
};
export const envFilePath = () => {
const variant = getVariant();
if (variant === "implicit" || variant === "none") return null;
const name = join(findRoot(), `.env.${variant}`);
return existsSync(name) ? name : null;
};
export const envContents = () => {
const path = envFilePath();
if (!path) {
// Fallback to process.env when running in production environments
const raw = {
/* eslint-disable @typescript-eslint/naming-convention */
SUPABASE_URL: process.env.SUPABASE_URL,
SUPABASE_PUBLISHABLE_KEY: process.env.SUPABASE_PUBLISHABLE_KEY,
NEXT_API_ROOT: process.env.NEXT_API_ROOT,
/* eslint-enable @typescript-eslint/naming-convention */
};
return Object.fromEntries(Object.entries(raw).filter(([, v]) => !!v));
}
const data = readFileSync(path, "utf8");
return dotenv.parse(data);
};
let configDone = false;
export const config = () => {
if (configDone) return;
const path = envFilePath();
if (path) dotenv.config({ path });
configDone = true;
};