UNPKG

@mondaycom/apps-cli

Version:

A cli tool to manage apps (and monday-code projects) in monday.com

65 lines (64 loc) 2.18 kB
import { Command, Flags } from '@oclif/core'; import { printGeneratedCommand } from '../utils/command-printer.js'; import logger from '../utils/logger.js'; export class BaseCommand extends Command { static _withPrintCommand = true; _printCommandCalled = false; _printContext = { command: this }; forcefullyExitAfterRun = true; get printContext() { return this._printContext; } set printContext(value) { this._printContext = value; } static get withPrintCommand() { return this._withPrintCommand; } static set withPrintCommand(value) { this._withPrintCommand = value; } preparePrintCommand(command, flags, args) { this._printCommandCalled = true; this._printContext = { command, flags, args }; } static serializeFlags(flags) { return { ...this.sharedFlags, ...flags, }; } static sharedFlags = { verbose: Flags.boolean({ description: 'Print advanced logs (optional).', default: false, helpGroup: 'global', }), 'print-command': Flags.boolean({ aliases: ['pc'], description: 'Print the command that was executed (optional).', default: false, helpGroup: 'global', }), }; catch(err) { err?.message && logger.error(err.message); logger.debug(err); return process.exit(1); } async finally(_) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const withPrintCommand = this.constructor.withPrintCommand; if (withPrintCommand && !this._printCommandCalled) { throw new Error('Print command was not called and withPrintCommand is true'); } else if (withPrintCommand && this._printCommandCalled) { printGeneratedCommand(this._printContext.command, this._printContext.flags, this._printContext.args); } if (this.forcefullyExitAfterRun) { return process.exit(0); } } }