@kubb/core 4.11.3 → 4.12.0

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 (68) hide show
  1. package/dist/fs-DhLl4-lT.cjs +177 -0
  2. package/dist/fs-DhLl4-lT.cjs.map +1 -0
  3. package/dist/fs-ph1OdgEr.js +108 -0
  4. package/dist/fs-ph1OdgEr.js.map +1 -0
  5. package/dist/fs.cjs +8 -0
  6. package/dist/fs.d.cts +21 -0
  7. package/dist/fs.d.ts +21 -0
  8. package/dist/fs.js +3 -0
  9. package/dist/{getBarrelFiles-ZIHk_1ln.d.ts → getBarrelFiles-BEWbZEZf.d.ts} +3 -6
  10. package/dist/{getBarrelFiles-8VEWWk9Z.cjs → getBarrelFiles-BcgToi50.cjs} +71 -62
  11. package/dist/getBarrelFiles-BcgToi50.cjs.map +1 -0
  12. package/dist/{getBarrelFiles-DQ0hksqD.js → getBarrelFiles-ClyWjO0d.js} +65 -55
  13. package/dist/getBarrelFiles-ClyWjO0d.js.map +1 -0
  14. package/dist/{getBarrelFiles-B_2WDywH.d.cts → getBarrelFiles-LW3anr-E.d.cts} +3 -6
  15. package/dist/hooks.cjs +1 -1
  16. package/dist/hooks.d.cts +1 -1
  17. package/dist/hooks.d.ts +1 -1
  18. package/dist/index.cjs +68 -212
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +8 -9
  21. package/dist/index.d.ts +8 -9
  22. package/dist/index.js +61 -204
  23. package/dist/index.js.map +1 -1
  24. package/dist/{transformers-B-zCAPV-.cjs → transformers-BqQRkrxy.cjs} +2 -2
  25. package/dist/{transformers-B-zCAPV-.cjs.map → transformers-BqQRkrxy.cjs.map} +1 -1
  26. package/dist/{transformers-CjdbeLAK.js → transformers-C481fXH-.js} +1 -1
  27. package/dist/{transformers-CjdbeLAK.js.map → transformers-C481fXH-.js.map} +1 -1
  28. package/dist/transformers.cjs +2 -2
  29. package/dist/transformers.js +1 -1
  30. package/dist/{types-DgfEZ3IN.d.ts → types-DZARm27h.d.ts} +253 -46
  31. package/dist/{types-CCEy_FVr.d.cts → types-icDNKrIP.d.cts} +253 -46
  32. package/dist/utils.cjs +6 -5
  33. package/dist/utils.d.cts +3 -3
  34. package/dist/utils.d.ts +3 -3
  35. package/dist/utils.js +3 -3
  36. package/package.json +10 -17
  37. package/src/BarrelManager.ts +3 -18
  38. package/src/Kubb.ts +249 -0
  39. package/src/PluginManager.ts +42 -73
  40. package/src/build.ts +38 -138
  41. package/src/defineLogger.ts +7 -0
  42. package/src/index.ts +2 -1
  43. package/src/types.ts +35 -2
  44. package/src/utils/getBarrelFiles.ts +7 -10
  45. package/src/utils/index.ts +1 -0
  46. package/dist/EventEmitter-BwU6Ixxt.cjs +0 -31
  47. package/dist/EventEmitter-BwU6Ixxt.cjs.map +0 -1
  48. package/dist/EventEmitter-DlzW04T3.js +0 -25
  49. package/dist/EventEmitter-DlzW04T3.js.map +0 -1
  50. package/dist/chunk-CbDLau6x.cjs +0 -34
  51. package/dist/getBarrelFiles-8VEWWk9Z.cjs.map +0 -1
  52. package/dist/getBarrelFiles-DQ0hksqD.js.map +0 -1
  53. package/dist/logger-CQn6sdC0.js +0 -1152
  54. package/dist/logger-CQn6sdC0.js.map +0 -1
  55. package/dist/logger-US5g7KdM.cjs +0 -1187
  56. package/dist/logger-US5g7KdM.cjs.map +0 -1
  57. package/dist/logger-mq06Cxxv.d.cts +0 -94
  58. package/dist/logger-o16AyvGp.d.ts +0 -94
  59. package/dist/logger.cjs +0 -6
  60. package/dist/logger.d.cts +0 -2
  61. package/dist/logger.d.ts +0 -2
  62. package/dist/logger.js +0 -3
  63. package/dist/prompt-CWSscQpj.cjs +0 -852
  64. package/dist/prompt-CWSscQpj.cjs.map +0 -1
  65. package/dist/prompt-Dt0jyRBe.js +0 -848
  66. package/dist/prompt-Dt0jyRBe.js.map +0 -1
  67. package/src/logger.ts +0 -236
  68. package/src/utils/ciDetection.ts +0 -40
package/src/Kubb.ts ADDED
@@ -0,0 +1,249 @@
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
+ }
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
35
+ }
36
+
37
+ /**
38
+ * Events emitted during the Kubb code generation lifecycle.
39
+ * These events can be listened to for logging, progress tracking, and custom integrations.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import type { AsyncEventEmitter } from '@kubb/core'
44
+ * import type { KubbEvents } from '@kubb/core'
45
+ *
46
+ * const events: AsyncEventEmitter<KubbEvents> = new AsyncEventEmitter()
47
+ *
48
+ * events.on('lifecycle:start', () => {
49
+ * console.log('Starting Kubb generation')
50
+ * })
51
+ *
52
+ * events.on('plugin:end', (plugin, duration) => {
53
+ * console.log(`Plugin ${plugin.name} completed in ${duration}ms`)
54
+ * })
55
+ * ```
56
+ */
57
+ export interface KubbEvents {
58
+ /**
59
+ * Emitted at the beginning of the Kubb lifecycle, before any code generation starts.
60
+ */
61
+ 'lifecycle:start': [version: string]
62
+
63
+ /**
64
+ * Emitted at the end of the Kubb lifecycle, after all code generation is complete.
65
+ */
66
+ 'lifecycle:end': []
67
+
68
+ /**
69
+ * Emitted when configuration loading starts.
70
+ */
71
+ 'config:start': []
72
+
73
+ /**
74
+ * Emitted when configuration loading is complete.
75
+ */
76
+ 'config:end': [configs: Array<Config>]
77
+
78
+ /**
79
+ * Emitted when code generation phase starts.
80
+ */
81
+ 'generation:start': [config: Config]
82
+
83
+ /**
84
+ * Emitted when code generation phase completes.
85
+ */
86
+ 'generation:end': [Config: Config]
87
+
88
+ /**
89
+ * Emitted with a summary of the generation results.
90
+ * Contains summary lines, title, and success status.
91
+ */
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
+ ]
102
+
103
+ /**
104
+ * Emitted when code formatting starts (e.g., running Biome or Prettier).
105
+ */
106
+ 'format:start': []
107
+
108
+ /**
109
+ * Emitted when code formatting completes.
110
+ */
111
+ 'format:end': []
112
+
113
+ /**
114
+ * Emitted when linting starts.
115
+ */
116
+ 'lint:start': []
117
+
118
+ /**
119
+ * Emitted when linting completes.
120
+ */
121
+ 'lint:end': []
122
+
123
+ /**
124
+ * Emitted when plugin hooks execution starts.
125
+ */
126
+ 'hooks:start': []
127
+
128
+ /**
129
+ * Emitted when plugin hooks execution completes.
130
+ */
131
+ 'hooks:end': []
132
+
133
+ /**
134
+ * Emitted when a single hook execution starts.
135
+ */
136
+ 'hook:start': [command: string]
137
+
138
+ /**
139
+ * Emitted to execute a hook command (e.g., format or lint).
140
+ * The callback should be invoked when the command completes.
141
+ */
142
+ 'hook:execute': [{ command: string | URL; args?: readonly string[] }, cb: () => void]
143
+
144
+ /**
145
+ * Emitted when a single hook execution completes.
146
+ */
147
+ 'hook:end': [command: string]
148
+
149
+ /**
150
+ * Emitted when a new version of Kubb is available.
151
+ */
152
+ 'version:new': [currentVersion: string, latestVersion: string]
153
+
154
+ /**
155
+ * Informational message event.
156
+ */
157
+ info: [message: string, info?: string]
158
+
159
+ /**
160
+ * Error event. Emitted when an error occurs during code generation.
161
+ */
162
+ error: [error: Error, meta?: Record<string, unknown>]
163
+
164
+ /**
165
+ * Success message event.
166
+ */
167
+ success: [message: string, info?: string]
168
+
169
+ /**
170
+ * Warning message event.
171
+ */
172
+ warn: [message: string, info?: string]
173
+
174
+ /**
175
+ * Debug event for detailed logging.
176
+ * Contains timestamp, log messages, and optional filename.
177
+ */
178
+ debug: [meta: DebugEvent]
179
+
180
+ /**
181
+ * Emitted when file processing starts.
182
+ * Contains the list of files to be processed.
183
+ */
184
+ 'files:processing:start': [files: Array<KubbFile.ResolvedFile>]
185
+
186
+ /**
187
+ * Emitted for each file being processed, providing progress updates.
188
+ * Contains processed count, total count, percentage, and file details.
189
+ */
190
+ 'file:processing:update': [
191
+ {
192
+ /** Number of files processed so far */
193
+ processed: number
194
+ /** Total number of files to process */
195
+ total: number
196
+ /** Processing percentage (0-100) */
197
+ percentage: number
198
+ /** Optional source identifier */
199
+ source?: string
200
+ /** The file being processed */
201
+ file: KubbFile.ResolvedFile
202
+ /**
203
+ * Kubb configuration (not present in Fabric).
204
+ * Provides access to the current config during file processing.
205
+ */
206
+ config: Config
207
+ },
208
+ ]
209
+
210
+ /**
211
+ * Emitted when file processing completes.
212
+ * Contains the list of processed files.
213
+ */
214
+ 'files:processing:end': [files: KubbFile.ResolvedFile[]]
215
+
216
+ /**
217
+ * Emitted when a plugin starts executing.
218
+ */
219
+ 'plugin:start': [plugin: Plugin]
220
+
221
+ /**
222
+ * Emitted when a plugin completes execution.
223
+ */
224
+ 'plugin:end': [plugin: Plugin, duration: number]
225
+
226
+ /**
227
+ * Emitted when plugin hook progress tracking starts.
228
+ * Contains the hook name and list of plugins to execute.
229
+ */
230
+ 'plugins:hook:progress:start': [meta: ProgressStartMeta]
231
+
232
+ /**
233
+ * Emitted when plugin hook progress tracking ends.
234
+ * Contains the hook name that completed.
235
+ */
236
+ 'plugins:hook:progress:end': [meta: ProgressStopMeta]
237
+
238
+ /**
239
+ * Emitted when a plugin hook starts processing.
240
+ * Contains strategy, hook name, plugin, parameters, and output.
241
+ */
242
+ 'plugins:hook:processing:start': [meta: ExecutingMeta]
243
+
244
+ /**
245
+ * Emitted when a plugin hook completes processing.
246
+ * Contains duration, strategy, hook name, plugin, parameters, and output.
247
+ */
248
+ 'plugins:hook:processing:end': [meta: ExecutedMeta]
249
+ }
@@ -3,13 +3,13 @@ import { performance } from 'node:perf_hooks'
3
3
  import type { KubbFile } from '@kubb/fabric-core/types'
4
4
  import type { Fabric } from '@kubb/react-fabric'
5
5
  import { ValidationPluginError } from './errors.ts'
6
- import type { Logger } from './logger.ts'
7
6
  import { isPromiseRejectedResult, PromiseManager } from './PromiseManager.ts'
8
7
  import { transformReservedWord } from './transformers/transformReservedWord.ts'
9
8
  import { trim } from './transformers/trim.ts'
10
9
  import type {
11
10
  Config,
12
11
  GetPluginFactoryOptions,
12
+ KubbEvents,
13
13
  Plugin,
14
14
  PluginContext,
15
15
  PluginFactoryOptions,
@@ -21,46 +21,12 @@ import type {
21
21
  ResolvePathParams,
22
22
  UserPlugin,
23
23
  } from './types.ts'
24
- import { EventEmitter } from './utils/EventEmitter.ts'
24
+ import type { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'
25
25
  import { setUniqueName } from './utils/uniqueName.ts'
26
26
 
27
27
  type RequiredPluginLifecycle = Required<PluginLifecycle>
28
28
 
29
- type Strategy = 'hookFirst' | 'hookForPlugin' | 'hookParallel' | 'hookSeq'
30
-
31
- type ExecutingMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
32
- strategy: Strategy
33
- hookName: H
34
- plugin: Plugin
35
- parameters?: unknown[] | undefined
36
- output?: unknown
37
- }
38
-
39
- type ExecutedMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
40
- duration: number
41
- strategy: Strategy
42
- hookName: H
43
- plugin: Plugin
44
- parameters?: unknown[] | undefined
45
- output?: unknown
46
- }
47
-
48
- type ErrorMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
49
- hookName: H
50
- duration: number
51
- strategy: Strategy
52
- parameters?: unknown[] | undefined
53
- plugin: Plugin
54
- }
55
-
56
- type ProgressStartMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
57
- hookName: H
58
- plugins: Array<Plugin>
59
- }
60
-
61
- type ProgressStopMeta<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
62
- hookName: H
63
- }
29
+ export type Strategy = 'hookFirst' | 'hookForPlugin' | 'hookParallel' | 'hookSeq'
64
30
 
65
31
  type ParseResult<H extends PluginLifecycleHooks> = RequiredPluginLifecycle[H]
66
32
 
@@ -73,21 +39,13 @@ type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseRe
73
39
 
74
40
  type Options = {
75
41
  fabric: Fabric
76
- logger: Logger
42
+ events: AsyncEventEmitter<KubbEvents>
77
43
  /**
78
44
  * @default Number.POSITIVE_INFINITY
79
45
  */
80
46
  concurrency?: number
81
47
  }
82
48
 
83
- type Events = {
84
- progress_start: [meta: ProgressStartMeta]
85
- progress_stop: [meta: ProgressStopMeta]
86
- executing: [meta: ExecutingMeta]
87
- executed: [meta: ExecutedMeta]
88
- error: [error: Error, meta: ErrorMeta]
89
- }
90
-
91
49
  type GetFileProps<TOptions = object> = {
92
50
  name: string
93
51
  mode?: KubbFile.Mode
@@ -104,8 +62,6 @@ export function getMode(fileOrFolder: string | undefined | null): KubbFile.Mode
104
62
  }
105
63
 
106
64
  export class PluginManager {
107
- readonly events: EventEmitter<Events> = new EventEmitter()
108
-
109
65
  readonly config: Config
110
66
  readonly options: Options
111
67
 
@@ -128,13 +84,17 @@ export class PluginManager {
128
84
  return this
129
85
  }
130
86
 
87
+ get events() {
88
+ return this.options.events
89
+ }
90
+
131
91
  getContext<TOptions extends PluginFactoryOptions>(plugin: Plugin<TOptions>): PluginContext<TOptions> & Record<string, any> {
132
92
  const plugins = [...this.#plugins]
133
93
  const baseContext = {
134
94
  fabric: this.options.fabric,
135
95
  config: this.config,
136
96
  plugin,
137
- logger: this.options.logger,
97
+ events: this.options.events,
138
98
  pluginManager: this,
139
99
  mode: getMode(path.resolve(this.config.root, this.config.output.path)),
140
100
  addFile: async (...files: Array<KubbFile.File>) => {
@@ -228,13 +188,6 @@ export class PluginManager {
228
188
  return transformReservedWord(name)
229
189
  }
230
190
 
231
- /**
232
- * Instead of calling `pluginManager.events.on` you can use `pluginManager.on`. This one also has better types.
233
- */
234
- on<TEventName extends keyof Events & string>(eventName: TEventName, handler: (...eventArg: Events[TEventName]) => void): void {
235
- this.events.on(eventName, handler as any)
236
- }
237
-
238
191
  /**
239
192
  * Run a specific hookName for plugin x.
240
193
  */
@@ -249,7 +202,7 @@ export class PluginManager {
249
202
  }): Promise<Array<ReturnType<ParseResult<H>> | null>> {
250
203
  const plugins = this.getPluginsByKey(hookName, pluginKey)
251
204
 
252
- this.events.emit('progress_start', {
205
+ this.events.emit('plugins:hook:progress:start', {
253
206
  hookName,
254
207
  plugins,
255
208
  })
@@ -269,7 +222,7 @@ export class PluginManager {
269
222
  }
270
223
  }
271
224
 
272
- this.events.emit('progress_stop', { hookName })
225
+ this.events.emit('plugins:hook:progress:end', { hookName })
273
226
 
274
227
  return items
275
228
  }
@@ -318,7 +271,7 @@ export class PluginManager {
318
271
  return skipped ? skipped.has(plugin) : true
319
272
  })
320
273
 
321
- this.events.emit('progress_start', { hookName, plugins })
274
+ this.events.emit('plugins:hook:progress:start', { hookName, plugins })
322
275
 
323
276
  const promises = plugins.map((plugin) => {
324
277
  return async () => {
@@ -338,7 +291,7 @@ export class PluginManager {
338
291
 
339
292
  const result = await this.#promiseManager.run('first', promises)
340
293
 
341
- this.events.emit('progress_stop', { hookName })
294
+ this.events.emit('plugins:hook:progress:end', { hookName })
342
295
 
343
296
  return result
344
297
  }
@@ -390,7 +343,7 @@ export class PluginManager {
390
343
  parameters?: Parameters<RequiredPluginLifecycle[H]> | undefined
391
344
  }): Promise<Awaited<TOuput>[]> {
392
345
  const plugins = this.#getSortedPlugins(hookName)
393
- this.events.emit('progress_start', { hookName, plugins })
346
+ this.events.emit('plugins:hook:progress:start', { hookName, plugins })
394
347
 
395
348
  const promises = plugins.map((plugin) => {
396
349
  return () =>
@@ -411,12 +364,18 @@ export class PluginManager {
411
364
  const plugin = this.#getSortedPlugins(hookName)[index]
412
365
 
413
366
  if (plugin) {
414
- this.events.emit('error', result.reason, { plugin, hookName, strategy: 'hookParallel', duration: 0, parameters })
367
+ this.events.emit('error', result.reason, {
368
+ plugin,
369
+ hookName,
370
+ strategy: 'hookParallel',
371
+ duration: 0,
372
+ parameters,
373
+ })
415
374
  }
416
375
  }
417
376
  })
418
377
 
419
- this.events.emit('progress_stop', { hookName })
378
+ this.events.emit('plugins:hook:progress:end', { hookName })
420
379
 
421
380
  return results.filter((result) => result.status === 'fulfilled').map((result) => (result as PromiseFulfilledResult<Awaited<TOuput>>).value)
422
381
  }
@@ -426,7 +385,7 @@ export class PluginManager {
426
385
  */
427
386
  async hookSeq<H extends PluginLifecycleHooks>({ hookName, parameters }: { hookName: H; parameters?: PluginParameter<H> }): Promise<void> {
428
387
  const plugins = this.#getSortedPlugins(hookName)
429
- this.events.emit('progress_start', { hookName, plugins })
388
+ this.events.emit('plugins:hook:progress:start', { hookName, plugins })
430
389
 
431
390
  const promises = plugins.map((plugin) => {
432
391
  return () =>
@@ -440,7 +399,7 @@ export class PluginManager {
440
399
 
441
400
  await this.#promiseManager.run('seq', promises)
442
401
 
443
- this.events.emit('progress_stop', { hookName })
402
+ this.events.emit('plugins:hook:progress:end', { hookName })
444
403
  }
445
404
 
446
405
  #getSortedPlugins(hookName?: keyof PluginLifecycle): Array<Plugin> {
@@ -541,7 +500,7 @@ export class PluginManager {
541
500
  return null
542
501
  }
543
502
 
544
- this.events.emit('executing', {
503
+ this.events.emit('plugins:hook:processing:start', {
545
504
  strategy,
546
505
  hookName,
547
506
  parameters,
@@ -558,7 +517,7 @@ export class PluginManager {
558
517
 
559
518
  output = result
560
519
 
561
- this.events.emit('executed', {
520
+ this.events.emit('plugins:hook:processing:end', {
562
521
  duration: Math.round(performance.now() - startTime),
563
522
  parameters,
564
523
  output,
@@ -572,7 +531,7 @@ export class PluginManager {
572
531
 
573
532
  output = hook
574
533
 
575
- this.events.emit('executed', {
534
+ this.events.emit('plugins:hook:processing:end', {
576
535
  duration: Math.round(performance.now() - startTime),
577
536
  parameters,
578
537
  output,
@@ -583,7 +542,12 @@ export class PluginManager {
583
542
 
584
543
  return hook
585
544
  } catch (e) {
586
- this.events.emit('error', e as Error, { plugin, hookName, strategy, duration: Math.round(performance.now() - startTime) })
545
+ this.events.emit('error', e as Error, {
546
+ plugin,
547
+ hookName,
548
+ strategy,
549
+ duration: Math.round(performance.now() - startTime),
550
+ })
587
551
 
588
552
  return null
589
553
  }
@@ -617,7 +581,7 @@ export class PluginManager {
617
581
  return null
618
582
  }
619
583
 
620
- this.events.emit('executing', {
584
+ this.events.emit('plugins:hook:processing:start', {
621
585
  strategy,
622
586
  hookName,
623
587
  parameters,
@@ -633,7 +597,7 @@ export class PluginManager {
633
597
 
634
598
  output = fn
635
599
 
636
- this.events.emit('executed', {
600
+ this.events.emit('plugins:hook:processing:end', {
637
601
  duration: Math.round(performance.now() - startTime),
638
602
  parameters,
639
603
  output,
@@ -647,7 +611,7 @@ export class PluginManager {
647
611
 
648
612
  output = hook
649
613
 
650
- this.events.emit('executed', {
614
+ this.events.emit('plugins:hook:processing:end', {
651
615
  duration: Math.round(performance.now() - startTime),
652
616
  parameters,
653
617
  output,
@@ -658,7 +622,12 @@ export class PluginManager {
658
622
 
659
623
  return hook
660
624
  } catch (e) {
661
- this.events.emit('error', e as Error, { plugin, hookName, strategy, duration: Math.round(performance.now() - startTime) })
625
+ this.events.emit('error', e as Error, {
626
+ plugin,
627
+ hookName,
628
+ strategy,
629
+ duration: Math.round(performance.now() - startTime),
630
+ })
662
631
 
663
632
  return null
664
633
  }