Skip to content

Commit

Permalink
feat: allow to use different exit codes and use oclif hooks for lifecyle
Browse files Browse the repository at this point in the history
  • Loading branch information
tagoro9 committed Dec 20, 2022
1 parent 15c8edb commit bb72424
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/cli/FotingoCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ import { renderUi } from 'src/ui/ui';

const returnIfFalse = (message: string) => ifElse(equals(true), always(undefined), () => message);

class FotingoError extends Error {
static CODES = {
MISSING_REQUIRED_CONFIG: 20,
};

public readonly code?: number;

constructor(message: string, code: number) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
this.code = code;
}
}

/**
* Abstract class every fotingo command must extend. It provides
* defaults and helper methods to make it easier to
Expand Down Expand Up @@ -143,7 +157,11 @@ export abstract class FotingoCommand<T, R> extends Command {
return of({});
}
return throwError(
() => new Error(`Missing required configuration: ${configurations.join(', ')}`),
() =>
new FotingoError(
`Missing required configuration: ${configurations.join(', ')}`,
FotingoError.CODES.MISSING_REQUIRED_CONFIG,
),
);
}),
);
Expand Down Expand Up @@ -171,6 +189,18 @@ export abstract class FotingoCommand<T, R> extends Command {
return super.init();
}

async catch(error: Error): Promise<void> {
const unknownError = error as unknown as Record<string, unknown>;
const errorCode =
'code' in unknownError && typeof unknownError.code === 'number' ? unknownError.code : 1;
this.exit(errorCode);
}

async finally(error?: Error): Promise<void> {
this.debug(`Ran command in ${Date.now() - this.startTime}ms`);
super.finally(error);
}

async run(): Promise<void> {
const { waitUntilExit } = renderUi({
cmd: () => {
Expand All @@ -182,14 +212,7 @@ export abstract class FotingoCommand<T, R> extends Command {
programStartTime: this.startTime,
messenger: this.messenger,
});
try {
await waitUntilExit();
} catch {
// eslint-disable-next-line no-process-exit, unicorn/no-process-exit
process.exit(1);
} finally {
this.debug(`Ran command in ${Date.now() - this.startTime}ms`);
}
await waitUntilExit();
}

/**
Expand Down

0 comments on commit bb72424

Please sign in to comment.