Files
moreminimore-vibe/src/ipc/handlers/app_env_vars_handlers.ts
Will Chen b0f08eaf15 Neon / portal template support (#713)
TODOs:
- [x] Do restart when checkout / restore if there is a DB
- [x] List all branches (branch id, name, date)
- [x] Allow checking out versions with no DB
- [x] safeguard to never delete main branches
- [x] create app hook for neon template
- [x] weird UX with connector on configure panel
- [x] tiny neon logo in connector
- [x] deploy to vercel
- [x] build forgot password page
- [x] what about email setup
- [x] lots of imgix errors
- [x] edit file - db snapshot
- [x] DYAD_DISABLE_DB_PUSH
- [ ] update portal doc
- [x] switch preview branch to be read-only endpoint
- [x] disable supabase sys prompt if neon is enabled
- [ ] https://payloadcms.com/docs/upload/storage-adapters
- [x] need to use main branch...

Phase 2?
- [x] generate DB migrations
2025-08-04 16:36:09 -07:00

86 lines
2.5 KiB
TypeScript

/**
* DO NOT USE LOGGER HERE.
* Environment variables are sensitive and should not be logged.
*/
import { ipcMain } from "electron";
import * as fs from "fs";
import * as path from "path";
import { db } from "../../db";
import { apps } from "../../db/schema";
import { eq } from "drizzle-orm";
import { getDyadAppPath } from "../../paths/paths";
import { GetAppEnvVarsParams, SetAppEnvVarsParams } from "../ipc_types";
import {
ENV_FILE_NAME,
parseEnvFile,
serializeEnvFile,
} from "../utils/app_env_var_utils";
export function registerAppEnvVarsHandlers() {
// Handler to get app environment variables
ipcMain.handle(
"get-app-env-vars",
async (event, { appId }: GetAppEnvVarsParams) => {
try {
const app = await db.query.apps.findFirst({
where: eq(apps.id, appId),
});
if (!app) {
throw new Error("App not found");
}
const appPath = getDyadAppPath(app.path);
const envFilePath = path.join(appPath, ENV_FILE_NAME);
// If .env.local doesn't exist, return empty array
try {
await fs.promises.access(envFilePath);
} catch {
return [];
}
const content = await fs.promises.readFile(envFilePath, "utf8");
const envVars = parseEnvFile(content);
return envVars;
} catch (error) {
console.error("Error getting app environment variables:", error);
throw new Error(
`Failed to get environment variables: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}
},
);
// Handler to set app environment variables
ipcMain.handle(
"set-app-env-vars",
async (event, { appId, envVars }: SetAppEnvVarsParams) => {
try {
const app = await db.query.apps.findFirst({
where: eq(apps.id, appId),
});
if (!app) {
throw new Error("App not found");
}
const appPath = getDyadAppPath(app.path);
const envFilePath = path.join(appPath, ENV_FILE_NAME);
// Serialize environment variables to .env.local format
const content = serializeEnvFile(envVars);
// Write to .env.local file
await fs.promises.writeFile(envFilePath, content, "utf8");
} catch (error) {
console.error("Error setting app environment variables:", error);
throw new Error(
`Failed to set environment variables: ${error instanceof Error ? error.message : "Unknown error"}`,
);
}
},
);
}