mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
Refactor: Simplify error handling in CLI helpers
This commit is contained in:
5
.changeset/easy-dodos-lay.md
Normal file
5
.changeset/easy-dodos-lay.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"create-better-t-stack": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Refactor: Simplify error handling in CLI helpers
|
||||||
@@ -26,8 +26,7 @@ import {
|
|||||||
setupFrontendTemplates,
|
setupFrontendTemplates,
|
||||||
} from "./template-manager";
|
} from "./template-manager";
|
||||||
|
|
||||||
export async function createProject(options: ProjectConfig): Promise<string> {
|
export async function createProject(options: ProjectConfig) {
|
||||||
const s = spinner();
|
|
||||||
const projectDir = path.resolve(process.cwd(), options.projectName);
|
const projectDir = path.resolve(process.cwd(), options.projectName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -86,12 +85,10 @@ export async function createProject(options: ProjectConfig): Promise<string> {
|
|||||||
|
|
||||||
return projectDir;
|
return projectDir;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
s.stop(pc.red("Failed"));
|
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
cancel(pc.red(`Error during project creation: ${error.message}`));
|
cancel(pc.red(`Error during project creation: ${error.message}`));
|
||||||
console.error(error.stack);
|
console.error(error.stack);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,5 @@ export async function setupDatabase(config: ProjectConfig): Promise<void> {
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
consola.error(pc.red(error.message));
|
consola.error(pc.red(error.message));
|
||||||
}
|
}
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ export async function installDependencies({
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
consola.error(pc.red(`Installation error: ${error.message}`));
|
consola.error(pc.red(`Installation error: ${error.message}`));
|
||||||
}
|
}
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,7 +108,6 @@ async function writeEnvFile(projectDir: string, config?: MongoDBConfig) {
|
|||||||
await fs.writeFile(envPath, envContent.trim());
|
await fs.writeFile(envPath, envContent.trim());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
consola.error("Failed to update environment configuration");
|
consola.error("Failed to update environment configuration");
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ async function executeNeonCommand(
|
|||||||
if (s) s.start(spinnerText);
|
if (s) s.start(spinnerText);
|
||||||
const result = await execa(fullCommand, { shell: true });
|
const result = await execa(fullCommand, { shell: true });
|
||||||
if (s) s.stop(spinnerText);
|
if (s) s.stop(spinnerText);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (s) s.stop(pc.red(`Failed: ${spinnerText}`));
|
if (s) s.stop(pc.red(`Failed: ${spinnerText}`));
|
||||||
@@ -61,14 +60,13 @@ async function authenticateWithNeon(packageManager: ProjectPackageManager) {
|
|||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
consola.error(pc.red("Failed to authenticate with Neon"));
|
consola.error(pc.red("Failed to authenticate with Neon"));
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createNeonProject(
|
async function createNeonProject(
|
||||||
projectName: string,
|
projectName: string,
|
||||||
packageManager: ProjectPackageManager,
|
packageManager: ProjectPackageManager,
|
||||||
): Promise<NeonConfig | null> {
|
) {
|
||||||
try {
|
try {
|
||||||
const commandArgsString = `neonctl projects create --name "${projectName}" --output json`;
|
const commandArgsString = `neonctl projects create --name "${projectName}" --output json`;
|
||||||
const { stdout } = await executeNeonCommand(
|
const { stdout } = await executeNeonCommand(
|
||||||
@@ -101,7 +99,6 @@ async function createNeonProject(
|
|||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
consola.error(pc.red("Failed to create Neon project"));
|
consola.error(pc.red("Failed to create Neon project"));
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,6 @@ async function writeEnvFile(projectDir: string, config?: PrismaConfig) {
|
|||||||
await fs.writeFile(envPath, envContent.trim());
|
await fs.writeFile(envPath, envContent.trim());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
consola.error("Failed to update environment configuration");
|
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 prismaIndexPath = path.join(serverDir, "prisma/index.ts");
|
||||||
const prismaIndexContent = `
|
const prismaIndexContent = `
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from "./generated/client";
|
||||||
import { withAccelerate } from "@prisma/extension-accelerate";
|
import { withAccelerate } from "@prisma/extension-accelerate";
|
||||||
|
|
||||||
const prisma = new PrismaClient().$extends(withAccelerate());
|
const prisma = new PrismaClient().$extends(withAccelerate());
|
||||||
|
|||||||
@@ -47,6 +47,5 @@ export async function setupStarlight(config: ProjectConfig): Promise<void> {
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
consola.error(pc.red(error.message));
|
consola.error(pc.red(error.message));
|
||||||
}
|
}
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,6 +90,5 @@ export async function setupTauri(config: ProjectConfig): Promise<void> {
|
|||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
consola.error(pc.red(error.message));
|
consola.error(pc.red(error.message));
|
||||||
}
|
}
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ async function loginToTurso() {
|
|||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
s.stop(pc.red("Failed to log in to Turso"));
|
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");
|
throw new Error("Installation cancelled");
|
||||||
}
|
}
|
||||||
s.stop(pc.red("Failed to install Turso CLI"));
|
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;
|
return selectedGroup as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createTursoDatabase(
|
async function createTursoDatabase(dbName: string, groupName: string | null) {
|
||||||
dbName: string,
|
|
||||||
groupName: string | null,
|
|
||||||
): Promise<TursoConfig> {
|
|
||||||
const s = spinner();
|
const s = spinner();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -158,7 +153,6 @@ async function createTursoDatabase(
|
|||||||
if (error instanceof Error && error.message.includes("already exists")) {
|
if (error instanceof Error && error.message.includes("already exists")) {
|
||||||
throw new Error("DATABASE_EXISTS");
|
throw new Error("DATABASE_EXISTS");
|
||||||
}
|
}
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.start("Retrieving database connection details...");
|
s.start("Retrieving database connection details...");
|
||||||
@@ -174,7 +168,6 @@ async function createTursoDatabase(
|
|||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
s.stop(pc.red("Failed to retrieve database connection details"));
|
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`));
|
log.warn(pc.yellow(`Database "${pc.red(dbName)}" already exists`));
|
||||||
suggestedName = `${dbName}-${Math.floor(Math.random() * 1000)}`;
|
suggestedName = `${dbName}-${Math.floor(Math.random() * 1000)}`;
|
||||||
} else {
|
} else {
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
/node_modules/
|
node_modules
|
||||||
.turbo
|
.turbo
|
||||||
|
|||||||
Reference in New Issue
Block a user