@kubb/core 5.0.0-alpha.6 → 5.0.0-alpha.60

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 (72) hide show
  1. package/README.md +3 -2
  2. package/dist/PluginDriver-Bc0HQM8V.js +948 -0
  3. package/dist/PluginDriver-Bc0HQM8V.js.map +1 -0
  4. package/dist/PluginDriver-Dyl2fwfQ.cjs +1039 -0
  5. package/dist/PluginDriver-Dyl2fwfQ.cjs.map +1 -0
  6. package/dist/index.cjs +691 -1798
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +279 -265
  9. package/dist/index.js +678 -1765
  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-i0b4_23K.d.ts +1903 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +110 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +205 -130
  21. package/src/PluginDriver.ts +424 -0
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +25 -0
  24. package/src/createKubb.ts +527 -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/defineMiddleware.ts +59 -0
  30. package/src/defineParser.ts +45 -0
  31. package/src/definePlugin.ts +78 -7
  32. package/src/defineResolver.ts +521 -0
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +13 -17
  35. package/src/mocks.ts +171 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +40 -11
  38. package/src/storages/memoryStorage.ts +4 -3
  39. package/src/types.ts +738 -218
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/isInputPath.ts +10 -0
  42. package/src/utils/packageJSON.ts +99 -0
  43. package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -60
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -64
  48. package/dist/hooks.js +0 -56
  49. package/dist/hooks.js.map +0 -1
  50. package/src/BarrelManager.ts +0 -74
  51. package/src/PackageManager.ts +0 -180
  52. package/src/PluginManager.ts +0 -667
  53. package/src/PromiseManager.ts +0 -40
  54. package/src/build.ts +0 -419
  55. package/src/config.ts +0 -56
  56. package/src/defineAdapter.ts +0 -22
  57. package/src/defineStorage.ts +0 -56
  58. package/src/errors.ts +0 -1
  59. package/src/hooks/index.ts +0 -4
  60. package/src/hooks/useKubb.ts +0 -55
  61. package/src/hooks/useMode.ts +0 -11
  62. package/src/hooks/usePlugin.ts +0 -11
  63. package/src/hooks/usePluginManager.ts +0 -11
  64. package/src/utils/FunctionParams.ts +0 -155
  65. package/src/utils/TreeNode.ts +0 -215
  66. package/src/utils/executeStrategies.ts +0 -81
  67. package/src/utils/formatters.ts +0 -56
  68. package/src/utils/getBarrelFiles.ts +0 -79
  69. package/src/utils/getConfigs.ts +0 -30
  70. package/src/utils/getPlugins.ts +0 -23
  71. package/src/utils/linters.ts +0 -25
  72. package/src/utils/resolveOptions.ts +0 -93
package/src/Kubb.ts CHANGED
@@ -1,37 +1,73 @@
1
- import type { KubbFile } from '@kubb/fabric-core/types'
2
- import type { Strategy } from './PluginManager.ts'
3
- import type { Config, Plugin, PluginLifecycleHooks } from './types'
4
-
5
- type DebugEvent = {
6
- date: Date
7
- logs: string[]
8
- fileName?: string
9
- }
10
-
11
- type ProgressStartMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
12
- hookName: H
13
- plugins: Array<Plugin>
14
- }
15
-
16
- type ProgressStopMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
17
- hookName: H
18
- }
19
-
20
- type ExecutingMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
21
- strategy: Strategy
22
- hookName: H
23
- plugin: Plugin
24
- parameters?: unknown[] | undefined
25
- output?: unknown
26
- }
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'
27
31
 
28
- type ExecutedMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
29
- duration: number
30
- strategy: Strategy
31
- hookName: H
32
- plugin: Plugin
33
- parameters?: unknown[] | undefined
34
- output?: unknown
32
+ /**
33
+ * The instance returned by {@link createKubb}.
34
+ */
35
+ export type Kubb = {
36
+ /**
37
+ * The shared event emitter. Attach listeners here before calling `setup()` or `build()`.
38
+ */
39
+ readonly hooks: AsyncEventEmitter<KubbHooks>
40
+ /**
41
+ * Raw generated source, keyed by absolute file path.
42
+ * Populated after a successful `build()` or `safeBuild()` call.
43
+ */
44
+ readonly sources: Map<string, string>
45
+ /**
46
+ * The plugin driver. Available after `setup()` has been called.
47
+ */
48
+ readonly driver: PluginDriver | undefined
49
+ /**
50
+ * The resolved config with applied defaults. Available after `setup()` has been called.
51
+ */
52
+ readonly config: Config | undefined
53
+ /**
54
+ * Initializes all Kubb infrastructure: validates input, applies config defaults,
55
+ * runs the adapter, and creates the PluginDriver.
56
+ *
57
+ * Calling `build()` or `safeBuild()` without calling `setup()` first will
58
+ * automatically invoke `setup()` before proceeding.
59
+ */
60
+ setup(): Promise<void>
61
+ /**
62
+ * Runs a full Kubb build and throws on any error or plugin failure.
63
+ * Automatically calls `setup()` if it has not been called yet.
64
+ */
65
+ build(): Promise<BuildOutput>
66
+ /**
67
+ * Runs a full Kubb build and captures errors instead of throwing.
68
+ * Automatically calls `setup()` if it has not been called yet.
69
+ */
70
+ safeBuild(): Promise<BuildOutput>
35
71
  }
36
72
 
37
73
  /**
@@ -40,213 +76,252 @@ type ExecutedMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
40
76
  *
41
77
  * @example
42
78
  * ```typescript
43
- * import type { AsyncEventEmitter } from '@kubb/core'
44
- * import type { KubbEvents } from '@kubb/core'
79
+ * import type { AsyncEventEmitter } from '@internals/utils'
80
+ * import type { KubbHooks } from '@kubb/core'
45
81
  *
46
- * const events: AsyncEventEmitter<KubbEvents> = new AsyncEventEmitter()
82
+ * const hooks: AsyncEventEmitter<KubbHooks> = new AsyncEventEmitter()
47
83
  *
48
- * events.on('lifecycle:start', () => {
84
+ * hooks.on('kubb:lifecycle:start', () => {
49
85
  * console.log('Starting Kubb generation')
50
86
  * })
51
87
  *
52
- * events.on('plugin:end', (plugin, { duration }) => {
88
+ * hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
53
89
  * console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
54
90
  * })
55
91
  * ```
56
92
  */
57
- export interface KubbEvents {
93
+ export interface KubbHooks {
58
94
  /**
59
95
  * Emitted at the beginning of the Kubb lifecycle, before any code generation starts.
60
96
  */
61
- 'lifecycle:start': [version: string]
62
-
97
+ 'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext]
63
98
  /**
64
99
  * Emitted at the end of the Kubb lifecycle, after all code generation is complete.
65
100
  */
66
- 'lifecycle:end': []
101
+ 'kubb:lifecycle:end': []
67
102
 
68
103
  /**
69
104
  * Emitted when configuration loading starts.
70
105
  */
71
- 'config:start': []
72
-
106
+ 'kubb:config:start': []
73
107
  /**
74
108
  * Emitted when configuration loading is complete.
75
109
  */
76
- 'config:end': [configs: Array<Config>]
110
+ 'kubb:config:end': [ctx: KubbConfigEndContext]
77
111
 
78
112
  /**
79
113
  * Emitted when code generation phase starts.
80
114
  */
81
- 'generation:start': [config: Config]
82
-
115
+ 'kubb:generation:start': [ctx: KubbGenerationStartContext]
83
116
  /**
84
117
  * Emitted when code generation phase completes.
85
118
  */
86
- 'generation:end': [Config: Config, files: Array<KubbFile.ResolvedFile>, sources: Map<KubbFile.Path, string>]
87
-
119
+ 'kubb:generation:end': [ctx: KubbGenerationEndContext]
88
120
  /**
89
121
  * Emitted with a summary of the generation results.
90
122
  * Contains summary lines, title, and success status.
91
123
  */
92
- 'generation:summary': [
93
- Config: Config,
94
- {
95
- failedPlugins: Set<{ plugin: Plugin; error: Error }>
96
- status: 'success' | 'failed'
97
- hrStart: [number, number]
98
- filesCreated: number
99
- pluginTimings?: Map<string, number>
100
- },
101
- ]
124
+ 'kubb:generation:summary': [ctx: KubbGenerationSummaryContext]
102
125
 
103
126
  /**
104
127
  * Emitted when code formatting starts (e.g., running Biome or Prettier).
105
128
  */
106
- 'format:start': []
107
-
129
+ 'kubb:format:start': []
108
130
  /**
109
131
  * Emitted when code formatting completes.
110
132
  */
111
- 'format:end': []
133
+ 'kubb:format:end': []
112
134
 
113
135
  /**
114
136
  * Emitted when linting starts.
115
137
  */
116
- 'lint:start': []
117
-
138
+ 'kubb:lint:start': []
118
139
  /**
119
140
  * Emitted when linting completes.
120
141
  */
121
- 'lint:end': []
142
+ 'kubb:lint:end': []
122
143
 
123
144
  /**
124
145
  * Emitted when plugin hooks execution starts.
125
146
  */
126
- 'hooks:start': []
127
-
147
+ 'kubb:hooks:start': []
128
148
  /**
129
149
  * Emitted when plugin hooks execution completes.
130
150
  */
131
- 'hooks:end': []
151
+ 'kubb:hooks:end': []
132
152
 
133
153
  /**
134
- * Emitted when a single hook execution starts. (e.g., format or lint).
154
+ * Emitted when a single hook execution starts (e.g., format or lint).
135
155
  * The callback should be invoked when the command completes.
136
156
  */
137
- 'hook:start': [{ id?: string; command: string; args?: readonly string[] }]
157
+ 'kubb:hook:start': [ctx: KubbHookStartContext]
138
158
  /**
139
159
  * Emitted when a single hook execution completes.
140
160
  */
141
- 'hook:end': [
142
- {
143
- id?: string
144
- command: string
145
- args?: readonly string[]
146
- success: boolean
147
- error: Error | null
148
- },
149
- ]
161
+ 'kubb:hook:end': [ctx: KubbHookEndContext]
150
162
 
151
163
  /**
152
164
  * Emitted when a new version of Kubb is available.
153
165
  */
154
- 'version:new': [currentVersion: string, latestVersion: string]
166
+ 'kubb:version:new': [ctx: KubbVersionNewContext]
155
167
 
156
168
  /**
157
169
  * Informational message event.
158
170
  */
159
- info: [message: string, info?: string]
160
-
171
+ 'kubb:info': [ctx: KubbInfoContext]
161
172
  /**
162
173
  * Error event. Emitted when an error occurs during code generation.
163
174
  */
164
- error: [error: Error, meta?: Record<string, unknown>]
165
-
175
+ 'kubb:error': [ctx: KubbErrorContext]
166
176
  /**
167
177
  * Success message event.
168
178
  */
169
- success: [message: string, info?: string]
170
-
179
+ 'kubb:success': [ctx: KubbSuccessContext]
171
180
  /**
172
181
  * Warning message event.
173
182
  */
174
- warn: [message: string, info?: string]
175
-
183
+ 'kubb:warn': [ctx: KubbWarnContext]
176
184
  /**
177
185
  * Debug event for detailed logging.
178
186
  * Contains timestamp, log messages, and optional filename.
179
187
  */
180
- debug: [meta: DebugEvent]
188
+ 'kubb:debug': [ctx: KubbDebugContext]
181
189
 
182
190
  /**
183
191
  * Emitted when file processing starts.
184
192
  * Contains the list of files to be processed.
185
193
  */
186
- 'files:processing:start': [files: Array<KubbFile.ResolvedFile>]
187
-
194
+ 'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext]
188
195
  /**
189
196
  * Emitted for each file being processed, providing progress updates.
190
197
  * Contains processed count, total count, percentage, and file details.
191
198
  */
192
- 'file:processing:update': [
193
- {
194
- /** Number of files processed so far */
195
- processed: number
196
- /** Total number of files to process */
197
- total: number
198
- /** Processing percentage (0-100) */
199
- percentage: number
200
- /** Optional source identifier */
201
- source?: string
202
- /** The file being processed */
203
- file: KubbFile.ResolvedFile
204
- /**
205
- * Kubb configuration (not present in Fabric).
206
- * Provides access to the current config during file processing.
207
- */
208
- config: Config
209
- },
210
- ]
211
-
199
+ 'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext]
212
200
  /**
213
201
  * Emitted when file processing completes.
214
202
  * Contains the list of processed files.
215
203
  */
216
- 'files:processing:end': [files: KubbFile.ResolvedFile[]]
204
+ 'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext]
217
205
 
218
206
  /**
219
207
  * Emitted when a plugin starts executing.
220
208
  */
221
- 'plugin:start': [plugin: Plugin]
222
-
209
+ 'kubb:plugin:start': [ctx: KubbPluginStartContext]
223
210
  /**
224
211
  * Emitted when a plugin completes execution.
225
- * Duration in ms
212
+ * Duration in ms.
226
213
  */
227
- 'plugin:end': [plugin: Plugin, meta: { duration: number; success: boolean; error?: Error }]
214
+ 'kubb:plugin:end': [ctx: KubbPluginEndContext]
228
215
 
229
216
  /**
230
- * Emitted when plugin hook progress tracking starts.
231
- * Contains the hook name and list of plugins to execute.
217
+ * Fired once — before any plugin's `buildStart` runs so that hook-style plugins
218
+ * can register generators, configure resolvers/transformers/renderers, or inject
219
+ * extra files. All `kubb:plugin:setup` handlers registered via `definePlugin` receive
220
+ * a plugin-specific context (with the correct `addGenerator` closure).
221
+ * External tooling can observe this event via `hooks.on('kubb:plugin:setup', …)`.
232
222
  */
233
- 'plugins:hook:progress:start': [meta: ProgressStartMeta]
234
-
223
+ 'kubb:plugin:setup': [ctx: KubbPluginSetupContext]
235
224
  /**
236
- * Emitted when plugin hook progress tracking ends.
237
- * Contains the hook name that completed.
225
+ * Fired immediately before the plugin execution loop begins.
226
+ * The adapter has already parsed the source and `inputNode` is available.
238
227
  */
239
- 'plugins:hook:progress:end': [meta: ProgressStopMeta]
240
-
228
+ 'kubb:build:start': [ctx: KubbBuildStartContext]
241
229
  /**
242
- * Emitted when a plugin hook starts processing.
243
- * Contains strategy, hook name, plugin, parameters, and output.
230
+ * Fired after all plugins have run and all per-plugin barrels have been generated,
231
+ * but BEFORE files are written to disk.
232
+ * Use this event to inject final files (e.g. a root barrel) that must be persisted
233
+ * in the same write pass as the plugin output.
244
234
  */
245
- 'plugins:hook:processing:start': [meta: ExecutingMeta]
235
+ 'kubb:plugins:end': [ctx: KubbPluginsEndContext]
236
+ /**
237
+ * Fired after all files have been written to disk.
238
+ */
239
+ 'kubb:build:end': [ctx: KubbBuildEndContext]
246
240
 
247
241
  /**
248
- * Emitted when a plugin hook completes processing.
249
- * Contains duration, strategy, hook name, plugin, parameters, and output.
242
+ * Emitted for each schema node during the AST walk.
243
+ * Generator listeners registered via `addGenerator()` in `kubb:plugin:setup` respond to this event.
244
+ * The `ctx.plugin.name` identifies which plugin is driving the current walk.
245
+ * `ctx.options` carries the per-node resolved options (after exclude/include/override).
250
246
  */
251
- 'plugins:hook:processing:end': [meta: ExecutedMeta]
247
+ 'kubb:generate:schema': [node: SchemaNode, ctx: GeneratorContext]
248
+ /**
249
+ * Emitted for each operation node during the AST walk.
250
+ * Generator listeners registered via `addGenerator()` in `kubb:plugin:setup` respond to this event.
251
+ * The `ctx.plugin.name` identifies which plugin is driving the current walk.
252
+ * `ctx.options` carries the per-node resolved options (after exclude/include/override).
253
+ */
254
+ 'kubb:generate:operation': [node: OperationNode, ctx: GeneratorContext]
255
+ /**
256
+ * Emitted once after all operations have been walked, with the full collected array.
257
+ * Generator listeners with an `operations()` method respond to this event.
258
+ * The `ctx.plugin.name` identifies which plugin is driving the current walk.
259
+ * `ctx.options` carries the plugin-level resolved options for the batch call.
260
+ */
261
+ 'kubb:generate:operations': [nodes: Array<OperationNode>, ctx: GeneratorContext]
262
+ }
263
+
264
+ declare global {
265
+ namespace Kubb {
266
+ /**
267
+ * Registry that maps plugin names to their `PluginFactoryOptions`.
268
+ * Augment this interface in each plugin's `types.ts` to enable automatic
269
+ * typing for `getPlugin` and `requirePlugin`.
270
+ *
271
+ * @example
272
+ * ```ts
273
+ * // packages/plugin-ts/src/types.ts
274
+ * declare global {
275
+ * namespace Kubb {
276
+ * interface PluginRegistry {
277
+ * 'plugin-ts': PluginTs
278
+ * }
279
+ * }
280
+ * }
281
+ * ```
282
+ */
283
+ interface PluginRegistry {}
284
+
285
+ /**
286
+ * Extension point for root `Config['output']` options.
287
+ * Augment the `output` key in middleware or plugin packages to add extra fields
288
+ * to the global output configuration without touching core types.
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * // packages/middleware-barrel/src/types.ts
293
+ * declare global {
294
+ * namespace Kubb {
295
+ * interface ConfigOptionsRegistry {
296
+ * output: {
297
+ * barrelType?: import('./types.ts').BarrelType | false
298
+ * }
299
+ * }
300
+ * }
301
+ * }
302
+ * ```
303
+ */
304
+ interface ConfigOptionsRegistry {}
305
+
306
+ /**
307
+ * Extension point for per-plugin `Output` options.
308
+ * Augment the `output` key in middleware or plugin packages to add extra fields
309
+ * to the per-plugin output configuration without touching core types.
310
+ *
311
+ * @example
312
+ * ```ts
313
+ * // packages/middleware-barrel/src/types.ts
314
+ * declare global {
315
+ * namespace Kubb {
316
+ * interface PluginOptionsRegistry {
317
+ * output: {
318
+ * barrelType?: import('./types.ts').BarrelType | false
319
+ * }
320
+ * }
321
+ * }
322
+ * }
323
+ * ```
324
+ */
325
+ interface PluginOptionsRegistry {}
326
+ }
252
327
  }