@kubb/core 5.0.0-alpha.4 → 5.0.0-alpha.41

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