@kubb/core 5.0.0-beta.7 → 5.0.0-beta.70

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 (49) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +20 -123
  3. package/dist/diagnostics-CJtO1uSM.d.ts +2892 -0
  4. package/dist/index.cjs +2340 -1129
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -289
  7. package/dist/index.js +2330 -1124
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-B4VTTIpQ.js +905 -0
  10. package/dist/memoryStorage-B4VTTIpQ.js.map +1 -0
  11. package/dist/memoryStorage-CfycFGzX.cjs +1043 -0
  12. package/dist/memoryStorage-CfycFGzX.cjs.map +1 -0
  13. package/dist/mocks.cjs +84 -24
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +37 -11
  16. package/dist/mocks.js +86 -28
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +9 -23
  19. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  20. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  21. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  22. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  23. package/dist/types-ChyWgIgi.d.ts +0 -2159
  24. package/src/FileManager.ts +0 -115
  25. package/src/FileProcessor.ts +0 -86
  26. package/src/Kubb.ts +0 -300
  27. package/src/PluginDriver.ts +0 -426
  28. package/src/constants.ts +0 -35
  29. package/src/createAdapter.ts +0 -32
  30. package/src/createKubb.ts +0 -573
  31. package/src/createRenderer.ts +0 -57
  32. package/src/createStorage.ts +0 -70
  33. package/src/defineGenerator.ts +0 -87
  34. package/src/defineLogger.ts +0 -36
  35. package/src/defineMiddleware.ts +0 -62
  36. package/src/defineParser.ts +0 -44
  37. package/src/definePlugin.ts +0 -83
  38. package/src/defineResolver.ts +0 -521
  39. package/src/devtools.ts +0 -59
  40. package/src/index.ts +0 -20
  41. package/src/mocks.ts +0 -178
  42. package/src/renderNode.ts +0 -35
  43. package/src/storages/fsStorage.ts +0 -114
  44. package/src/storages/memoryStorage.ts +0 -55
  45. package/src/types.ts +0 -1305
  46. package/src/utils/diagnostics.ts +0 -18
  47. package/src/utils/isInputPath.ts +0 -10
  48. package/src/utils/packageJSON.ts +0 -99
  49. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,115 +0,0 @@
1
- import type { FileNode } from '@kubb/ast'
2
- import { createFile } from '@kubb/ast'
3
-
4
- function mergeFile<TMeta extends object = object>(a: FileNode<TMeta>, b: FileNode<TMeta>): FileNode<TMeta> {
5
- return {
6
- ...a,
7
- // Incoming file (b) takes precedence for banner/footer so that barrel files,
8
- // which never carry a banner, can clear banners set by plugin-generated files
9
- // at the same path.
10
- banner: b.banner,
11
- footer: b.footer,
12
- sources: [...(a.sources || []), ...(b.sources || [])],
13
- imports: [...(a.imports || []), ...(b.imports || [])],
14
- exports: [...(a.exports || []), ...(b.exports || [])],
15
- }
16
- }
17
-
18
- /**
19
- * Collapses a list of files so that duplicates sharing the same `path` are merged
20
- * in arrival order. Keeps the original order of first occurrence.
21
- */
22
- function mergeFilesByPath(files: ReadonlyArray<FileNode>): Map<string, FileNode> {
23
- const merged = new Map<string, FileNode>()
24
- for (const file of files) {
25
- const existing = merged.get(file.path)
26
- merged.set(file.path, existing ? mergeFile(existing, file) : file)
27
- }
28
- return merged
29
- }
30
-
31
- /**
32
- * In-memory file store for generated files.
33
- *
34
- * Files with the same `path` are merged — sources, imports, and exports are concatenated.
35
- * The `files` getter returns all stored files sorted by path length (shortest first).
36
- *
37
- * @example
38
- * ```ts
39
- * import { FileManager } from '@kubb/core'
40
- *
41
- * const manager = new FileManager()
42
- * manager.upsert(myFile)
43
- * console.log(manager.files) // all stored files
44
- * ```
45
- */
46
- export class FileManager {
47
- readonly #cache = new Map<string, FileNode>()
48
- #filesCache: Array<FileNode> | null = null
49
-
50
- /**
51
- * Adds one or more files. Incoming files with the same path are merged
52
- * (sources/imports/exports concatenated), but existing cache entries are
53
- * replaced — use {@link upsert} when you want to merge into the cache too.
54
- */
55
- add(...files: Array<FileNode>): Array<FileNode> {
56
- return this.#store(files, false)
57
- }
58
-
59
- /**
60
- * Adds or merges one or more files.
61
- * If a file with the same path already exists in the cache, its
62
- * sources/imports/exports are merged into the incoming file.
63
- */
64
- upsert(...files: Array<FileNode>): Array<FileNode> {
65
- return this.#store(files, true)
66
- }
67
-
68
- #store(files: ReadonlyArray<FileNode>, mergeExisting: boolean): Array<FileNode> {
69
- const resolvedFiles: Array<FileNode> = []
70
- for (const file of mergeFilesByPath(files).values()) {
71
- const existing = mergeExisting ? this.#cache.get(file.path) : undefined
72
- const resolvedFile = createFile(existing ? mergeFile(existing, file) : file)
73
- this.#cache.set(resolvedFile.path, resolvedFile)
74
- resolvedFiles.push(resolvedFile)
75
- }
76
- this.#filesCache = null
77
- return resolvedFiles
78
- }
79
-
80
- getByPath(path: string): FileNode | null {
81
- return this.#cache.get(path) ?? null
82
- }
83
-
84
- deleteByPath(path: string): void {
85
- this.#cache.delete(path)
86
- this.#filesCache = null
87
- }
88
-
89
- clear(): void {
90
- this.#cache.clear()
91
- this.#filesCache = null
92
- }
93
-
94
- /**
95
- * All stored files, sorted by path length (shorter paths first).
96
- */
97
- get files(): Array<FileNode> {
98
- if (this.#filesCache) {
99
- return this.#filesCache
100
- }
101
-
102
- this.#filesCache = [...this.#cache.values()].sort((a, b) => {
103
- const lenDiff = a.path.length - b.path.length
104
- if (lenDiff !== 0) return lenDiff
105
- // Within the same length bucket, index.ts barrel files go last so other
106
- // files are always processed before their barrel file.
107
- const aIsIndex = a.path.endsWith('/index.ts') || a.path === 'index.ts'
108
- const bIsIndex = b.path.endsWith('/index.ts') || b.path === 'index.ts'
109
- if (aIsIndex && !bIsIndex) return 1
110
- if (!aIsIndex && bIsIndex) return -1
111
- return 0
112
- })
113
- return this.#filesCache
114
- }
115
- }
@@ -1,86 +0,0 @@
1
- import type { CodeNode, FileNode } from '@kubb/ast'
2
- import { extractStringsFromNodes } from '@kubb/ast'
3
- import pLimit from 'p-limit'
4
- import { PARALLEL_CONCURRENCY_LIMIT } from './constants.ts'
5
- import type { Parser } from './defineParser.ts'
6
-
7
- type ParseOptions = {
8
- parsers?: Map<FileNode['extname'], Parser>
9
- extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
10
- }
11
-
12
- type RunOptions = ParseOptions & {
13
- /**
14
- * @default 'sequential'
15
- */
16
- mode?: 'sequential' | 'parallel'
17
- onStart?: (files: Array<FileNode>) => Promise<void> | void
18
- onEnd?: (files: Array<FileNode>) => Promise<void> | void
19
- onUpdate?: (params: { file: FileNode; source?: string; processed: number; total: number; percentage: number }) => Promise<void> | void
20
- }
21
-
22
- function joinSources(file: FileNode): string {
23
- return file.sources
24
- .map((item) => extractStringsFromNodes(item.nodes as Array<CodeNode>))
25
- .filter(Boolean)
26
- .join('\n\n')
27
- }
28
-
29
- /**
30
- * Converts a single file to a string using the registered parsers.
31
- * Falls back to joining source values when no matching parser is found.
32
- *
33
- * @internal
34
- */
35
- export class FileProcessor {
36
- readonly #limit = pLimit(PARALLEL_CONCURRENCY_LIMIT)
37
-
38
- async parse(file: FileNode, { parsers, extension }: ParseOptions = {}): Promise<string> {
39
- const parseExtName = extension?.[file.extname] || undefined
40
-
41
- if (!parsers || !file.extname) {
42
- return joinSources(file)
43
- }
44
-
45
- const parser = parsers.get(file.extname)
46
-
47
- if (!parser) {
48
- return joinSources(file)
49
- }
50
-
51
- return parser.parse(file, { extname: parseExtName })
52
- }
53
-
54
- async run(files: Array<FileNode>, { parsers, mode = 'sequential', extension, onStart, onEnd, onUpdate }: RunOptions = {}): Promise<Array<FileNode>> {
55
- await onStart?.(files)
56
-
57
- const total = files.length
58
- let processed = 0
59
-
60
- const processOne = async (file: FileNode) => {
61
- const source = await this.parse(file, { extension, parsers })
62
- const currentProcessed = ++processed
63
- const percentage = (currentProcessed / total) * 100
64
-
65
- await onUpdate?.({
66
- file,
67
- source,
68
- processed: currentProcessed,
69
- percentage,
70
- total,
71
- })
72
- }
73
-
74
- if (mode === 'sequential') {
75
- for (const file of files) {
76
- await processOne(file)
77
- }
78
- } else {
79
- await Promise.all(files.map((file) => this.#limit(() => processOne(file))))
80
- }
81
-
82
- await onEnd?.(files)
83
-
84
- return files
85
- }
86
- }
package/src/Kubb.ts DELETED
@@ -1,300 +0,0 @@
1
- import type { AsyncEventEmitter } from '@internals/utils'
2
- import type { OperationNode, SchemaNode } from '@kubb/ast'
3
- import type { BuildOutput } from './createKubb.ts'
4
- import type { PluginDriver } from './PluginDriver.ts'
5
- import type {
6
- Config,
7
- GeneratorContext,
8
- KubbBuildEndContext,
9
- KubbBuildStartContext,
10
- KubbConfigEndContext,
11
- KubbDebugContext,
12
- KubbErrorContext,
13
- KubbFileProcessingUpdateContext,
14
- KubbFilesProcessingEndContext,
15
- KubbFilesProcessingStartContext,
16
- KubbGenerationEndContext,
17
- KubbGenerationStartContext,
18
- KubbGenerationSummaryContext,
19
- KubbHookEndContext,
20
- KubbHookStartContext,
21
- KubbInfoContext,
22
- KubbLifecycleStartContext,
23
- KubbPluginEndContext,
24
- KubbPluginSetupContext,
25
- KubbPluginStartContext,
26
- KubbPluginsEndContext,
27
- KubbSuccessContext,
28
- KubbVersionNewContext,
29
- KubbWarnContext,
30
- } from './types'
31
-
32
- /**
33
- * Kubb code generation instance returned by {@link createKubb}.
34
- *
35
- * Use this when orchestrating multiple builds, inspecting plugin timings, or integrating Kubb into a larger toolchain.
36
- * For a single one-off build, chain directly: `await createKubb(config).build()`.
37
- */
38
- export type Kubb = {
39
- /**
40
- * Shared event emitter for lifecycle and status events. Attach listeners before calling `setup()` or `build()`.
41
- */
42
- readonly hooks: AsyncEventEmitter<KubbHooks>
43
- /**
44
- * Generated source code keyed by absolute file path. Available after `build()` or `safeBuild()` completes.
45
- */
46
- readonly sources: Map<string, string>
47
- /**
48
- * Plugin driver managing all plugins. Available after `setup()` completes.
49
- */
50
- readonly driver: PluginDriver | undefined
51
- /**
52
- * Resolved configuration with defaults applied. Available after `setup()` completes.
53
- */
54
- readonly config: Config | undefined
55
- /**
56
- * Resolves config and initializes the driver. `build()` calls this automatically.
57
- */
58
- setup(): Promise<void>
59
- /**
60
- * Runs the full pipeline and throws on any plugin error. Automatically calls `setup()` if needed.
61
- */
62
- build(): Promise<BuildOutput>
63
- /**
64
- * Runs the full pipeline and captures errors in `BuildOutput` instead of throwing. Automatically calls `setup()` if needed.
65
- */
66
- safeBuild(): Promise<BuildOutput>
67
- }
68
-
69
- /**
70
- * Lifecycle events emitted during Kubb code generation.
71
- * Use these for logging, progress tracking, and custom integrations.
72
- *
73
- * @example
74
- * ```typescript
75
- * import type { AsyncEventEmitter } from '@internals/utils'
76
- * import type { KubbHooks } from '@kubb/core'
77
- *
78
- * const hooks: AsyncEventEmitter<KubbHooks> = new AsyncEventEmitter()
79
- *
80
- * hooks.on('kubb:lifecycle:start', () => {
81
- * console.log('Starting Kubb generation')
82
- * })
83
- *
84
- * hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
85
- * console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
86
- * })
87
- * ```
88
- */
89
- export interface KubbHooks {
90
- /**
91
- * Fires at the start of the Kubb lifecycle, before code generation begins.
92
- */
93
- 'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext]
94
- /**
95
- * Fires at the end of the Kubb lifecycle, after all code generation completes.
96
- */
97
- 'kubb:lifecycle:end': []
98
-
99
- /**
100
- * Fires when configuration loading starts.
101
- */
102
- 'kubb:config:start': []
103
- /**
104
- * Fires when configuration loading completes.
105
- */
106
- 'kubb:config:end': [ctx: KubbConfigEndContext]
107
-
108
- /**
109
- * Fires when code generation starts.
110
- */
111
- 'kubb:generation:start': [ctx: KubbGenerationStartContext]
112
- /**
113
- * Fires when code generation completes.
114
- */
115
- 'kubb:generation:end': [ctx: KubbGenerationEndContext]
116
- /**
117
- * Fires with a generation summary including summary lines, title, and success status.
118
- */
119
- 'kubb:generation:summary': [ctx: KubbGenerationSummaryContext]
120
-
121
- /**
122
- * Fires when code formatting starts (e.g., Biome or Prettier).
123
- */
124
- 'kubb:format:start': []
125
- /**
126
- * Fires when code formatting completes.
127
- */
128
- 'kubb:format:end': []
129
-
130
- /**
131
- * Fires when linting starts.
132
- */
133
- 'kubb:lint:start': []
134
- /**
135
- * Fires when linting completes.
136
- */
137
- 'kubb:lint:end': []
138
-
139
- /**
140
- * Fires when plugin hooks execution starts.
141
- */
142
- 'kubb:hooks:start': []
143
- /**
144
- * Fires when plugin hooks execution completes.
145
- */
146
- 'kubb:hooks:end': []
147
-
148
- /**
149
- * Fires when a single hook executes (e.g., format or lint). The callback is invoked when the command finishes.
150
- */
151
- 'kubb:hook:start': [ctx: KubbHookStartContext]
152
- /**
153
- * Fires when a single hook execution completes.
154
- */
155
- 'kubb:hook:end': [ctx: KubbHookEndContext]
156
-
157
- /**
158
- * Fires when a new Kubb version is available.
159
- */
160
- 'kubb:version:new': [ctx: KubbVersionNewContext]
161
-
162
- /**
163
- * Informational message event.
164
- */
165
- 'kubb:info': [ctx: KubbInfoContext]
166
- /**
167
- * Error event, fired when an error occurs during generation.
168
- */
169
- 'kubb:error': [ctx: KubbErrorContext]
170
- /**
171
- * Success message event.
172
- */
173
- 'kubb:success': [ctx: KubbSuccessContext]
174
- /**
175
- * Warning message event.
176
- */
177
- 'kubb:warn': [ctx: KubbWarnContext]
178
- /**
179
- * Debug event for detailed logging with timestamp and optional filename.
180
- */
181
- 'kubb:debug': [ctx: KubbDebugContext]
182
-
183
- /**
184
- * Fires when file processing starts with the list of files to process.
185
- */
186
- 'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext]
187
- /**
188
- * Fires for each file with progress updates: processed count, total, percentage, and file details.
189
- */
190
- 'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext]
191
- /**
192
- * Fires when file processing completes with the list of processed files.
193
- */
194
- 'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext]
195
-
196
- /**
197
- * Fires when a plugin starts execution.
198
- */
199
- 'kubb:plugin:start': [ctx: KubbPluginStartContext]
200
- /**
201
- * Fires when a plugin completes execution. Duration measured in milliseconds.
202
- */
203
- 'kubb:plugin:end': [ctx: KubbPluginEndContext]
204
-
205
- /**
206
- * Fires once before plugins execute — allowing plugins to register generators, configure resolvers/transformers/renderers, or inject files.
207
- */
208
- 'kubb:plugin:setup': [ctx: KubbPluginSetupContext]
209
- /**
210
- * Fires before the plugin execution loop begins. The adapter has already parsed the source and `inputNode` is available.
211
- */
212
- 'kubb:build:start': [ctx: KubbBuildStartContext]
213
- /**
214
- * Fires after all plugins run and per-plugin barrels generate, but before files write to disk.
215
- * Use this to inject final files that must persist in the same write pass as plugin output.
216
- */
217
- 'kubb:plugins:end': [ctx: KubbPluginsEndContext]
218
- /**
219
- * Fires after all files write to disk.
220
- */
221
- 'kubb:build:end': [ctx: KubbBuildEndContext]
222
-
223
- /**
224
- * Fires for each schema node during AST traversal. Generator listeners respond to this.
225
- */
226
- 'kubb:generate:schema': [node: SchemaNode, ctx: GeneratorContext]
227
- /**
228
- * Fires for each operation node during AST traversal. Generator listeners respond to this.
229
- */
230
- 'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext]
231
- /**
232
- * Fires once after all operations traverse with the full collected array. Batch generator listeners respond to this.
233
- */
234
- 'kubb:generate:operations': [nodes: Array<OperationNode>, ctx: GeneratorContext]
235
- }
236
-
237
- declare global {
238
- namespace Kubb {
239
- /**
240
- * Registry that maps plugin names to their `PluginFactoryOptions`.
241
- * Augment this interface in each plugin's `types.ts` to enable automatic
242
- * typing for `getPlugin` and `requirePlugin`.
243
- *
244
- * @example
245
- * ```ts
246
- * // packages/plugin-ts/src/types.ts
247
- * declare global {
248
- * namespace Kubb {
249
- * interface PluginRegistry {
250
- * 'plugin-ts': PluginTs
251
- * }
252
- * }
253
- * }
254
- * ```
255
- */
256
- interface PluginRegistry {}
257
-
258
- /**
259
- * Extension point for root `Config['output']` options.
260
- * Augment the `output` key in middleware or plugin packages to add extra fields
261
- * to the global output configuration without touching core types.
262
- *
263
- * @example
264
- * ```ts
265
- * // packages/middleware-barrel/src/types.ts
266
- * declare global {
267
- * namespace Kubb {
268
- * interface ConfigOptionsRegistry {
269
- * output: {
270
- * barrel?: import('./types.ts').BarrelConfig | false
271
- * }
272
- * }
273
- * }
274
- * }
275
- * ```
276
- */
277
- interface ConfigOptionsRegistry {}
278
-
279
- /**
280
- * Extension point for per-plugin `Output` options.
281
- * Augment the `output` key in middleware or plugin packages to add extra fields
282
- * to the per-plugin output configuration without touching core types.
283
- *
284
- * @example
285
- * ```ts
286
- * // packages/middleware-barrel/src/types.ts
287
- * declare global {
288
- * namespace Kubb {
289
- * interface PluginOptionsRegistry {
290
- * output: {
291
- * barrel?: import('./types.ts').PluginBarrelConfig | false
292
- * }
293
- * }
294
- * }
295
- * }
296
- * ```
297
- */
298
- interface PluginOptionsRegistry {}
299
- }
300
- }