Topic specific custom lifecycle events and hooks #1028
-
Goal: Add a lifecycle event similar to 'prerun' but specific to commands in a topic. Question: Is my proposed solution the right way to go about this? Context: I have a suite of commands organized into topics. One topic has commands that are extensible by plugins that add "providers". A custom lifecycle event "mytopic:load-provider" will allow plugins to register providers by including hook that registers them with a provider registry. Proposed Solution: The topic has a base class which commands in the topic extend. export default abstract class MyTopicBaseCommand<T extends typeof Command> {
async _run<T>(): Promise<T> {
await this.config.runHook<'mytopic:load-provider'>('mytopic:load-provider', {})
return super._run<T>()
}
// other stuff
} This would execute the custom 'mytopic:load-provider' hooks after the prerun hook. Plugins could then add providers with the following: import { Hook } from '@oclif/core';
import { ProviderRegistry } from '@myorg/core';
const hook: Hook<'mytopic:load-provider'> = async function () {
ProviderRegistry.register(new Provider());
};
export default hook; Alternative Considered: We could have done this with a prerun hook that would exit quickly if the command.id indicated that the command was not from the relevant topic but that seems wasteful given that the provider loading is relevant only for small percentage of the suite. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@jpshack-at-palomar That looks like it would work. We're doing something similar in the Salesforce CLI: https://github.com/salesforcecli/sf-plugins-core/blob/7.1.16/src/sfCommand.ts#L312 |
Beta Was this translation helpful? Give feedback.
@jpshack-at-palomar That looks like it would work. We're doing something similar in the Salesforce CLI: https://github.com/salesforcecli/sf-plugins-core/blob/7.1.16/src/sfCommand.ts#L312