Skip to content

Commit 210a59b

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 require it everywhere. Ensure stepdef and scripts are type-checked, now that they're not compiled.
1 parent 7023fbf commit 210a59b

File tree

14 files changed

+140
-71
lines changed

14 files changed

+140
-71
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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"default": "./dist/src/lib/*.js"
1111
},
1212
"./dbDotEnv": {
13-
"types": "./types/dbDotEnv.d.ts",
14-
"import": "./src/dbDotEnv.mjs",
15-
"require": "./src/dbDotEnv.mjs",
16-
"default": "./src/dbDotEnv.mjs"
13+
"types": "./dist/src/dbDotEnv.d.mts",
14+
"import": "./dist/src/dbDotEnv.mjs",
15+
"require": "./dist/src/dbDotEnv.mjs",
16+
"default": "./dist/src/dbDotEnv.mjs"
1717
},
1818
"./dbTypes": {
1919
"types": "./dist/src/dbTypes.d.ts",
@@ -31,7 +31,7 @@
3131
"dist/src/lib/*.d.ts"
3232
],
3333
"./dbDotEnv": [
34-
"src/dbDotEnv.d.ts"
34+
"dist/src/dbDotEnv.d.mts"
3535
],
3636
"./dbTypes": [
3737
"dist/src/dbTypes.d.ts"
@@ -47,7 +47,7 @@
4747
"compile": "tsc",
4848
"serve": "supabase start && tsx scripts/createEnv.mts && supabase functions serve",
4949
"stop": "supabase stop",
50-
"check-types": "tsc --emitDeclarationOnly --skipLibCheck",
50+
"check-types": "tsc -p tsconfig_lint.json",
5151
"check-schema": "tsx scripts/lint.ts && supabase stop && npm run dbdiff",
5252
"lint": "eslint . && tsx scripts/lint.ts",
5353
"lint:fix": "tsx scripts/lint.ts -f",
@@ -77,6 +77,7 @@
7777
"supabase": "^2.39.2",
7878
"ts-node-maintained": "^10.9.5",
7979
"tsx": "^4.20.3",
80+
"typescript": "^5.9.2",
8081
"vercel": "46.1.1"
8182
},
8283
"prettier": {

packages/database/scripts/build.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/dev.ts

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

55
const __dirname = dirname(__filename);
66
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,20 +18,24 @@ 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
24-
? process.argv[useDbArgPos + 1]
25-
: process.env["SUPABASE_USE_DB"];
34+
(useDbArgPos > 0
35+
? (process.argv[useDbArgPos + 1])
36+
: (process.env["SUPABASE_USE_DB"])) as Variant | undefined;
2637

27-
if (
28-
["local", "branch", "production", "none", "implicit", undefined].indexOf(
29-
variant,
30-
) === -1
31-
) {
38+
if (variant !== undefined && !VARIANTS.has(variant)) {
3239
throw new Error("Invalid variant: " + variant);
3340
}
3441

@@ -57,14 +64,14 @@ export const getVariant = () => {
5764
return variant;
5865
};
5966

60-
export const envFilePath = () => {
67+
export const envFilePath = (): string | null => {
6168
const variant = getVariant();
6269
if (variant === "implicit" || variant === "none") return null;
6370
const name = join(findRoot(), `.env.${variant}`);
6471
return existsSync(name) ? name : null;
6572
};
6673

67-
export const envContents = () => {
74+
export const envContents = (): Partial<Record<string, string>> => {
6875
const path = envFilePath();
6976
if (!path) {
7077
// Fallback to process.env when running in production environments
@@ -81,7 +88,7 @@ export const envContents = () => {
8188

8289
let configDone = false;
8390

84-
export const config = () => {
91+
export const config = (): void => {
8592
if (configDone) return;
8693
const path = envFilePath();
8794
if (path) dotenv.config({ path });

0 commit comments

Comments
 (0)