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({
|
addPackageDependency({
|
||||||
dependencies: ["better-auth"],
|
dependencies: ["better-auth"],
|
||||||
devDependencies: false,
|
|
||||||
projectDir: serverDir,
|
projectDir: serverDir,
|
||||||
});
|
});
|
||||||
addPackageDependency({
|
addPackageDependency({
|
||||||
dependencies: ["better-auth"],
|
dependencies: ["better-auth"],
|
||||||
devDependencies: false,
|
|
||||||
projectDir: clientDir,
|
projectDir: clientDir,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -62,64 +62,7 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
|||||||
await setupAddons(projectDir, options.addons);
|
await setupAddons(projectDir, options.addons);
|
||||||
}
|
}
|
||||||
|
|
||||||
const rootPackageJsonPath = path.join(projectDir, "package.json");
|
await updatePackageConfigurations(projectDir, options);
|
||||||
if (await fs.pathExists(rootPackageJsonPath)) {
|
|
||||||
const packageJson = await fs.readJson(rootPackageJsonPath);
|
|
||||||
packageJson.name = options.projectName;
|
|
||||||
|
|
||||||
if (options.packageManager !== "bun") {
|
|
||||||
packageJson.packageManager =
|
|
||||||
options.packageManager === "npm"
|
|
||||||
? "npm@10.9.2"
|
|
||||||
: options.packageManager === "pnpm"
|
|
||||||
? "pnpm@10.6.4"
|
|
||||||
: options.packageManager === "yarn"
|
|
||||||
? "yarn@4.1.0"
|
|
||||||
: "bun@1.2.5";
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.writeJson(rootPackageJsonPath, packageJson, { spaces: 2 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const serverPackageJsonPath = path.join(
|
|
||||||
projectDir,
|
|
||||||
"packages/server/package.json",
|
|
||||||
);
|
|
||||||
if (await fs.pathExists(serverPackageJsonPath)) {
|
|
||||||
const serverPackageJson = await fs.readJson(serverPackageJsonPath);
|
|
||||||
|
|
||||||
if (options.database !== "none") {
|
|
||||||
if (options.database === "sqlite") {
|
|
||||||
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";
|
|
||||||
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";
|
|
||||||
}
|
|
||||||
} 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);
|
await createReadme(projectDir, options);
|
||||||
|
|
||||||
@@ -142,6 +85,61 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
packageJson.name = options.projectName;
|
||||||
|
|
||||||
|
if (options.packageManager !== "bun") {
|
||||||
|
packageJson.packageManager =
|
||||||
|
options.packageManager === "npm"
|
||||||
|
? "npm@10.9.2"
|
||||||
|
: options.packageManager === "pnpm"
|
||||||
|
? "pnpm@10.6.4"
|
||||||
|
: options.packageManager === "yarn"
|
||||||
|
? "yarn@4.1.0"
|
||||||
|
: "bun@1.2.5";
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.writeJson(rootPackageJsonPath, packageJson, { spaces: 2 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const serverPackageJsonPath = path.join(
|
||||||
|
projectDir,
|
||||||
|
"packages/server/package.json",
|
||||||
|
);
|
||||||
|
if (await fs.pathExists(serverPackageJsonPath)) {
|
||||||
|
const serverPackageJson = await fs.readJson(serverPackageJsonPath);
|
||||||
|
|
||||||
|
if (options.database !== "none") {
|
||||||
|
if (options.database === "sqlite") {
|
||||||
|
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";
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getOrmTemplateDir(orm: ProjectOrm, database: ProjectDatabase): string {
|
function getOrmTemplateDir(orm: ProjectOrm, database: ProjectDatabase): string {
|
||||||
if (orm === "drizzle") {
|
if (orm === "drizzle") {
|
||||||
return database === "sqlite"
|
return database === "sqlite"
|
||||||
|
|||||||
@@ -69,10 +69,10 @@ ${generateScriptsList(packageManagerRunCmd, database, orm, auth)}
|
|||||||
}
|
}
|
||||||
|
|
||||||
function generateFeaturesList(
|
function generateFeaturesList(
|
||||||
database: string,
|
database: ProjectDatabase,
|
||||||
auth: boolean,
|
auth: boolean,
|
||||||
features: string[],
|
features: string[],
|
||||||
orm: string,
|
orm: ProjectOrm,
|
||||||
): string {
|
): string {
|
||||||
const featuresList = [
|
const featuresList = [
|
||||||
"- **TypeScript** - For type safety and improved developer experience",
|
"- **TypeScript** - For type safety and improved developer experience",
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ import path from "node:path";
|
|||||||
import { log, spinner } from "@clack/prompts";
|
import { log, spinner } from "@clack/prompts";
|
||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import pc from "picocolors";
|
import pc from "picocolors";
|
||||||
|
import type { ProjectDatabase, ProjectOrm } from "../types";
|
||||||
import { addPackageDependency } from "../utils/add-package-deps";
|
import { addPackageDependency } from "../utils/add-package-deps";
|
||||||
import { setupTurso } from "./turso-setup";
|
import { setupTurso } from "./turso-setup";
|
||||||
|
|
||||||
export async function setupDatabase(
|
export async function setupDatabase(
|
||||||
projectDir: string,
|
projectDir: string,
|
||||||
databaseType: string,
|
databaseType: ProjectDatabase,
|
||||||
orm: string,
|
orm: ProjectOrm,
|
||||||
setupTursoDb = true,
|
setupTursoDb = true,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const s = spinner();
|
const s = spinner();
|
||||||
@@ -23,49 +24,32 @@ export async function setupDatabase(
|
|||||||
if (databaseType === "sqlite") {
|
if (databaseType === "sqlite") {
|
||||||
if (orm === "drizzle") {
|
if (orm === "drizzle") {
|
||||||
addPackageDependency({
|
addPackageDependency({
|
||||||
dependencies: ["drizzle-orm", "drizzle-kit", "@libsql/client"],
|
dependencies: ["drizzle-orm", "@libsql/client"],
|
||||||
devDependencies: false,
|
devDependencies: ["drizzle-kit"],
|
||||||
projectDir: serverDir,
|
projectDir: serverDir,
|
||||||
});
|
});
|
||||||
if (setupTursoDb) {
|
|
||||||
await setupTurso(projectDir, true);
|
|
||||||
}
|
|
||||||
} else if (orm === "prisma") {
|
} else if (orm === "prisma") {
|
||||||
addPackageDependency({
|
addPackageDependency({
|
||||||
dependencies: ["@prisma/client"],
|
dependencies: ["@prisma/client"],
|
||||||
devDependencies: false,
|
devDependencies: ["prisma"],
|
||||||
projectDir: serverDir,
|
projectDir: serverDir,
|
||||||
});
|
});
|
||||||
addPackageDependency({
|
}
|
||||||
dependencies: ["prisma"],
|
|
||||||
devDependencies: true,
|
if (setupTursoDb) {
|
||||||
projectDir: serverDir,
|
await setupTurso(projectDir, true);
|
||||||
});
|
|
||||||
if (setupTursoDb) {
|
|
||||||
await setupTurso(projectDir, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (databaseType === "postgres") {
|
} else if (databaseType === "postgres") {
|
||||||
if (orm === "drizzle") {
|
if (orm === "drizzle") {
|
||||||
addPackageDependency({
|
addPackageDependency({
|
||||||
dependencies: ["drizzle-orm", "postgres"],
|
dependencies: ["drizzle-orm", "postgres"],
|
||||||
devDependencies: false,
|
devDependencies: ["drizzle-kit"],
|
||||||
projectDir: serverDir,
|
|
||||||
});
|
|
||||||
addPackageDependency({
|
|
||||||
dependencies: ["drizzle-kit"],
|
|
||||||
devDependencies: true,
|
|
||||||
projectDir: serverDir,
|
projectDir: serverDir,
|
||||||
});
|
});
|
||||||
} else if (orm === "prisma") {
|
} else if (orm === "prisma") {
|
||||||
addPackageDependency({
|
addPackageDependency({
|
||||||
dependencies: ["@prisma/client"],
|
dependencies: ["@prisma/client"],
|
||||||
devDependencies: false,
|
devDependencies: ["prisma"],
|
||||||
projectDir: serverDir,
|
|
||||||
});
|
|
||||||
addPackageDependency({
|
|
||||||
dependencies: ["prisma"],
|
|
||||||
devDependencies: true,
|
|
||||||
projectDir: serverDir,
|
projectDir: serverDir,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ export async function setupEnvironmentVariables(
|
|||||||
if (!envContent.includes("CORS_ORIGIN")) {
|
if (!envContent.includes("CORS_ORIGIN")) {
|
||||||
envContent += "\nCORS_ORIGIN=http://localhost:3001";
|
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") {
|
if (options.database !== "none") {
|
||||||
@@ -40,22 +46,12 @@ export async function setupEnvironmentVariables(
|
|||||||
envContent += databaseUrlLine;
|
envContent += databaseUrlLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.database === "sqlite") {
|
if (options.database === "sqlite" && !options.turso) {
|
||||||
if (!envContent.includes("TURSO_CONNECTION_URL")) {
|
if (!envContent.includes("TURSO_CONNECTION_URL")) {
|
||||||
if (!options.turso) {
|
envContent += "\nTURSO_CONNECTION_URL=http://127.0.0.1:8080";
|
||||||
envContent += "\nTURSO_CONNECTION_URL=http://127.0.0.1:8080";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await fs.writeFile(envPath, envContent.trim());
|
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";
|
import { type AvailableDependencies, dependencyVersionMap } from "../constants";
|
||||||
|
|
||||||
export const addPackageDependency = (opts: {
|
export const addPackageDependency = (opts: {
|
||||||
dependencies: AvailableDependencies[];
|
dependencies?: AvailableDependencies[];
|
||||||
devDependencies: boolean;
|
devDependencies?: AvailableDependencies[];
|
||||||
projectDir: string;
|
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) {
|
for (const pkgName of dependencies) {
|
||||||
const version = dependencyVersionMap[pkgName];
|
const version = dependencyVersionMap[pkgName];
|
||||||
|
pkgJson.dependencies[pkgName] = version;
|
||||||
if (devDependencies && pkgJson.devDependencies) {
|
|
||||||
pkgJson.devDependencies[pkgName] = version;
|
|
||||||
} else if (pkgJson.dependencies) {
|
|
||||||
pkgJson.dependencies[pkgName] = version;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeJSONSync(path.join(projectDir, "package.json"), pkgJson, {
|
for (const pkgName of devDependencies) {
|
||||||
|
const version = dependencyVersionMap[pkgName];
|
||||||
|
pkgJson.devDependencies[pkgName] = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeJSONSync(pkgJsonPath, pkgJson, {
|
||||||
spaces: 2,
|
spaces: 2,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user