Skip to content

Commit

Permalink
fix: preserve oclif exit code (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley authored Dec 7, 2023
1 parent 9f82aa6 commit 8a3c370
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ export abstract class SfCommand<T> 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({
Expand Down Expand Up @@ -476,6 +477,10 @@ export abstract class SfCommand<T> 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);
Expand Down

0 comments on commit 8a3c370

Please sign in to comment.