Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions apps/roam/scripts/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,7 @@ const getVersion = (): string => {
const getBuildDate = (): string => {
return new Date().toISOString().split("T")[0]; // YYYY-MM-DD format
};
let envContents = null;

try {
const dbDotEnv = require("@repo/database/dbDotEnv");
envContents = dbDotEnv.envContents;
} catch (error) {
if ((error as Error).message.includes("Cannot find module")) {
console.error("Build the database module before compiling roam");
process.exit(1);
}
throw error;
}
const { envContents } = require("@repo/database/dbDotEnv");

// https://github.com/evanw/esbuild/issues/337#issuecomment-954633403
const importAsGlobals = (
Expand Down
2 changes: 1 addition & 1 deletion apps/roam/scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { put } from "@vercel/blob";
import fs, { readFileSync } from "fs";
import { join } from "path";
import { compile } from "./compile";
import { config } from "@repo/database/dbDotEnv";
const { config } = require("@repo/database/dbDotEnv");
import { execSync } from "child_process";

if (process.env.NODE_ENV !== "production") {
Expand Down
2 changes: 1 addition & 1 deletion apps/website/app/utils/supabase/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServerClient } from "@supabase/ssr";
import { NextResponse, type NextRequest } from "next/server";
import { envContents } from "@repo/database/dbDotEnv";
const { envContents } = require("@repo/database/dbDotEnv");

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

Expand Down
2 changes: 1 addition & 1 deletion apps/website/app/utils/supabase/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { cookies } from "next/headers";
import type { Database } from "@repo/database/dbTypes";
import { envContents } from "@repo/database/dbDotEnv";
const { envContents } = require("@repo/database/dbDotEnv");

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

Expand Down
2 changes: 1 addition & 1 deletion apps/website/next.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextConfig } from "next";
import { config } from "@repo/database/dbDotEnv";
const { config } = require("@repo/database/dbDotEnv");

config();

Expand Down
3 changes: 3 additions & 0 deletions packages/database/doc/tasks.dot
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ deploy
deploy_functions
migrate
gentypes
bootstrap

/* supabase functions */
node [color=purple]
Expand Down Expand Up @@ -58,6 +59,7 @@ cucumber_js
s_migrate -> migrations_up
dev -> setup
dev -> serve
setup -> bootstrap
setup -> genenv
setup -> migrate
serve -> s_serve
Expand All @@ -83,4 +85,5 @@ cucumber_js
build -> tsc
s_genTypes -> generate_schema
s_migrate -> sup_start
bootstrap -> tsc
}
14 changes: 8 additions & 6 deletions packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,30 @@
"exports": {
"./lib/*": "./src/lib/*.ts",
"./dbDotEnv": {
"types": "./types/dbDotEnv.d.ts",
"import": "./src/dbDotEnv.mjs",
"default": "./src/dbDotEnv.mjs"
"types": "./dist/src/dbDotEnv.d.mts",
"import": "./dist/src/dbDotEnv.mjs",
"require": "./dist/src/dbDotEnv.mjs",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line annoys me; according to coderabbit, I should create a cjs wrapper and use it here.
Removing the line (as I did in the original PR) breaks the build. So may look into wrapping, even though things do work as-is.

"default": "./dist/src/dbDotEnv.mjs"
},
"./dbTypes": "./src/dbTypes.ts",
"./inputTypes": "./src/inputTypes.ts"
},
"typesVersions": {
"*": {
"./dbDotEnv": [
"types/dbDotEnv.d.ts"
"dist/src/dbDotEnv.d.mts"
]
}
},
"scripts": {
"init": "supabase login",
"setup": "pnpm run genenv && pnpm run migrate",
"setup": "pnpm run bootstrap && pnpm run genenv && pnpm run migrate",
"bootstrap": "tsc -p tsconfig_bootstrap.json",
"dev": "pnpm run setup && pnpm run serve",
"build": "pnpm run setup && tsc",
"serve": "tsx scripts/serve.ts",
"stop": "supabase stop",
"check-types": "tsc --noEmit --skipLibCheck",
"check-types": "tsc -p tsconfig_lint.json",
"check-schema": "tsx scripts/lintSchemas.ts && supabase stop && pnpm run dbdiff",
"lint": "eslint . && tsx scripts/lintSchemas.ts && tsx scripts/lintFunctions.ts",
"lint:fix": "eslint --fix . && tsx scripts/lintSchemas.ts -f && tsx scripts/lintFunctions.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/database/scripts/genTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from "node:child_process";
import { writeFileSync } from "fs";
import { join, dirname } from "path";
import { getVariant } from "@repo/database/dbDotEnv";
import { getVariant } from "../src/dbDotEnv.mts";

const __dirname = dirname(__filename);
const projectRoot = join(__dirname, "..");
Expand Down
2 changes: 1 addition & 1 deletion packages/database/scripts/serve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from "node:child_process";
import { writeFileSync } from "fs";
import { join, dirname } from "path";
import { getVariant } from "@repo/database/dbDotEnv";
import { getVariant } from "../src/dbDotEnv.mts";

const __dirname = dirname(__filename);
const projectRoot = join(__dirname, "..");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { join, dirname, basename } from "node:path";
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.
export type Variant =
| "none"
| "local"
| "branch"
| "production"
| "implicit";

const findRoot = () => {
let dir = fileURLToPath(import.meta.url);
Expand All @@ -15,24 +18,28 @@ const findRoot = () => {
return dir;
};

export const getVariant = () => {
const VARIANTS = new Set<Variant>([
"none",
"local",
"branch",
"production",
"implicit",
]);

export const getVariant = (): Variant => {
const processHasVars =
!!process.env["SUPABASE_URL"] && !!process.env["SUPABASE_ANON_KEY"];
const useDbArgPos = (process.argv || []).indexOf("--use-db");
let variant =
useDbArgPos > 0
(useDbArgPos > 0
? process.argv[useDbArgPos + 1]
: process.env["SUPABASE_USE_DB"];
: process.env["SUPABASE_USE_DB"]) as Variant | undefined;
if (variant === undefined) {
dotenv.config();
variant = process.env["SUPABASE_USE_DB"];
variant = process.env["SUPABASE_USE_DB"] as Variant | undefined;
}

if (
["local", "branch", "production", "none", "implicit", undefined].indexOf(
variant,
) === -1
) {
if (variant !== undefined && !VARIANTS.has(variant)) {
throw new Error("Invalid variant: " + variant);
}

Expand Down Expand Up @@ -61,14 +68,14 @@ export const getVariant = () => {
return variant;
};

export const envFilePath = () => {
export const envFilePath = (): string | null => {
const variant = getVariant();
if (variant === "implicit" || variant === "none") return null;
const name = join(findRoot(), `.env.${variant}`);
return existsSync(name) ? name : null;
};

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

let configDone = false;

export const config = () => {
export const config = (): void => {
if (configDone) return;
const path = envFilePath();
if (path) dotenv.config({ path });
Expand Down
7 changes: 1 addition & 6 deletions packages/database/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
"rootDir": ".",
"outDir": "dist"
},
"include": [
"./src/**/*",
"./scripts/**/*",
"./features/step-definitions/**/*",
"./features/support/**/*"
],
"include": ["./src/**/*"],
"exclude": ["supabase"]
}
9 changes: 9 additions & 0 deletions packages/database/tsconfig_bootstrap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@repo/typescript-config/base.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"outDir": "dist"
},
"include": ["./src/dbDotEnv.mts"]
}
20 changes: 20 additions & 0 deletions packages/database/tsconfig_lint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "@repo/typescript-config/base.json",
"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"outDir": "dist",
"allowImportingTsExtensions": true,
"module": "esnext",
"moduleResolution": "bundler",
"declaration": true,
"noEmit": true
},
"include": [
"./src/**/*",
"./scripts/**/*",
"./features/step-definitions/**/*",
"./features/support/**/*"
],
"exclude": ["supabase"]
}
10 changes: 0 additions & 10 deletions packages/database/types/dbDotEnv.d.ts

This file was deleted.

14 changes: 11 additions & 3 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@
],
"tasks": {
"build": {
"dependsOn": ["@repo/database#genenv"],
"dependsOn": ["^build"],
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"outputs": [".next/**", "!.next/cache/**", "dist/**", "src/dbTypes.ts"]
"outputs": [
".next/**",
"!.next/cache/**",
"dist/**",
"src/dbTypes.ts",
".env*"
]
},
"build-schema": {
"inputs": ["$TURBO_DEFAULT$", ".env*"],
Expand All @@ -58,8 +64,10 @@
"passThroughEnv": ["OBSIDIAN_PLUGIN_PATH"],
"cache": false,
"persistent": true,
"inputs": ["$TURBO_DEFAULT$", ".env*"]
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"dependsOn": ["@repo/database#bootstrap"]
},
"bootstrap": {},
"roam#dev": {
"with": ["@repo/database#dev"]
},
Expand Down