@kubb/core 5.0.0-alpha.9 → 5.0.0-beta.2

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 (64) hide show
  1. package/README.md +24 -21
  2. package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
  3. package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
  4. package/dist/PluginDriver-DV3p2Hky.js +945 -0
  5. package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
  6. package/dist/index.cjs +752 -1641
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +271 -225
  9. package/dist/index.js +736 -1609
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +145 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +80 -0
  14. package/dist/mocks.js +140 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-CC09VtBt.d.ts +2148 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +115 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +207 -131
  21. package/src/PluginDriver.ts +325 -564
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +13 -6
  24. package/src/createKubb.ts +574 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +13 -1
  27. package/src/defineGenerator.ts +77 -124
  28. package/src/defineLogger.ts +4 -2
  29. package/src/defineMiddleware.ts +62 -0
  30. package/src/defineParser.ts +44 -0
  31. package/src/definePlugin.ts +83 -0
  32. package/src/defineResolver.ts +418 -28
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +13 -15
  35. package/src/mocks.ts +178 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +41 -11
  38. package/src/storages/memoryStorage.ts +4 -2
  39. package/src/types.ts +1031 -283
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/isInputPath.ts +10 -0
  42. package/src/utils/packageJSON.ts +50 -12
  43. package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -103
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -77
  48. package/dist/hooks.js +0 -98
  49. package/dist/hooks.js.map +0 -1
  50. package/src/build.ts +0 -418
  51. package/src/config.ts +0 -56
  52. package/src/createPlugin.ts +0 -28
  53. package/src/hooks/index.ts +0 -4
  54. package/src/hooks/useKubb.ts +0 -143
  55. package/src/hooks/useMode.ts +0 -11
  56. package/src/hooks/usePlugin.ts +0 -11
  57. package/src/hooks/usePluginDriver.ts +0 -11
  58. package/src/utils/FunctionParams.ts +0 -155
  59. package/src/utils/TreeNode.ts +0 -215
  60. package/src/utils/executeStrategies.ts +0 -81
  61. package/src/utils/formatters.ts +0 -56
  62. package/src/utils/getBarrelFiles.ts +0 -141
  63. package/src/utils/getConfigs.ts +0 -12
  64. package/src/utils/linters.ts +0 -25
package/src/Kubb.ts CHANGED
@@ -1,224 +1,300 @@
1
- import type { KubbFile } from '@kubb/fabric-core/types'
2
- import type { Strategy } from './PluginDriver.ts'
3
- import type { Config, Plugin, PluginLifecycleHooks } from './types'
4
-
5
- type DebugInfo = {
6
- date: Date
7
- logs: Array<string>
8
- fileName?: string
9
- }
10
-
11
- type HookProgress<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
12
- hookName: H
13
- plugins: Array<Plugin>
14
- }
15
-
16
- type HookExecution<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
17
- strategy: Strategy
18
- hookName: H
19
- plugin: Plugin
20
- parameters?: Array<unknown>
21
- output?: unknown
22
- }
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'
23
31
 
24
- type HookResult<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
25
- duration: number
26
- strategy: Strategy
27
- hookName: H
28
- plugin: Plugin
29
- parameters?: Array<unknown>
30
- output?: unknown
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>
31
67
  }
32
68
 
33
69
  /**
34
- * Events emitted during the Kubb code generation lifecycle.
35
- * These events can be listened to for logging, progress tracking, and custom integrations.
70
+ * Lifecycle events emitted during Kubb code generation.
71
+ * Use these for logging, progress tracking, and custom integrations.
36
72
  *
37
73
  * @example
38
74
  * ```typescript
39
75
  * import type { AsyncEventEmitter } from '@internals/utils'
40
- * import type { KubbEvents } from '@kubb/core'
76
+ * import type { KubbHooks } from '@kubb/core'
41
77
  *
42
- * const events: AsyncEventEmitter<KubbEvents> = new AsyncEventEmitter()
78
+ * const hooks: AsyncEventEmitter<KubbHooks> = new AsyncEventEmitter()
43
79
  *
44
- * events.on('lifecycle:start', () => {
80
+ * hooks.on('kubb:lifecycle:start', () => {
45
81
  * console.log('Starting Kubb generation')
46
82
  * })
47
83
  *
48
- * events.on('plugin:end', (plugin, { duration }) => {
84
+ * hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
49
85
  * console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
50
86
  * })
51
87
  * ```
52
88
  */
53
- export interface KubbEvents {
89
+ export interface KubbHooks {
54
90
  /**
55
- * Emitted at the beginning of the Kubb lifecycle, before any code generation starts.
91
+ * Fires at the start of the Kubb lifecycle, before code generation begins.
56
92
  */
57
- 'lifecycle:start': [version: string]
93
+ 'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext]
58
94
  /**
59
- * Emitted at the end of the Kubb lifecycle, after all code generation is complete.
95
+ * Fires at the end of the Kubb lifecycle, after all code generation completes.
60
96
  */
61
- 'lifecycle:end': []
97
+ 'kubb:lifecycle:end': []
62
98
 
63
99
  /**
64
- * Emitted when configuration loading starts.
100
+ * Fires when configuration loading starts.
65
101
  */
66
- 'config:start': []
102
+ 'kubb:config:start': []
67
103
  /**
68
- * Emitted when configuration loading is complete.
104
+ * Fires when configuration loading completes.
69
105
  */
70
- 'config:end': [configs: Array<Config>]
106
+ 'kubb:config:end': [ctx: KubbConfigEndContext]
71
107
 
72
108
  /**
73
- * Emitted when code generation phase starts.
109
+ * Fires when code generation starts.
74
110
  */
75
- 'generation:start': [config: Config]
111
+ 'kubb:generation:start': [ctx: KubbGenerationStartContext]
76
112
  /**
77
- * Emitted when code generation phase completes.
113
+ * Fires when code generation completes.
78
114
  */
79
- 'generation:end': [config: Config, files: Array<KubbFile.ResolvedFile>, sources: Map<KubbFile.Path, string>]
115
+ 'kubb:generation:end': [ctx: KubbGenerationEndContext]
80
116
  /**
81
- * Emitted with a summary of the generation results.
82
- * Contains summary lines, title, and success status.
117
+ * Fires with a generation summary including summary lines, title, and success status.
83
118
  */
84
- 'generation:summary': [
85
- config: Config,
86
- {
87
- failedPlugins: Set<{ plugin: Plugin; error: Error }>
88
- status: 'success' | 'failed'
89
- hrStart: [number, number]
90
- filesCreated: number
91
- pluginTimings?: Map<Plugin['name'], number>
92
- },
93
- ]
119
+ 'kubb:generation:summary': [ctx: KubbGenerationSummaryContext]
94
120
 
95
121
  /**
96
- * Emitted when code formatting starts (e.g., running Biome or Prettier).
122
+ * Fires when code formatting starts (e.g., Biome or Prettier).
97
123
  */
98
- 'format:start': []
124
+ 'kubb:format:start': []
99
125
  /**
100
- * Emitted when code formatting completes.
126
+ * Fires when code formatting completes.
101
127
  */
102
- 'format:end': []
128
+ 'kubb:format:end': []
103
129
 
104
130
  /**
105
- * Emitted when linting starts.
131
+ * Fires when linting starts.
106
132
  */
107
- 'lint:start': []
133
+ 'kubb:lint:start': []
108
134
  /**
109
- * Emitted when linting completes.
135
+ * Fires when linting completes.
110
136
  */
111
- 'lint:end': []
137
+ 'kubb:lint:end': []
112
138
 
113
139
  /**
114
- * Emitted when plugin hooks execution starts.
140
+ * Fires when plugin hooks execution starts.
115
141
  */
116
- 'hooks:start': []
142
+ 'kubb:hooks:start': []
117
143
  /**
118
- * Emitted when plugin hooks execution completes.
144
+ * Fires when plugin hooks execution completes.
119
145
  */
120
- 'hooks:end': []
146
+ 'kubb:hooks:end': []
121
147
 
122
148
  /**
123
- * Emitted when a single hook execution starts (e.g., format or lint).
124
- * The callback should be invoked when the command completes.
149
+ * Fires when a single hook executes (e.g., format or lint). The callback is invoked when the command finishes.
125
150
  */
126
- 'hook:start': [{ id?: string; command: string; args?: readonly string[] }]
151
+ 'kubb:hook:start': [ctx: KubbHookStartContext]
127
152
  /**
128
- * Emitted when a single hook execution completes.
153
+ * Fires when a single hook execution completes.
129
154
  */
130
- 'hook:end': [{ id?: string; command: string; args?: readonly string[]; success: boolean; error: Error | null }]
155
+ 'kubb:hook:end': [ctx: KubbHookEndContext]
131
156
 
132
157
  /**
133
- * Emitted when a new version of Kubb is available.
158
+ * Fires when a new Kubb version is available.
134
159
  */
135
- 'version:new': [currentVersion: string, latestVersion: string]
160
+ 'kubb:version:new': [ctx: KubbVersionNewContext]
136
161
 
137
162
  /**
138
163
  * Informational message event.
139
164
  */
140
- info: [message: string, info?: string]
165
+ 'kubb:info': [ctx: KubbInfoContext]
141
166
  /**
142
- * Error event. Emitted when an error occurs during code generation.
167
+ * Error event, fired when an error occurs during generation.
143
168
  */
144
- error: [error: Error, meta?: Record<string, unknown>]
169
+ 'kubb:error': [ctx: KubbErrorContext]
145
170
  /**
146
171
  * Success message event.
147
172
  */
148
- success: [message: string, info?: string]
173
+ 'kubb:success': [ctx: KubbSuccessContext]
149
174
  /**
150
175
  * Warning message event.
151
176
  */
152
- warn: [message: string, info?: string]
177
+ 'kubb:warn': [ctx: KubbWarnContext]
153
178
  /**
154
- * Debug event for detailed logging.
155
- * Contains timestamp, log messages, and optional filename.
179
+ * Debug event for detailed logging with timestamp and optional filename.
156
180
  */
157
- debug: [info: DebugInfo]
181
+ 'kubb:debug': [ctx: KubbDebugContext]
158
182
 
159
183
  /**
160
- * Emitted when file processing starts.
161
- * Contains the list of files to be processed.
184
+ * Fires when file processing starts with the list of files to process.
162
185
  */
163
- 'files:processing:start': [files: Array<KubbFile.ResolvedFile>]
186
+ 'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext]
164
187
  /**
165
- * Emitted for each file being processed, providing progress updates.
166
- * Contains processed count, total count, percentage, and file details.
188
+ * Fires for each file with progress updates: processed count, total, percentage, and file details.
167
189
  */
168
- 'file:processing:update': [
169
- {
170
- /** Number of files processed so far. */
171
- processed: number
172
- /** Total number of files to process. */
173
- total: number
174
- /** Processing percentage (0–100). */
175
- percentage: number
176
- /** Optional source identifier. */
177
- source?: string
178
- /** The file being processed. */
179
- file: KubbFile.ResolvedFile
180
- /**
181
- * Kubb configuration (not present in Fabric).
182
- * Provides access to the current config during file processing.
183
- */
184
- config: Config
185
- },
186
- ]
190
+ 'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext]
187
191
  /**
188
- * Emitted when file processing completes.
189
- * Contains the list of processed files.
192
+ * Fires when file processing completes with the list of processed files.
190
193
  */
191
- 'files:processing:end': [files: Array<KubbFile.ResolvedFile>]
194
+ 'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext]
192
195
 
193
196
  /**
194
- * Emitted when a plugin starts executing.
197
+ * Fires when a plugin starts execution.
195
198
  */
196
- 'plugin:start': [plugin: Plugin]
199
+ 'kubb:plugin:start': [ctx: KubbPluginStartContext]
197
200
  /**
198
- * Emitted when a plugin completes execution.
199
- * Duration in ms.
201
+ * Fires when a plugin completes execution. Duration measured in milliseconds.
200
202
  */
201
- 'plugin:end': [plugin: Plugin, result: { duration: number; success: boolean; error?: Error }]
203
+ 'kubb:plugin:end': [ctx: KubbPluginEndContext]
202
204
 
203
205
  /**
204
- * Emitted when plugin hook progress tracking starts.
205
- * Contains the hook name and list of plugins to execute.
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.
206
211
  */
207
- 'plugins:hook:progress:start': [progress: HookProgress]
212
+ 'kubb:build:start': [ctx: KubbBuildStartContext]
208
213
  /**
209
- * Emitted when plugin hook progress tracking ends.
210
- * Contains the hook name that completed.
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.
211
216
  */
212
- 'plugins:hook:progress:end': [{ hookName: PluginLifecycleHooks }]
217
+ 'kubb:plugins:end': [ctx: KubbPluginsEndContext]
218
+ /**
219
+ * Fires after all files write to disk.
220
+ */
221
+ 'kubb:build:end': [ctx: KubbBuildEndContext]
213
222
 
214
223
  /**
215
- * Emitted when a plugin hook starts processing.
216
- * Contains strategy, hook name, plugin, parameters, and output.
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.
217
229
  */
218
- 'plugins:hook:processing:start': [execution: HookExecution]
230
+ 'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext]
219
231
  /**
220
- * Emitted when a plugin hook completes processing.
221
- * Contains duration, strategy, hook name, plugin, parameters, and output.
232
+ * Fires once after all operations traverse with the full collected array. Batch generator listeners respond to this.
222
233
  */
223
- 'plugins:hook:processing:end': [result: HookResult]
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
+ }
224
300
  }