Before and After Lifecycle Functions
All events expose an identical interface with two functions that can be handled by the plugin and responsible for a different step in the lifecycle of the event. One for "before" the event is emitted and another for "after" the event is emitted.
Event Structure and Parameters
export interface PluginEventType<T extends EventParams> {
before?: PluginBeforeEvent<T>;
after?: PluginAfterEvent<T>;
}
For each event, the type of EventParams
is different, providing access to relevant data for the specific event.
export interface EventParams {}
Before and After Function Signatures
export type PluginBeforeEvent<T extends EventParams> = (
dsgContext: DsgContext,
eventParams: T
) => Promisable<T>;
// Node.js [version](https://github.com/amplication/amplication/blob/master/libs/util/code-gen-types/src/plugins.types.ts#L21)
export type PluginAfterEvent<T extends EventParams> = (
dsgContext: DsgContext,
eventParams: T,
modules: ModuleMap
) => Promisable<ModuleMap>;
// .NET [version](https://github.com/amplication/amplication/blob/master/libs/util/code-gen-types/src/dotnet-plugins.types.ts#L22)
export type PluginAfterEvent<T extends EventParams, F> = (
dsgContext: DsgContext,
eventParams: T,
files: FileMap<F>
) => Promisable<FileMap<F>>;
In the before
and after
functions, we have an access to the context and the event params.
The context is used to gather common parts between events.
The event params manipulate the default behavior by passing different values.
Accessing and Modifying Generated Files
In the after
function, we also have access to the generated files. An example of using this parameter is when you want to restructure the generated files in a different folder structure.
Function Examples
Most of the functions include examples. But, if you're looking for more comprehensive and real-world examples, you can explore the code of the Amplication plugins directly. These plugins showcase various implementations and use cases for the before and after lifecycle functions.
Best Practices and Cautions
- In the
after
function, avoid unintentionally overriding the entire generated file. Opt for smaller changes instead. - In the
before
function, take care when modifying templates to not unintentionally affect code generation. - In the
before
function, only chooseskipDefaultBehavior
if skipping code generation entirely, including default generation, is intended.