Simplify auth setup, centralize environment variable management and fix

create readme
This commit is contained in:
Aman Varshney
2025-03-19 13:33:51 +05:30
parent d0540e41cd
commit 03d9559e55
11 changed files with 170 additions and 222 deletions

View File

@@ -0,0 +1,65 @@
import path from "node:path";
import fs from "fs-extra";
import type { ProjectConfig } from "../types";
import { generateAuthSecret } from "./auth-setup";
export async function setupEnvironmentVariables(
projectDir: string,
options: ProjectConfig,
): Promise<void> {
const serverDir = path.join(projectDir, "packages/server");
const clientDir = path.join(projectDir, "packages/client");
// Set up server env variables
const envPath = path.join(serverDir, ".env");
let envContent = "";
if (await fs.pathExists(envPath)) {
envContent = await fs.readFile(envPath, "utf8");
}
// Auth environment variables
if (options.auth) {
if (!envContent.includes("BETTER_AUTH_SECRET")) {
envContent += `\nBETTER_AUTH_SECRET=${generateAuthSecret()}`;
}
if (!envContent.includes("BETTER_AUTH_URL")) {
envContent += "\nBETTER_AUTH_URL=http://localhost:3000";
}
if (!envContent.includes("CORS_ORIGIN")) {
envContent += "\nCORS_ORIGIN=http://localhost:3001";
}
}
// Database environment variables
if (options.database !== "none") {
if (options.orm === "prisma" && !envContent.includes("DATABASE_URL")) {
const databaseUrlLine =
options.database === "sqlite"
? `\nDATABASE_URL="file:./dev.db"`
: `\nDATABASE_URL="postgresql://postgres:postgres@localhost:5432/mydb?schema=public"`;
envContent += databaseUrlLine;
}
if (
options.database === "sqlite" &&
options.turso &&
!envContent.includes("TURSO_CONNECTION_URL")
) {
envContent += "\nTURSO_CONNECTION_URL=http://127.0.0.1:8080";
}
}
await fs.writeFile(envPath, envContent.trim());
// Set up client env variables
if (options.auth) {
const clientEnvPath = path.join(clientDir, ".env");
if (!(await fs.pathExists(clientEnvPath))) {
const clientEnvContent = "VITE_SERVER_URL=http://localhost:3000\n";
await fs.writeFile(clientEnvPath, clientEnvContent);
}
}
}