mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
feat(cli): add programmatic api (#494)
This commit is contained in:
@@ -2,13 +2,23 @@ import { cancel } from "@clack/prompts";
|
||||
import consola from "consola";
|
||||
import pc from "picocolors";
|
||||
|
||||
function isProgrammatic(): boolean {
|
||||
return process.env.BTS_PROGRAMMATIC === "1";
|
||||
}
|
||||
|
||||
export function exitWithError(message: string): never {
|
||||
consola.error(pc.red(message));
|
||||
if (isProgrammatic()) {
|
||||
throw new Error(message);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export function exitCancelled(message = "Operation cancelled"): never {
|
||||
cancel(pc.red(message));
|
||||
if (isProgrammatic()) {
|
||||
throw new Error(message);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
@@ -16,5 +26,8 @@ export function handleError(error: unknown, fallbackMessage?: string): never {
|
||||
const message =
|
||||
error instanceof Error ? error.message : fallbackMessage || String(error);
|
||||
consola.error(pc.red(message));
|
||||
if (isProgrammatic()) {
|
||||
throw new Error(message);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { exitCancelled, handleError } from "./errors";
|
||||
|
||||
export async function handleDirectoryConflict(
|
||||
currentPathInput: string,
|
||||
silent = false,
|
||||
): Promise<{
|
||||
finalPathInput: string;
|
||||
shouldClearDirectory: boolean;
|
||||
@@ -20,6 +21,12 @@ export async function handleDirectoryConflict(
|
||||
return { finalPathInput: currentPathInput, shouldClearDirectory: false };
|
||||
}
|
||||
|
||||
if (silent) {
|
||||
throw new Error(
|
||||
`Directory "${currentPathInput}" already exists and is not empty. In silent mode, please provide a different project name or clear the directory manually.`,
|
||||
);
|
||||
}
|
||||
|
||||
log.warn(
|
||||
`Directory "${pc.yellow(
|
||||
currentPathInput,
|
||||
|
||||
Reference in New Issue
Block a user