From 637c0d48f3812c3bf6f534dca1da9768abe5ca83 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 29 Nov 2023 11:00:52 -0700 Subject: [PATCH] fix: preserve oclif exit code --- src/sfCommand.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/sfCommand.ts b/src/sfCommand.ts index cd8e5450b..737a21847 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -428,7 +428,8 @@ export abstract class SfCommand extends Command { // transform an unknown error into one that conforms to the interface // @ts-expect-error because exitCode is not on Error - const codeFromError = (error.exitCode as number) ?? 1; + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access + const codeFromError = (error.exitCode as number | undefined) ?? (error.oclif?.exit as number | undefined) ?? 1; process.exitCode ??= codeFromError; const sfErrorProperties = removeEmpty({ @@ -476,6 +477,10 @@ export abstract class SfCommand extends Command { // @ts-expect-error because skipOclifErrorHandling is not on SfError err.skipOclifErrorHandling = true; + // Add oclif exit code to the error so that oclif can use the exit code when exiting. + // @ts-expect-error because oclif is not on SfError + err.oclif = { exit: process.exitCode }; + // Emit an event for plugin-telemetry prerun hook to pick up. // @ts-expect-error because TS is strict about the events that can be emitted on process. process.emit('sfCommandError', err);