mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
Refactor package dependency management and environment setup
This commit is contained in:
@@ -27,12 +27,10 @@ export async function setupAuth(
|
||||
}
|
||||
addPackageDependency({
|
||||
dependencies: ["better-auth"],
|
||||
devDependencies: false,
|
||||
projectDir: serverDir,
|
||||
});
|
||||
addPackageDependency({
|
||||
dependencies: ["better-auth"],
|
||||
devDependencies: false,
|
||||
projectDir: clientDir,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -62,6 +62,33 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
||||
await setupAddons(projectDir, options.addons);
|
||||
}
|
||||
|
||||
await updatePackageConfigurations(projectDir, options);
|
||||
|
||||
await createReadme(projectDir, options);
|
||||
|
||||
displayPostInstallInstructions(
|
||||
options.database,
|
||||
options.projectName,
|
||||
options.packageManager,
|
||||
!options.noInstall,
|
||||
options.orm,
|
||||
);
|
||||
|
||||
return projectDir;
|
||||
} catch (error) {
|
||||
s.message(pc.red("Failed"));
|
||||
if (error instanceof Error) {
|
||||
cancel(pc.red(`Error during project creation: ${error.message}`));
|
||||
process.exit(1);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function updatePackageConfigurations(
|
||||
projectDir: string,
|
||||
options: ProjectConfig,
|
||||
) {
|
||||
const rootPackageJsonPath = path.join(projectDir, "package.json");
|
||||
if (await fs.pathExists(rootPackageJsonPath)) {
|
||||
const packageJson = await fs.readJson(rootPackageJsonPath);
|
||||
@@ -90,13 +117,13 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
||||
|
||||
if (options.database !== "none") {
|
||||
if (options.database === "sqlite") {
|
||||
serverPackageJson.scripts["db:local"] =
|
||||
"turso dev --db-file local.db";
|
||||
serverPackageJson.scripts["db:local"] = "turso dev --db-file local.db";
|
||||
}
|
||||
|
||||
if (options.auth) {
|
||||
serverPackageJson.scripts["auth:generate"] =
|
||||
"npx @better-auth/cli generate --output ./src/db/auth-schema.ts";
|
||||
}
|
||||
|
||||
if (options.orm === "prisma") {
|
||||
serverPackageJson.scripts["db:push"] = "prisma db push";
|
||||
@@ -105,41 +132,12 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
||||
serverPackageJson.scripts["db:push"] = "drizzle-kit push";
|
||||
serverPackageJson.scripts["db:studio"] = "drizzle-kit studio";
|
||||
}
|
||||
} else {
|
||||
if (options.orm === "prisma") {
|
||||
serverPackageJson.scripts["db:push"] = "prisma db push";
|
||||
serverPackageJson.scripts["db:studio"] = "prisma studio";
|
||||
} else if (options.orm === "drizzle") {
|
||||
serverPackageJson.scripts["db:push"] = "drizzle-kit push";
|
||||
serverPackageJson.scripts["db:studio"] = "drizzle-kit studio";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await fs.writeJson(serverPackageJsonPath, serverPackageJson, {
|
||||
spaces: 2,
|
||||
});
|
||||
}
|
||||
|
||||
await createReadme(projectDir, options);
|
||||
|
||||
displayPostInstallInstructions(
|
||||
options.database,
|
||||
options.projectName,
|
||||
options.packageManager,
|
||||
!options.noInstall,
|
||||
options.orm,
|
||||
);
|
||||
|
||||
return projectDir;
|
||||
} catch (error) {
|
||||
s.message(pc.red("Failed"));
|
||||
if (error instanceof Error) {
|
||||
cancel(pc.red(`Error during project creation: ${error.message}`));
|
||||
process.exit(1);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function getOrmTemplateDir(orm: ProjectOrm, database: ProjectDatabase): string {
|
||||
|
||||
@@ -69,10 +69,10 @@ ${generateScriptsList(packageManagerRunCmd, database, orm, auth)}
|
||||
}
|
||||
|
||||
function generateFeaturesList(
|
||||
database: string,
|
||||
database: ProjectDatabase,
|
||||
auth: boolean,
|
||||
features: string[],
|
||||
orm: string,
|
||||
orm: ProjectOrm,
|
||||
): string {
|
||||
const featuresList = [
|
||||
"- **TypeScript** - For type safety and improved developer experience",
|
||||
|
||||
@@ -2,13 +2,14 @@ import path from "node:path";
|
||||
import { log, spinner } from "@clack/prompts";
|
||||
import fs from "fs-extra";
|
||||
import pc from "picocolors";
|
||||
import type { ProjectDatabase, ProjectOrm } from "../types";
|
||||
import { addPackageDependency } from "../utils/add-package-deps";
|
||||
import { setupTurso } from "./turso-setup";
|
||||
|
||||
export async function setupDatabase(
|
||||
projectDir: string,
|
||||
databaseType: string,
|
||||
orm: string,
|
||||
databaseType: ProjectDatabase,
|
||||
orm: ProjectOrm,
|
||||
setupTursoDb = true,
|
||||
): Promise<void> {
|
||||
const s = spinner();
|
||||
@@ -23,49 +24,32 @@ export async function setupDatabase(
|
||||
if (databaseType === "sqlite") {
|
||||
if (orm === "drizzle") {
|
||||
addPackageDependency({
|
||||
dependencies: ["drizzle-orm", "drizzle-kit", "@libsql/client"],
|
||||
devDependencies: false,
|
||||
dependencies: ["drizzle-orm", "@libsql/client"],
|
||||
devDependencies: ["drizzle-kit"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
if (setupTursoDb) {
|
||||
await setupTurso(projectDir, true);
|
||||
}
|
||||
} else if (orm === "prisma") {
|
||||
addPackageDependency({
|
||||
dependencies: ["@prisma/client"],
|
||||
devDependencies: false,
|
||||
projectDir: serverDir,
|
||||
});
|
||||
addPackageDependency({
|
||||
dependencies: ["prisma"],
|
||||
devDependencies: true,
|
||||
devDependencies: ["prisma"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
}
|
||||
|
||||
if (setupTursoDb) {
|
||||
await setupTurso(projectDir, true);
|
||||
}
|
||||
}
|
||||
} else if (databaseType === "postgres") {
|
||||
if (orm === "drizzle") {
|
||||
addPackageDependency({
|
||||
dependencies: ["drizzle-orm", "postgres"],
|
||||
devDependencies: false,
|
||||
projectDir: serverDir,
|
||||
});
|
||||
addPackageDependency({
|
||||
dependencies: ["drizzle-kit"],
|
||||
devDependencies: true,
|
||||
devDependencies: ["drizzle-kit"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
} else if (orm === "prisma") {
|
||||
addPackageDependency({
|
||||
dependencies: ["@prisma/client"],
|
||||
devDependencies: false,
|
||||
projectDir: serverDir,
|
||||
});
|
||||
addPackageDependency({
|
||||
dependencies: ["prisma"],
|
||||
devDependencies: true,
|
||||
devDependencies: ["prisma"],
|
||||
projectDir: serverDir,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,6 +29,12 @@ export async function setupEnvironmentVariables(
|
||||
if (!envContent.includes("CORS_ORIGIN")) {
|
||||
envContent += "\nCORS_ORIGIN=http://localhost:3001";
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.database !== "none") {
|
||||
@@ -40,22 +46,12 @@ export async function setupEnvironmentVariables(
|
||||
envContent += databaseUrlLine;
|
||||
}
|
||||
|
||||
if (options.database === "sqlite") {
|
||||
if (options.database === "sqlite" && !options.turso) {
|
||||
if (!envContent.includes("TURSO_CONNECTION_URL")) {
|
||||
if (!options.turso) {
|
||||
envContent += "\nTURSO_CONNECTION_URL=http://127.0.0.1:8080";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await fs.writeFile(envPath, envContent.trim());
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,25 +4,29 @@ import fs from "fs-extra";
|
||||
import { type AvailableDependencies, dependencyVersionMap } from "../constants";
|
||||
|
||||
export const addPackageDependency = (opts: {
|
||||
dependencies: AvailableDependencies[];
|
||||
devDependencies: boolean;
|
||||
dependencies?: AvailableDependencies[];
|
||||
devDependencies?: AvailableDependencies[];
|
||||
projectDir: string;
|
||||
}) => {
|
||||
const { dependencies, devDependencies, projectDir } = opts;
|
||||
const { dependencies = [], devDependencies = [], projectDir } = opts;
|
||||
|
||||
const pkgJson = fs.readJSONSync(path.join(projectDir, "package.json"));
|
||||
const pkgJsonPath = path.join(projectDir, "package.json");
|
||||
const pkgJson = fs.readJSONSync(pkgJsonPath);
|
||||
|
||||
if (!pkgJson.dependencies) pkgJson.dependencies = {};
|
||||
if (!pkgJson.devDependencies) pkgJson.devDependencies = {};
|
||||
|
||||
for (const pkgName of dependencies) {
|
||||
const version = dependencyVersionMap[pkgName];
|
||||
|
||||
if (devDependencies && pkgJson.devDependencies) {
|
||||
pkgJson.devDependencies[pkgName] = version;
|
||||
} else if (pkgJson.dependencies) {
|
||||
pkgJson.dependencies[pkgName] = version;
|
||||
}
|
||||
|
||||
for (const pkgName of devDependencies) {
|
||||
const version = dependencyVersionMap[pkgName];
|
||||
pkgJson.devDependencies[pkgName] = version;
|
||||
}
|
||||
|
||||
fs.writeJSONSync(path.join(projectDir, "package.json"), pkgJson, {
|
||||
fs.writeJSONSync(pkgJsonPath, pkgJson, {
|
||||
spaces: 2,
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user