import { defineExtension as coreDefineExtension, type KubbPluginSetupContext, type Plugin, type PluginDefinition, type PluginFactory, type PluginType, type PluginVisitHooks, } from '@kubb/core' import { addGenerator, addMacro, addTemplate, provide, setMacros, setPrinter, setResolver, slot } from './composables.ts' import { generatorVisitor } from './visitor.ts' type AnyHook = (...args: Array) => unknown const define = coreDefineExtension as (definition: unknown) => unknown /** * The setup context of a plugin defined with kit's `defineExtension`: core's setup context plus kit's * setup composables as methods. `kubb.addTemplate(...)` and `addTemplate(...)` register the same. */ export type KitSetupContext = KubbPluginSetupContext & { addTemplate: typeof addTemplate /** @deprecated Use `defineTemplate` and `addTemplate`. Removed in Kubb v6. */ addGenerator: typeof addGenerator setResolver: typeof setResolver addMacro: typeof addMacro /** @deprecated Use `addMacro(...macros)`, and pass the user's `macros` option to it yourself. Removed in Kubb v6. */ setMacros: typeof setMacros setPrinter: typeof setPrinter provide: typeof provide } /** * Core's plugin definition, with `setup` and `kubb:plugin:setup` typed to receive {@link KitSetupContext}. */ export type KitPluginDefinition = Omit, 'setup' | 'hooks'> & { hooks?: Omit['hooks'], 'kubb:plugin:setup'> & { 'kubb:plugin:setup'?(this: KitSetupContext, ctx: KitSetupContext): void | Promise } setup?( this: KitSetupContext, options: TFactory['options'], kubb: KitSetupContext, ): TFactory['resolvedOptions'] | Promise } export type KitDefineExtension = (( definition: { kind: 'plugin' } & KitPluginDefinition, ) => PluginFactory) & typeof coreDefineExtension /** * Core's `defineExtension`. For `kind: 'plugin'` it also keeps the plugin's generators, resolver * and macros in memo and runs them through the visit hooks, before any visit hooks the plugin * defines itself. The setup context also gets kit's setup composables as methods. */ export const defineExtension = ((definition: { kind: string } & Partial) => { if (definition.kind !== 'plugin') return define(definition) const visitor = generatorVisitor() const hooks: Record = { ...definition.hooks } for (const name of Object.keys(visitor) as Array) { const own = definition.hooks?.[name] as AnyHook | undefined const kit = visitor[name] as AnyHook hooks[name] = own ? async function (this: unknown, ...args: Array) { await kit.apply(this, args) await own.apply(this, args) } : kit } return define({ ...definition, hooks, setup(options: object, kubb: KubbPluginSetupContext) { kubb.memo.set(slot.kit(kubb.plugin.name), true) // `kubb:plugin:setup` gets this same object, so it sees the methods too. A context that already // has a method, such as a test mock with spies, keeps its own. const ctx = kubb as KitSetupContext ctx.addTemplate ??= addTemplate ctx.addGenerator ??= addGenerator ctx.setResolver ??= setResolver ctx.addMacro ??= addMacro ctx.setMacros ??= setMacros ctx.setPrinter ??= setPrinter ctx.provide ??= provide return definition.setup ? definition.setup.call(ctx, options, ctx) : options }, }) }) as KitDefineExtension /** * Defines a plugin through kit's `defineExtension`, so it runs kit's visitor. * * @deprecated Use `defineExtension` with `kind: 'plugin'` instead. Removed in Kubb v6. */ export function definePlugin(definition: KitPluginDefinition): PluginFactory { return defineExtension({ kind: 'plugin', ...definition }) }