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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +25 -158
  3. package/dist/diagnostics-DiaUv_iK.d.ts +2904 -0
  4. package/dist/index.cjs +2523 -1071
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -273
  7. package/dist/index.js +2513 -1067
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-CUj1hrxa.cjs +823 -0
  10. package/dist/memoryStorage-CUj1hrxa.cjs.map +1 -0
  11. package/dist/memoryStorage-CWFzAz4o.js +714 -0
  12. package/dist/memoryStorage-CWFzAz4o.js.map +1 -0
  13. package/dist/mocks.cjs +83 -23
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +36 -10
  16. package/dist/mocks.js +85 -27
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +8 -28
  19. package/src/FileManager.ts +86 -64
  20. package/src/FileProcessor.ts +170 -44
  21. package/src/KubbDriver.ts +909 -0
  22. package/src/Transform.ts +105 -0
  23. package/src/constants.ts +111 -20
  24. package/src/createAdapter.ts +112 -17
  25. package/src/createKubb.ts +140 -517
  26. package/src/createRenderer.ts +43 -28
  27. package/src/createReporter.ts +134 -0
  28. package/src/createStorage.ts +36 -23
  29. package/src/defineGenerator.ts +140 -17
  30. package/src/defineParser.ts +30 -12
  31. package/src/definePlugin.ts +375 -21
  32. package/src/defineResolver.ts +402 -212
  33. package/src/diagnostics.ts +662 -0
  34. package/src/index.ts +8 -8
  35. package/src/mocks.ts +97 -26
  36. package/src/reporters/cliReporter.ts +89 -0
  37. package/src/reporters/fileReporter.ts +103 -0
  38. package/src/reporters/jsonReporter.ts +20 -0
  39. package/src/reporters/report.ts +85 -0
  40. package/src/storages/fsStorage.ts +23 -55
  41. package/src/types.ts +411 -887
  42. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  43. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  44. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  45. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  46. package/dist/types-DVPKmzw_.d.ts +0 -2159
  47. package/src/Kubb.ts +0 -300
  48. package/src/PluginDriver.ts +0 -426
  49. package/src/defineLogger.ts +0 -19
  50. package/src/defineMiddleware.ts +0 -62
  51. package/src/devtools.ts +0 -59
  52. package/src/renderNode.ts +0 -35
  53. package/src/utils/diagnostics.ts +0 -18
  54. package/src/utils/isInputPath.ts +0 -10
  55. package/src/utils/packageJSON.ts +0 -99
  56. /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
@@ -1,426 +0,0 @@
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
- if (!hooks) return
142
-
143
- // kubb:plugin:setup gets special treatment: the globally emitted context is wrapped with
144
- // plugin-specific implementations so that addGenerator / setResolver / etc. target
145
- // this plugin's normalizedPlugin entry rather than being no-ops.
146
- if (hooks['kubb:plugin:setup']) {
147
- const setupHandler = (globalCtx: KubbPluginSetupContext) => {
148
- const pluginCtx: KubbPluginSetupContext = {
149
- ...globalCtx,
150
- options: hookPlugin.options ?? {},
151
- addGenerator: (gen) => {
152
- this.registerGenerator(normalizedPlugin.name, gen)
153
- },
154
- setResolver: (resolver) => {
155
- this.setPluginResolver(normalizedPlugin.name, resolver)
156
- },
157
- setTransformer: (visitor) => {
158
- normalizedPlugin.transformer = visitor
159
- },
160
- setRenderer: (renderer) => {
161
- normalizedPlugin.renderer = renderer
162
- },
163
- setOptions: (opts) => {
164
- normalizedPlugin.options = { ...normalizedPlugin.options, ...opts }
165
- },
166
- injectFile: (userFileNode) => {
167
- this.fileManager.add(createFile(userFileNode))
168
- },
169
- }
170
- return hooks['kubb:plugin:setup']!(pluginCtx)
171
- }
172
-
173
- this.hooks.on('kubb:plugin:setup', setupHandler)
174
- this.#trackHookListener('kubb:plugin:setup', setupHandler as (...args: never[]) => void | Promise<void>)
175
- }
176
-
177
- // All other hooks are registered as direct pass-through listeners on the shared emitter.
178
- for (const [event, handler] of Object.entries(hooks) as Array<[keyof KubbHooks, ((...args: never[]) => void | Promise<void>) | undefined]>) {
179
- if (event === 'kubb:plugin:setup' || !handler) continue
180
-
181
- this.hooks.on(event, handler as never)
182
- this.#trackHookListener(event, handler as (...args: never[]) => void | Promise<void>)
183
- }
184
- }
185
-
186
- /**
187
- * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
188
- * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
189
- *
190
- * Call this once from `safeBuild` before the plugin execution loop begins.
191
- */
192
- async emitSetupHooks(): Promise<void> {
193
- const noop = () => {}
194
- await this.hooks.emit('kubb:plugin:setup', {
195
- config: this.config,
196
- options: {},
197
- addGenerator: noop,
198
- setResolver: noop,
199
- setTransformer: noop,
200
- setRenderer: noop,
201
- setOptions: noop,
202
- injectFile: noop,
203
- updateConfig: noop,
204
- })
205
- }
206
-
207
- /**
208
- * Registers a generator for the given plugin on the shared event emitter.
209
- *
210
- * The generator's `schema`, `operation`, and `operations` methods are registered as
211
- * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
212
- * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
213
- * so that generators from different plugins do not cross-fire.
214
- *
215
- * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
216
- * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
217
- * declares a renderer.
218
- *
219
- * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
220
- */
221
- registerGenerator(pluginName: string, gen: Generator): void {
222
- const resolveRenderer = () => {
223
- const plugin = this.plugins.get(pluginName)
224
- return gen.renderer === null ? undefined : (gen.renderer ?? plugin?.renderer ?? this.config.renderer)
225
- }
226
-
227
- if (gen.schema) {
228
- const schemaHandler = async (node: SchemaNode, ctx: GeneratorContext) => {
229
- if (ctx.plugin.name !== pluginName) return
230
- const result = await gen.schema!(node, ctx)
231
- await applyHookResult(result, this, resolveRenderer())
232
- }
233
-
234
- this.hooks.on('kubb:generate:schema', schemaHandler)
235
- this.#trackHookListener('kubb:generate:schema', schemaHandler as (...args: never[]) => void | Promise<void>)
236
- }
237
-
238
- if (gen.operation) {
239
- const operationHandler = async (node: OperationNode, ctx: GeneratorContext) => {
240
- if (ctx.plugin.name !== pluginName) return
241
- const result = await gen.operation!(node, ctx)
242
- await applyHookResult(result, this, resolveRenderer())
243
- }
244
-
245
- this.hooks.on('kubb:generate:operation', operationHandler)
246
- this.#trackHookListener('kubb:generate:operation', operationHandler as (...args: never[]) => void | Promise<void>)
247
- }
248
-
249
- if (gen.operations) {
250
- const operationsHandler = async (nodes: Array<OperationNode>, ctx: GeneratorContext) => {
251
- if (ctx.plugin.name !== pluginName) return
252
- const result = await gen.operations!(nodes, ctx)
253
- await applyHookResult(result, this, resolveRenderer())
254
- }
255
-
256
- this.hooks.on('kubb:generate:operations', operationsHandler)
257
- this.#trackHookListener('kubb:generate:operations', operationsHandler as (...args: never[]) => void | Promise<void>)
258
- }
259
-
260
- this.#pluginsWithEventGenerators.add(pluginName)
261
- }
262
-
263
- /**
264
- * Returns `true` when at least one generator was registered for the given plugin
265
- * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
266
- *
267
- * Used by the build loop to decide whether to walk the AST and emit generator events
268
- * for a plugin that has no static `plugin.generators`.
269
- */
270
- hasRegisteredGenerators(pluginName: string): boolean {
271
- return this.#pluginsWithEventGenerators.has(pluginName)
272
- }
273
-
274
- /**
275
- * Unregisters all plugin lifecycle listeners from the shared event emitter.
276
- * Called at the end of a build to prevent listener leaks across repeated builds.
277
- *
278
- * @internal
279
- */
280
- dispose(): void {
281
- for (const [event, handlers] of this.#hookListeners) {
282
- for (const handler of handlers) {
283
- this.hooks.off(event, handler as never)
284
- }
285
- }
286
- this.#hookListeners.clear()
287
- this.#pluginsWithEventGenerators.clear()
288
- }
289
-
290
- #trackHookListener(event: keyof KubbHooks, handler: (...args: never[]) => void | Promise<void>): void {
291
- let handlers = this.#hookListeners.get(event)
292
- if (!handlers) {
293
- handlers = new Set()
294
- this.#hookListeners.set(event, handlers)
295
- }
296
- handlers.add(handler)
297
- }
298
-
299
- #createDefaultResolver(pluginName: string): Resolver {
300
- const existingResolver = this.#defaultResolvers.get(pluginName)
301
- if (existingResolver) {
302
- return existingResolver
303
- }
304
-
305
- const resolver = defineResolver<PluginFactoryOptions>((_ctx) => ({
306
- name: 'default',
307
- pluginName,
308
- }))
309
- this.#defaultResolvers.set(pluginName, resolver)
310
- return resolver
311
- }
312
-
313
- /**
314
- * Merges `partial` with the plugin's default resolver and stores the result.
315
- * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
316
- * get the up-to-date resolver without going through `getResolver()`.
317
- */
318
- setPluginResolver(pluginName: string, partial: Partial<Resolver>): void {
319
- const defaultResolver = this.#createDefaultResolver(pluginName)
320
- const merged = { ...defaultResolver, ...partial }
321
- this.#resolvers.set(pluginName, merged)
322
- const plugin = this.plugins.get(pluginName)
323
- if (plugin) {
324
- plugin.resolver = merged
325
- }
326
- }
327
-
328
- /**
329
- * Returns the resolver for the given plugin.
330
- *
331
- * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
332
- * plugin → lazily created default resolver (identity name, no path transforms).
333
- */
334
- getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver']
335
- getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver
336
- getResolver(pluginName: string): Resolver {
337
- return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName)
338
- }
339
-
340
- getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown> {
341
- const driver = this
342
-
343
- const baseContext = {
344
- config: driver.config,
345
- get root(): string {
346
- return resolve(driver.config.root, driver.config.output.path)
347
- },
348
- getMode(output: { path: string }): 'single' | 'split' {
349
- return PluginDriver.getMode(resolve(driver.config.root, driver.config.output.path, output.path))
350
- },
351
- hooks: driver.hooks,
352
- plugin,
353
- getPlugin: driver.getPlugin.bind(driver),
354
- requirePlugin: driver.requirePlugin.bind(driver),
355
- getResolver: driver.getResolver.bind(driver),
356
- driver,
357
- addFile: async (...files: Array<FileNode>) => {
358
- driver.fileManager.add(...files)
359
- },
360
- upsertFile: async (...files: Array<FileNode>) => {
361
- driver.fileManager.upsert(...files)
362
- },
363
- get inputNode(): InputNode | undefined {
364
- return driver.inputNode
365
- },
366
- get adapter(): Adapter | undefined {
367
- return driver.adapter
368
- },
369
- get resolver() {
370
- return driver.getResolver(plugin.name)
371
- },
372
- get transformer() {
373
- return plugin.transformer
374
- },
375
- warn(message: string) {
376
- driver.hooks.emit('kubb:warn', { message })
377
- },
378
- error(error: string | Error) {
379
- driver.hooks.emit('kubb:error', { error: typeof error === 'string' ? new Error(error) : error })
380
- },
381
- info(message: string) {
382
- driver.hooks.emit('kubb:info', { message })
383
- },
384
- openInStudio(options?: DevtoolsOptions) {
385
- if (!driver.config.devtools || driver.#studioIsOpen) {
386
- return
387
- }
388
-
389
- if (typeof driver.config.devtools !== 'object') {
390
- throw new Error('Devtools must be an object')
391
- }
392
-
393
- if (!driver.inputNode || !driver.adapter) {
394
- throw new Error('adapter is not defined, make sure you have set the parser in kubb.config.ts')
395
- }
396
-
397
- driver.#studioIsOpen = true
398
-
399
- const studioUrl = driver.config.devtools?.studioUrl ?? DEFAULT_STUDIO_URL
400
-
401
- return openInStudioFn(driver.inputNode, studioUrl, options)
402
- },
403
- } as unknown as GeneratorContext<TOptions>
404
-
405
- return baseContext
406
- }
407
-
408
- getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
409
- getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined
410
- getPlugin(pluginName: string): Plugin | undefined {
411
- return this.plugins.get(pluginName)
412
- }
413
-
414
- /**
415
- * Like `getPlugin` but throws a descriptive error when the plugin is not found.
416
- */
417
- requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>
418
- requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>
419
- requirePlugin(pluginName: string): Plugin {
420
- const plugin = this.plugins.get(pluginName)
421
- if (!plugin) {
422
- throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`)
423
- }
424
- return plugin
425
- }
426
- }
@@ -1,19 +0,0 @@
1
- import type { Logger, LoggerOptions, UserLogger } from './types.ts'
2
-
3
- /**
4
- * Wraps a logger definition into a typed {@link Logger}.
5
- *
6
- * @example
7
- * ```ts
8
- * export const myLogger = defineLogger({
9
- * name: 'my-logger',
10
- * install(context, options) {
11
- * context.on('kubb:info', (message) => console.log('ℹ', message))
12
- * context.on('kubb:error', (error) => console.error('✗', error.message))
13
- * },
14
- * })
15
- * ```
16
- */
17
- export function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options> {
18
- return logger
19
- }
@@ -1,62 +0,0 @@
1
- import type { KubbHooks } from './Kubb.ts'
2
-
3
- /**
4
- * A middleware instance produced by calling a factory created with `defineMiddleware`.
5
- * It declares event handlers under a `hooks` object which are registered on the
6
- * shared emitter after all plugin hooks, so middleware handlers for any event
7
- * always fire last.
8
- */
9
- export type Middleware = {
10
- /**
11
- * Unique identifier for this middleware.
12
- */
13
- name: string
14
- /**
15
- * Lifecycle event handlers for this middleware.
16
- * Any event from the global `KubbHooks` map can be subscribed to here.
17
- * Handlers are registered after all plugin handlers, so they always fire last.
18
- */
19
- hooks: {
20
- [K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void>
21
- }
22
- }
23
-
24
- /**
25
- * Creates a middleware factory using the hook-style `hooks` API.
26
- *
27
- * Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
28
- * Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
29
- *
30
- * @note The factory can accept typed options. See examples for using options and per-build state patterns.
31
- *
32
- * @example
33
- * ```ts
34
- * import { defineMiddleware } from '@kubb/core'
35
- *
36
- * // Stateless middleware
37
- * export const logMiddleware = defineMiddleware(() => ({
38
- * name: 'log-middleware',
39
- * hooks: {
40
- * 'kubb:build:end'({ files }) {
41
- * console.log(`Build complete with ${files.length} files`)
42
- * },
43
- * },
44
- * }))
45
- *
46
- * // Middleware with options and per-build state
47
- * export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
48
- * const seen = new Set<string>()
49
- * return {
50
- * name: 'prefix-middleware',
51
- * hooks: {
52
- * 'kubb:plugin:end'({ plugin }) {
53
- * seen.add(`${options.prefix}${plugin.name}`)
54
- * },
55
- * },
56
- * }
57
- * })
58
- * ```
59
- */
60
- export function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware {
61
- return (options) => factory(options ?? ({} as TOptions))
62
- }
package/src/devtools.ts DELETED
@@ -1,59 +0,0 @@
1
- import type { InputNode } from '@kubb/ast'
2
- import { deflateSync, inflateSync } from 'fflate'
3
- import { x } from 'tinyexec'
4
- import type { DevtoolsOptions } from './types.ts'
5
-
6
- /**
7
- * Encodes an `InputNode` as a compressed, URL-safe string.
8
- *
9
- * The JSON representation is deflate-compressed with {@link deflateSync} before
10
- * base64url encoding, which typically reduces payload size by 70–80 % and
11
- * keeps URLs well within browser and server path-length limits.
12
- *
13
- * Use {@link decodeAst} to reverse.
14
- */
15
- export function encodeAst(input: InputNode): string {
16
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)))
17
- return Buffer.from(compressed).toString('base64url')
18
- }
19
-
20
- /**
21
- * Decodes an `InputNode` from a string produced by {@link encodeAst}.
22
- *
23
- * Works in both Node.js and the browser — no streaming APIs required.
24
- */
25
- export function decodeAst(encoded: string): InputNode {
26
- const bytes = Buffer.from(encoded, 'base64url')
27
- return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as InputNode
28
- }
29
-
30
- /**
31
- * Constructs the Kubb Studio URL for the given `InputNode`.
32
- * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
33
- * The `input` is encoded and attached as the `?root=` query parameter so Studio
34
- * can decode and render it without a round-trip to any server.
35
- */
36
- export function getStudioUrl(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): string {
37
- const baseUrl = studioUrl.replace(/\/$/, '')
38
- const path = options.ast ? '/ast' : ''
39
-
40
- return `${baseUrl}${path}?root=${encodeAst(input)}`
41
- }
42
-
43
- /**
44
- * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
45
- *
46
- * Falls back to printing the URL if the browser cannot be launched.
47
- */
48
- export async function openInStudio(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
49
- const url = getStudioUrl(input, studioUrl, options)
50
-
51
- const cmd = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open'
52
- const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]
53
-
54
- try {
55
- await x(cmd, args)
56
- } catch {
57
- console.log(`\n ${url}\n`)
58
- }
59
- }
package/src/renderNode.ts DELETED
@@ -1,35 +0,0 @@
1
- import type { FileNode } from '@kubb/ast'
2
- import type { RendererFactory } from './createRenderer.ts'
3
- import type { PluginDriver } from './PluginDriver.ts'
4
-
5
- /**
6
- * Handles the return value of a plugin AST hook or generator method.
7
- *
8
- * - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
9
- * - `Array<FileNode>` → added directly into `driver.fileManager`
10
- * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
11
- *
12
- * Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
13
- * may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
14
- */
15
- export async function applyHookResult<TElement = unknown>(
16
- result: TElement | Array<FileNode> | void,
17
- driver: PluginDriver,
18
- rendererFactory?: RendererFactory<TElement>,
19
- ): Promise<void> {
20
- if (!result) return
21
-
22
- if (Array.isArray(result)) {
23
- driver.fileManager.upsert(...(result as Array<FileNode>))
24
- return
25
- }
26
-
27
- if (!rendererFactory) {
28
- return
29
- }
30
-
31
- const renderer = rendererFactory()
32
- await renderer.render(result)
33
- driver.fileManager.upsert(...renderer.files)
34
- renderer.unmount()
35
- }
@@ -1,18 +0,0 @@
1
- import { version as nodeVersion } from 'node:process'
2
- import { version as KubbVersion } from '../../package.json'
3
-
4
- /**
5
- * Returns a snapshot of the current runtime environment.
6
- *
7
- * Useful for attaching context to debug logs and error reports so that
8
- * issues can be reproduced without manual information gathering.
9
- */
10
- export function getDiagnosticInfo() {
11
- return {
12
- nodeVersion,
13
- KubbVersion,
14
- platform: process.platform,
15
- arch: process.arch,
16
- cwd: process.cwd(),
17
- } as const
18
- }
@@ -1,10 +0,0 @@
1
- import type { Config, InputPath, UserConfig } from '../types'
2
-
3
- /**
4
- * Type guard to check if a given config has an `input.path`.
5
- */
6
- export function isInputPath(config: UserConfig | undefined): config is UserConfig<InputPath> & { input: InputPath }
7
- export function isInputPath(config: Config | undefined): config is Config<InputPath> & { input: InputPath }
8
- export function isInputPath(config: Config | UserConfig | undefined): config is (Config<InputPath> | UserConfig<InputPath>) & { input: InputPath } {
9
- return typeof config?.input === 'object' && config.input !== null && 'path' in config.input
10
- }