diff --git a/.changeset/curly-worlds-sing.md b/.changeset/curly-worlds-sing.md new file mode 100644 index 0000000..4ab1646 --- /dev/null +++ b/.changeset/curly-worlds-sing.md @@ -0,0 +1,7 @@ +--- +"@vlandoss/clibuddy": patch +"@vlandoss/run-run": patch +"@vlandoss/starter": patch +--- + +Improve fatal error logging diff --git a/packages/clibuddy/src/index.ts b/packages/clibuddy/src/index.ts index e7fcd10..997a31c 100644 --- a/packages/clibuddy/src/index.ts +++ b/packages/clibuddy/src/index.ts @@ -1,3 +1,4 @@ export * from "./colors"; +export * from "./run"; export * from "./services"; export * from "./version"; diff --git a/packages/clibuddy/src/run.ts b/packages/clibuddy/src/run.ts new file mode 100644 index 0000000..724cb35 --- /dev/null +++ b/packages/clibuddy/src/run.ts @@ -0,0 +1,26 @@ +import { isProcessOutput } from "./services/shell/utils"; + +function hasMessage(error: unknown): error is { message: string } { + return ( + typeof error === "object" && + error !== null && + "message" in error && + typeof (error as { message: unknown }).message === "string" + ); +} + +function formatError(error: unknown): string { + if (hasMessage(error)) return error.message; + return String(error); +} + +export async function run(fn: () => Promise, logger: { error: (...args: unknown[]) => void }) { + try { + await fn(); + } catch (error) { + if (!isProcessOutput(error)) { + logger.error(formatError(error)); + } + process.exit(1); + } +} diff --git a/packages/run-run/src/main.ts b/packages/run-run/src/main.ts index 5c2a65e..de8bf37 100644 --- a/packages/run-run/src/main.ts +++ b/packages/run-run/src/main.ts @@ -1,16 +1,11 @@ -import { isProcessOutput } from "@vlandoss/clibuddy"; +import { run } from "@vlandoss/clibuddy"; import { createProgram, type Options } from "./program"; import { parseArgs } from "./program/parse-args"; import { logger } from "./services/logger"; export async function main(options: Options) { - try { + await run(async () => { const { program } = await createProgram(options); await program.parseAsync(parseArgs(), { from: "user" }); - } catch (error) { - if (!isProcessOutput(error)) { - logger.error("Cannot run main successfully", error); - } - process.exit(1); - } + }, logger); } diff --git a/packages/starter/src/main.ts b/packages/starter/src/main.ts index f2a9450..4fb32a1 100644 --- a/packages/starter/src/main.ts +++ b/packages/starter/src/main.ts @@ -1,12 +1,10 @@ +import { run } from "@vlandoss/clibuddy"; import { createProgram, type Options } from "./program"; import { logger } from "./services/logger"; export async function main(options: Options) { - try { + await run(async () => { const program = await createProgram(options); await program.parseAsync(); - } catch (error) { - logger.error("Cannot run main successfully", error); - process.exit(1); - } + }, logger); }