fix(cli): return nonzero status for operation failures (#121)
This commit is contained in:
+21
-3
@@ -1,5 +1,21 @@
|
||||
import type { Logger } from "./logger.js";
|
||||
|
||||
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
|
||||
typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
|
||||
/** Identify caller-visible CLI outcomes that mean the requested operation failed. */
|
||||
export const isCliOperationFailure = (value: unknown): boolean => {
|
||||
if (!isRecord(value)) return false;
|
||||
if (typeof value.error === "string") return true;
|
||||
if (value.healthy === false) return true;
|
||||
return (
|
||||
value.status === "failed" ||
|
||||
value.status === "needs_confirmation" ||
|
||||
value.status === "needs_human" ||
|
||||
value.status === "planned"
|
||||
);
|
||||
};
|
||||
|
||||
/** Log one CLI command with duration and a stable success or failure status. */
|
||||
export const logCliCommand = async <Value>(
|
||||
logger: Logger,
|
||||
@@ -9,14 +25,16 @@ export const logCliCommand = async <Value>(
|
||||
const startedAt = performance.now();
|
||||
try {
|
||||
const value = await execute();
|
||||
logger.info(
|
||||
const failed = isCliOperationFailure(value);
|
||||
logger[failed ? "error" : "info"](
|
||||
{
|
||||
command,
|
||||
durationMs: Math.round((performance.now() - startedAt) * 100) / 100,
|
||||
status: "ok",
|
||||
status: failed ? "error" : "ok",
|
||||
},
|
||||
"CLI command completed",
|
||||
failed ? "CLI command failed" : "CLI command completed",
|
||||
);
|
||||
if (failed) process.exitCode = 1;
|
||||
return value;
|
||||
} catch (cause: unknown) {
|
||||
logger.error(
|
||||
|
||||
Reference in New Issue
Block a user