@kubb/core 5.0.0-beta.40 → 5.0.0-beta.42

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.
package/README.md CHANGED
@@ -5,9 +5,9 @@
5
5
 
6
6
  [![npm version][npm-version-src]][npm-version-href]
7
7
  [![npm downloads][npm-downloads-src]][npm-downloads-href]
8
- [![Coverage][coverage-src]][coverage-href]
8
+ [![Stars][stars-src]][stars-href]
9
9
  [![License][license-src]][license-href]
10
- [![Sponsors][sponsors-src]][sponsors-href]
10
+ [![Node][node-src]][node-href]
11
11
 
12
12
  <h4>
13
13
  <a href="https://kubb.dev" target="_blank">Documentation</a>
@@ -26,7 +26,7 @@
26
26
 
27
27
  Core engine for Kubb's plugin-based code generation system. Provides the plugin driver, file manager, `defineConfig`, `definePlugin`, `defineMiddleware`, and the build orchestration layer used by every Kubb plugin.
28
28
 
29
- > **Note:** Most users should install the [`kubb`](https://npmjs.com/package/kubb) meta-package instead of `@kubb/core` directly. Install `@kubb/core` only when building custom plugins or extending the Kubb internals.
29
+ > **Note:** Most users should install the [`kubb`](https://npmx.dev/package/kubb) meta-package instead of `@kubb/core` directly. Install `@kubb/core` only when building custom plugins or extending the Kubb internals.
30
30
 
31
31
  ## Installation
32
32
 
@@ -57,13 +57,13 @@ Kubb is an open source project, and its development is funded entirely by sponso
57
57
 
58
58
  <!-- Badges -->
59
59
 
60
- [npm-version-src]: https://img.shields.io/npm/v/@kubb/core?flat&colorA=18181B&colorB=f58517
61
- [npm-version-href]: https://npmjs.com/package/@kubb/core
62
- [npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/core?flat&colorA=18181B&colorB=f58517
63
- [npm-downloads-href]: https://npmjs.com/package/@kubb/core
64
- [license-src]: https://img.shields.io/npm/l/%40kubb%2Fcore?flat&colorA=18181B&colorB=f58517
65
- [license-href]: https://github.com/kubb-labs/kubb/blob/main/licenses/LICENSE-MIT
66
- [coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
67
- [coverage-href]: https://www.npmjs.com/package/@kubb/core
68
- [sponsors-src]: https://img.shields.io/github/sponsors/stijnvanhulle?style=flat&colorA=18181B&colorB=f58517
69
- [sponsors-href]: https://github.com/sponsors/stijnvanhulle/
60
+ [npm-version-src]: https://shieldcn.dev/npm/v/@kubb/core.svg?variant=secondary&size=xs&theme=zinc&mode=dark
61
+ [npm-version-href]: https://npmx.dev/package/@kubb/core
62
+ [npm-downloads-src]: https://shieldcn.dev/npm/dm/@kubb/core.svg?variant=secondary&size=xs&theme=zinc&mode=dark
63
+ [npm-downloads-href]: https://npmx.dev/package/@kubb/core
64
+ [stars-src]: https://shieldcn.dev/github/stars/kubb-labs/kubb.svg?variant=secondary&size=xs&theme=zinc&mode=dark
65
+ [stars-href]: https://github.com/kubb-labs/kubb
66
+ [license-src]: https://shieldcn.dev/npm/license/@kubb/core.svg?variant=secondary&size=xs&theme=zinc
67
+ [license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
68
+ [node-src]: https://shieldcn.dev/npm/node/@kubb/core.svg?variant=secondary&size=xs&theme=zinc&mode=dark
69
+ [node-href]: https://npmx.dev/package/@kubb/core
@@ -238,6 +238,83 @@ type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) =
238
238
  */
239
239
  declare function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T>;
240
240
  //#endregion
241
+ //#region src/createCache.d.ts
242
+ /**
243
+ * A snapshot of a completed build: the final rendered source of every generated
244
+ * file, keyed by its path relative to the output root. Restoring a snapshot writes
245
+ * those sources straight to storage, skipping generation entirely.
246
+ *
247
+ * Paths are relative, not absolute, so the snapshot never depends on where the
248
+ * project lives on disk.
249
+ */
250
+ type CachedSnapshot = {
251
+ /**
252
+ * Final source per output file, keyed by the path relative to
253
+ * `resolve(config.root, config.output.path)`.
254
+ */
255
+ files: Record<string, string>;
256
+ };
257
+ /**
258
+ * Backend that stores build snapshots for incremental ("hot") rebuilds. When the
259
+ * input fingerprint matches a stored key, Kubb restores the snapshot instead of
260
+ * regenerating. Kubb ships with `fsCache` (local disk). Implement this interface to
261
+ * back the cache with any other store.
262
+ *
263
+ * @see {@link createCache} to build a custom backend.
264
+ */
265
+ type Cache = {
266
+ /**
267
+ * Identifier used in logs and diagnostics (`'fs'`, `'memory'`).
268
+ */
269
+ readonly name: string;
270
+ /**
271
+ * Returns the snapshot stored under `key`, or `null` on a miss. A backend never
272
+ * throws on a miss or a transient failure. It returns `null` so the build falls
273
+ * through to regeneration.
274
+ */
275
+ restore(params: {
276
+ key: string;
277
+ }): Promise<CachedSnapshot | null>;
278
+ /**
279
+ * Stores `snapshot` under `key`. Only called after a successful build with no
280
+ * error diagnostics.
281
+ */
282
+ persist(params: {
283
+ key: string;
284
+ snapshot: CachedSnapshot;
285
+ }): Promise<void>;
286
+ /**
287
+ * Optional teardown called after the build. Use to flush buffers or close
288
+ * connections.
289
+ */
290
+ dispose?(): Promise<void>;
291
+ };
292
+ /**
293
+ * Defines a custom cache backend. The builder receives user options and returns a
294
+ * {@link Cache}. Reach for this when the filesystem backend doesn't fit, for
295
+ * example to store snapshots in Redis or a database.
296
+ *
297
+ * @example In-memory cache (the built-in implementation)
298
+ * ```ts
299
+ * import { createCache } from '@kubb/core'
300
+ *
301
+ * export const memoryCache = createCache(() => {
302
+ * const store = new Map<string, CachedSnapshot>()
303
+ *
304
+ * return {
305
+ * name: 'memory',
306
+ * async restore({ key }) {
307
+ * return store.get(key) ?? null
308
+ * },
309
+ * async persist({ key, snapshot }) {
310
+ * store.set(key, snapshot)
311
+ * },
312
+ * }
313
+ * })
314
+ * ```
315
+ */
316
+ declare function createCache<TOptions = Record<string, never>>(build: (options: TOptions) => Cache): (options?: TOptions) => Cache;
317
+ //#endregion
241
318
  //#region src/constants.d.ts
242
319
  /**
243
320
  * Stable codes Kubb attaches to a `Diagnostic`. Each maps to a known failure mode
@@ -493,6 +570,47 @@ type RendererFactory<TElement = unknown> = () => Renderer<TElement>;
493
570
  */
494
571
  declare function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement>;
495
572
  //#endregion
573
+ //#region src/caches/fsCache.d.ts
574
+ /**
575
+ * Options for {@link fsCache}.
576
+ */
577
+ type FsCacheOptions = {
578
+ /**
579
+ * Directory that holds the cache. Resolved against `process.cwd()` when relative.
580
+ *
581
+ * @default 'node_modules/.cache/kubb'
582
+ */
583
+ dir?: string;
584
+ /**
585
+ * Maximum number of build snapshots to keep. The least-recently-used entries are
586
+ * evicted once the cache grows past it.
587
+ *
588
+ * @default 50
589
+ */
590
+ maxEntries?: number;
591
+ /**
592
+ * Days a snapshot may go untouched before it is evicted.
593
+ *
594
+ * @default 7
595
+ */
596
+ ttlDays?: number;
597
+ };
598
+ /**
599
+ * Local filesystem cache. Stores each build snapshot as content blobs plus an index,
600
+ * tracked by a manifest under `node_modules/.cache/kubb/` (the Nx and Vitest
601
+ * convention). Least-recently-used and expired entries are pruned on every persist.
602
+ *
603
+ * @example
604
+ * ```ts
605
+ * import { fsCache } from '@kubb/core'
606
+ *
607
+ * export default defineConfig({
608
+ * cache: fsCache(),
609
+ * })
610
+ * ```
611
+ */
612
+ declare const fsCache: (options?: FsCacheOptions | undefined) => Cache;
613
+ //#endregion
496
614
  //#region src/defineLogger.d.ts
497
615
  /**
498
616
  * Numeric log-level thresholds used internally to compare verbosity.
@@ -524,27 +642,25 @@ type LoggerContext = AsyncEventEmitter<KubbHooks>;
524
642
  /**
525
643
  * Logger contract. A logger receives the build's event emitter and subscribes
526
644
  * to whichever lifecycle events it wants to forward to its destination
527
- * (console, file, remote sink).
645
+ * (console, file, remote service).
528
646
  */
529
- type Logger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = {
647
+ type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
530
648
  /**
531
649
  * Display name used in diagnostics.
532
650
  */
533
651
  name: string;
534
652
  /**
535
- * Called once per build with the shared event emitter. Subscribe to events
536
- * here. The return value (if any) is forwarded to whoever installed the
537
- * logger, which is handy for sink factories.
653
+ * Called once per build with the shared event emitter. Subscribe to the
654
+ * lifecycle events the logger wants to forward to its destination.
538
655
  */
539
- install: (context: LoggerContext, options?: TOptions) => TInstallReturn | Promise<TInstallReturn>;
656
+ install: (context: LoggerContext, options?: TOptions) => void | Promise<void>;
540
657
  };
541
- type UserLogger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn = void> = Logger<TOptions, TInstallReturn>;
658
+ type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Logger<TOptions>;
542
659
  /**
543
- * Defines a typed logger. Use the second type parameter to declare a return
544
- * value from `install`, which is handy when the logger exposes a sink factory
545
- * or cleanup callback to the caller.
660
+ * Defines a typed logger. The `install` method subscribes to lifecycle events
661
+ * on the shared emitter and forwards them to the logger's destination.
546
662
  *
547
- * @example Basic logger
663
+ * @example
548
664
  * ```ts
549
665
  * import { defineLogger } from '@kubb/core'
550
666
  *
@@ -556,22 +672,8 @@ type UserLogger<TOptions extends LoggerOptions = LoggerOptions, TInstallReturn =
556
672
  * },
557
673
  * })
558
674
  * ```
559
- *
560
- * @example Logger that returns a hook sink factory
561
- * ```ts
562
- * import { defineLogger, type LoggerOptions } from '@kubb/core'
563
- * import type { HookSinkFactory } from './sinks'
564
- *
565
- * export const myLogger = defineLogger<LoggerOptions, HookSinkFactory>({
566
- * name: 'my-logger',
567
- * install(context) {
568
- * // … register event handlers …
569
- * return () => ({ onStdout: console.log })
570
- * },
571
- * })
572
- * ```
573
675
  */
574
- declare function defineLogger<Options extends LoggerOptions = LoggerOptions, TInstallReturn = void>(logger: UserLogger<Options, TInstallReturn>): Logger<Options, TInstallReturn>;
676
+ declare function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options>;
575
677
  //#endregion
576
678
  //#region src/createReporter.d.ts
577
679
  /**
@@ -695,7 +797,7 @@ type Parser<TMeta extends object = object, TNode = unknown> = {
695
797
  */
696
798
  extNames: Array<FileNode['extname']> | undefined;
697
799
  /**
698
- * Serialise the file's AST into source code.
800
+ * Serialize the file's AST into source code.
699
801
  */
700
802
  parse(file: FileNode<TMeta>, options?: PrintOptions): string;
701
803
  /**
@@ -1992,6 +2094,26 @@ type Config<TInput = Input> = {
1992
2094
  * @see {@link Storage} interface for implementing custom backends.
1993
2095
  */
1994
2096
  storage: Storage;
2097
+ /**
2098
+ * Incremental build cache. Kubb fingerprints the inputs (spec content, config, plugin options,
2099
+ * versions) and, on an unchanged "hot" run, restores the previously generated output instead of
2100
+ * regenerating it. Same idea as Nx's computation cache.
2101
+ *
2102
+ * `defineConfig` enables `fsCache()` (local disk under `node_modules/.cache/kubb`) by default.
2103
+ * Pass another backend to change where snapshots live, or `false` to turn caching off. A bare
2104
+ * `createKubb` leaves it off unless a cache is provided.
2105
+ *
2106
+ * @example
2107
+ * ```ts
2108
+ * import { fsCache } from '@kubb/core'
2109
+ *
2110
+ * cache: fsCache({ dir: '.kubb-cache' })
2111
+ * cache: false
2112
+ * ```
2113
+ *
2114
+ * @see {@link Cache} interface for implementing custom backends.
2115
+ */
2116
+ cache?: Cache;
1995
2117
  /**
1996
2118
  * Plugins that run during the build to generate code and transform the AST. Each one processes
1997
2119
  * the adapter's AST and can emit files for a different target (TypeScript, Zod, Faker). A plugin
@@ -2094,7 +2216,12 @@ type Config<TInput = Input> = {
2094
2216
  * })
2095
2217
  * ```
2096
2218
  */
2097
- type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter' | 'storage' | 'reporters'> & {
2219
+ type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter' | 'storage' | 'reporters' | 'cache'> & {
2220
+ /**
2221
+ * Incremental build cache. Defaults to `fsCache()` (local disk). Pass another {@link Cache}
2222
+ * backend, or `false` to turn caching off.
2223
+ */
2224
+ cache?: Cache | false;
2098
2225
  /**
2099
2226
  * Project root directory, absolute or relative to the config file location.
2100
2227
  * @default process.cwd()
@@ -2218,6 +2345,7 @@ interface KubbHooks {
2218
2345
  'kubb:hooks:start': [];
2219
2346
  'kubb:hooks:end': [];
2220
2347
  'kubb:hook:start': [ctx: KubbHookStartContext];
2348
+ 'kubb:hook:line': [ctx: KubbHookLineContext];
2221
2349
  'kubb:hook:end': [ctx: KubbHookEndContext];
2222
2350
  'kubb:info': [ctx: KubbInfoContext];
2223
2351
  'kubb:error': [ctx: KubbErrorContext];
@@ -2398,7 +2526,7 @@ type KubbDiagnosticContext = {
2398
2526
  };
2399
2527
  type KubbFilesProcessingStartContext = {
2400
2528
  /**
2401
- * Files about to be serialised and written.
2529
+ * Files about to be serialized and written.
2402
2530
  */
2403
2531
  files: Array<FileNode>;
2404
2532
  };
@@ -2416,7 +2544,7 @@ type KubbFileProcessingUpdate = {
2416
2544
  */
2417
2545
  percentage: number;
2418
2546
  /**
2419
- * Serialised file content, or `undefined` when the file produced no output.
2547
+ * Serialized file content, or `undefined` when the file produced no output.
2420
2548
  */
2421
2549
  source?: string;
2422
2550
  /**
@@ -2436,7 +2564,7 @@ type KubbFilesProcessingUpdateContext = {
2436
2564
  };
2437
2565
  type KubbFilesProcessingEndContext = {
2438
2566
  /**
2439
- * All files that were serialised in this batch.
2567
+ * All files that were serialized in this batch.
2440
2568
  */
2441
2569
  files: Array<FileNode>;
2442
2570
  };
@@ -2454,6 +2582,20 @@ type KubbHookStartContext = {
2454
2582
  */
2455
2583
  args?: ReadonlyArray<string>;
2456
2584
  };
2585
+ /**
2586
+ * Emitted for each line streamed from a hook's stdout while it runs.
2587
+ * A logger correlates the line to its active UI element via `id`.
2588
+ */
2589
+ type KubbHookLineContext = {
2590
+ /**
2591
+ * Identifier matching the corresponding `kubb:hook:start` event.
2592
+ */
2593
+ id: string;
2594
+ /**
2595
+ * A single streamed stdout line, without its trailing newline.
2596
+ */
2597
+ line: string;
2598
+ };
2457
2599
  type KubbHookEndContext = {
2458
2600
  /**
2459
2601
  * Optional identifier matching the corresponding `kubb:hook:start` event.
@@ -2475,6 +2617,14 @@ type KubbHookEndContext = {
2475
2617
  * Error thrown by the command, or `null` on success.
2476
2618
  */
2477
2619
  error: Error | null;
2620
+ /**
2621
+ * Captured stdout from the process, populated when it exits non-zero.
2622
+ */
2623
+ stdout?: string;
2624
+ /**
2625
+ * Captured stderr from the process, populated when it exits non-zero.
2626
+ */
2627
+ stderr?: string;
2478
2628
  };
2479
2629
  /**
2480
2630
  * CLI options derived from command-line flags.
@@ -2959,5 +3109,5 @@ declare class Diagnostics {
2959
3109
  static formatLines(diagnostic: Diagnostic): Array<string>;
2960
3110
  }
2961
3111
  //#endregion
2962
- export { Override as $, KubbHookEndContext as A, logLevel as At, createKubb as B, AsyncEventEmitter as Bt, KubbErrorContext as C, createReporter as Ct, KubbFilesProcessingUpdateContext as D, LoggerOptions as Dt, KubbFilesProcessingStartContext as E, LoggerContext as Et, KubbPluginsEndContext as F, createStorage as Ft, Exclude$1 as G, GeneratorContext as H, KubbSuccessContext as I, Adapter as It, KubbPluginEndContext as J, Group as K, KubbWarnContext as L, AdapterFactoryOptions as Lt, KubbHooks as M, RendererFactory as Mt, KubbInfoContext as N, createRenderer as Nt, KubbGenerationEndContext as O, UserLogger as Ot, KubbLifecycleStartContext as P, Storage as Pt, Output as Q, PossibleConfig as R, AdapterSource as Rt, KubbDiagnosticContext as S, UserReporter as St, KubbFilesProcessingEndContext as T, Logger as Tt, defineGenerator as U, Generator$1 as V, KubbDriver as W, KubbPluginStartContext as X, KubbPluginSetupContext as Y, NormalizedPlugin as Z, InputPath as _, defineParser as _t, DiagnosticLocation as a, ResolveBannerFile as at, KubbBuildStartContext as b, ReporterContext as bt, PerformanceDiagnostic as c, ResolverContext as ct, SerializedDiagnostic as d, defineResolver as dt, Plugin as et, UpdateDiagnostic as f, Middleware as ft, InputData as g, Parser as gt, Config as h, ParsedFile as ht, DiagnosticKind as i, ResolveBannerContext as it, KubbHookStartContext as j, Renderer as jt, KubbGenerationStartContext as k, defineLogger as kt, ProblemCode as l, ResolverFileParams as lt, CLIOptions as m, FileProcessorHooks as mt, DiagnosticByCode as n, definePlugin as nt, DiagnosticSeverity as o, ResolveOptionsContext as ot, BuildOutput as p, defineMiddleware as pt, Include as q, DiagnosticDoc as r, BannerMeta as rt, Diagnostics as s, Resolver as st, Diagnostic as t, PluginFactoryOptions as tt, ProblemDiagnostic as u, ResolverPathParams as ut, Kubb$1 as v, GenerationResult as vt, KubbFileProcessingUpdate as w, selectReporters as wt, KubbConfigEndContext as x, ReporterName as xt, KubbBuildEndContext as y, Reporter as yt, UserConfig as z, createAdapter as zt };
2963
- //# sourceMappingURL=diagnostics-DhfW8YpT.d.ts.map
3112
+ export { Output as $, KubbHookEndContext as A, defineLogger as At, UserConfig as B, CachedSnapshot as Bt, KubbErrorContext as C, UserReporter as Ct, KubbFilesProcessingUpdateContext as D, LoggerContext as Dt, KubbFilesProcessingStartContext as E, Logger as Et, KubbLifecycleStartContext as F, RendererFactory as Ft, KubbDriver as G, createAdapter as Gt, Generator$1 as H, Adapter as Ht, KubbPluginsEndContext as I, createRenderer as It, Include as J, Exclude$1 as K, AsyncEventEmitter as Kt, KubbSuccessContext as L, Storage as Lt, KubbHookStartContext as M, FsCacheOptions as Mt, KubbHooks as N, fsCache as Nt, KubbGenerationEndContext as O, LoggerOptions as Ot, KubbInfoContext as P, Renderer as Pt, NormalizedPlugin as Q, KubbWarnContext as R, createStorage as Rt, KubbDiagnosticContext as S, ReporterName as St, KubbFilesProcessingEndContext as T, selectReporters as Tt, GeneratorContext as U, AdapterFactoryOptions as Ut, createKubb as V, createCache as Vt, defineGenerator as W, AdapterSource as Wt, KubbPluginSetupContext as X, KubbPluginEndContext as Y, KubbPluginStartContext as Z, InputPath as _, Parser as _t, DiagnosticLocation as a, ResolveBannerContext as at, KubbBuildStartContext as b, Reporter as bt, PerformanceDiagnostic as c, Resolver as ct, SerializedDiagnostic as d, ResolverPathParams as dt, Override as et, UpdateDiagnostic as f, defineResolver as ft, InputData as g, ParsedFile as gt, Config as h, FileProcessorHooks as ht, DiagnosticKind as i, BannerMeta as it, KubbHookLineContext as j, logLevel as jt, KubbGenerationStartContext as k, UserLogger as kt, ProblemCode as l, ResolverContext as lt, CLIOptions as m, defineMiddleware as mt, DiagnosticByCode as n, PluginFactoryOptions as nt, DiagnosticSeverity as o, ResolveBannerFile as ot, BuildOutput as p, Middleware as pt, Group as q, DiagnosticDoc as r, definePlugin as rt, Diagnostics as s, ResolveOptionsContext as st, Diagnostic as t, Plugin as tt, ProblemDiagnostic as u, ResolverFileParams as ut, Kubb$1 as v, defineParser as vt, KubbFileProcessingUpdate as w, createReporter as wt, KubbConfigEndContext as x, ReporterContext as xt, KubbBuildEndContext as y, GenerationResult as yt, PossibleConfig as z, Cache as zt };
3113
+ //# sourceMappingURL=diagnostics-DZGgDzSv.d.ts.map