@kubb/core 5.0.0-beta.6 → 5.0.0-beta.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +25 -158
  3. package/dist/diagnostics-DiaUv_iK.d.ts +2904 -0
  4. package/dist/index.cjs +2523 -1071
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -273
  7. package/dist/index.js +2513 -1067
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-CUj1hrxa.cjs +823 -0
  10. package/dist/memoryStorage-CUj1hrxa.cjs.map +1 -0
  11. package/dist/memoryStorage-CWFzAz4o.js +714 -0
  12. package/dist/memoryStorage-CWFzAz4o.js.map +1 -0
  13. package/dist/mocks.cjs +83 -23
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +36 -10
  16. package/dist/mocks.js +85 -27
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +8 -28
  19. package/src/FileManager.ts +86 -64
  20. package/src/FileProcessor.ts +170 -44
  21. package/src/KubbDriver.ts +909 -0
  22. package/src/Transform.ts +105 -0
  23. package/src/constants.ts +111 -20
  24. package/src/createAdapter.ts +112 -17
  25. package/src/createKubb.ts +140 -517
  26. package/src/createRenderer.ts +43 -28
  27. package/src/createReporter.ts +134 -0
  28. package/src/createStorage.ts +36 -23
  29. package/src/defineGenerator.ts +140 -17
  30. package/src/defineParser.ts +30 -12
  31. package/src/definePlugin.ts +375 -21
  32. package/src/defineResolver.ts +402 -212
  33. package/src/diagnostics.ts +662 -0
  34. package/src/index.ts +8 -8
  35. package/src/mocks.ts +97 -26
  36. package/src/reporters/cliReporter.ts +89 -0
  37. package/src/reporters/fileReporter.ts +103 -0
  38. package/src/reporters/jsonReporter.ts +20 -0
  39. package/src/reporters/report.ts +85 -0
  40. package/src/storages/fsStorage.ts +23 -55
  41. package/src/types.ts +411 -887
  42. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  43. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  44. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  45. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  46. package/dist/types-DVPKmzw_.d.ts +0 -2159
  47. package/src/Kubb.ts +0 -300
  48. package/src/PluginDriver.ts +0 -426
  49. package/src/defineLogger.ts +0 -19
  50. package/src/defineMiddleware.ts +0 -62
  51. package/src/devtools.ts +0 -59
  52. package/src/renderNode.ts +0 -35
  53. package/src/utils/diagnostics.ts +0 -18
  54. package/src/utils/isInputPath.ts +0 -10
  55. package/src/utils/packageJSON.ts +0 -99
  56. /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
@@ -0,0 +1,909 @@
1
+ import { resolve } from 'node:path'
2
+ import { arrayToAsyncIterable, type AsyncEventEmitter, forBatches, getElapsedMs, isPromise, memoize, Url } from '@internals/utils'
3
+ import * as factory from '@kubb/ast/factory'
4
+ import { collectUsedSchemaNames } from '@kubb/ast/utils'
5
+ import type { FileNode, InputMeta, InputNode, OperationNode, SchemaNode } from '@kubb/ast'
6
+ import { OPERATION_FILTER_TYPES, SCHEMA_PARALLEL } from './constants.ts'
7
+ import { type Diagnostic, Diagnostics, type ProblemDiagnostic } from './diagnostics.ts'
8
+ import type { RendererFactory } from './createRenderer.ts'
9
+ import type { Storage } from './createStorage.ts'
10
+ import type { Generator } from './defineGenerator.ts'
11
+ import type { Parser } from './defineParser.ts'
12
+ import type { Plugin } from './definePlugin.ts'
13
+ import { normalizeOutput } from './definePlugin.ts'
14
+ import { defineResolver } from './defineResolver.ts'
15
+ import { FileManager } from './FileManager.ts'
16
+ import { FileProcessor } from './FileProcessor.ts'
17
+ import { Transform } from './Transform.ts'
18
+
19
+ import type {
20
+ Adapter,
21
+ AdapterSource,
22
+ Config,
23
+ GeneratorContext,
24
+ Group,
25
+ KubbHooks,
26
+ KubbPluginSetupContext,
27
+ NormalizedPlugin,
28
+ PluginFactoryOptions,
29
+ Resolver,
30
+ } from './types.ts'
31
+
32
+ type Options = {
33
+ hooks: AsyncEventEmitter<KubbHooks>
34
+ }
35
+
36
+ type HookListener<TArgs extends Array<unknown>, TResult = void> = (...args: TArgs) => TResult | Promise<TResult>
37
+
38
+ type ListenerEntry = [event: keyof KubbHooks & string, handler: HookListener<Array<unknown>, unknown>]
39
+
40
+ type RequirePluginContext = {
41
+ /**
42
+ * Name of the plugin that declared the dependency, included in the error so users can
43
+ * trace which plugin needs the missing one.
44
+ */
45
+ requiredBy?: string
46
+ }
47
+
48
+ function enforceOrder(enforce: 'pre' | 'post' | undefined): number {
49
+ return enforce === 'pre' ? -1 : enforce === 'post' ? 1 : 0
50
+ }
51
+
52
+ export class KubbDriver {
53
+ readonly config: Config
54
+ readonly options: Options
55
+
56
+ /**
57
+ * The streaming `InputNode<true>` produced by the adapter. Set after adapter setup.
58
+ * Parse-only adapters are wrapped automatically.
59
+ */
60
+ inputNode: InputNode<true> | null = null
61
+ adapter: Adapter | null = null
62
+ /**
63
+ * Raw adapter source so `adapter.parse()` / `adapter.stream()` can run lazily.
64
+ * Intentionally outlives the build, cleared by `dispose()`.
65
+ */
66
+ #adapterSource: AdapterSource | null = null
67
+
68
+ /**
69
+ * Central file store for all generated files.
70
+ * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
71
+ * add files. This property gives direct read/write access when needed.
72
+ */
73
+ readonly fileManager = new FileManager()
74
+ readonly plugins = new Map<string, NormalizedPlugin>()
75
+
76
+ /**
77
+ * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
78
+ * Used by the build loop to decide whether to emit generator events for a given plugin.
79
+ */
80
+ readonly #eventGeneratorPlugins = new Set<string>()
81
+ readonly #resolvers = new Map<string, Resolver>()
82
+ readonly #defaultResolvers = new Map<string, Resolver>()
83
+
84
+ /**
85
+ * Tracks every listener the driver added (plugin, generator) so `dispose()` can remove them
86
+ * in one pass. External `hooks.on(...)` listeners are not tracked.
87
+ */
88
+ readonly #listeners: Array<ListenerEntry> = []
89
+
90
+ /**
91
+ * Transform registry. Plugins populate it during `kubb:plugin:setup` via `addMacro`/`setMacros`,
92
+ * and `#runGenerators` reads it once per `(plugin, node)` pair through `applyTo`.
93
+ */
94
+ readonly #transforms = new Transform()
95
+
96
+ constructor(config: Config, options: Options) {
97
+ this.config = config
98
+ this.options = options
99
+ this.adapter = config.adapter ?? null
100
+ }
101
+
102
+ /**
103
+ * Attaches a listener to the shared emitter and tracks it so `dispose()` can remove it later.
104
+ * Listeners attached directly via `hooks.on(...)` are not tracked and survive disposal.
105
+ */
106
+ #trackListener<K extends keyof KubbHooks & string>(event: K, handler: HookListener<KubbHooks[K], unknown>): void {
107
+ this.hooks.on(event, handler as HookListener<KubbHooks[K]>)
108
+ this.#listeners.push([event, handler as HookListener<Array<unknown>, unknown>])
109
+ }
110
+
111
+ async setup() {
112
+ const normalized: Array<NormalizedPlugin> = this.config.plugins.map((rawPlugin) => this.#normalizePlugin(rawPlugin as Plugin))
113
+
114
+ const dependenciesByName = new Map(normalized.map((plugin) => [plugin.name, new Set(plugin.dependencies ?? [])]))
115
+
116
+ normalized.sort((a, b) => {
117
+ if (dependenciesByName.get(b.name)?.has(a.name)) return -1
118
+ if (dependenciesByName.get(a.name)?.has(b.name)) return 1
119
+
120
+ return enforceOrder(a.enforce) - enforceOrder(b.enforce)
121
+ })
122
+
123
+ for (const plugin of normalized) {
124
+ if (plugin.apply) {
125
+ plugin.apply(this.config)
126
+ }
127
+
128
+ this.#registerPlugin(plugin)
129
+ this.plugins.set(plugin.name, plugin)
130
+ }
131
+
132
+ if (this.config.adapter) {
133
+ this.#adapterSource = inputToAdapterSource(this.config)
134
+ }
135
+ }
136
+
137
+ get hooks() {
138
+ return this.options.hooks
139
+ }
140
+
141
+ /**
142
+ * Creates an `NormalizedPlugin` from a hook-style plugin and registers
143
+ * its lifecycle handlers on the `AsyncEventEmitter`.
144
+ */
145
+ #normalizePlugin(plugin: Plugin): NormalizedPlugin {
146
+ const normalized: NormalizedPlugin = {
147
+ name: plugin.name,
148
+ dependencies: plugin.dependencies,
149
+ enforce: plugin.enforce,
150
+ hooks: plugin.hooks,
151
+ options: plugin.options ?? { output: { path: '.', mode: 'directory' }, exclude: [], override: [] },
152
+ } as NormalizedPlugin
153
+
154
+ if ('apply' in plugin && typeof plugin.apply === 'function') {
155
+ normalized.apply = plugin.apply as (config: Config) => boolean
156
+ }
157
+
158
+ return normalized
159
+ }
160
+
161
+ /**
162
+ * Parses the adapter source into `this.inputNode`. Idempotent, so repeated calls from
163
+ * `run` do not re-parse. Adapters with `stream()` are used directly.
164
+ * Adapters with only `parse()` are wrapped via `factory.createInput({ stream: true })` so the dispatch loop
165
+ * stays stream-only.
166
+ */
167
+ async #parseInput(): Promise<void> {
168
+ if (this.inputNode || !this.adapter || !this.#adapterSource) return
169
+
170
+ const adapter = this.adapter
171
+ const source = this.#adapterSource
172
+
173
+ if (adapter.stream) {
174
+ this.inputNode = await adapter.stream(source)
175
+ return
176
+ }
177
+
178
+ const parsed = await adapter.parse(source)
179
+ this.inputNode = factory.createInput({
180
+ stream: true,
181
+ schemas: arrayToAsyncIterable(parsed.schemas),
182
+ operations: arrayToAsyncIterable(parsed.operations),
183
+ meta: parsed.meta,
184
+ })
185
+ }
186
+
187
+ /**
188
+ * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
189
+ *
190
+ * The `kubb:plugin:setup` listener wraps the global context in a plugin-specific one so
191
+ * `addGenerator`, `setResolver`, and `setMacros` target the right `normalizedPlugin`.
192
+ * Every other `KubbHooks` event registers as a pass-through listener that external tooling
193
+ * can observe via `hooks.on(...)`.
194
+ *
195
+ * @internal
196
+ */
197
+ #registerPlugin(plugin: NormalizedPlugin): void {
198
+ const { hooks } = plugin
199
+
200
+ if (!hooks) return
201
+
202
+ // kubb:plugin:setup gets special treatment: the globally emitted context is wrapped with
203
+ // plugin-specific implementations so that addGenerator / setResolver / etc. target
204
+ // this plugin's normalizedPlugin entry rather than being no-ops.
205
+ if (hooks['kubb:plugin:setup']) {
206
+ const setupHandler = (globalCtx: KubbPluginSetupContext) => {
207
+ const pluginCtx: KubbPluginSetupContext = {
208
+ ...globalCtx,
209
+ options: plugin.options ?? {},
210
+ addGenerator: (gen) => {
211
+ this.registerGenerator(plugin.name, gen)
212
+ },
213
+ setResolver: (resolver) => {
214
+ this.setPluginResolver(plugin.name, resolver)
215
+ },
216
+ addMacro: (macro) => {
217
+ this.#transforms.add(plugin.name, macro)
218
+ },
219
+ setMacros: (macros) => {
220
+ this.#transforms.set(plugin.name, macros)
221
+ },
222
+ setOptions: (opts) => {
223
+ plugin.options = { ...plugin.options, ...opts }
224
+ if (plugin.options.output) {
225
+ const group = 'group' in plugin.options ? (plugin.options.group as Group | null | undefined) : undefined
226
+ plugin.options.output = normalizeOutput({ output: plugin.options.output, group, pluginName: plugin.name })
227
+ }
228
+ },
229
+ injectFile: (userFileNode) => {
230
+ this.fileManager.add(factory.createFile(userFileNode))
231
+ },
232
+ }
233
+ return hooks['kubb:plugin:setup']!(pluginCtx)
234
+ }
235
+
236
+ this.#trackListener('kubb:plugin:setup', setupHandler)
237
+ }
238
+
239
+ // All other hooks are registered as direct pass-through listeners on the shared emitter.
240
+ for (const event of Object.keys(hooks) as Array<keyof KubbHooks & string>) {
241
+ if (event === 'kubb:plugin:setup') continue
242
+ const handler = hooks[event]
243
+ if (!handler) continue
244
+
245
+ this.#trackListener(event, handler as HookListener<KubbHooks[typeof event], unknown>)
246
+ }
247
+ }
248
+
249
+ /**
250
+ * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
251
+ * can configure generators, resolvers, macros and renderers before `buildStart` runs.
252
+ *
253
+ * Call this once from `safeBuild` before the plugin execution loop begins.
254
+ */
255
+ async emitSetupHooks(): Promise<void> {
256
+ const noop = () => {}
257
+
258
+ await this.hooks.emit('kubb:plugin:setup', {
259
+ config: this.config,
260
+ options: {},
261
+ addGenerator: noop,
262
+ setResolver: noop,
263
+ addMacro: noop,
264
+ setMacros: noop,
265
+ setOptions: noop,
266
+ injectFile: noop,
267
+ updateConfig: noop,
268
+ })
269
+ }
270
+
271
+ /**
272
+ * Registers a generator for the given plugin on the shared event emitter.
273
+ *
274
+ * The generator's `schema`, `operation`, and `operations` methods are registered as
275
+ * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
276
+ * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
277
+ * so that generators from different plugins do not cross-fire.
278
+ *
279
+ * The renderer comes from `generator.renderer`. Set `generator.renderer = null` (or leave it
280
+ * unset) to opt out of rendering.
281
+ *
282
+ * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
283
+ */
284
+ registerGenerator(pluginName: string, generator: Generator): void {
285
+ if (generator.schema) {
286
+ const schemaHandler = async (node: SchemaNode, ctx: GeneratorContext) => {
287
+ if (ctx.plugin.name !== pluginName) return
288
+ const result = await generator.schema!(node, ctx)
289
+
290
+ await this.dispatch({ result, renderer: generator.renderer })
291
+ }
292
+
293
+ this.#trackListener('kubb:generate:schema', schemaHandler)
294
+ }
295
+
296
+ if (generator.operation) {
297
+ const operationHandler = async (node: OperationNode, ctx: GeneratorContext) => {
298
+ if (ctx.plugin.name !== pluginName) return
299
+
300
+ const result = await generator.operation!(node, ctx)
301
+ await this.dispatch({ result, renderer: generator.renderer })
302
+ }
303
+
304
+ this.#trackListener('kubb:generate:operation', operationHandler)
305
+ }
306
+
307
+ if (generator.operations) {
308
+ const operationsHandler = async (nodes: Array<OperationNode>, ctx: GeneratorContext) => {
309
+ if (ctx.plugin.name !== pluginName) return
310
+ const result = await generator.operations!(nodes, ctx)
311
+ await this.dispatch({ result, renderer: generator.renderer })
312
+ }
313
+
314
+ this.#trackListener('kubb:generate:operations', operationsHandler)
315
+ }
316
+
317
+ this.#eventGeneratorPlugins.add(pluginName)
318
+ }
319
+
320
+ /**
321
+ * Returns `true` when at least one generator was registered for the given plugin
322
+ * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
323
+ *
324
+ * Used by the build loop to decide whether to walk the AST and emit generator events
325
+ * for a plugin that has no static `plugin.generators`.
326
+ */
327
+ hasEventGenerators(pluginName: string): boolean {
328
+ return this.#eventGeneratorPlugins.has(pluginName)
329
+ }
330
+
331
+ /**
332
+ * Runs the full plugin pipeline. Returns the diagnostics collected so far even
333
+ * when an outer hook throws, since the orchestrator preserves partial state by capturing
334
+ * the failure as a {@link Diagnostic} instead of propagating. Each plugin also
335
+ * contributes a `timing` diagnostic for the run summary.
336
+ */
337
+ async run({ storage }: { storage: Storage }): Promise<{ diagnostics: Array<Diagnostic> }> {
338
+ const { hooks, config } = this
339
+ const diagnostics: Array<Diagnostic> = []
340
+ const parsersMap = new Map<FileNode['extname'], Parser>()
341
+
342
+ for (const parser of config.parsers) {
343
+ if (parser.extNames) {
344
+ for (const ext of parser.extNames) parsersMap.set(ext, parser)
345
+ }
346
+ }
347
+
348
+ const processor = new FileProcessor({ parsers: parsersMap, storage, extension: config.output.extension })
349
+ // Bridge processor lifecycle to the user-facing kubb hooks so existing listeners on
350
+ // kubb:files:processing:* keep firing.
351
+ processor.hooks.on('start', async (files) => {
352
+ await hooks.emit('kubb:files:processing:start', { files })
353
+ })
354
+ const updateBuffer: Array<{ file: FileNode; source?: string; processed: number; total: number; percentage: number }> = []
355
+ processor.hooks.on('update', (item) => {
356
+ updateBuffer.push(item)
357
+ })
358
+ processor.hooks.on('end', async (files) => {
359
+ await hooks.emit('kubb:files:processing:update', {
360
+ files: updateBuffer.map((item) => ({ ...item, config })),
361
+ })
362
+ updateBuffer.length = 0
363
+ await hooks.emit('kubb:files:processing:end', { files })
364
+ })
365
+ const onFileUpsert = (file: FileNode): void => {
366
+ processor.enqueue(file)
367
+ }
368
+ this.fileManager.hooks.on('upsert', onFileUpsert)
369
+
370
+ // Make `diagnostics` the active sink so deep code (adapter parse, lazily consumed
371
+ // streams, generators) can report into this run via `Diagnostics.report`.
372
+ return Diagnostics.scope(
373
+ (diagnostic) => diagnostics.push(diagnostic),
374
+ async () => {
375
+ try {
376
+ const outputRoot = resolve(config.root, config.output.path)
377
+
378
+ // Parse the adapter source into the streaming `InputNode`.
379
+ await this.#parseInput()
380
+ // Emit `kubb:plugin:setup` so plugins can register macros via `addMacro`/`setMacros`.
381
+ // Each call writes into `this.#transforms`, which `#runGenerators` later reads through
382
+ // `transforms.applyTo`.
383
+ await this.emitSetupHooks()
384
+
385
+ if (this.adapter && this.inputNode) {
386
+ await hooks.emit(
387
+ 'kubb:build:start',
388
+ Object.assign({ config, adapter: this.adapter, meta: this.inputNode.meta, getPlugin: this.getPlugin.bind(this) }, this.#filesPayload()),
389
+ )
390
+ }
391
+
392
+ const generatorPlugins: Array<{ plugin: NormalizedPlugin; context: Omit<GeneratorContext, 'options'>; hrStart: ReturnType<typeof process.hrtime> }> =
393
+ []
394
+
395
+ for (const plugin of this.plugins.values()) {
396
+ const context = this.getContext(plugin)
397
+ const hrStart = process.hrtime()
398
+
399
+ try {
400
+ await hooks.emit('kubb:plugin:start', { plugin })
401
+ } catch (caughtError) {
402
+ const error = caughtError as Error
403
+ const duration = getElapsedMs(hrStart)
404
+
405
+ await this.#emitPluginEnd({ plugin, duration, success: false, error })
406
+
407
+ diagnostics.push({ ...Diagnostics.from(error), plugin: plugin.name }, Diagnostics.performance({ plugin: plugin.name, duration }))
408
+
409
+ continue
410
+ }
411
+
412
+ if (this.hasEventGenerators(plugin.name)) {
413
+ generatorPlugins.push({ plugin, context, hrStart })
414
+
415
+ continue
416
+ }
417
+
418
+ const duration = getElapsedMs(hrStart)
419
+ diagnostics.push(Diagnostics.performance({ plugin: plugin.name, duration }))
420
+
421
+ await this.#emitPluginEnd({ plugin, duration, success: true })
422
+ }
423
+
424
+ // Stream every node through the transform registry and into each plugin's generators.
425
+ // Handles the empty-entries and missing-`inputNode` cases by closing out each entry's
426
+ // `kubb:plugin:end` directly.
427
+ diagnostics.push(...(await this.#runGenerators(generatorPlugins, () => processor.flush())))
428
+ // Wait for the last in-flight batch and write anything still pending.
429
+ await processor.drain()
430
+
431
+ await hooks.emit('kubb:plugins:end', Object.assign({ config }, this.#filesPayload()))
432
+
433
+ // Plugins-end listeners (barrel plugin etc.) may have queued more files.
434
+ await processor.drain()
435
+
436
+ await hooks.emit('kubb:build:end', { files: this.fileManager.files, config, outputDir: outputRoot })
437
+
438
+ return { diagnostics: Diagnostics.dedupe(diagnostics) }
439
+ } catch (caughtError) {
440
+ diagnostics.push(Diagnostics.from(caughtError))
441
+ return { diagnostics: Diagnostics.dedupe(diagnostics) }
442
+ } finally {
443
+ this.fileManager.hooks.off('upsert', onFileUpsert)
444
+ }
445
+ },
446
+ )
447
+ }
448
+
449
+ // Returns a fresh object with a lazy `files` getter and a bound `upsertFile`.
450
+ // Caller must use `Object.assign(extra, this.#filesPayload())`, not object spread.
451
+ // Spread would eagerly invoke the getter and freeze a stale snapshot into the payload.
452
+ #filesPayload(): { readonly files: Array<FileNode>; upsertFile: (...files: Array<FileNode>) => Array<FileNode> } {
453
+ const driver = this
454
+
455
+ return {
456
+ get files() {
457
+ return driver.fileManager.files
458
+ },
459
+ upsertFile: (...files: Array<FileNode>) => driver.fileManager.upsert(...files),
460
+ }
461
+ }
462
+
463
+ #emitPluginEnd({ plugin, duration, success, error }: { plugin: NormalizedPlugin; duration: number; success: boolean; error?: Error }): Promise<void> | void {
464
+ return this.hooks.emit(
465
+ 'kubb:plugin:end',
466
+ Object.assign({ plugin, duration, success, ...(error ? { error } : {}), config: this.config }, this.#filesPayload()),
467
+ )
468
+ }
469
+
470
+ /**
471
+ * Streams schemas and operations through every plugin's generators. Each node is run
472
+ * through the plugin's macros (from `this.#transforms`) before the generator sees it,
473
+ * so plugins stay isolated and the hot path stays per-node. Schemas run before operations
474
+ * because the two passes share `flushPending` and the FileProcessor's event emitter.
475
+ * A failing plugin contributes an error diagnostic so the rest of the build continues.
476
+ * Every plugin also contributes a `timing` diagnostic.
477
+ *
478
+ * Plugins run sequentially so `kubb:plugin:end` fires as each plugin completes, instead
479
+ * of all at once after every plugin has marched through the parallel batches together.
480
+ * That ordering is what drives the CLI's `Plugins N/M` counter. Without it the bar would
481
+ * sit at the initial value until the very end of the run.
482
+ *
483
+ * When `entries` is empty or `this.inputNode` is `null`, every entry still gets a
484
+ * `kubb:plugin:end` so post-plugin listeners (the barrel writer and friends) complete.
485
+ */
486
+ async #runGenerators(
487
+ entries: Array<{ plugin: NormalizedPlugin; context: Omit<GeneratorContext, 'options'>; hrStart: ReturnType<typeof process.hrtime> }>,
488
+ flushPending: () => Promise<void>,
489
+ ): Promise<Array<Diagnostic>> {
490
+ const diagnostics: Array<Diagnostic> = []
491
+
492
+ if (entries.length === 0) return diagnostics
493
+
494
+ if (!this.inputNode) {
495
+ for (const { plugin, hrStart } of entries) {
496
+ const duration = getElapsedMs(hrStart)
497
+ diagnostics.push(Diagnostics.performance({ plugin: plugin.name, duration }))
498
+ await this.#emitPluginEnd({ plugin, duration, success: true })
499
+ }
500
+ return diagnostics
501
+ }
502
+
503
+ const transforms = this.#transforms
504
+ const { schemas, operations } = this.inputNode
505
+
506
+ type PluginState = {
507
+ plugin: NormalizedPlugin
508
+ generatorContext: Omit<GeneratorContext, 'options'>
509
+ generators: Array<Generator>
510
+ hrStart: ReturnType<typeof process.hrtime>
511
+ failed: boolean
512
+ error: Error | null
513
+ optionsAreStatic: boolean
514
+ allowedSchemaNames: Set<string> | null
515
+ }
516
+
517
+ const states: Array<PluginState> = entries.map(({ plugin, context, hrStart }) => {
518
+ const { exclude, include, override } = plugin.options
519
+ const hasExclude = Array.isArray(exclude) && exclude.length > 0
520
+ const hasInclude = Array.isArray(include) && include.length > 0
521
+ const hasOverride = Array.isArray(override) && override.length > 0
522
+ return {
523
+ plugin,
524
+ generatorContext: { ...context, resolver: this.getResolver(plugin.name) },
525
+ generators: plugin.generators ?? [],
526
+ hrStart,
527
+ failed: false,
528
+ error: null,
529
+ optionsAreStatic: !hasExclude && !hasInclude && !hasOverride,
530
+ allowedSchemaNames: null,
531
+ }
532
+ })
533
+
534
+ const emitsSchemaHook = this.hooks.listenerCount('kubb:generate:schema') > 0
535
+ const emitsOperationHook = this.hooks.listenerCount('kubb:generate:operation') > 0
536
+ const emitsOperationsHook = this.hooks.listenerCount('kubb:generate:operations') > 0
537
+
538
+ // Buffer the streaming adapter's nodes once. Each plugin reads the same buffer
539
+ // instead of re-parsing the document per pass, and the pruning pre-scan below
540
+ // shares it too (previously it iterated its own copies).
541
+ const schemasBuffer: Array<SchemaNode> = await Array.fromAsync(schemas)
542
+ const operationsBuffer: Array<OperationNode> = await Array.fromAsync(operations)
543
+
544
+ // Pre-scan: plugins with operation-based includes (but no schemaName include) need
545
+ // the reachable schema set. This requires the full schema graph in memory at once,
546
+ // since transitive reachability can't be derived from a single node.
547
+ const pruningStates = states.filter(({ plugin }) => {
548
+ const { include } = plugin.options
549
+ return (include?.some(({ type }) => OPERATION_FILTER_TYPES.has(type)) ?? false) && !(include?.some(({ type }) => type === 'schemaName') ?? false)
550
+ })
551
+
552
+ if (pruningStates.length > 0) {
553
+ const includedOpsByState = new Map<PluginState, Array<OperationNode>>(pruningStates.map((state) => [state, []]))
554
+ for (const operation of operationsBuffer) {
555
+ for (const state of pruningStates) {
556
+ const { exclude, include, override } = state.plugin.options
557
+ const options = state.generatorContext.resolver.resolveOptions(operation, { options: state.plugin.options, exclude, include, override })
558
+ if (options !== null) includedOpsByState.get(state)?.push(operation)
559
+ }
560
+ }
561
+
562
+ for (const state of pruningStates) {
563
+ state.allowedSchemaNames = collectUsedSchemaNames(includedOpsByState.get(state) ?? [], schemasBuffer)
564
+ includedOpsByState.delete(state)
565
+ }
566
+ }
567
+
568
+ // Apply the plugin's macros, then resolve options (skipping the resolver when
569
+ // optionsAreStatic). Returns null when include/exclude/override rules out the node.
570
+ // The per-node dispatch and the collected-operations tail both go through this so
571
+ // they agree on what a plugin sees.
572
+ const resolveForPlugin = <TNode extends SchemaNode | OperationNode>(
573
+ state: PluginState,
574
+ node: TNode,
575
+ ): { transformedNode: TNode; options: NormalizedPlugin['options'] } | null => {
576
+ const { plugin, generatorContext } = state
577
+ const transformedNode = transforms.applyTo(plugin.name, node)
578
+ if (state.optionsAreStatic) return { transformedNode, options: plugin.options }
579
+
580
+ const { exclude, include, override } = plugin.options
581
+ const options = generatorContext.resolver.resolveOptions(transformedNode, { options: plugin.options, exclude, include, override })
582
+ if (options === null) return null
583
+ return { transformedNode, options }
584
+ }
585
+
586
+ // Schema and operation passes share this body. They differ only in which generator
587
+ // method runs, which hook is emitted, and the schema-only `allowedSchemaNames` prune
588
+ // (operations don't carry that constraint).
589
+ const dispatchNode = async <TNode extends SchemaNode | OperationNode>(
590
+ state: PluginState,
591
+ node: TNode,
592
+ dispatch: {
593
+ method: 'schema' | 'operation'
594
+ checkAllowedNames: boolean
595
+ emit: ((node: TNode, ctx: GeneratorContext) => Promise<void> | void) | null
596
+ },
597
+ ): Promise<void> => {
598
+ if (state.failed) return
599
+ try {
600
+ const resolved = resolveForPlugin(state, node)
601
+ if (!resolved) return
602
+
603
+ const { transformedNode, options } = resolved
604
+ if (
605
+ dispatch.checkAllowedNames &&
606
+ state.allowedSchemaNames !== null &&
607
+ 'name' in transformedNode &&
608
+ transformedNode.name &&
609
+ !state.allowedSchemaNames.has(transformedNode.name)
610
+ ) {
611
+ return
612
+ }
613
+
614
+ const ctx = { ...state.generatorContext, options }
615
+ for (const gen of state.generators) {
616
+ const run = gen[dispatch.method] as ((node: TNode, ctx: GeneratorContext) => unknown) | undefined
617
+ if (!run) continue
618
+ const raw = run(transformedNode, ctx)
619
+ const result = isPromise(raw) ? await raw : raw
620
+ const applied = this.dispatch({ result, renderer: gen.renderer })
621
+ if (isPromise(applied)) await applied
622
+ }
623
+ if (dispatch.emit) await dispatch.emit(transformedNode, ctx)
624
+ } catch (caughtError) {
625
+ state.failed = true
626
+ state.error = caughtError as Error
627
+ }
628
+ }
629
+
630
+ const schemaDispatch = {
631
+ method: 'schema',
632
+ checkAllowedNames: true,
633
+ emit: emitsSchemaHook ? (node: SchemaNode, ctx: GeneratorContext) => this.hooks.emit('kubb:generate:schema', node, ctx) : null,
634
+ } as const
635
+ const operationDispatch = {
636
+ method: 'operation',
637
+ checkAllowedNames: false,
638
+ emit: emitsOperationHook ? (node: OperationNode, ctx: GeneratorContext) => this.hooks.emit('kubb:generate:operation', node, ctx) : null,
639
+ } as const
640
+
641
+ for (const state of states) {
642
+ // Skip building the aggregated operations array when this plugin doesn't consume it.
643
+ // Saves an N-sized allocation when the plugin only defines per-node `gen.operation`.
644
+ const needsCollectedOperations = emitsOperationsHook || state.generators.some((gen) => !!gen.operations)
645
+ const collectedOperations: Array<OperationNode> | undefined = needsCollectedOperations ? [] : undefined
646
+
647
+ // Run schemas before operations: the two passes share `flushPending` and the
648
+ // FileProcessor's event emitter, so running them concurrently would interleave
649
+ // `kubb:files:processing:start|end` events and race on the shared dirty list.
650
+ await forBatches(schemasBuffer, (nodes) => Promise.all(nodes.map((node) => dispatchNode(state, node, schemaDispatch))), {
651
+ concurrency: SCHEMA_PARALLEL,
652
+ flush: flushPending,
653
+ })
654
+
655
+ await forBatches(
656
+ operationsBuffer,
657
+ (nodes) => {
658
+ if (needsCollectedOperations) collectedOperations?.push(...nodes)
659
+ return Promise.all(nodes.map((node) => dispatchNode(state, node, operationDispatch)))
660
+ },
661
+ { concurrency: SCHEMA_PARALLEL, flush: flushPending },
662
+ )
663
+
664
+ if (!state.failed && needsCollectedOperations) {
665
+ try {
666
+ const { plugin, generatorContext, generators } = state
667
+ const ctx = { ...generatorContext, options: plugin.options }
668
+ // Match what the per-node dispatch passes to gen.operation(): the transformed node,
669
+ // already filtered by excludes/includes/overrides.
670
+ const ops = collectedOperations ?? []
671
+ const pluginOperations = ops.reduce<Array<OperationNode>>((acc, node) => {
672
+ const resolved = resolveForPlugin(state, node)
673
+ if (resolved) acc.push(resolved.transformedNode)
674
+ return acc
675
+ }, [])
676
+ for (const gen of generators) {
677
+ if (!gen.operations) continue
678
+ const result = await gen.operations(pluginOperations, ctx)
679
+ await this.dispatch({ result, renderer: gen.renderer })
680
+ }
681
+ await this.hooks.emit('kubb:generate:operations', pluginOperations, ctx)
682
+ } catch (caughtError) {
683
+ state.failed = true
684
+ state.error = caughtError as Error
685
+ }
686
+ }
687
+
688
+ const duration = getElapsedMs(state.hrStart)
689
+ await this.#emitPluginEnd({ plugin: state.plugin, duration, success: !state.failed, error: state.failed && state.error ? state.error : undefined })
690
+
691
+ if (state.failed && state.error) {
692
+ diagnostics.push({ ...Diagnostics.from(state.error), plugin: state.plugin.name })
693
+ }
694
+ diagnostics.push(Diagnostics.performance({ plugin: state.plugin.name, duration }))
695
+ }
696
+
697
+ return diagnostics
698
+ }
699
+
700
+ /**
701
+ * Stores whatever a generator method or `kubb:generate:*` hook returned.
702
+ *
703
+ * - An `Array<FileNode>` goes straight into `fileManager` via `upsert`.
704
+ * - A renderer element runs through `renderer` (the renderer factory, e.g. JSX) and the
705
+ * produced files go to `fileManager.upsert`.
706
+ * - A falsy result is treated as a no-op. The generator wrote files itself via
707
+ * `ctx.upsertFile`.
708
+ *
709
+ * Pass `renderer` when the result may be a renderer element. Generators that only return
710
+ * `Array<FileNode>` do not need one.
711
+ */
712
+ async dispatch<TElement = unknown>({
713
+ result,
714
+ renderer,
715
+ }: {
716
+ result: TElement | Array<FileNode> | undefined | null
717
+ renderer?: RendererFactory<TElement> | null
718
+ }): Promise<void> {
719
+ if (!result) return
720
+
721
+ if (Array.isArray(result)) {
722
+ this.fileManager.upsert(...(result as Array<FileNode>))
723
+ return
724
+ }
725
+
726
+ if (!renderer) {
727
+ return
728
+ }
729
+
730
+ using instance = renderer()
731
+ if (instance.stream) {
732
+ for (const file of instance.stream(result)) {
733
+ this.fileManager.upsert(file)
734
+ }
735
+ return
736
+ }
737
+
738
+ await instance.render(result)
739
+ this.fileManager.upsert(...instance.files)
740
+ }
741
+
742
+ /**
743
+ * Removes every listener the driver added. Listeners attached directly to `hooks` from outside
744
+ * the driver survive. Called at the end of a build to prevent leaks across repeated builds.
745
+ *
746
+ * @internal
747
+ */
748
+ dispose(): void {
749
+ for (const [event, handler] of this.#listeners) {
750
+ this.hooks.off(event, handler as HookListener<KubbHooks[typeof event]>)
751
+ }
752
+ this.#listeners.length = 0
753
+ this.#eventGeneratorPlugins.clear()
754
+ this.#transforms.dispose()
755
+ // Release resolver closures. The driver is rebuilt for each build() call
756
+ // so there is no value in retaining these maps after disposal.
757
+ this.#resolvers.clear()
758
+ this.#defaultResolvers.clear()
759
+ // Release the FileNode cache and parsed adapter graph so memory is reclaimed
760
+ // between builds. The returned `BuildOutput.files` array still references any
761
+ // FileNodes the caller needs to inspect.
762
+ this.fileManager.dispose()
763
+ this.inputNode = null
764
+ this.#adapterSource = null
765
+ }
766
+
767
+ [Symbol.dispose](): void {
768
+ this.dispose()
769
+ }
770
+
771
+ #getDefaultResolver = memoize(
772
+ this.#defaultResolvers,
773
+ (pluginName: string): Resolver => defineResolver<PluginFactoryOptions>(() => ({ name: 'default', pluginName })),
774
+ )
775
+
776
+ /**
777
+ * Merges `partial` with the plugin's default resolver and stores the result.
778
+ * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
779
+ * get the up-to-date resolver without going through `getResolver()`.
780
+ */
781
+ setPluginResolver(pluginName: string, partial: Partial<Resolver>): void {
782
+ const defaultResolver = this.#getDefaultResolver(pluginName)
783
+ const merged = { ...defaultResolver, ...partial }
784
+ this.#resolvers.set(pluginName, merged)
785
+ const plugin = this.plugins.get(pluginName)
786
+ if (plugin) {
787
+ plugin.resolver = merged
788
+ }
789
+ }
790
+
791
+ /**
792
+ * Returns the resolver for the given plugin.
793
+ *
794
+ * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
795
+ * plugin → lazily created default resolver (identity name, no path transforms).
796
+ */
797
+ getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver']
798
+ getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver
799
+ getResolver(pluginName: string): Resolver {
800
+ return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#getDefaultResolver(pluginName)
801
+ }
802
+
803
+ getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {
804
+ const driver = this
805
+
806
+ // Collect into the active build only. The host renders each collected diagnostic once after the
807
+ // build (the CLI via `Diagnostics.emit`, the agent via its post-build loop), so emitting a live
808
+ // `kubb:error`/`kubb:warn`/`kubb:info` here would render it twice.
809
+ const report = (diagnostic: Omit<ProblemDiagnostic, 'plugin'>): void => {
810
+ Diagnostics.report({ ...diagnostic, plugin: plugin.name })
811
+ }
812
+
813
+ return {
814
+ config: driver.config,
815
+ get root(): string {
816
+ return resolve(driver.config.root, driver.config.output.path)
817
+ },
818
+ hooks: driver.hooks,
819
+ plugin,
820
+ getPlugin: driver.getPlugin.bind(driver),
821
+ // Close over the owning plugin so a missing dependency error names who required it.
822
+ requirePlugin: ((name: string) => driver.requirePlugin(name, { requiredBy: plugin.name })) as GeneratorContext<TOptions>['requirePlugin'],
823
+ getResolver: driver.getResolver.bind(driver),
824
+ driver,
825
+ addFile: async (...files: Array<FileNode>) => {
826
+ driver.fileManager.add(...files)
827
+ },
828
+ upsertFile: async (...files: Array<FileNode>) => {
829
+ driver.fileManager.upsert(...files)
830
+ },
831
+ get meta(): InputMeta {
832
+ return driver.inputNode?.meta ?? { circularNames: [], enumNames: [] }
833
+ },
834
+ get adapter(): Adapter {
835
+ // Generators only read `adapter` during AST hooks, which run after the
836
+ // adapter is set, so it is guaranteed defined at read time.
837
+ return driver.adapter!
838
+ },
839
+ get resolver() {
840
+ return driver.getResolver(plugin.name)
841
+ },
842
+ warn(message: string) {
843
+ report({ code: Diagnostics.code.pluginWarning, severity: 'warning', message })
844
+ },
845
+ error(error: string | Error) {
846
+ const cause = typeof error === 'string' ? undefined : error
847
+ report({ code: Diagnostics.code.pluginFailed, severity: 'error', message: typeof error === 'string' ? error : error.message, cause })
848
+ },
849
+ info(message: string) {
850
+ report({ code: Diagnostics.code.pluginInfo, severity: 'info', message })
851
+ },
852
+ }
853
+ }
854
+
855
+ getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
856
+ getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined
857
+ getPlugin(pluginName: string): Plugin | undefined {
858
+ return this.plugins.get(pluginName)
859
+ }
860
+
861
+ /**
862
+ * Like `getPlugin` but throws a descriptive error when the plugin is not found.
863
+ */
864
+ requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName, context?: RequirePluginContext): Plugin<Kubb.PluginRegistry[TName]>
865
+ requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string, context?: RequirePluginContext): Plugin<TOptions>
866
+ requirePlugin(pluginName: string, context?: RequirePluginContext): Plugin {
867
+ const plugin = this.plugins.get(pluginName)
868
+ if (!plugin) {
869
+ const requiredBy = context?.requiredBy
870
+ throw new Diagnostics.Error({
871
+ code: Diagnostics.code.pluginNotFound,
872
+ severity: 'error',
873
+ message: requiredBy
874
+ ? `Plugin "${pluginName}" is required by "${requiredBy}" but not found. Make sure it is included in your Kubb config.`
875
+ : `Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`,
876
+ help: requiredBy
877
+ ? `Add "${pluginName}" to the \`plugins\` array in kubb.config.ts (required by "${requiredBy}"), or remove the dependency on it.`
878
+ : `Add "${pluginName}" to the \`plugins\` array in kubb.config.ts, or remove the dependency on it.`,
879
+ location: { kind: 'config' },
880
+ })
881
+ }
882
+ return plugin
883
+ }
884
+ }
885
+
886
+ function inputToAdapterSource(config: Config): AdapterSource {
887
+ const input = config.input
888
+ if (!input) {
889
+ throw new Diagnostics.Error({
890
+ code: Diagnostics.code.inputRequired,
891
+ severity: 'error',
892
+ message: 'An adapter is configured without an input.',
893
+ help: 'Provide `input.path` (a file or URL) or `input.data` (an inline spec) in your Kubb config.',
894
+ location: { kind: 'config' },
895
+ })
896
+ }
897
+
898
+ if ('data' in input) {
899
+ return { type: 'data', data: input.data }
900
+ }
901
+
902
+ if (Url.canParse(input.path)) {
903
+ return { type: 'path', path: input.path }
904
+ }
905
+
906
+ const resolved = resolve(config.root, input.path)
907
+
908
+ return { type: 'path', path: resolved }
909
+ }