UNPKG

@enspirit/emb

Version:

A replacement for our Makefile-for-monorepos

56 lines (55 loc) 1.89 kB
import { getContext, setContext } from '../../index.js'; import { Flags } from '@oclif/core'; import { JsonPatchError } from 'fast-json-patch'; import { BaseCommand } from './BaseCommand.js'; export class FlavoredCommand extends BaseCommand { // define flags that can be inherited by any command that extends FlavoredCommand static baseFlags = { flavor: Flags.string({ description: 'Specify the flavor to use.', name: 'flavor', required: false, }), }; // add the --json flag static enableJsonFlag = true; args; flags; async catch(err) { if (err instanceof JsonPatchError) { this.log('INVALID', err.operation); this.error('Invalid patch detected while applying flavor', { code: err.name, message: `Path \`${err.operation?.path}\``, }); return; } // add any custom logic to handle errors from the command // or simply return the parent class error handling return super.catch(err); } async init() { await super.init(); const { args, flags } = await this.parse({ args: this.ctor.args, baseFlags: super.ctor.baseFlags, enableJsonFlag: this.ctor.enableJsonFlag, flags: this.ctor.flags, strict: this.ctor.strict, }); this.flags = flags; this.args = args; // Get monorepo config const context = getContext(); // Installing flavor if relevant // no validation as the monorepo will // complain properly if incorrect const { flavor } = this.flags; if (flavor) { this.context = setContext({ ...context, monorepo: await context.monorepo.withFlavor(flavor), }); } } }