@kubb/core 5.0.0-alpha.5 → 5.0.0-alpha.50

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 (71) hide show
  1. package/README.md +3 -2
  2. package/dist/PluginDriver-DtwggkXg.cjs +1082 -0
  3. package/dist/PluginDriver-DtwggkXg.cjs.map +1 -0
  4. package/dist/PluginDriver-mXeqWp-U.js +979 -0
  5. package/dist/PluginDriver-mXeqWp-U.js.map +1 -0
  6. package/dist/index.cjs +1013 -1829
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +274 -265
  9. package/dist/index.js +1003 -1799
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +138 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +74 -0
  14. package/dist/mocks.js +133 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-DfEv9d_c.d.ts +1721 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +133 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +154 -101
  21. package/src/PluginDriver.ts +418 -0
  22. package/src/constants.ts +43 -47
  23. package/src/createAdapter.ts +25 -0
  24. package/src/createKubb.ts +614 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +58 -0
  27. package/src/defineGenerator.ts +88 -100
  28. package/src/defineLogger.ts +13 -3
  29. package/src/defineParser.ts +45 -0
  30. package/src/definePlugin.ts +68 -7
  31. package/src/defineResolver.ts +501 -0
  32. package/src/devtools.ts +14 -14
  33. package/src/index.ts +12 -17
  34. package/src/mocks.ts +171 -0
  35. package/src/renderNode.ts +35 -0
  36. package/src/storages/fsStorage.ts +40 -11
  37. package/src/storages/memoryStorage.ts +4 -3
  38. package/src/types.ts +575 -205
  39. package/src/utils/TreeNode.ts +47 -9
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/getBarrelFiles.ts +94 -16
  42. package/src/utils/isInputPath.ts +10 -0
  43. package/src/utils/packageJSON.ts +99 -0
  44. package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
  45. package/dist/chunk-ByKO4r7w.cjs +0 -38
  46. package/dist/hooks.cjs +0 -60
  47. package/dist/hooks.cjs.map +0 -1
  48. package/dist/hooks.d.ts +0 -56
  49. package/dist/hooks.js +0 -56
  50. package/dist/hooks.js.map +0 -1
  51. package/src/BarrelManager.ts +0 -74
  52. package/src/PackageManager.ts +0 -180
  53. package/src/PluginManager.ts +0 -667
  54. package/src/PromiseManager.ts +0 -40
  55. package/src/build.ts +0 -419
  56. package/src/config.ts +0 -56
  57. package/src/defineAdapter.ts +0 -22
  58. package/src/defineStorage.ts +0 -56
  59. package/src/errors.ts +0 -1
  60. package/src/hooks/index.ts +0 -4
  61. package/src/hooks/useKubb.ts +0 -46
  62. package/src/hooks/useMode.ts +0 -11
  63. package/src/hooks/usePlugin.ts +0 -11
  64. package/src/hooks/usePluginManager.ts +0 -11
  65. package/src/utils/FunctionParams.ts +0 -155
  66. package/src/utils/executeStrategies.ts +0 -81
  67. package/src/utils/formatters.ts +0 -56
  68. package/src/utils/getConfigs.ts +0 -30
  69. package/src/utils/getPlugins.ts +0 -23
  70. package/src/utils/linters.ts +0 -25
  71. package/src/utils/resolveOptions.ts +0 -93
@@ -0,0 +1,501 @@
1
+ import path from 'node:path'
2
+ import { camelCase, pascalCase } from '@internals/utils'
3
+ import type { FileNode, InputNode, Node, OperationNode, SchemaNode } from '@kubb/ast'
4
+ import { createFile, isOperationNode, isSchemaNode } from '@kubb/ast'
5
+ import { PluginDriver } from './PluginDriver.ts'
6
+ import type {
7
+ Config,
8
+ PluginFactoryOptions,
9
+ ResolveBannerContext,
10
+ ResolveOptionsContext,
11
+ Resolver,
12
+ ResolverContext,
13
+ ResolverFileParams,
14
+ ResolverPathParams,
15
+ } from './types.ts'
16
+
17
+ /**
18
+ * Builder type for the plugin-specific resolver fields.
19
+ *
20
+ * `default`, `resolveOptions`, `resolvePath`, `resolveFile`, `resolveBanner`, and `resolveFooter`
21
+ * are optional — built-in fallbacks are injected when omitted.
22
+ */
23
+ type ResolverBuilder<T extends PluginFactoryOptions> = () => Omit<
24
+ T['resolver'],
25
+ 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter' | 'name' | 'pluginName'
26
+ > &
27
+ Partial<Pick<T['resolver'], 'default' | 'resolveOptions' | 'resolvePath' | 'resolveFile' | 'resolveBanner' | 'resolveFooter'>> & {
28
+ name: string
29
+ pluginName: T['name']
30
+ } & ThisType<T['resolver']>
31
+
32
+ // String patterns are compiled lazily and cached — the same filter is reused for every node.
33
+ const stringPatternCache = new Map<string, RegExp>()
34
+
35
+ function testPattern(value: string, pattern: string | RegExp): boolean {
36
+ if (typeof pattern === 'string') {
37
+ let regex = stringPatternCache.get(pattern)
38
+ if (!regex) {
39
+ regex = new RegExp(pattern)
40
+ stringPatternCache.set(pattern, regex)
41
+ }
42
+ return regex.test(value)
43
+ }
44
+ // Use .match() for user-supplied RegExp to preserve semantics regardless of `g`/`y` flags.
45
+ return value.match(pattern) !== null
46
+ }
47
+
48
+ /**
49
+ * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
50
+ */
51
+ function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
52
+ switch (type) {
53
+ case 'tag':
54
+ return node.tags.some((tag) => testPattern(tag, pattern))
55
+ case 'operationId':
56
+ return testPattern(node.operationId, pattern)
57
+ case 'path':
58
+ return testPattern(node.path, pattern)
59
+ case 'method':
60
+ return testPattern(node.method.toLowerCase(), pattern)
61
+ case 'contentType':
62
+ return node.requestBody?.contentType ? testPattern(node.requestBody.contentType, pattern) : false
63
+ default:
64
+ return false
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Checks if a schema matches a pattern for a given filter type (`schemaName`).
70
+ *
71
+ * Returns `null` when the filter type doesn't apply to schemas.
72
+ */
73
+ function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
74
+ switch (type) {
75
+ case 'schemaName':
76
+ return node.name ? testPattern(node.name, pattern) : false
77
+ default:
78
+ return null
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Default name resolver used by `defineResolver`.
84
+ *
85
+ * - `camelCase` for `function` and `file` types.
86
+ * - `PascalCase` for `type`.
87
+ * - `camelCase` for everything else.
88
+ */
89
+ function defaultResolver(name: string, type?: 'file' | 'function' | 'type' | 'const'): string {
90
+ let resolvedName = camelCase(name)
91
+
92
+ if (type === 'file' || type === 'function') {
93
+ resolvedName = camelCase(name, {
94
+ isFile: type === 'file',
95
+ })
96
+ }
97
+
98
+ if (type === 'type') {
99
+ resolvedName = pascalCase(name)
100
+ }
101
+
102
+ return resolvedName
103
+ }
104
+
105
+ /**
106
+ * Default option resolver — applies include/exclude filters and merges matching override options.
107
+ *
108
+ * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
109
+ *
110
+ * @example Include/exclude filtering
111
+ * ```ts
112
+ * const options = defaultResolveOptions(operationNode, {
113
+ * options: { output: 'types' },
114
+ * exclude: [{ type: 'tag', pattern: 'internal' }],
115
+ * })
116
+ * // → null when node has tag 'internal'
117
+ * ```
118
+ *
119
+ * @example Override merging
120
+ * ```ts
121
+ * const options = defaultResolveOptions(operationNode, {
122
+ * options: { enumType: 'asConst' },
123
+ * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
124
+ * })
125
+ * // → { enumType: 'enum' } when operationId matches
126
+ * ```
127
+ */
128
+ export function defaultResolveOptions<TOptions>(
129
+ node: Node,
130
+ { options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>,
131
+ ): TOptions | null {
132
+ if (isOperationNode(node)) {
133
+ const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
134
+ if (isExcluded) {
135
+ return null
136
+ }
137
+
138
+ if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
139
+ return null
140
+ }
141
+
142
+ const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
143
+
144
+ return { ...options, ...overrideOptions }
145
+ }
146
+
147
+ if (isSchemaNode(node)) {
148
+ if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
149
+ return null
150
+ }
151
+
152
+ if (include) {
153
+ const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
154
+ const applicable = results.filter((r) => r !== null)
155
+ if (applicable.length > 0 && !applicable.includes(true)) {
156
+ return null
157
+ }
158
+ }
159
+
160
+ const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
161
+
162
+ return { ...options, ...overrideOptions }
163
+ }
164
+
165
+ return options
166
+ }
167
+
168
+ /**
169
+ * Default path resolver used by `defineResolver`.
170
+ *
171
+ * - Returns the output directory in `single` mode.
172
+ * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
173
+ * - Falls back to a flat `output/baseName` path otherwise.
174
+ *
175
+ * A custom `group.name` function overrides the default subdirectory naming.
176
+ * For `tag` groups the default is `${camelCase(tag)}Controller`.
177
+ * For `path` groups the default is the first path segment after `/`.
178
+ *
179
+ * @example Flat output
180
+ * ```ts
181
+ * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
182
+ * // → '/src/types/petTypes.ts'
183
+ * ```
184
+ *
185
+ * @example Tag-based grouping
186
+ * ```ts
187
+ * defaultResolvePath(
188
+ * { baseName: 'petTypes.ts', tag: 'pets' },
189
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
190
+ * )
191
+ * // → '/src/types/petsController/petTypes.ts'
192
+ * ```
193
+ *
194
+ * @example Path-based grouping
195
+ * ```ts
196
+ * defaultResolvePath(
197
+ * { baseName: 'petTypes.ts', path: '/pets/list' },
198
+ * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
199
+ * )
200
+ * // → '/src/types/pets/petTypes.ts'
201
+ * ```
202
+ *
203
+ * @example Single-file mode
204
+ * ```ts
205
+ * defaultResolvePath(
206
+ * { baseName: 'petTypes.ts', pathMode: 'single' },
207
+ * { root: '/src', output: { path: 'types' } },
208
+ * )
209
+ * // → '/src/types'
210
+ * ```
211
+ */
212
+ export function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }: ResolverPathParams, { root, output, group }: ResolverContext): string {
213
+ const mode = pathMode ?? PluginDriver.getMode(path.resolve(root, output.path))
214
+
215
+ if (mode === 'single') {
216
+ return path.resolve(root, output.path)
217
+ }
218
+
219
+ let result: string
220
+
221
+ if (group && (groupPath || tag)) {
222
+ const groupValue = group.type === 'path' ? groupPath! : tag!
223
+ const defaultName =
224
+ group.type === 'tag'
225
+ ? ({ group: g }: { group: string }) => `${camelCase(g)}Controller`
226
+ : ({ group: g }: { group: string }) => {
227
+ // Strip traversal components (empty, '.', '..') before taking the first meaningful segment.
228
+ // When every segment is a traversal component (e.g. '../../') we fall back to '' so the
229
+ // file is placed directly in the output root — the boundary check below ensures safety.
230
+ const segment = g.split('/').filter((s) => s !== '' && s !== '.' && s !== '..')[0]
231
+ return segment ? camelCase(segment) : ''
232
+ }
233
+ const resolveName = group.name ?? defaultName
234
+ result = path.resolve(root, output.path, resolveName({ group: groupValue }), baseName)
235
+ } else {
236
+ result = path.resolve(root, output.path, baseName)
237
+ }
238
+
239
+ // Ensure the resolved path stays within the configured output directory.
240
+ // This prevents path traversal from malicious OpenAPI specs or custom group.name functions.
241
+ // `result === outputDir` is intentionally permitted: it matches single-file mode paths and
242
+ // edge cases where baseName resolves to the output directory itself.
243
+ const outputDir = path.resolve(root, output.path)
244
+ const outputDirWithSep = outputDir.endsWith(path.sep) ? outputDir : `${outputDir}${path.sep}`
245
+ if (result !== outputDir && !result.startsWith(outputDirWithSep)) {
246
+ throw new Error(
247
+ `[Kubb] Resolved path "${result}" is outside the output directory "${outputDir}". ` +
248
+ 'This may indicate a path traversal attempt in the OpenAPI specification or a misconfigured group.name function.',
249
+ )
250
+ }
251
+
252
+ return result
253
+ }
254
+
255
+ /**
256
+ * Default file resolver used by `defineResolver`.
257
+ *
258
+ * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
259
+ * path resolution (`resolver.resolvePath`). The resolved file always has empty
260
+ * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
261
+ *
262
+ * In `single` mode the name is omitted and the file sits directly in the output directory.
263
+ *
264
+ * @example Resolve a schema file
265
+ * ```ts
266
+ * const file = defaultResolveFile.call(resolver,
267
+ * { name: 'pet', extname: '.ts' },
268
+ * { root: '/src', output: { path: 'types' } },
269
+ * )
270
+ * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
271
+ * ```
272
+ *
273
+ * @example Resolve an operation file with tag grouping
274
+ * ```ts
275
+ * const file = defaultResolveFile.call(resolver,
276
+ * { name: 'listPets', extname: '.ts', tag: 'pets' },
277
+ * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
278
+ * )
279
+ * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
280
+ * ```
281
+ */
282
+ export function defaultResolveFile(this: Resolver, { name, extname, tag, path: groupPath }: ResolverFileParams, context: ResolverContext): FileNode {
283
+ const pathMode = PluginDriver.getMode(path.resolve(context.root, context.output.path))
284
+ const resolvedName = pathMode === 'single' ? '' : this.default(name, 'file')
285
+ const baseName = `${resolvedName}${extname}` as FileNode['baseName']
286
+ const filePath = this.resolvePath({ baseName, pathMode, tag, path: groupPath }, context)
287
+
288
+ return createFile({
289
+ path: filePath,
290
+ baseName: path.basename(filePath) as `${string}.${string}`,
291
+ meta: {
292
+ pluginName: this.pluginName,
293
+ },
294
+ sources: [],
295
+ imports: [],
296
+ exports: [],
297
+ })
298
+ }
299
+
300
+ /**
301
+ * Generates the default "Generated by Kubb" banner from config and optional node metadata.
302
+ */
303
+ export function buildDefaultBanner({
304
+ title,
305
+ description,
306
+ version,
307
+ config,
308
+ }: {
309
+ title?: string
310
+ description?: string
311
+ version?: string
312
+ config: Config
313
+ }): string {
314
+ try {
315
+ let source = ''
316
+ if (Array.isArray(config.input)) {
317
+ const first = config.input[0]
318
+ if (first && 'path' in first) {
319
+ source = path.basename(first.path)
320
+ }
321
+ } else if ('path' in config.input) {
322
+ source = path.basename(config.input.path)
323
+ } else if ('data' in config.input) {
324
+ source = 'text content'
325
+ }
326
+
327
+ let banner = '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n'
328
+
329
+ if (config.output.defaultBanner === 'simple') {
330
+ banner += '*/\n'
331
+ return banner
332
+ }
333
+
334
+ if (source) {
335
+ banner += `* Source: ${source}\n`
336
+ }
337
+
338
+ if (title) {
339
+ banner += `* Title: ${title}\n`
340
+ }
341
+
342
+ if (description) {
343
+ const formattedDescription = description.replace(/\n/gm, '\n* ')
344
+ banner += `* Description: ${formattedDescription}\n`
345
+ }
346
+
347
+ if (version) {
348
+ banner += `* OpenAPI spec version: ${version}\n`
349
+ }
350
+
351
+ banner += '*/\n'
352
+ return banner
353
+ } catch (_error) {
354
+ return '/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/'
355
+ }
356
+ }
357
+
358
+ /**
359
+ * Default banner resolver — returns the banner string for a generated file.
360
+ *
361
+ * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
362
+ * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
363
+ * from the OAS spec when a `node` is provided).
364
+ *
365
+ * - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
366
+ * - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
367
+ * - When `output.banner` is a string, returns it directly.
368
+ * - When `config.output.defaultBanner` is `false`, returns `undefined`.
369
+ * - Otherwise returns the Kubb "Generated by Kubb" notice.
370
+ *
371
+ * @example String banner overrides default
372
+ * ```ts
373
+ * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
374
+ * // → '// my banner'
375
+ * ```
376
+ *
377
+ * @example Function banner with node
378
+ * ```ts
379
+ * defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
380
+ * // → '// v3.0.0'
381
+ * ```
382
+ *
383
+ * @example No user banner — Kubb notice with OAS metadata
384
+ * ```ts
385
+ * defaultResolveBanner(inputNode, { config })
386
+ * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
387
+ * ```
388
+ *
389
+ * @example Disabled default banner
390
+ * ```ts
391
+ * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
392
+ * // → undefined
393
+ * ```
394
+ */
395
+ export function defaultResolveBanner(node: InputNode | undefined, { output, config }: ResolveBannerContext): string | undefined {
396
+ if (typeof output?.banner === 'function') {
397
+ return output.banner(node)
398
+ }
399
+
400
+ if (typeof output?.banner === 'string') {
401
+ return output.banner
402
+ }
403
+
404
+ if (config.output.defaultBanner === false) {
405
+ return undefined
406
+ }
407
+
408
+ return buildDefaultBanner({
409
+ title: node?.meta?.title,
410
+ version: node?.meta?.version,
411
+ config,
412
+ })
413
+ }
414
+
415
+ /**
416
+ * Default footer resolver — returns the footer string for a generated file.
417
+ *
418
+ * - When `output.footer` is a function and `node` is provided, calls it with the node.
419
+ * - When `output.footer` is a function and `node` is absent, returns `undefined`.
420
+ * - When `output.footer` is a string, returns it directly.
421
+ * - Otherwise returns `undefined`.
422
+ *
423
+ * @example String footer
424
+ * ```ts
425
+ * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
426
+ * // → '// end of file'
427
+ * ```
428
+ *
429
+ * @example Function footer with node
430
+ * ```ts
431
+ * defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
432
+ * // → '// Pet Store'
433
+ * ```
434
+ */
435
+ export function defaultResolveFooter(node: InputNode | undefined, { output }: ResolveBannerContext): string | undefined {
436
+ if (typeof output?.footer === 'function') {
437
+ return node ? output.footer(node) : undefined
438
+ }
439
+ if (typeof output?.footer === 'string') {
440
+ return output.footer
441
+ }
442
+ return undefined
443
+ }
444
+
445
+ /**
446
+ * Defines a resolver for a plugin, injecting built-in defaults for name casing,
447
+ * include/exclude/override filtering, path resolution, and file construction.
448
+ *
449
+ * All four defaults can be overridden by providing them in the builder function:
450
+ * - `default` — name casing strategy (camelCase / PascalCase)
451
+ * - `resolveOptions` — include/exclude/override filtering
452
+ * - `resolvePath` — output path computation
453
+ * - `resolveFile` — full `FileNode` construction
454
+ *
455
+ * Methods in the builder have access to `this` (the full resolver object), so they
456
+ * can call other resolver methods without circular imports.
457
+ *
458
+ * @example Basic resolver with naming helpers
459
+ * ```ts
460
+ * export const resolver = defineResolver<PluginTs>(() => ({
461
+ * name: 'default',
462
+ * resolveName(node) {
463
+ * return this.default(node.name, 'function')
464
+ * },
465
+ * resolveTypedName(node) {
466
+ * return this.default(node.name, 'type')
467
+ * },
468
+ * }))
469
+ * ```
470
+ *
471
+ * @example Override resolvePath for a custom output structure
472
+ * ```ts
473
+ * export const resolver = defineResolver<PluginTs>(() => ({
474
+ * name: 'custom',
475
+ * resolvePath({ baseName }, { root, output }) {
476
+ * return path.resolve(root, output.path, 'generated', baseName)
477
+ * },
478
+ * }))
479
+ * ```
480
+ *
481
+ * @example Use this.default inside a helper
482
+ * ```ts
483
+ * export const resolver = defineResolver<PluginTs>(() => ({
484
+ * name: 'default',
485
+ * resolveParamName(node, param) {
486
+ * return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
487
+ * },
488
+ * }))
489
+ * ```
490
+ */
491
+ export function defineResolver<T extends PluginFactoryOptions>(build: ResolverBuilder<T>): T['resolver'] {
492
+ return {
493
+ default: defaultResolver,
494
+ resolveOptions: defaultResolveOptions,
495
+ resolvePath: defaultResolvePath,
496
+ resolveFile: defaultResolveFile,
497
+ resolveBanner: defaultResolveBanner,
498
+ resolveFooter: defaultResolveFooter,
499
+ ...build(),
500
+ } as T['resolver']
501
+ }
package/src/devtools.ts CHANGED
@@ -1,10 +1,10 @@
1
- import type { RootNode } from '@kubb/ast/types'
1
+ import type { InputNode } from '@kubb/ast'
2
2
  import { deflateSync, inflateSync } from 'fflate'
3
3
  import { x } from 'tinyexec'
4
4
  import type { DevtoolsOptions } from './types.ts'
5
5
 
6
6
  /**
7
- * Encodes a `RootNode` as a compressed, URL-safe string.
7
+ * Encodes an `InputNode` as a compressed, URL-safe string.
8
8
  *
9
9
  * The JSON representation is deflate-compressed with {@link deflateSync} before
10
10
  * base64url encoding, which typically reduces payload size by 70–80 % and
@@ -12,41 +12,41 @@ import type { DevtoolsOptions } from './types.ts'
12
12
  *
13
13
  * Use {@link decodeAst} to reverse.
14
14
  */
15
- export function encodeAst(root: RootNode): string {
16
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(root)))
15
+ export function encodeAst(input: InputNode): string {
16
+ const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)))
17
17
  return Buffer.from(compressed).toString('base64url')
18
18
  }
19
19
 
20
20
  /**
21
- * Decodes a `RootNode` from a string produced by {@link encodeAst}.
21
+ * Decodes an `InputNode` from a string produced by {@link encodeAst}.
22
22
  *
23
23
  * Works in both Node.js and the browser — no streaming APIs required.
24
24
  */
25
- export function decodeAst(encoded: string): RootNode {
25
+ export function decodeAst(encoded: string): InputNode {
26
26
  const bytes = Buffer.from(encoded, 'base64url')
27
- return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as RootNode
27
+ return JSON.parse(new TextDecoder().decode(inflateSync(bytes))) as InputNode
28
28
  }
29
29
 
30
30
  /**
31
- * Constructs the Kubb Studio URL for the given `RootNode`.
31
+ * Constructs the Kubb Studio URL for the given `InputNode`.
32
32
  * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
33
- * The `root` is encoded and attached as the `?root=` query parameter so Studio
33
+ * The `input` is encoded and attached as the `?root=` query parameter so Studio
34
34
  * can decode and render it without a round-trip to any server.
35
35
  */
36
- export function getStudioUrl(root: RootNode, studioUrl: string, options: DevtoolsOptions = {}): string {
36
+ export function getStudioUrl(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): string {
37
37
  const baseUrl = studioUrl.replace(/\/$/, '')
38
38
  const path = options.ast ? '/ast' : ''
39
39
 
40
- return `${baseUrl}${path}?root=${encodeAst(root)}`
40
+ return `${baseUrl}${path}?root=${encodeAst(input)}`
41
41
  }
42
42
 
43
43
  /**
44
- * Opens the Kubb Studio URL for the given `RootNode` in the default browser —
44
+ * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
45
45
  *
46
46
  * Falls back to printing the URL if the browser cannot be launched.
47
47
  */
48
- export async function openInStudio(root: RootNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
49
- const url = getStudioUrl(root, studioUrl, options)
48
+ export async function openInStudio(input: InputNode, studioUrl: string, options: DevtoolsOptions = {}): Promise<void> {
49
+ const url = getStudioUrl(input, studioUrl, options)
50
50
 
51
51
  const cmd = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'xdg-open'
52
52
  const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url]
package/src/index.ts CHANGED
@@ -1,24 +1,19 @@
1
1
  export { AsyncEventEmitter, URLPath } from '@internals/utils'
2
- export { definePrinter } from '@kubb/ast'
3
- export { build, build as default, safeBuild, setup } from './build.ts'
4
- export { type CLIOptions, type ConfigInput, defineConfig, isInputPath } from './config.ts'
5
- export { formatters, linters, logLevel } from './constants.ts'
6
- export { defineAdapter } from './defineAdapter.ts'
2
+ export * as ast from '@kubb/ast'
3
+ export { logLevel } from './constants.ts'
4
+ export { createAdapter } from './createAdapter.ts'
5
+ export { createKubb } from './createKubb.ts'
6
+ export { createRenderer } from './createRenderer.ts'
7
+ export { createStorage } from './createStorage.ts'
7
8
  export { defineGenerator } from './defineGenerator.ts'
8
9
  export { defineLogger } from './defineLogger.ts'
10
+ export { defineParser } from './defineParser.ts'
9
11
  export { definePlugin } from './definePlugin.ts'
10
- export { defineStorage } from './defineStorage.ts'
11
- export { PackageManager } from './PackageManager.ts'
12
- export { getMode, PluginManager } from './PluginManager.ts'
13
- export { PromiseManager } from './PromiseManager.ts'
12
+ export { defineResolver } from './defineResolver.ts'
13
+ export { FileManager } from './FileManager.ts'
14
+ export { FileProcessor } from './FileProcessor.ts'
15
+ export { PluginDriver } from './PluginDriver.ts'
14
16
  export { fsStorage } from './storages/fsStorage.ts'
15
17
  export { memoryStorage } from './storages/memoryStorage.ts'
16
18
  export * from './types.ts'
17
- export type { FunctionParamsAST } from './utils/FunctionParams.ts'
18
- export { FunctionParams } from './utils/FunctionParams.ts'
19
- export { detectFormatter } from './utils/formatters.ts'
20
- export type { FileMetaBase } from './utils/getBarrelFiles.ts'
21
- export { getBarrelFiles } from './utils/getBarrelFiles.ts'
22
- export { getConfigs } from './utils/getConfigs.ts'
23
- export { detectLinter } from './utils/linters.ts'
24
- export { resolveOptions } from './utils/resolveOptions.ts'
19
+ export { isInputPath } from './utils/isInputPath.ts'