diff --git a/bun.lockb b/bun.lockb index 9ae8371..95a973b 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index bf519bb..27a6143 100644 --- a/package.json +++ b/package.json @@ -16,12 +16,13 @@ "bun-types": "latest" }, "peerDependencies": { - "typescript": "^5.0.0" + "typescript": "^5.5.2" }, "dependencies": { "brotli-wasm": "^2.0.1", + "dotenv": "^16.4.5", "exit-hook": "^4.0.0", - "firebase-admin": "^12.0.0", - "prisma": "^5.8.1" + "firebase-admin": "^12.2.0", + "prisma": "^5.16.0" } } \ No newline at end of file diff --git a/pkgs/api/_deploy.ts b/pkgs/api/_deploy.ts index ea791c0..a5a8bb5 100644 --- a/pkgs/api/_deploy.ts +++ b/pkgs/api/_deploy.ts @@ -5,7 +5,7 @@ import { apiContext } from "service-srv"; import { deploy } from "utils/deploy"; import { dir } from "utils/dir"; import { g } from "utils/global"; -import { parse } from "utils/parse-env"; +import { genEnv, parseEnv } from "utils/parse-env"; import { restartServer } from "utils/restart"; export const _ = { @@ -68,12 +68,11 @@ export const _ = { case "db-update": if (action.url) { g.dburl = action.url; - await Bun.write( - dir("app/db/.env"), - `\ -DATABASE_URL="${action.url}" -` - ); + const env = genEnv({ + ...parseEnv(await Bun.file(dir(".env")).text()), + DATABASE_URL: action.url, + }); + await Bun.write(dir(".env"), env); } return "ok"; case "db-gen": @@ -88,9 +87,9 @@ DATABASE_URL="${action.url}" break; case "db-pull": { - const env = await readAsync(dir("app/db/.env")); + const env = await readAsync(dir(".env")); if (env) { - const ENV = parse(env); + const ENV = parseEnv(env); if (typeof ENV.DATABASE_URL === "string") { const type = ENV.DATABASE_URL.split("://").shift(); if (type) { @@ -194,7 +193,7 @@ export const downloadFile = async ( _url.hostname = "127.0.0.1"; } g.log.info(`Downloading ${url} to ${filePath}`); - const res = await fetch(_url); + const res = await fetch(_url as any); if (res.body) { const file = Bun.file(filePath); diff --git a/pkgs/index.ts b/pkgs/index.ts index 9e95ac3..5f0817d 100644 --- a/pkgs/index.ts +++ b/pkgs/index.ts @@ -11,6 +11,7 @@ import { prepareAPITypes } from "./server/prep-api-ts"; import { config } from "./utils/config"; import { g } from "./utils/global"; import { createLogger } from "./utils/logger"; +import { genEnv, parseEnv } from "utils/parse-env"; g.mode = process.argv.includes("dev") ? "dev" : "prod"; g.datadir = g.mode === "prod" ? "../data" : ".data"; @@ -29,7 +30,8 @@ if (!(await existsAsync(dir("app/srv")))) { if (!process.env.PORT) { g.port = 3000; - await Bun.write(".env", `PORT=${g.port}`); + const env = genEnv({ ...parseEnv(".env"), PORT: g.port }); + await Bun.write(".env", env); } else { g.port = parseInt(process.env.PORT); } @@ -46,7 +48,6 @@ await config.init(); g.log.info(g.mode === "dev" ? "DEVELOPMENT" : "PRODUCTION"); - await deploy.init(); if (g.mode === "dev") { await startDevWatcher(); diff --git a/pkgs/prod.ts b/pkgs/prod.ts index 56839ea..62bc1cd 100644 --- a/pkgs/prod.ts +++ b/pkgs/prod.ts @@ -4,6 +4,7 @@ import exitHook from "exit-hook"; import { existsAsync } from "fs-jetpack"; import { dir } from "utils/dir"; import { checkPort, randomBetween } from "utils/ensure"; +import "dotenv/config"; let port = 0; @@ -35,7 +36,7 @@ const main = { console.log("Process Manager running at port:", port); -if (await existsAsync(dir("app/db/.env"))) { +if (process.env.DATABASE_URL) { if (!(await existsAsync(dir("node_modules/.prisma")))) { try { await $({ cwd: dir(`app/db`) })`bun install`; diff --git a/pkgs/utils/parse-env.ts b/pkgs/utils/parse-env.ts index a03ed2f..7f74341 100644 --- a/pkgs/utils/parse-env.ts +++ b/pkgs/utils/parse-env.ts @@ -20,7 +20,7 @@ function removeQuotes(str: string) { } /** Parse an envfile string. */ -export function parse(src: string): Data { +export function parseEnv(src: string): Data { const result: Data = {}; const lines = splitInLines(src); for (const line of lines) { @@ -35,7 +35,7 @@ export function parse(src: string): Data { } /** Turn an object into an envfile string. */ -export function stringify(obj: Input): string { +export function genEnv(obj: Input): string { let result = ""; for (const [key, value] of Object.entries(obj)) { if (key) { diff --git a/pkgs/utils/prisma.ts b/pkgs/utils/prisma.ts index bcfc9ac..c39bd90 100644 --- a/pkgs/utils/prisma.ts +++ b/pkgs/utils/prisma.ts @@ -4,12 +4,12 @@ import { $ } from "execa"; import { g } from "./global"; export const preparePrisma = async () => { - if ((await existsAsync(dir("app/db/.env"))) && !g.db) { + if (process.env.DATABASE_URL && !g.db) { try { if (g.mode !== "dev") { await $({ cwd: dir(`app/db`) })`bun prisma generate`; } - + const { PrismaClient } = await import("../../app/db/db"); g.db = new PrismaClient(); } catch (e) {