@kubb/core 5.0.0-beta.63 → 5.0.0-beta.64

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