Skip to content

Commit a19ccaf

Browse files
committed
ENG-815:
There are circular dependencies within `packages/database`, especially of stepdefs.ts on dbDotEnv, which is why I made it a mjs module. The alternative is to not compile stepdefs; cucumber can use it by reference. Then dbDotEnv can then be a mts module. We then have to compile it first (in a bootstrap stage) and then require it everywhere. Ensure stepdef and scripts are type-checked, now that they're not compiled.
1 parent fff8975 commit a19ccaf

File tree

15 files changed

+79
-59
lines changed

15 files changed

+79
-59
lines changed

apps/roam/scripts/compile.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,7 @@ const getVersion = (): string => {
1818
const getBuildDate = (): string => {
1919
return new Date().toISOString().split("T")[0]; // YYYY-MM-DD format
2020
};
21-
let envContents = null;
22-
23-
try {
24-
const dbDotEnv = require("@repo/database/dbDotEnv");
25-
envContents = dbDotEnv.envContents;
26-
} catch (error) {
27-
if ((error as Error).message.includes("Cannot find module")) {
28-
console.error("Build the database module before compiling roam");
29-
process.exit(1);
30-
}
31-
throw error;
32-
}
21+
const { envContents } = require("@repo/database/dbDotEnv");
3322

3423
// https://github.com/evanw/esbuild/issues/337#issuecomment-954633403
3524
const importAsGlobals = (

apps/roam/scripts/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { put } from "@vercel/blob";
22
import fs, { readFileSync } from "fs";
33
import { join } from "path";
44
import { compile } from "./compile";
5-
import { config } from "@repo/database/dbDotEnv";
5+
const { config } = require("@repo/database/dbDotEnv");
66
import { execSync } from "child_process";
77

88
if (process.env.NODE_ENV !== "production") {

apps/website/app/utils/supabase/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createServerClient } from "@supabase/ssr";
22
import { NextResponse, type NextRequest } from "next/server";
3-
import { envContents } from "@repo/database/dbDotEnv";
3+
const { envContents } = require("@repo/database/dbDotEnv");
44

55
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
66

apps/website/app/utils/supabase/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createServerClient, type CookieOptions } from "@supabase/ssr";
22
import { cookies } from "next/headers";
33
import type { Database } from "@repo/database/dbTypes";
4-
import { envContents } from "@repo/database/dbDotEnv";
4+
const { envContents } = require("@repo/database/dbDotEnv");
55

66
// Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth
77

apps/website/next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NextConfig } from "next";
2-
import { config } from "@repo/database/dbDotEnv";
2+
const { config } = require("@repo/database/dbDotEnv");
33

44
config();
55

packages/database/features/step-definitions/stepdefs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "assert";
22
import { Given, When, Then, world, type DataTable } from "@cucumber/cucumber";
33
import { createClient } from "@supabase/supabase-js";
44
import { Constants, type Database, type Enums } from "@repo/database/dbTypes";
5-
import { getVariant, config } from "@repo/database/dbDotEnv";
5+
const { getVariant, config } = require("@repo/database/dbDotEnv");
66

77
import {
88
spaceAnonUserEmail,

packages/database/package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,30 @@
66
"exports": {
77
"./lib/*": "./src/lib/*.ts",
88
"./dbDotEnv": {
9-
"types": "./types/dbDotEnv.d.ts",
10-
"import": "./src/dbDotEnv.mjs",
11-
"default": "./src/dbDotEnv.mjs"
9+
"types": "./dist/src/dbDotEnv.d.mts",
10+
"import": "./dist/src/dbDotEnv.mjs",
11+
"require": "./dist/src/dbDotEnv.mjs",
12+
"default": "./dist/src/dbDotEnv.mjs"
1213
},
1314
"./dbTypes": "./src/dbTypes.ts",
1415
"./inputTypes": "./src/inputTypes.ts"
1516
},
1617
"typesVersions": {
1718
"*": {
1819
"./dbDotEnv": [
19-
"types/dbDotEnv.d.ts"
20+
"dist/src/dbDotEnv.d.mts"
2021
]
2122
}
2223
},
2324
"scripts": {
2425
"init": "supabase login",
25-
"setup": "npm run genenv && npm run migrate",
26+
"setup": "npm run bootstrap && npm run genenv && npm run migrate",
27+
"bootstrap": "tsc -p tsconfig_bootstrap.json",
2628
"dev": "npm run setup && npm run serve",
2729
"build": "npm run setup && tsc",
2830
"serve": "tsx scripts/serve.ts",
2931
"stop": "supabase stop",
30-
"check-types": "tsc --noEmit --skipLibCheck",
32+
"check-types": "tsc -p tsconfig_lint.json",
3133
"check-schema": "tsx scripts/lintSchemas.ts && supabase stop && npm run dbdiff",
3234
"lint": "eslint . && tsx scripts/lintSchemas.ts && tsx scripts/lintFunctions.ts",
3335
"lint:fix": "eslint --fix . && tsx scripts/lintSchemas.ts -f && tsx scripts/lintFunctions.ts",

packages/database/scripts/genTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execSync } from "node:child_process";
22
import { writeFileSync } from "fs";
33
import { join, dirname } from "path";
4-
import { getVariant } from "@repo/database/dbDotEnv";
4+
import { getVariant } from "../src/dbDotEnv.mts";
55

66
const __dirname = dirname(__filename);
77
const projectRoot = join(__dirname, "..");

packages/database/scripts/serve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execSync } from "node:child_process";
22
import { writeFileSync } from "fs";
33
import { join, dirname } from "path";
4-
import { getVariant } from "@repo/database/dbDotEnv";
4+
import { getVariant } from "../src/dbDotEnv.mts";
55

66
const __dirname = dirname(__filename);
77
const projectRoot = join(__dirname, "..");
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import { join, dirname, basename } from "node:path";
33
import { fileURLToPath } from "node:url";
44
import dotenv from "dotenv";
55

6-
// Note: This file is written as mjs so it can be used before typescript compilation.
7-
// This means the corresponding .d.ts file is currently maintained by hand.
8-
// Remember to update it as needed.
6+
export type Variant =
7+
| "none"
8+
| "local"
9+
| "branch"
10+
| "production"
11+
| "implicit";
912

1013
const findRoot = () => {
1114
let dir = fileURLToPath(import.meta.url);
@@ -15,24 +18,28 @@ const findRoot = () => {
1518
return dir;
1619
};
1720

18-
export const getVariant = () => {
21+
const VARIANTS = new Set<Variant>([
22+
"none",
23+
"local",
24+
"branch",
25+
"production",
26+
"implicit",
27+
]);
28+
29+
export const getVariant = (): Variant => {
1930
const processHasVars =
2031
!!process.env["SUPABASE_URL"] && !!process.env["SUPABASE_ANON_KEY"];
2132
const useDbArgPos = (process.argv || []).indexOf("--use-db");
2233
let variant =
23-
useDbArgPos > 0
34+
(useDbArgPos > 0
2435
? process.argv[useDbArgPos + 1]
25-
: process.env["SUPABASE_USE_DB"];
36+
: process.env["SUPABASE_USE_DB"]) as Variant | undefined;
2637
if (variant === undefined) {
2738
dotenv.config();
28-
variant = process.env["SUPABASE_USE_DB"];
39+
variant = process.env["SUPABASE_USE_DB"] as Variant | undefined;
2940
}
3041

31-
if (
32-
["local", "branch", "production", "none", "implicit", undefined].indexOf(
33-
variant,
34-
) === -1
35-
) {
42+
if (variant !== undefined && !VARIANTS.has(variant)) {
3643
throw new Error("Invalid variant: " + variant);
3744
}
3845

@@ -61,14 +68,14 @@ export const getVariant = () => {
6168
return variant;
6269
};
6370

64-
export const envFilePath = () => {
71+
export const envFilePath = (): string | null => {
6572
const variant = getVariant();
6673
if (variant === "implicit" || variant === "none") return null;
6774
const name = join(findRoot(), `.env.${variant}`);
6875
return existsSync(name) ? name : null;
6976
};
7077

71-
export const envContents = () => {
78+
export const envContents = (): Partial<Record<string, string>> => {
7279
const path = envFilePath();
7380
if (!path) {
7481
// Fallback to process.env when running in production environments
@@ -85,7 +92,7 @@ export const envContents = () => {
8592

8693
let configDone = false;
8794

88-
export const config = () => {
95+
export const config = (): void => {
8996
if (configDone) return;
9097
const path = envFilePath();
9198
if (path) dotenv.config({ path });

0 commit comments

Comments
 (0)