@kubb/core 5.0.0-alpha.8 → 5.0.0-beta.75

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 (70) hide show
  1. package/README.md +23 -20
  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 +756 -1693
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +297 -239
  9. package/dist/index.js +743 -1661
  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-CuNocrbJ.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 +208 -160
  21. package/src/PluginDriver.ts +326 -565
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +16 -6
  24. package/src/createKubb.ts +548 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +40 -26
  27. package/src/defineGenerator.ts +87 -0
  28. package/src/defineLogger.ts +19 -0
  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 +521 -0
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +14 -17
  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 +1054 -270
  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/PluginDriver-DRfJIbG1.d.ts +0 -1056
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -102
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -75
  48. package/dist/hooks.js +0 -97
  49. package/dist/hooks.js.map +0 -1
  50. package/src/PackageManager.ts +0 -180
  51. package/src/build.ts +0 -419
  52. package/src/config.ts +0 -56
  53. package/src/createGenerator.ts +0 -106
  54. package/src/createLogger.ts +0 -7
  55. package/src/createPlugin.ts +0 -12
  56. package/src/errors.ts +0 -1
  57. package/src/hooks/index.ts +0 -4
  58. package/src/hooks/useKubb.ts +0 -138
  59. package/src/hooks/useMode.ts +0 -11
  60. package/src/hooks/usePlugin.ts +0 -11
  61. package/src/hooks/usePluginDriver.ts +0 -11
  62. package/src/utils/FunctionParams.ts +0 -155
  63. package/src/utils/TreeNode.ts +0 -215
  64. package/src/utils/executeStrategies.ts +0 -81
  65. package/src/utils/formatters.ts +0 -56
  66. package/src/utils/getBarrelFiles.ts +0 -141
  67. package/src/utils/getConfigs.ts +0 -30
  68. package/src/utils/getPlugins.ts +0 -23
  69. package/src/utils/linters.ts +0 -25
  70. package/src/utils/resolveOptions.ts +0 -93
package/src/Kubb.ts CHANGED
@@ -1,252 +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 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
+ * 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>
35
67
  }
36
68
 
37
69
  /**
38
- * Events emitted during the Kubb code generation lifecycle.
39
- * 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.
40
72
  *
41
73
  * @example
42
74
  * ```typescript
43
- * import type { AsyncEventEmitter } from '@kubb/core'
44
- * import type { KubbEvents } from '@kubb/core'
75
+ * import type { AsyncEventEmitter } from '@internals/utils'
76
+ * import type { KubbHooks } from '@kubb/core'
45
77
  *
46
- * const events: AsyncEventEmitter<KubbEvents> = new AsyncEventEmitter()
78
+ * const hooks: AsyncEventEmitter<KubbHooks> = new AsyncEventEmitter()
47
79
  *
48
- * events.on('lifecycle:start', () => {
80
+ * hooks.on('kubb:lifecycle:start', () => {
49
81
  * console.log('Starting Kubb generation')
50
82
  * })
51
83
  *
52
- * events.on('plugin:end', (plugin, { duration }) => {
84
+ * hooks.on('kubb:plugin:end', ({ plugin, duration }) => {
53
85
  * console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
54
86
  * })
55
87
  * ```
56
88
  */
57
- export interface KubbEvents {
89
+ export interface KubbHooks {
58
90
  /**
59
- * 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.
60
92
  */
61
- 'lifecycle:start': [version: string]
62
-
93
+ 'kubb:lifecycle:start': [ctx: KubbLifecycleStartContext]
63
94
  /**
64
- * 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.
65
96
  */
66
- 'lifecycle:end': []
97
+ 'kubb:lifecycle:end': []
67
98
 
68
99
  /**
69
- * Emitted when configuration loading starts.
100
+ * Fires when configuration loading starts.
70
101
  */
71
- 'config:start': []
72
-
102
+ 'kubb:config:start': []
73
103
  /**
74
- * Emitted when configuration loading is complete.
104
+ * Fires when configuration loading completes.
75
105
  */
76
- 'config:end': [configs: Array<Config>]
106
+ 'kubb:config:end': [ctx: KubbConfigEndContext]
77
107
 
78
108
  /**
79
- * Emitted when code generation phase starts.
109
+ * Fires when code generation starts.
80
110
  */
81
- 'generation:start': [config: Config]
82
-
111
+ 'kubb:generation:start': [ctx: KubbGenerationStartContext]
83
112
  /**
84
- * Emitted when code generation phase completes.
113
+ * Fires when code generation completes.
85
114
  */
86
- 'generation:end': [Config: Config, files: Array<KubbFile.ResolvedFile>, sources: Map<KubbFile.Path, string>]
87
-
115
+ 'kubb:generation:end': [ctx: KubbGenerationEndContext]
88
116
  /**
89
- * Emitted with a summary of the generation results.
90
- * Contains summary lines, title, and success status.
117
+ * Fires with a generation summary including summary lines, title, and success status.
91
118
  */
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
- ]
119
+ 'kubb:generation:summary': [ctx: KubbGenerationSummaryContext]
102
120
 
103
121
  /**
104
- * Emitted when code formatting starts (e.g., running Biome or Prettier).
122
+ * Fires when code formatting starts (e.g., Biome or Prettier).
105
123
  */
106
- 'format:start': []
107
-
124
+ 'kubb:format:start': []
108
125
  /**
109
- * Emitted when code formatting completes.
126
+ * Fires when code formatting completes.
110
127
  */
111
- 'format:end': []
128
+ 'kubb:format:end': []
112
129
 
113
130
  /**
114
- * Emitted when linting starts.
131
+ * Fires when linting starts.
115
132
  */
116
- 'lint:start': []
117
-
133
+ 'kubb:lint:start': []
118
134
  /**
119
- * Emitted when linting completes.
135
+ * Fires when linting completes.
120
136
  */
121
- 'lint:end': []
137
+ 'kubb:lint:end': []
122
138
 
123
139
  /**
124
- * Emitted when plugin hooks execution starts.
140
+ * Fires when plugin hooks execution starts.
125
141
  */
126
- 'hooks:start': []
127
-
142
+ 'kubb:hooks:start': []
128
143
  /**
129
- * Emitted when plugin hooks execution completes.
144
+ * Fires when plugin hooks execution completes.
130
145
  */
131
- 'hooks:end': []
146
+ 'kubb:hooks:end': []
132
147
 
133
148
  /**
134
- * Emitted when a single hook execution starts. (e.g., format or lint).
135
- * 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.
136
150
  */
137
- 'hook:start': [{ id?: string; command: string; args?: readonly string[] }]
151
+ 'kubb:hook:start': [ctx: KubbHookStartContext]
138
152
  /**
139
- * Emitted when a single hook execution completes.
153
+ * Fires when a single hook execution completes.
140
154
  */
141
- 'hook:end': [
142
- {
143
- id?: string
144
- command: string
145
- args?: readonly string[]
146
- success: boolean
147
- error: Error | null
148
- },
149
- ]
155
+ 'kubb:hook:end': [ctx: KubbHookEndContext]
150
156
 
151
157
  /**
152
- * Emitted when a new version of Kubb is available.
158
+ * Fires when a new Kubb version is available.
153
159
  */
154
- 'version:new': [currentVersion: string, latestVersion: string]
160
+ 'kubb:version:new': [ctx: KubbVersionNewContext]
155
161
 
156
162
  /**
157
163
  * Informational message event.
158
164
  */
159
- info: [message: string, info?: string]
160
-
165
+ 'kubb:info': [ctx: KubbInfoContext]
161
166
  /**
162
- * Error event. Emitted when an error occurs during code generation.
167
+ * Error event, fired when an error occurs during generation.
163
168
  */
164
- error: [error: Error, meta?: Record<string, unknown>]
165
-
169
+ 'kubb:error': [ctx: KubbErrorContext]
166
170
  /**
167
171
  * Success message event.
168
172
  */
169
- success: [message: string, info?: string]
170
-
173
+ 'kubb:success': [ctx: KubbSuccessContext]
171
174
  /**
172
175
  * Warning message event.
173
176
  */
174
- warn: [message: string, info?: string]
175
-
177
+ 'kubb:warn': [ctx: KubbWarnContext]
176
178
  /**
177
- * Debug event for detailed logging.
178
- * Contains timestamp, log messages, and optional filename.
179
+ * Debug event for detailed logging with timestamp and optional filename.
179
180
  */
180
- debug: [meta: DebugEvent]
181
+ 'kubb:debug': [ctx: KubbDebugContext]
181
182
 
182
183
  /**
183
- * Emitted when file processing starts.
184
- * Contains the list of files to be processed.
184
+ * Fires when file processing starts with the list of files to process.
185
185
  */
186
- 'files:processing:start': [files: Array<KubbFile.ResolvedFile>]
187
-
186
+ 'kubb:files:processing:start': [ctx: KubbFilesProcessingStartContext]
188
187
  /**
189
- * Emitted for each file being processed, providing progress updates.
190
- * Contains processed count, total count, percentage, and file details.
188
+ * Fires for each file with progress updates: processed count, total, percentage, and file details.
191
189
  */
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
-
190
+ 'kubb:file:processing:update': [ctx: KubbFileProcessingUpdateContext]
212
191
  /**
213
- * Emitted when file processing completes.
214
- * Contains the list of processed files.
192
+ * Fires when file processing completes with the list of processed files.
215
193
  */
216
- 'files:processing:end': [files: KubbFile.ResolvedFile[]]
194
+ 'kubb:files:processing:end': [ctx: KubbFilesProcessingEndContext]
217
195
 
218
196
  /**
219
- * Emitted when a plugin starts executing.
197
+ * Fires when a plugin starts execution.
220
198
  */
221
- 'plugin:start': [plugin: Plugin]
222
-
199
+ 'kubb:plugin:start': [ctx: KubbPluginStartContext]
223
200
  /**
224
- * Emitted when a plugin completes execution.
225
- * Duration in ms
201
+ * Fires when a plugin completes execution. Duration measured in milliseconds.
226
202
  */
227
- 'plugin:end': [plugin: Plugin, meta: { duration: number; success: boolean; error?: Error }]
203
+ 'kubb:plugin:end': [ctx: KubbPluginEndContext]
228
204
 
229
205
  /**
230
- * Emitted when plugin hook progress tracking starts.
231
- * 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.
232
207
  */
233
- 'plugins:hook:progress:start': [meta: ProgressStartMeta]
234
-
208
+ 'kubb:plugin:setup': [ctx: KubbPluginSetupContext]
235
209
  /**
236
- * Emitted when plugin hook progress tracking ends.
237
- * Contains the hook name that completed.
210
+ * Fires before the plugin execution loop begins. The adapter has already parsed the source and `inputNode` is available.
238
211
  */
239
- 'plugins:hook:progress:end': [meta: ProgressStopMeta]
240
-
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]
241
218
  /**
242
- * Emitted when a plugin hook starts processing.
243
- * Contains strategy, hook name, plugin, parameters, and output.
219
+ * Fires after all files write to disk.
244
220
  */
245
- 'plugins:hook:processing:start': [meta: ExecutingMeta]
221
+ 'kubb:build:end': [ctx: KubbBuildEndContext]
246
222
 
247
223
  /**
248
- * Emitted when a plugin hook completes processing.
249
- * Contains duration, strategy, hook name, plugin, parameters, and output.
224
+ * Fires for each schema node during AST traversal. Generator listeners respond to this.
250
225
  */
251
- 'plugins:hook:processing:end': [meta: ExecutedMeta]
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
+ * barrelType?: import('./types.ts').BarrelType | 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
+ * barrelType?: import('./types.ts').BarrelType | false
292
+ * }
293
+ * }
294
+ * }
295
+ * }
296
+ * ```
297
+ */
298
+ interface PluginOptionsRegistry {}
299
+ }
252
300
  }