import * as errors from '../errors' import type { Logger } from '../logger' import type { CommandArgv, CommandDefinition } from '../typings' export abstract class BaseCommand { public constructor( protected readonly logger: Logger, protected readonly argv: CommandArgv ) {} protected abstract run(): Promise protected bootstrap?(): Promise protected teardown?(): Promise private get _cmdName(): string { return this.constructor.name } public async handler(): Promise<{ exitCode: number }> { let exitCode = 0 try { if (this.bootstrap) { await this.bootstrap() } await this.run() } catch (thrown) { const error = errors.BotpressCLIError.map(thrown) this.logger.error(error.message) this.logger.debug(`[${this._cmdName}] ${errors.BotpressCLIError.fullStack(error)}`) exitCode = 1 } finally { if (this.teardown) { await this.teardown() } } return { exitCode } } }