Refactor: Simplify error handling in CLI helpers

This commit is contained in:
Aman Varshney
2025-04-28 04:14:23 +05:30
parent a735bd1769
commit aef7aa428a
11 changed files with 10 additions and 25 deletions

View File

@@ -0,0 +1,5 @@
---
"create-better-t-stack": patch
---
Refactor: Simplify error handling in CLI helpers

View File

@@ -26,8 +26,7 @@ import {
setupFrontendTemplates,
} from "./template-manager";
export async function createProject(options: ProjectConfig): Promise<string> {
const s = spinner();
export async function createProject(options: ProjectConfig) {
const projectDir = path.resolve(process.cwd(), options.projectName);
try {
@@ -86,12 +85,10 @@ export async function createProject(options: ProjectConfig): Promise<string> {
return projectDir;
} catch (error) {
s.stop(pc.red("Failed"));
if (error instanceof Error) {
cancel(pc.red(`Error during project creation: ${error.message}`));
console.error(error.stack);
process.exit(1);
}
throw error;
}
}

View File

@@ -68,6 +68,5 @@ export async function setupDatabase(config: ProjectConfig): Promise<void> {
if (error instanceof Error) {
consola.error(pc.red(error.message));
}
throw error;
}
}

View File

@@ -33,7 +33,6 @@ export async function installDependencies({
if (error instanceof Error) {
consola.error(pc.red(`Installation error: ${error.message}`));
}
throw error;
}
}

View File

@@ -108,7 +108,6 @@ async function writeEnvFile(projectDir: string, config?: MongoDBConfig) {
await fs.writeFile(envPath, envContent.trim());
} catch (error) {
consola.error("Failed to update environment configuration");
throw error;
}
}

View File

@@ -29,7 +29,6 @@ async function executeNeonCommand(
if (s) s.start(spinnerText);
const result = await execa(fullCommand, { shell: true });
if (s) s.stop(spinnerText);
return result;
} catch (error) {
if (s) s.stop(pc.red(`Failed: ${spinnerText}`));
@@ -61,14 +60,13 @@ async function authenticateWithNeon(packageManager: ProjectPackageManager) {
return true;
} catch (error) {
consola.error(pc.red("Failed to authenticate with Neon"));
throw error;
}
}
async function createNeonProject(
projectName: string,
packageManager: ProjectPackageManager,
): Promise<NeonConfig | null> {
) {
try {
const commandArgsString = `neonctl projects create --name "${projectName}" --output json`;
const { stdout } = await executeNeonCommand(
@@ -101,7 +99,6 @@ async function createNeonProject(
return null;
} catch (error) {
consola.error(pc.red("Failed to create Neon project"));
throw error;
}
}

View File

@@ -95,7 +95,6 @@ async function writeEnvFile(projectDir: string, config?: PrismaConfig) {
await fs.writeFile(envPath, envContent.trim());
} catch (error) {
consola.error("Failed to update environment configuration");
throw error;
}
}
@@ -119,7 +118,7 @@ async function addPrismaAccelerateExtension(serverDir: string) {
const prismaIndexPath = path.join(serverDir, "prisma/index.ts");
const prismaIndexContent = `
import { PrismaClient } from '@prisma/client';
import { PrismaClient } from "./generated/client";
import { withAccelerate } from "@prisma/extension-accelerate";
const prisma = new PrismaClient().$extends(withAccelerate());

View File

@@ -47,6 +47,5 @@ export async function setupStarlight(config: ProjectConfig): Promise<void> {
if (error instanceof Error) {
consola.error(pc.red(error.message));
}
throw error;
}
}

View File

@@ -90,6 +90,5 @@ export async function setupTauri(config: ProjectConfig): Promise<void> {
if (error instanceof Error) {
consola.error(pc.red(error.message));
}
throw error;
}
}

View File

@@ -49,7 +49,6 @@ async function loginToTurso() {
return true;
} catch (error) {
s.stop(pc.red("Failed to log in to Turso"));
throw error;
}
}
@@ -75,7 +74,6 @@ async function installTursoCLI(isMac: boolean) {
throw new Error("Installation cancelled");
}
s.stop(pc.red("Failed to install Turso CLI"));
throw error;
}
}
@@ -135,10 +133,7 @@ async function selectTursoGroup(): Promise<string | null> {
return selectedGroup as string;
}
async function createTursoDatabase(
dbName: string,
groupName: string | null,
): Promise<TursoConfig> {
async function createTursoDatabase(dbName: string, groupName: string | null) {
const s = spinner();
try {
@@ -158,7 +153,6 @@ async function createTursoDatabase(
if (error instanceof Error && error.message.includes("already exists")) {
throw new Error("DATABASE_EXISTS");
}
throw error;
}
s.start("Retrieving database connection details...");
@@ -174,7 +168,6 @@ async function createTursoDatabase(
};
} catch (error) {
s.stop(pc.red("Failed to retrieve database connection details"));
throw error;
}
}
@@ -289,7 +282,6 @@ export async function setupTurso(config: ProjectConfig): Promise<void> {
log.warn(pc.yellow(`Database "${pc.red(dbName)}" already exists`));
suggestedName = `${dbName}-${Math.floor(Math.random() * 1000)}`;
} else {
throw error;
}
}
}

View File

@@ -1,2 +1,2 @@
/node_modules/
node_modules
.turbo