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