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