@kubb/core 5.0.0-alpha.6 → 5.0.0-alpha.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.
- package/README.md +3 -2
- package/dist/PluginDriver-Bc0HQM8V.js +948 -0
- package/dist/PluginDriver-Bc0HQM8V.js.map +1 -0
- package/dist/PluginDriver-Dyl2fwfQ.cjs +1039 -0
- package/dist/PluginDriver-Dyl2fwfQ.cjs.map +1 -0
- package/dist/index.cjs +691 -1798
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +279 -265
- package/dist/index.js +678 -1765
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +138 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +133 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-mW3-Ihuf.d.ts +1903 -0
- package/package.json +51 -57
- package/src/FileManager.ts +110 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +205 -130
- package/src/PluginDriver.ts +424 -0
- package/src/constants.ts +20 -47
- package/src/createAdapter.ts +25 -0
- package/src/createKubb.ts +527 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +58 -0
- package/src/defineGenerator.ts +88 -100
- package/src/defineLogger.ts +13 -3
- package/src/defineMiddleware.ts +59 -0
- package/src/defineParser.ts +45 -0
- package/src/definePlugin.ts +78 -7
- package/src/defineResolver.ts +521 -0
- package/src/devtools.ts +14 -14
- package/src/index.ts +13 -17
- package/src/mocks.ts +171 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +40 -11
- package/src/storages/memoryStorage.ts +4 -3
- package/src/types.ts +738 -218
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +99 -0
- package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -60
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -64
- package/dist/hooks.js +0 -56
- package/dist/hooks.js.map +0 -1
- package/src/BarrelManager.ts +0 -74
- package/src/PackageManager.ts +0 -180
- package/src/PluginManager.ts +0 -667
- package/src/PromiseManager.ts +0 -40
- package/src/build.ts +0 -419
- package/src/config.ts +0 -56
- package/src/defineAdapter.ts +0 -22
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -55
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginManager.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/TreeNode.ts +0 -215
- package/src/utils/executeStrategies.ts +0 -81
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getBarrelFiles.ts +0 -79
- package/src/utils/getConfigs.ts +0 -30
- package/src/utils/getPlugins.ts +0 -23
- package/src/utils/linters.ts +0 -25
- package/src/utils/resolveOptions.ts +0 -93
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { extname, resolve } from 'node:path'
|
|
2
|
+
import type { AsyncEventEmitter } from '@internals/utils'
|
|
3
|
+
import type { FileNode, InputNode, OperationNode, SchemaNode } from '@kubb/ast'
|
|
4
|
+
import { createFile } from '@kubb/ast'
|
|
5
|
+
import { DEFAULT_STUDIO_URL } from './constants.ts'
|
|
6
|
+
import type { Generator } from './defineGenerator.ts'
|
|
7
|
+
import type { Plugin } from './definePlugin.ts'
|
|
8
|
+
import { defineResolver } from './defineResolver.ts'
|
|
9
|
+
import { openInStudio as openInStudioFn } from './devtools.ts'
|
|
10
|
+
import { FileManager } from './FileManager.ts'
|
|
11
|
+
import { applyHookResult } from './renderNode.ts'
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
Adapter,
|
|
15
|
+
Config,
|
|
16
|
+
DevtoolsOptions,
|
|
17
|
+
GeneratorContext,
|
|
18
|
+
KubbHooks,
|
|
19
|
+
KubbPluginSetupContext,
|
|
20
|
+
NormalizedPlugin,
|
|
21
|
+
PluginFactoryOptions,
|
|
22
|
+
Resolver,
|
|
23
|
+
} from './types.ts'
|
|
24
|
+
|
|
25
|
+
// inspired by: https://github.com/rollup/rollup/blob/master/src/utils/PluginDriver.ts#
|
|
26
|
+
|
|
27
|
+
type Options = {
|
|
28
|
+
hooks: AsyncEventEmitter<KubbHooks>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function enforceOrder(enforce: 'pre' | 'post' | undefined): number {
|
|
32
|
+
return enforce === 'pre' ? -1 : enforce === 'post' ? 1 : 0
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class PluginDriver {
|
|
36
|
+
readonly config: Config
|
|
37
|
+
readonly options: Options
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* PluginDriver.getMode('src/gen/types.ts') // 'single'
|
|
45
|
+
* PluginDriver.getMode('src/gen/types') // 'split'
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
static getMode(fileOrFolder: string | undefined | null): 'single' | 'split' {
|
|
49
|
+
if (!fileOrFolder) {
|
|
50
|
+
return 'split'
|
|
51
|
+
}
|
|
52
|
+
return extname(fileOrFolder) ? 'single' : 'split'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The universal `@kubb/ast` `InputNode` produced by the adapter, set by
|
|
57
|
+
* the build pipeline after the adapter's `parse()` resolves.
|
|
58
|
+
*/
|
|
59
|
+
inputNode: InputNode | undefined = undefined
|
|
60
|
+
adapter: Adapter | undefined = undefined
|
|
61
|
+
#studioIsOpen = false
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Central file store for all generated files.
|
|
65
|
+
* Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
|
|
66
|
+
* add files; this property gives direct read/write access when needed.
|
|
67
|
+
*/
|
|
68
|
+
readonly fileManager = new FileManager()
|
|
69
|
+
|
|
70
|
+
readonly plugins = new Map<string, NormalizedPlugin>()
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Tracks which plugins have generators registered via `addGenerator()` (event-based path).
|
|
74
|
+
* Used by the build loop to decide whether to emit generator events for a given plugin.
|
|
75
|
+
*/
|
|
76
|
+
readonly #pluginsWithEventGenerators = new Set<string>()
|
|
77
|
+
readonly #resolvers = new Map<string, Resolver>()
|
|
78
|
+
readonly #defaultResolvers = new Map<string, Resolver>()
|
|
79
|
+
readonly #hookListeners = new Map<keyof KubbHooks, Set<(...args: never[]) => void | Promise<void>>>()
|
|
80
|
+
|
|
81
|
+
constructor(config: Config, options: Options) {
|
|
82
|
+
this.config = config
|
|
83
|
+
this.options = options
|
|
84
|
+
config.plugins
|
|
85
|
+
.map((rawPlugin) => this.#normalizePlugin(rawPlugin as Plugin))
|
|
86
|
+
.filter((plugin) => {
|
|
87
|
+
if (typeof plugin.apply === 'function') {
|
|
88
|
+
return plugin.apply(config)
|
|
89
|
+
}
|
|
90
|
+
return true
|
|
91
|
+
})
|
|
92
|
+
.sort((a, b) => {
|
|
93
|
+
if (b.dependencies?.includes(a.name)) return -1
|
|
94
|
+
if (a.dependencies?.includes(b.name)) return 1
|
|
95
|
+
// enforce: 'pre' plugins run first, 'post' plugins run last
|
|
96
|
+
return enforceOrder(a.enforce) - enforceOrder(b.enforce)
|
|
97
|
+
})
|
|
98
|
+
.forEach((plugin) => {
|
|
99
|
+
this.plugins.set(plugin.name, plugin)
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get hooks() {
|
|
104
|
+
return this.options.hooks
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Creates an `NormalizedPlugin` from a hook-style plugin and registers
|
|
109
|
+
* its lifecycle handlers on the `AsyncEventEmitter`.
|
|
110
|
+
*/
|
|
111
|
+
#normalizePlugin(hookPlugin: Plugin): NormalizedPlugin {
|
|
112
|
+
const normalizedPlugin = {
|
|
113
|
+
name: hookPlugin.name,
|
|
114
|
+
dependencies: hookPlugin.dependencies,
|
|
115
|
+
enforce: hookPlugin.enforce,
|
|
116
|
+
options: { output: { path: '.' }, exclude: [], override: [] },
|
|
117
|
+
} as unknown as NormalizedPlugin
|
|
118
|
+
|
|
119
|
+
this.registerPluginHooks(hookPlugin, normalizedPlugin)
|
|
120
|
+
return normalizedPlugin
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
|
|
125
|
+
*
|
|
126
|
+
* For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
|
|
127
|
+
* plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
|
|
128
|
+
* `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
|
|
129
|
+
*
|
|
130
|
+
* All other hooks are iterated and registered directly as pass-through listeners.
|
|
131
|
+
* Any event key present in the global `KubbHooks` interface can be subscribed to.
|
|
132
|
+
*
|
|
133
|
+
* External tooling can subscribe to any of these events via `hooks.on(...)` to observe
|
|
134
|
+
* the plugin lifecycle without modifying plugin behavior.
|
|
135
|
+
*
|
|
136
|
+
* @internal
|
|
137
|
+
*/
|
|
138
|
+
registerPluginHooks(hookPlugin: Plugin, normalizedPlugin: NormalizedPlugin): void {
|
|
139
|
+
const { hooks } = hookPlugin
|
|
140
|
+
|
|
141
|
+
// kubb:plugin:setup gets special treatment: the globally emitted context is wrapped with
|
|
142
|
+
// plugin-specific implementations so that addGenerator / setResolver / etc. target
|
|
143
|
+
// this plugin's normalizedPlugin entry rather than being no-ops.
|
|
144
|
+
if (hooks['kubb:plugin:setup']) {
|
|
145
|
+
const setupHandler = (globalCtx: KubbPluginSetupContext) => {
|
|
146
|
+
const pluginCtx: KubbPluginSetupContext = {
|
|
147
|
+
...globalCtx,
|
|
148
|
+
options: hookPlugin.options ?? {},
|
|
149
|
+
addGenerator: (gen) => {
|
|
150
|
+
this.registerGenerator(normalizedPlugin.name, gen)
|
|
151
|
+
},
|
|
152
|
+
setResolver: (resolver) => {
|
|
153
|
+
this.setPluginResolver(normalizedPlugin.name, resolver)
|
|
154
|
+
},
|
|
155
|
+
setTransformer: (visitor) => {
|
|
156
|
+
normalizedPlugin.transformer = visitor
|
|
157
|
+
},
|
|
158
|
+
setRenderer: (renderer) => {
|
|
159
|
+
normalizedPlugin.renderer = renderer
|
|
160
|
+
},
|
|
161
|
+
setOptions: (opts) => {
|
|
162
|
+
normalizedPlugin.options = { ...normalizedPlugin.options, ...opts }
|
|
163
|
+
},
|
|
164
|
+
injectFile: ({ sources = [], ...rest }) => {
|
|
165
|
+
this.fileManager.add(createFile({ imports: [], exports: [], sources, ...rest }))
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
return hooks['kubb:plugin:setup']!(pluginCtx)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.hooks.on('kubb:plugin:setup', setupHandler)
|
|
172
|
+
this.#trackHookListener('kubb:plugin:setup', setupHandler as (...args: never[]) => void | Promise<void>)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// All other hooks are registered as direct pass-through listeners on the shared emitter.
|
|
176
|
+
for (const [event, handler] of Object.entries(hooks) as Array<[keyof KubbHooks, ((...args: never[]) => void | Promise<void>) | undefined]>) {
|
|
177
|
+
if (event === 'kubb:plugin:setup' || !handler) continue
|
|
178
|
+
|
|
179
|
+
this.hooks.on(event, handler as never)
|
|
180
|
+
this.#trackHookListener(event, handler as (...args: never[]) => void | Promise<void>)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
|
|
186
|
+
* can configure generators, resolvers, transformers and renderers before `buildStart` runs.
|
|
187
|
+
*
|
|
188
|
+
* Call this once from `safeBuild` before the plugin execution loop begins.
|
|
189
|
+
*/
|
|
190
|
+
async emitSetupHooks(): Promise<void> {
|
|
191
|
+
const noop = () => {}
|
|
192
|
+
await this.hooks.emit('kubb:plugin:setup', {
|
|
193
|
+
config: this.config,
|
|
194
|
+
options: {},
|
|
195
|
+
addGenerator: noop,
|
|
196
|
+
setResolver: noop,
|
|
197
|
+
setTransformer: noop,
|
|
198
|
+
setRenderer: noop,
|
|
199
|
+
setOptions: noop,
|
|
200
|
+
injectFile: noop,
|
|
201
|
+
updateConfig: noop,
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Registers a generator for the given plugin on the shared event emitter.
|
|
207
|
+
*
|
|
208
|
+
* The generator's `schema`, `operation`, and `operations` methods are registered as
|
|
209
|
+
* listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
|
|
210
|
+
* respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
|
|
211
|
+
* so that generators from different plugins do not cross-fire.
|
|
212
|
+
*
|
|
213
|
+
* The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
|
|
214
|
+
* Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
|
|
215
|
+
* declares a renderer.
|
|
216
|
+
*
|
|
217
|
+
* Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
|
|
218
|
+
*/
|
|
219
|
+
registerGenerator(pluginName: string, gen: Generator): void {
|
|
220
|
+
const resolveRenderer = () => {
|
|
221
|
+
const plugin = this.plugins.get(pluginName)
|
|
222
|
+
return gen.renderer === null ? undefined : (gen.renderer ?? plugin?.renderer ?? this.config.renderer)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (gen.schema) {
|
|
226
|
+
const schemaHandler = async (node: SchemaNode, ctx: GeneratorContext) => {
|
|
227
|
+
if (ctx.plugin.name !== pluginName) return
|
|
228
|
+
const result = await gen.schema!(node, ctx)
|
|
229
|
+
await applyHookResult(result, this, resolveRenderer())
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
this.hooks.on('kubb:generate:schema', schemaHandler)
|
|
233
|
+
this.#trackHookListener('kubb:generate:schema', schemaHandler as (...args: never[]) => void | Promise<void>)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (gen.operation) {
|
|
237
|
+
const operationHandler = async (node: OperationNode, ctx: GeneratorContext) => {
|
|
238
|
+
if (ctx.plugin.name !== pluginName) return
|
|
239
|
+
const result = await gen.operation!(node, ctx)
|
|
240
|
+
await applyHookResult(result, this, resolveRenderer())
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.hooks.on('kubb:generate:operation', operationHandler)
|
|
244
|
+
this.#trackHookListener('kubb:generate:operation', operationHandler as (...args: never[]) => void | Promise<void>)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (gen.operations) {
|
|
248
|
+
const operationsHandler = async (nodes: Array<OperationNode>, ctx: GeneratorContext) => {
|
|
249
|
+
if (ctx.plugin.name !== pluginName) return
|
|
250
|
+
const result = await gen.operations!(nodes, ctx)
|
|
251
|
+
await applyHookResult(result, this, resolveRenderer())
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
this.hooks.on('kubb:generate:operations', operationsHandler)
|
|
255
|
+
this.#trackHookListener('kubb:generate:operations', operationsHandler as (...args: never[]) => void | Promise<void>)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.#pluginsWithEventGenerators.add(pluginName)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Returns `true` when at least one generator was registered for the given plugin
|
|
263
|
+
* via `addGenerator()` in `kubb:plugin:setup` (event-based path).
|
|
264
|
+
*
|
|
265
|
+
* Used by the build loop to decide whether to walk the AST and emit generator events
|
|
266
|
+
* for a plugin that has no static `plugin.generators`.
|
|
267
|
+
*/
|
|
268
|
+
hasRegisteredGenerators(pluginName: string): boolean {
|
|
269
|
+
return this.#pluginsWithEventGenerators.has(pluginName)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Unregisters all plugin lifecycle listeners from the shared event emitter.
|
|
274
|
+
* Called at the end of a build to prevent listener leaks across repeated builds.
|
|
275
|
+
*
|
|
276
|
+
* @internal
|
|
277
|
+
*/
|
|
278
|
+
dispose(): void {
|
|
279
|
+
for (const [event, handlers] of this.#hookListeners) {
|
|
280
|
+
for (const handler of handlers) {
|
|
281
|
+
this.hooks.off(event, handler as never)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
this.#hookListeners.clear()
|
|
285
|
+
this.#pluginsWithEventGenerators.clear()
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
#trackHookListener(event: keyof KubbHooks, handler: (...args: never[]) => void | Promise<void>): void {
|
|
289
|
+
let handlers = this.#hookListeners.get(event)
|
|
290
|
+
if (!handlers) {
|
|
291
|
+
handlers = new Set()
|
|
292
|
+
this.#hookListeners.set(event, handlers)
|
|
293
|
+
}
|
|
294
|
+
handlers.add(handler)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
#createDefaultResolver(pluginName: string): Resolver {
|
|
298
|
+
const existingResolver = this.#defaultResolvers.get(pluginName)
|
|
299
|
+
if (existingResolver) {
|
|
300
|
+
return existingResolver
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const resolver = defineResolver<PluginFactoryOptions>((_ctx) => ({
|
|
304
|
+
name: 'default',
|
|
305
|
+
pluginName,
|
|
306
|
+
}))
|
|
307
|
+
this.#defaultResolvers.set(pluginName, resolver)
|
|
308
|
+
return resolver
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Merges `partial` with the plugin's default resolver and stores the result.
|
|
313
|
+
* Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
|
|
314
|
+
* get the up-to-date resolver without going through `getResolver()`.
|
|
315
|
+
*/
|
|
316
|
+
setPluginResolver(pluginName: string, partial: Partial<Resolver>): void {
|
|
317
|
+
const defaultResolver = this.#createDefaultResolver(pluginName)
|
|
318
|
+
const merged = { ...defaultResolver, ...partial }
|
|
319
|
+
this.#resolvers.set(pluginName, merged)
|
|
320
|
+
const plugin = this.plugins.get(pluginName)
|
|
321
|
+
if (plugin) {
|
|
322
|
+
plugin.resolver = merged
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Returns the resolver for the given plugin.
|
|
328
|
+
*
|
|
329
|
+
* Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
|
|
330
|
+
* plugin → lazily created default resolver (identity name, no path transforms).
|
|
331
|
+
*/
|
|
332
|
+
getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver']
|
|
333
|
+
getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver
|
|
334
|
+
getResolver(pluginName: string): Resolver {
|
|
335
|
+
return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown> {
|
|
339
|
+
const driver = this
|
|
340
|
+
|
|
341
|
+
const baseContext = {
|
|
342
|
+
config: driver.config,
|
|
343
|
+
get root(): string {
|
|
344
|
+
return resolve(driver.config.root, driver.config.output.path)
|
|
345
|
+
},
|
|
346
|
+
getMode(output: { path: string }): 'single' | 'split' {
|
|
347
|
+
return PluginDriver.getMode(resolve(driver.config.root, driver.config.output.path, output.path))
|
|
348
|
+
},
|
|
349
|
+
hooks: driver.hooks,
|
|
350
|
+
plugin,
|
|
351
|
+
getPlugin: driver.getPlugin.bind(driver),
|
|
352
|
+
requirePlugin: driver.requirePlugin.bind(driver),
|
|
353
|
+
getResolver: driver.getResolver.bind(driver),
|
|
354
|
+
driver,
|
|
355
|
+
addFile: async (...files: Array<FileNode>) => {
|
|
356
|
+
driver.fileManager.add(...files)
|
|
357
|
+
},
|
|
358
|
+
upsertFile: async (...files: Array<FileNode>) => {
|
|
359
|
+
driver.fileManager.upsert(...files)
|
|
360
|
+
},
|
|
361
|
+
get inputNode(): InputNode | undefined {
|
|
362
|
+
return driver.inputNode
|
|
363
|
+
},
|
|
364
|
+
get adapter(): Adapter | undefined {
|
|
365
|
+
return driver.adapter
|
|
366
|
+
},
|
|
367
|
+
get resolver() {
|
|
368
|
+
return driver.getResolver(plugin.name)
|
|
369
|
+
},
|
|
370
|
+
get transformer() {
|
|
371
|
+
return plugin.transformer
|
|
372
|
+
},
|
|
373
|
+
warn(message: string) {
|
|
374
|
+
driver.hooks.emit('kubb:warn', { message })
|
|
375
|
+
},
|
|
376
|
+
error(error: string | Error) {
|
|
377
|
+
driver.hooks.emit('kubb:error', { error: typeof error === 'string' ? new Error(error) : error })
|
|
378
|
+
},
|
|
379
|
+
info(message: string) {
|
|
380
|
+
driver.hooks.emit('kubb:info', { message })
|
|
381
|
+
},
|
|
382
|
+
openInStudio(options?: DevtoolsOptions) {
|
|
383
|
+
if (!driver.config.devtools || driver.#studioIsOpen) {
|
|
384
|
+
return
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (typeof driver.config.devtools !== 'object') {
|
|
388
|
+
throw new Error('Devtools must be an object')
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (!driver.inputNode || !driver.adapter) {
|
|
392
|
+
throw new Error('adapter is not defined, make sure you have set the parser in kubb.config.ts')
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
driver.#studioIsOpen = true
|
|
396
|
+
|
|
397
|
+
const studioUrl = driver.config.devtools?.studioUrl ?? DEFAULT_STUDIO_URL
|
|
398
|
+
|
|
399
|
+
return openInStudioFn(driver.inputNode, studioUrl, options)
|
|
400
|
+
},
|
|
401
|
+
} as unknown as GeneratorContext<TOptions>
|
|
402
|
+
|
|
403
|
+
return baseContext
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
|
|
407
|
+
getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined
|
|
408
|
+
getPlugin(pluginName: string): Plugin | undefined {
|
|
409
|
+
return this.plugins.get(pluginName)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Like `getPlugin` but throws a descriptive error when the plugin is not found.
|
|
414
|
+
*/
|
|
415
|
+
requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>
|
|
416
|
+
requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>
|
|
417
|
+
requirePlugin(pluginName: string): Plugin {
|
|
418
|
+
const plugin = this.plugins.get(pluginName)
|
|
419
|
+
if (!plugin) {
|
|
420
|
+
throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`)
|
|
421
|
+
}
|
|
422
|
+
return plugin
|
|
423
|
+
}
|
|
424
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { FileNode } from '@kubb/ast'
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Base URL for the Kubb Studio web app.
|
|
5
|
+
*/
|
|
3
6
|
export const DEFAULT_STUDIO_URL = 'https://studio.kubb.dev' as const
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export const DEFAULT_CONCURRENCY = 15
|
|
10
|
-
|
|
11
|
-
export const BARREL_FILENAME = 'index.ts' as const
|
|
8
|
+
/**
|
|
9
|
+
* Maximum number of files processed in parallel by FileProcessor.
|
|
10
|
+
*/
|
|
11
|
+
export const PARALLEL_CONCURRENCY_LIMIT = 100
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Default banner style written at the top of every generated file.
|
|
15
|
+
*/
|
|
13
16
|
export const DEFAULT_BANNER = 'simple' as const
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Default file-extension mapping used when no explicit mapping is configured.
|
|
20
|
+
*/
|
|
21
|
+
export const DEFAULT_EXTENSION: Record<FileNode['extname'], FileNode['extname'] | ''> = { '.ts': '.ts' }
|
|
18
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Numeric log-level thresholds used internally to compare verbosity.
|
|
25
|
+
*
|
|
26
|
+
* Higher numbers are more verbose.
|
|
27
|
+
*/
|
|
19
28
|
export const logLevel = {
|
|
20
29
|
silent: Number.NEGATIVE_INFINITY,
|
|
21
30
|
error: 0,
|
|
@@ -24,39 +33,3 @@ export const logLevel = {
|
|
|
24
33
|
verbose: 4,
|
|
25
34
|
debug: 5,
|
|
26
35
|
} as const
|
|
27
|
-
|
|
28
|
-
export const linters = {
|
|
29
|
-
eslint: {
|
|
30
|
-
command: 'eslint',
|
|
31
|
-
args: (outputPath: string) => [outputPath, '--fix'],
|
|
32
|
-
errorMessage: 'Eslint not found',
|
|
33
|
-
},
|
|
34
|
-
biome: {
|
|
35
|
-
command: 'biome',
|
|
36
|
-
args: (outputPath: string) => ['lint', '--fix', outputPath],
|
|
37
|
-
errorMessage: 'Biome not found',
|
|
38
|
-
},
|
|
39
|
-
oxlint: {
|
|
40
|
-
command: 'oxlint',
|
|
41
|
-
args: (outputPath: string) => ['--fix', outputPath],
|
|
42
|
-
errorMessage: 'Oxlint not found',
|
|
43
|
-
},
|
|
44
|
-
} as const
|
|
45
|
-
|
|
46
|
-
export const formatters = {
|
|
47
|
-
prettier: {
|
|
48
|
-
command: 'prettier',
|
|
49
|
-
args: (outputPath: string) => ['--ignore-unknown', '--write', outputPath],
|
|
50
|
-
errorMessage: 'Prettier not found',
|
|
51
|
-
},
|
|
52
|
-
biome: {
|
|
53
|
-
command: 'biome',
|
|
54
|
-
args: (outputPath: string) => ['format', '--write', outputPath],
|
|
55
|
-
errorMessage: 'Biome not found',
|
|
56
|
-
},
|
|
57
|
-
oxfmt: {
|
|
58
|
-
command: 'oxfmt',
|
|
59
|
-
args: (outputPath: string) => [outputPath],
|
|
60
|
-
errorMessage: 'Oxfmt not found',
|
|
61
|
-
},
|
|
62
|
-
} as const
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Adapter, AdapterFactoryOptions } from './types.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builder type for an {@link Adapter} — takes options and returns the adapter instance.
|
|
5
|
+
*/
|
|
6
|
+
type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* export const myAdapter = createAdapter<MyAdapter>((options) => {
|
|
13
|
+
* return {
|
|
14
|
+
* name: 'my-adapter',
|
|
15
|
+
* options,
|
|
16
|
+
* async parse(source) { ... },
|
|
17
|
+
* }
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* // instantiate
|
|
21
|
+
* const adapter = myAdapter({ validate: true })
|
|
22
|
+
*/
|
|
23
|
+
export function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T> {
|
|
24
|
+
return (options) => build(options ?? ({} as T['options']))
|
|
25
|
+
}
|