feat(cli): add programmatic api (#494)

This commit is contained in:
Aman Varshney
2025-08-12 07:40:19 +05:30
committed by GitHub
parent 5b2827ef12
commit aecde5a54e
18 changed files with 1295 additions and 203 deletions

View File

@@ -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);
}

View File

@@ -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,