@kubb/core 5.0.0-alpha.34 → 5.0.0-alpha.36

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 (45) hide show
  1. package/dist/PluginDriver-B_65W4fv.js +1677 -0
  2. package/dist/PluginDriver-B_65W4fv.js.map +1 -0
  3. package/dist/{PluginDriver-BBi_41VF.d.ts → PluginDriver-C9iBgYbk.d.ts} +743 -376
  4. package/dist/PluginDriver-CCdkwR14.cjs +1806 -0
  5. package/dist/PluginDriver-CCdkwR14.cjs.map +1 -0
  6. package/dist/hooks.d.ts +1 -1
  7. package/dist/index.cjs +272 -1666
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +62 -141
  10. package/dist/index.js +231 -1623
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +165 -0
  13. package/dist/mocks.cjs.map +1 -0
  14. package/dist/mocks.d.ts +74 -0
  15. package/dist/mocks.js +159 -0
  16. package/dist/mocks.js.map +1 -0
  17. package/package.json +11 -5
  18. package/src/FileManager.ts +1 -1
  19. package/src/FileProcessor.ts +1 -1
  20. package/src/Kubb.ts +145 -38
  21. package/src/PluginDriver.ts +318 -40
  22. package/src/constants.ts +1 -1
  23. package/src/{build.ts → createKubb.ts} +180 -122
  24. package/src/createPlugin.ts +1 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/defineGenerator.ts +57 -84
  27. package/src/defineLogger.ts +2 -2
  28. package/src/defineParser.ts +3 -2
  29. package/src/definePlugin.ts +95 -0
  30. package/src/defineResolver.ts +1 -1
  31. package/src/devtools.ts +1 -1
  32. package/src/index.ts +7 -6
  33. package/src/mocks.ts +234 -0
  34. package/src/renderNode.ts +35 -0
  35. package/src/types.ts +275 -210
  36. package/src/utils/TreeNode.ts +1 -1
  37. package/src/utils/getBarrelFiles.ts +3 -3
  38. package/src/utils/getFunctionParams.ts +14 -7
  39. package/src/utils/isInputPath.ts +2 -2
  40. package/src/utils/packageJSON.ts +2 -3
  41. package/src/defineConfig.ts +0 -51
  42. package/src/definePresets.ts +0 -16
  43. package/src/renderNode.tsx +0 -28
  44. package/src/utils/getConfigs.ts +0 -16
  45. package/src/utils/getPreset.ts +0 -78
package/dist/index.js CHANGED
@@ -1,17 +1,14 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
1
+ import "./chunk--u3MIqq1.js";
2
+ import { S as camelCase, _ as DEFAULT_STUDIO_URL, a as buildDefaultBanner, b as logLevel, c as defaultResolveFooter, d as defineResolver, f as definePlugin, g as DEFAULT_EXTENSION, h as DEFAULT_BANNER, i as FileManager, l as defaultResolveOptions, m as BARREL_FILENAME, n as getMode, o as defaultResolveBanner, p as pLimit, r as applyHookResult, s as defaultResolveFile, t as PluginDriver, u as defaultResolvePath, v as formatters, x as isValidVarName, y as linters } from "./PluginDriver-B_65W4fv.js";
2
3
  import { EventEmitter } from "node:events";
3
- import { readFileSync } from "node:fs";
4
+ import { existsSync, readFileSync } from "node:fs";
4
5
  import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
5
- import path, { basename, dirname, extname, join, posix, resolve } from "node:path";
6
- import { composeTransformers, composeTransformers as composeTransformers$1, createExport, createFile, createSource, definePrinter, extractStringsFromNodes, isOperationNode, isSchemaNode, transform, walk } from "@kubb/ast";
7
- import { performance } from "node:perf_hooks";
8
- import { deflateSync } from "fflate";
6
+ import path, { dirname, join, posix, resolve } from "node:path";
7
+ import * as ast from "@kubb/ast";
8
+ import { composeTransformers, createExport, createFile, createSource, definePrinter, extractStringsFromNodes, transform, walk } from "@kubb/ast";
9
9
  import { x } from "tinyexec";
10
- import { createRenderer } from "@kubb/renderer-jsx";
11
- import { Fragment, jsx } from "@kubb/renderer-jsx/jsx-runtime";
12
10
  import { version } from "node:process";
13
11
  import { sortBy } from "remeda";
14
- import * as pkg from "empathic/package";
15
12
  import { coerce, satisfies } from "semver";
16
13
  //#region ../../internals/utils/src/errors.ts
17
14
  /**
@@ -129,6 +126,18 @@ var AsyncEventEmitter = class {
129
126
  this.#emitter.off(eventName, handler);
130
127
  }
131
128
  /**
129
+ * Returns the number of listeners registered for `eventName`.
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * emitter.on('build', handler)
134
+ * emitter.listenerCount('build') // 1
135
+ * ```
136
+ */
137
+ listenerCount(eventName) {
138
+ return this.#emitter.listenerCount(eventName);
139
+ }
140
+ /**
132
141
  * Removes all listeners from every event channel.
133
142
  *
134
143
  * @example
@@ -141,64 +150,6 @@ var AsyncEventEmitter = class {
141
150
  }
142
151
  };
143
152
  //#endregion
144
- //#region ../../internals/utils/src/casing.ts
145
- /**
146
- * Shared implementation for camelCase and PascalCase conversion.
147
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
148
- * and capitalizes each word according to `pascal`.
149
- *
150
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
151
- */
152
- function toCamelOrPascal(text, pascal) {
153
- return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
154
- if (word.length > 1 && word === word.toUpperCase()) return word;
155
- if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
156
- return word.charAt(0).toUpperCase() + word.slice(1);
157
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
158
- }
159
- /**
160
- * Splits `text` on `.` and applies `transformPart` to each segment.
161
- * The last segment receives `isLast = true`, all earlier segments receive `false`.
162
- * Segments are joined with `/` to form a file path.
163
- *
164
- * Only splits on dots followed by a letter so that version numbers
165
- * embedded in operationIds (e.g. `v2025.0`) are kept intact.
166
- */
167
- function applyToFileParts(text, transformPart) {
168
- const parts = text.split(/\.(?=[a-zA-Z])/);
169
- return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
170
- }
171
- /**
172
- * Converts `text` to camelCase.
173
- * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
174
- *
175
- * @example
176
- * camelCase('hello-world') // 'helloWorld'
177
- * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
178
- */
179
- function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
180
- if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
181
- prefix,
182
- suffix
183
- } : {}));
184
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
185
- }
186
- /**
187
- * Converts `text` to PascalCase.
188
- * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
189
- *
190
- * @example
191
- * pascalCase('hello-world') // 'HelloWorld'
192
- * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
193
- */
194
- function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
195
- if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
196
- prefix,
197
- suffix
198
- }) : camelCase(part));
199
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
200
- }
201
- //#endregion
202
153
  //#region ../../internals/utils/src/time.ts
203
154
  /**
204
155
  * Calculates elapsed time in milliseconds from a high-resolution `process.hrtime` start time.
@@ -234,6 +185,26 @@ function formatMs(ms) {
234
185
  //#endregion
235
186
  //#region ../../internals/utils/src/fs.ts
236
187
  /**
188
+ * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and
189
+ * returns the absolute path of the nearest `package.json`, or `null` when none
190
+ * is found before reaching the filesystem root.
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'
195
+ * ```
196
+ */
197
+ function findPackageJSON(cwd) {
198
+ let dir = cwd ? resolve(cwd) : process.cwd();
199
+ while (true) {
200
+ const pkgPath = join(dir, "package.json");
201
+ if (existsSync(pkgPath)) return pkgPath;
202
+ const parent = dirname(dir);
203
+ if (parent === dir) return null;
204
+ dir = parent;
205
+ }
206
+ }
207
+ /**
237
208
  * Converts all backslashes to forward slashes.
238
209
  * Extended-length Windows paths (`\\?\...`) are left unchanged.
239
210
  */
@@ -332,159 +303,6 @@ async function clean(path) {
332
303
  });
333
304
  }
334
305
  //#endregion
335
- //#region ../../internals/utils/src/string.ts
336
- /**
337
- * Strips the file extension from a path or file name.
338
- * Only removes the last `.ext` segment when the dot is not part of a directory name.
339
- *
340
- * @example
341
- * trimExtName('petStore.ts') // 'petStore'
342
- * trimExtName('/src/models/pet.ts') // '/src/models/pet'
343
- * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
344
- * trimExtName('noExtension') // 'noExtension'
345
- */
346
- function trimExtName$1(text) {
347
- const dotIndex = text.lastIndexOf(".");
348
- if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
349
- return text;
350
- }
351
- __name(trimExtName$1, "trimExtName");
352
- //#endregion
353
- //#region ../../internals/utils/src/promise.ts
354
- /** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.
355
- *
356
- * @example
357
- * ```ts
358
- * const results = await Promise.allSettled([p1, p2])
359
- * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)
360
- * ```
361
- */
362
- function isPromiseRejectedResult(result) {
363
- return result.status === "rejected";
364
- }
365
- //#endregion
366
- //#region ../../internals/utils/src/reserved.ts
367
- /**
368
- * JavaScript and Java reserved words.
369
- * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
370
- */
371
- const reservedWords = new Set([
372
- "abstract",
373
- "arguments",
374
- "boolean",
375
- "break",
376
- "byte",
377
- "case",
378
- "catch",
379
- "char",
380
- "class",
381
- "const",
382
- "continue",
383
- "debugger",
384
- "default",
385
- "delete",
386
- "do",
387
- "double",
388
- "else",
389
- "enum",
390
- "eval",
391
- "export",
392
- "extends",
393
- "false",
394
- "final",
395
- "finally",
396
- "float",
397
- "for",
398
- "function",
399
- "goto",
400
- "if",
401
- "implements",
402
- "import",
403
- "in",
404
- "instanceof",
405
- "int",
406
- "interface",
407
- "let",
408
- "long",
409
- "native",
410
- "new",
411
- "null",
412
- "package",
413
- "private",
414
- "protected",
415
- "public",
416
- "return",
417
- "short",
418
- "static",
419
- "super",
420
- "switch",
421
- "synchronized",
422
- "this",
423
- "throw",
424
- "throws",
425
- "transient",
426
- "true",
427
- "try",
428
- "typeof",
429
- "var",
430
- "void",
431
- "volatile",
432
- "while",
433
- "with",
434
- "yield",
435
- "Array",
436
- "Date",
437
- "hasOwnProperty",
438
- "Infinity",
439
- "isFinite",
440
- "isNaN",
441
- "isPrototypeOf",
442
- "length",
443
- "Math",
444
- "name",
445
- "NaN",
446
- "Number",
447
- "Object",
448
- "prototype",
449
- "String",
450
- "toString",
451
- "undefined",
452
- "valueOf"
453
- ]);
454
- /**
455
- * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.
456
- *
457
- * @example
458
- * ```ts
459
- * transformReservedWord('class') // '_class'
460
- * transformReservedWord('42foo') // '_42foo'
461
- * transformReservedWord('status') // 'status'
462
- * ```
463
- */
464
- function transformReservedWord(word) {
465
- const firstChar = word.charCodeAt(0);
466
- if (word && (reservedWords.has(word) || firstChar >= 48 && firstChar <= 57)) return `_${word}`;
467
- return word;
468
- }
469
- /**
470
- * Returns `true` when `name` is a syntactically valid JavaScript variable name.
471
- *
472
- * @example
473
- * ```ts
474
- * isValidVarName('status') // true
475
- * isValidVarName('class') // false (reserved word)
476
- * isValidVarName('42foo') // false (starts with digit)
477
- * ```
478
- */
479
- function isValidVarName(name) {
480
- try {
481
- new Function(`var ${name}`);
482
- } catch {
483
- return false;
484
- }
485
- return true;
486
- }
487
- //#endregion
488
306
  //#region ../../internals/utils/src/urlPath.ts
489
307
  /**
490
308
  * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
@@ -631,222 +449,24 @@ var URLPath = class {
631
449
  }
632
450
  };
633
451
  //#endregion
634
- //#region src/constants.ts
635
- /**
636
- * Base URL for the Kubb Studio web app.
637
- */
638
- const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
639
- /**
640
- * File name used for generated barrel (index) files.
641
- */
642
- const BARREL_FILENAME = "index.ts";
643
- /**
644
- * Default banner style written at the top of every generated file.
645
- */
646
- const DEFAULT_BANNER = "simple";
647
- /**
648
- * Default file-extension mapping used when no explicit mapping is configured.
649
- */
650
- const DEFAULT_EXTENSION = { ".ts": ".ts" };
651
- /**
652
- * Numeric log-level thresholds used internally to compare verbosity.
653
- *
654
- * Higher numbers are more verbose.
655
- */
656
- const logLevel = {
657
- silent: Number.NEGATIVE_INFINITY,
658
- error: 0,
659
- warn: 1,
660
- info: 3,
661
- verbose: 4,
662
- debug: 5
663
- };
452
+ //#region src/createAdapter.ts
664
453
  /**
665
- * CLI command descriptors for each supported linter.
454
+ * Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
666
455
  *
667
- * Each entry contains the executable `command`, an `args` factory that maps an
668
- * output path to the correct argument list, and an `errorMessage` shown when
669
- * the linter is not found.
670
- */
671
- const linters = {
672
- eslint: {
673
- command: "eslint",
674
- args: (outputPath) => [outputPath, "--fix"],
675
- errorMessage: "Eslint not found"
676
- },
677
- biome: {
678
- command: "biome",
679
- args: (outputPath) => [
680
- "lint",
681
- "--fix",
682
- outputPath
683
- ],
684
- errorMessage: "Biome not found"
685
- },
686
- oxlint: {
687
- command: "oxlint",
688
- args: (outputPath) => ["--fix", outputPath],
689
- errorMessage: "Oxlint not found"
690
- }
691
- };
692
- /**
693
- * CLI command descriptors for each supported code formatter.
456
+ * @example
457
+ * export const myAdapter = createAdapter<MyAdapter>((options) => {
458
+ * return {
459
+ * name: 'my-adapter',
460
+ * options,
461
+ * async parse(source) { ... },
462
+ * }
463
+ * })
694
464
  *
695
- * Each entry contains the executable `command`, an `args` factory that maps an
696
- * output path to the correct argument list, and an `errorMessage` shown when
697
- * the formatter is not found.
465
+ * // instantiate
466
+ * const adapter = myAdapter({ validate: true })
698
467
  */
699
- const formatters = {
700
- prettier: {
701
- command: "prettier",
702
- args: (outputPath) => [
703
- "--ignore-unknown",
704
- "--write",
705
- outputPath
706
- ],
707
- errorMessage: "Prettier not found"
708
- },
709
- biome: {
710
- command: "biome",
711
- args: (outputPath) => [
712
- "format",
713
- "--write",
714
- outputPath
715
- ],
716
- errorMessage: "Biome not found"
717
- },
718
- oxfmt: {
719
- command: "oxfmt",
720
- args: (outputPath) => [outputPath],
721
- errorMessage: "Oxfmt not found"
722
- }
723
- };
724
- //#endregion
725
- //#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
726
- var Node = class {
727
- value;
728
- next;
729
- constructor(value) {
730
- this.value = value;
731
- }
732
- };
733
- var Queue = class {
734
- #head;
735
- #tail;
736
- #size;
737
- constructor() {
738
- this.clear();
739
- }
740
- enqueue(value) {
741
- const node = new Node(value);
742
- if (this.#head) {
743
- this.#tail.next = node;
744
- this.#tail = node;
745
- } else {
746
- this.#head = node;
747
- this.#tail = node;
748
- }
749
- this.#size++;
750
- }
751
- dequeue() {
752
- const current = this.#head;
753
- if (!current) return;
754
- this.#head = this.#head.next;
755
- this.#size--;
756
- if (!this.#head) this.#tail = void 0;
757
- return current.value;
758
- }
759
- peek() {
760
- if (!this.#head) return;
761
- return this.#head.value;
762
- }
763
- clear() {
764
- this.#head = void 0;
765
- this.#tail = void 0;
766
- this.#size = 0;
767
- }
768
- get size() {
769
- return this.#size;
770
- }
771
- *[Symbol.iterator]() {
772
- let current = this.#head;
773
- while (current) {
774
- yield current.value;
775
- current = current.next;
776
- }
777
- }
778
- *drain() {
779
- while (this.#head) yield this.dequeue();
780
- }
781
- };
782
- //#endregion
783
- //#region ../../node_modules/.pnpm/p-limit@7.3.0/node_modules/p-limit/index.js
784
- function pLimit(concurrency) {
785
- let rejectOnClear = false;
786
- if (typeof concurrency === "object") ({concurrency, rejectOnClear = false} = concurrency);
787
- validateConcurrency(concurrency);
788
- if (typeof rejectOnClear !== "boolean") throw new TypeError("Expected `rejectOnClear` to be a boolean");
789
- const queue = new Queue();
790
- let activeCount = 0;
791
- const resumeNext = () => {
792
- if (activeCount < concurrency && queue.size > 0) {
793
- activeCount++;
794
- queue.dequeue().run();
795
- }
796
- };
797
- const next = () => {
798
- activeCount--;
799
- resumeNext();
800
- };
801
- const run = async (function_, resolve, arguments_) => {
802
- const result = (async () => function_(...arguments_))();
803
- resolve(result);
804
- try {
805
- await result;
806
- } catch {}
807
- next();
808
- };
809
- const enqueue = (function_, resolve, reject, arguments_) => {
810
- const queueItem = { reject };
811
- new Promise((internalResolve) => {
812
- queueItem.run = internalResolve;
813
- queue.enqueue(queueItem);
814
- }).then(run.bind(void 0, function_, resolve, arguments_));
815
- if (activeCount < concurrency) resumeNext();
816
- };
817
- const generator = (function_, ...arguments_) => new Promise((resolve, reject) => {
818
- enqueue(function_, resolve, reject, arguments_);
819
- });
820
- Object.defineProperties(generator, {
821
- activeCount: { get: () => activeCount },
822
- pendingCount: { get: () => queue.size },
823
- clearQueue: { value() {
824
- if (!rejectOnClear) {
825
- queue.clear();
826
- return;
827
- }
828
- const abortError = AbortSignal.abort().reason;
829
- while (queue.size > 0) queue.dequeue().reject(abortError);
830
- } },
831
- concurrency: {
832
- get: () => concurrency,
833
- set(newConcurrency) {
834
- validateConcurrency(newConcurrency);
835
- concurrency = newConcurrency;
836
- queueMicrotask(() => {
837
- while (activeCount < concurrency && queue.size > 0) resumeNext();
838
- });
839
- }
840
- },
841
- map: { async value(iterable, function_) {
842
- const promises = Array.from(iterable, (value, index) => this(function_, value, index));
843
- return Promise.all(promises);
844
- } }
845
- });
846
- return generator;
847
- }
848
- function validateConcurrency(concurrency) {
849
- if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
468
+ function createAdapter(build) {
469
+ return (options) => build(options ?? {});
850
470
  }
851
471
  //#endregion
852
472
  //#region src/FileProcessor.ts
@@ -892,634 +512,6 @@ var FileProcessor = class {
892
512
  }
893
513
  };
894
514
  //#endregion
895
- //#region src/devtools.ts
896
- /**
897
- * Encodes an `InputNode` as a compressed, URL-safe string.
898
- *
899
- * The JSON representation is deflate-compressed with {@link deflateSync} before
900
- * base64url encoding, which typically reduces payload size by 70–80 % and
901
- * keeps URLs well within browser and server path-length limits.
902
- *
903
- * Use {@link decodeAst} to reverse.
904
- */
905
- function encodeAst(input) {
906
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)));
907
- return Buffer.from(compressed).toString("base64url");
908
- }
909
- /**
910
- * Constructs the Kubb Studio URL for the given `InputNode`.
911
- * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
912
- * The `input` is encoded and attached as the `?root=` query parameter so Studio
913
- * can decode and render it without a round-trip to any server.
914
- */
915
- function getStudioUrl(input, studioUrl, options = {}) {
916
- return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(input)}`;
917
- }
918
- /**
919
- * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
920
- *
921
- * Falls back to printing the URL if the browser cannot be launched.
922
- */
923
- async function openInStudio(input, studioUrl, options = {}) {
924
- const url = getStudioUrl(input, studioUrl, options);
925
- const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
926
- const args = process.platform === "win32" ? [
927
- "/c",
928
- "start",
929
- "",
930
- url
931
- ] : [url];
932
- try {
933
- await x(cmd, args);
934
- } catch {
935
- console.log(`\n ${url}\n`);
936
- }
937
- }
938
- //#endregion
939
- //#region src/FileManager.ts
940
- function mergeFile(a, b) {
941
- return {
942
- ...a,
943
- sources: [...a.sources || [], ...b.sources || []],
944
- imports: [...a.imports || [], ...b.imports || []],
945
- exports: [...a.exports || [], ...b.exports || []]
946
- };
947
- }
948
- /**
949
- * In-memory file store for generated files.
950
- *
951
- * Files with the same `path` are merged — sources, imports, and exports are concatenated.
952
- * The `files` getter returns all stored files sorted by path length (shortest first).
953
- *
954
- * @example
955
- * ```ts
956
- * import { FileManager } from '@kubb/core'
957
- *
958
- * const manager = new FileManager()
959
- * manager.upsert(myFile)
960
- * console.log(manager.files) // all stored files
961
- * ```
962
- */
963
- var FileManager = class {
964
- #cache = /* @__PURE__ */ new Map();
965
- #filesCache = null;
966
- /**
967
- * Adds one or more files. Files with the same path are merged — sources, imports,
968
- * and exports from all calls with the same path are concatenated together.
969
- */
970
- add(...files) {
971
- const resolvedFiles = [];
972
- const mergedFiles = /* @__PURE__ */ new Map();
973
- files.forEach((file) => {
974
- const existing = mergedFiles.get(file.path);
975
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
976
- else mergedFiles.set(file.path, file);
977
- });
978
- for (const file of mergedFiles.values()) {
979
- const resolvedFile = createFile(file);
980
- this.#cache.set(resolvedFile.path, resolvedFile);
981
- this.#filesCache = null;
982
- resolvedFiles.push(resolvedFile);
983
- }
984
- return resolvedFiles;
985
- }
986
- /**
987
- * Adds or merges one or more files.
988
- * If a file with the same path already exists, its sources/imports/exports are merged together.
989
- */
990
- upsert(...files) {
991
- const resolvedFiles = [];
992
- const mergedFiles = /* @__PURE__ */ new Map();
993
- files.forEach((file) => {
994
- const existing = mergedFiles.get(file.path);
995
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
996
- else mergedFiles.set(file.path, file);
997
- });
998
- for (const file of mergedFiles.values()) {
999
- const existing = this.#cache.get(file.path);
1000
- const resolvedFile = createFile(existing ? mergeFile(existing, file) : file);
1001
- this.#cache.set(resolvedFile.path, resolvedFile);
1002
- this.#filesCache = null;
1003
- resolvedFiles.push(resolvedFile);
1004
- }
1005
- return resolvedFiles;
1006
- }
1007
- getByPath(path) {
1008
- return this.#cache.get(path) ?? null;
1009
- }
1010
- deleteByPath(path) {
1011
- this.#cache.delete(path);
1012
- this.#filesCache = null;
1013
- }
1014
- clear() {
1015
- this.#cache.clear();
1016
- this.#filesCache = null;
1017
- }
1018
- /**
1019
- * All stored files, sorted by path length (shorter paths first).
1020
- * Barrel/index files (e.g. index.ts) are sorted last within each length bucket.
1021
- */
1022
- get files() {
1023
- if (this.#filesCache) return this.#filesCache;
1024
- const keys = [...this.#cache.keys()].sort((a, b) => {
1025
- if (a.length !== b.length) return a.length - b.length;
1026
- const aIsIndex = trimExtName$1(a).endsWith("index");
1027
- if (aIsIndex !== trimExtName$1(b).endsWith("index")) return aIsIndex ? 1 : -1;
1028
- return 0;
1029
- });
1030
- const files = [];
1031
- for (const key of keys) {
1032
- const file = this.#cache.get(key);
1033
- if (file) files.push(file);
1034
- }
1035
- this.#filesCache = files;
1036
- return files;
1037
- }
1038
- };
1039
- //#endregion
1040
- //#region src/utils/executeStrategies.ts
1041
- /**
1042
- * Runs promise functions in sequence, threading each result into the next call.
1043
- *
1044
- * - Each function receives the accumulated state from the previous call.
1045
- * - Skips functions that return a falsy value (acts as a no-op for that step).
1046
- * - Returns an array of all individual results.
1047
- * @deprecated
1048
- */
1049
- function hookSeq(promises) {
1050
- return promises.filter(Boolean).reduce((promise, func) => {
1051
- if (typeof func !== "function") throw new Error("HookSeq needs a function that returns a promise `() => Promise<unknown>`");
1052
- return promise.then((state) => {
1053
- const calledFunc = func(state);
1054
- if (calledFunc) return calledFunc.then(Array.prototype.concat.bind(state));
1055
- return state;
1056
- });
1057
- }, Promise.resolve([]));
1058
- }
1059
- /**
1060
- * Runs promise functions in sequence and returns the first non-null result.
1061
- *
1062
- * - Stops as soon as `nullCheck` passes for a result (default: `!== null`).
1063
- * - Subsequent functions are skipped once a match is found.
1064
- * @deprecated
1065
- */
1066
- function hookFirst(promises, nullCheck = (state) => state !== null) {
1067
- let promise = Promise.resolve(null);
1068
- for (const func of promises.filter(Boolean)) promise = promise.then((state) => {
1069
- if (nullCheck(state)) return state;
1070
- return func(state);
1071
- });
1072
- return promise;
1073
- }
1074
- /**
1075
- * Runs promise functions concurrently and returns all settled results.
1076
- *
1077
- * - Limits simultaneous executions to `concurrency` (default: unlimited).
1078
- * - Uses `Promise.allSettled` so individual failures do not cancel other tasks.
1079
- * @deprecated
1080
- */
1081
- function hookParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
1082
- const limit = pLimit(concurrency);
1083
- const tasks = promises.filter(Boolean).map((promise) => limit(() => promise()));
1084
- return Promise.allSettled(tasks);
1085
- }
1086
- //#endregion
1087
- //#region src/PluginDriver.ts
1088
- /**
1089
- * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
1090
- *
1091
- * @example
1092
- * ```ts
1093
- * getMode('src/gen/types.ts') // 'single'
1094
- * getMode('src/gen/types') // 'split'
1095
- * ```
1096
- */
1097
- function getMode(fileOrFolder) {
1098
- if (!fileOrFolder) return "split";
1099
- return extname(fileOrFolder) ? "single" : "split";
1100
- }
1101
- const hookFirstNullCheck = (state) => !!state?.result;
1102
- var PluginDriver = class {
1103
- config;
1104
- options;
1105
- /**
1106
- * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
1107
- * the build pipeline after the adapter's `parse()` resolves.
1108
- */
1109
- inputNode = void 0;
1110
- adapter = void 0;
1111
- #studioIsOpen = false;
1112
- /**
1113
- * Central file store for all generated files.
1114
- * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
1115
- * add files; this property gives direct read/write access when needed.
1116
- */
1117
- fileManager = new FileManager();
1118
- plugins = /* @__PURE__ */ new Map();
1119
- constructor(config, options) {
1120
- this.config = config;
1121
- this.options = options;
1122
- config.plugins.map((plugin) => Object.assign({
1123
- buildStart() {},
1124
- buildEnd() {}
1125
- }, plugin)).filter((plugin) => {
1126
- if (typeof plugin.apply === "function") return plugin.apply(config);
1127
- return true;
1128
- }).sort((a, b) => {
1129
- if (b.pre?.includes(a.name)) return 1;
1130
- if (b.post?.includes(a.name)) return -1;
1131
- return 0;
1132
- }).forEach((plugin) => {
1133
- this.plugins.set(plugin.name, plugin);
1134
- });
1135
- }
1136
- get events() {
1137
- return this.options.events;
1138
- }
1139
- getContext(plugin) {
1140
- const driver = this;
1141
- const baseContext = {
1142
- config: driver.config,
1143
- get root() {
1144
- return resolve(driver.config.root, driver.config.output.path);
1145
- },
1146
- getMode(output) {
1147
- return getMode(resolve(driver.config.root, driver.config.output.path, output.path));
1148
- },
1149
- events: driver.options.events,
1150
- plugin,
1151
- getPlugin: driver.getPlugin.bind(driver),
1152
- requirePlugin: driver.requirePlugin.bind(driver),
1153
- driver,
1154
- addFile: async (...files) => {
1155
- driver.fileManager.add(...files);
1156
- },
1157
- upsertFile: async (...files) => {
1158
- driver.fileManager.upsert(...files);
1159
- },
1160
- get inputNode() {
1161
- return driver.inputNode;
1162
- },
1163
- get adapter() {
1164
- return driver.adapter;
1165
- },
1166
- get resolver() {
1167
- return plugin.resolver;
1168
- },
1169
- get transformer() {
1170
- return plugin.transformer;
1171
- },
1172
- warn(message) {
1173
- driver.events.emit("warn", message);
1174
- },
1175
- error(error) {
1176
- driver.events.emit("error", typeof error === "string" ? new Error(error) : error);
1177
- },
1178
- info(message) {
1179
- driver.events.emit("info", message);
1180
- },
1181
- openInStudio(options) {
1182
- if (!driver.config.devtools || driver.#studioIsOpen) return;
1183
- if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
1184
- if (!driver.inputNode || !driver.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
1185
- driver.#studioIsOpen = true;
1186
- const studioUrl = driver.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
1187
- return openInStudio(driver.inputNode, studioUrl, options);
1188
- }
1189
- };
1190
- const mergedExtras = {};
1191
- for (const p of this.plugins.values()) if (typeof p.inject === "function") {
1192
- const result = p.inject.call(baseContext);
1193
- if (result !== null && typeof result === "object") Object.assign(mergedExtras, result);
1194
- }
1195
- return {
1196
- ...baseContext,
1197
- ...mergedExtras
1198
- };
1199
- }
1200
- /**
1201
- * @deprecated use resolvers context instead
1202
- */
1203
- getFile({ name, mode, extname, pluginName, options }) {
1204
- const resolvedName = mode ? mode === "single" ? "" : this.resolveName({
1205
- name,
1206
- pluginName,
1207
- type: "file"
1208
- }) : name;
1209
- const path = this.resolvePath({
1210
- baseName: `${resolvedName}${extname}`,
1211
- mode,
1212
- pluginName,
1213
- options
1214
- });
1215
- if (!path) throw new Error(`Filepath should be defined for resolvedName "${resolvedName}" and pluginName "${pluginName}"`);
1216
- return createFile({
1217
- path,
1218
- baseName: basename(path),
1219
- meta: { pluginName },
1220
- sources: [],
1221
- imports: [],
1222
- exports: []
1223
- });
1224
- }
1225
- /**
1226
- * @deprecated use resolvers context instead
1227
- */
1228
- resolvePath = (params) => {
1229
- const defaultPath = resolve(resolve(this.config.root, this.config.output.path), params.baseName);
1230
- if (params.pluginName) return this.hookForPluginSync({
1231
- pluginName: params.pluginName,
1232
- hookName: "resolvePath",
1233
- parameters: [
1234
- params.baseName,
1235
- params.mode,
1236
- params.options
1237
- ]
1238
- })?.at(0) || defaultPath;
1239
- return this.hookFirstSync({
1240
- hookName: "resolvePath",
1241
- parameters: [
1242
- params.baseName,
1243
- params.mode,
1244
- params.options
1245
- ]
1246
- })?.result || defaultPath;
1247
- };
1248
- /**
1249
- * @deprecated use resolvers context instead
1250
- */
1251
- resolveName = (params) => {
1252
- if (params.pluginName) return transformReservedWord(this.hookForPluginSync({
1253
- pluginName: params.pluginName,
1254
- hookName: "resolveName",
1255
- parameters: [params.name.trim(), params.type]
1256
- })?.at(0) ?? params.name);
1257
- const name = this.hookFirstSync({
1258
- hookName: "resolveName",
1259
- parameters: [params.name.trim(), params.type]
1260
- })?.result;
1261
- return transformReservedWord(name ?? params.name);
1262
- };
1263
- /**
1264
- * Run a specific hookName for plugin x.
1265
- */
1266
- async hookForPlugin({ pluginName, hookName, parameters }) {
1267
- const plugin = this.plugins.get(pluginName);
1268
- if (!plugin) return [null];
1269
- this.events.emit("plugins:hook:progress:start", {
1270
- hookName,
1271
- plugins: [plugin]
1272
- });
1273
- const result = await this.#execute({
1274
- strategy: "hookFirst",
1275
- hookName,
1276
- parameters,
1277
- plugin
1278
- });
1279
- this.events.emit("plugins:hook:progress:end", { hookName });
1280
- return [result];
1281
- }
1282
- /**
1283
- * Run a specific hookName for plugin x.
1284
- */
1285
- hookForPluginSync({ pluginName, hookName, parameters }) {
1286
- const plugin = this.plugins.get(pluginName);
1287
- if (!plugin) return null;
1288
- const result = this.#executeSync({
1289
- strategy: "hookFirst",
1290
- hookName,
1291
- parameters,
1292
- plugin
1293
- });
1294
- return result !== null ? [result] : [];
1295
- }
1296
- /**
1297
- * Returns the first non-null result.
1298
- */
1299
- async hookFirst({ hookName, parameters, skipped }) {
1300
- const plugins = [];
1301
- for (const plugin of this.plugins.values()) if (hookName in plugin && (skipped ? !skipped.has(plugin) : true)) plugins.push(plugin);
1302
- this.events.emit("plugins:hook:progress:start", {
1303
- hookName,
1304
- plugins
1305
- });
1306
- const result = await hookFirst(plugins.map((plugin) => {
1307
- return async () => {
1308
- const value = await this.#execute({
1309
- strategy: "hookFirst",
1310
- hookName,
1311
- parameters,
1312
- plugin
1313
- });
1314
- return Promise.resolve({
1315
- plugin,
1316
- result: value
1317
- });
1318
- };
1319
- }), hookFirstNullCheck);
1320
- this.events.emit("plugins:hook:progress:end", { hookName });
1321
- return result;
1322
- }
1323
- /**
1324
- * Returns the first non-null result.
1325
- */
1326
- hookFirstSync({ hookName, parameters, skipped }) {
1327
- let parseResult = null;
1328
- for (const plugin of this.plugins.values()) {
1329
- if (!(hookName in plugin)) continue;
1330
- if (skipped?.has(plugin)) continue;
1331
- parseResult = {
1332
- result: this.#executeSync({
1333
- strategy: "hookFirst",
1334
- hookName,
1335
- parameters,
1336
- plugin
1337
- }),
1338
- plugin
1339
- };
1340
- if (parseResult.result != null) break;
1341
- }
1342
- return parseResult;
1343
- }
1344
- /**
1345
- * Runs all plugins in parallel based on `this.plugin` order and `pre`/`post` settings.
1346
- */
1347
- async hookParallel({ hookName, parameters }) {
1348
- const plugins = [];
1349
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
1350
- this.events.emit("plugins:hook:progress:start", {
1351
- hookName,
1352
- plugins
1353
- });
1354
- const pluginStartTimes = /* @__PURE__ */ new Map();
1355
- const results = await hookParallel(plugins.map((plugin) => {
1356
- return () => {
1357
- pluginStartTimes.set(plugin, performance.now());
1358
- return this.#execute({
1359
- strategy: "hookParallel",
1360
- hookName,
1361
- parameters,
1362
- plugin
1363
- });
1364
- };
1365
- }), this.options.concurrency);
1366
- results.forEach((result, index) => {
1367
- if (isPromiseRejectedResult(result)) {
1368
- const plugin = plugins[index];
1369
- if (plugin) {
1370
- const startTime = pluginStartTimes.get(plugin) ?? performance.now();
1371
- this.events.emit("error", result.reason, {
1372
- plugin,
1373
- hookName,
1374
- strategy: "hookParallel",
1375
- duration: Math.round(performance.now() - startTime),
1376
- parameters
1377
- });
1378
- }
1379
- }
1380
- });
1381
- this.events.emit("plugins:hook:progress:end", { hookName });
1382
- return results.reduce((acc, result) => {
1383
- if (result.status === "fulfilled") acc.push(result.value);
1384
- return acc;
1385
- }, []);
1386
- }
1387
- /**
1388
- * Chains plugins
1389
- */
1390
- async hookSeq({ hookName, parameters }) {
1391
- const plugins = [];
1392
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
1393
- this.events.emit("plugins:hook:progress:start", {
1394
- hookName,
1395
- plugins
1396
- });
1397
- await hookSeq(plugins.map((plugin) => {
1398
- return () => this.#execute({
1399
- strategy: "hookSeq",
1400
- hookName,
1401
- parameters,
1402
- plugin
1403
- });
1404
- }));
1405
- this.events.emit("plugins:hook:progress:end", { hookName });
1406
- }
1407
- getPlugin(pluginName) {
1408
- return this.plugins.get(pluginName);
1409
- }
1410
- requirePlugin(pluginName) {
1411
- const plugin = this.plugins.get(pluginName);
1412
- if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
1413
- return plugin;
1414
- }
1415
- /**
1416
- * Run an async plugin hook and return the result.
1417
- * @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`.
1418
- * @param args Arguments passed to the plugin hook.
1419
- * @param plugin The actual pluginObject to run.
1420
- */
1421
- #emitProcessingEnd({ startTime, output, strategy, hookName, plugin, parameters }) {
1422
- this.events.emit("plugins:hook:processing:end", {
1423
- duration: Math.round(performance.now() - startTime),
1424
- parameters,
1425
- output,
1426
- strategy,
1427
- hookName,
1428
- plugin
1429
- });
1430
- }
1431
- #execute({ strategy, hookName, parameters, plugin }) {
1432
- const hook = plugin[hookName];
1433
- if (!hook) return null;
1434
- this.events.emit("plugins:hook:processing:start", {
1435
- strategy,
1436
- hookName,
1437
- parameters,
1438
- plugin
1439
- });
1440
- const startTime = performance.now();
1441
- return (async () => {
1442
- try {
1443
- const output = typeof hook === "function" ? await Promise.resolve(hook.apply(this.getContext(plugin), parameters ?? [])) : hook;
1444
- this.#emitProcessingEnd({
1445
- startTime,
1446
- output,
1447
- strategy,
1448
- hookName,
1449
- plugin,
1450
- parameters
1451
- });
1452
- return output;
1453
- } catch (error) {
1454
- this.events.emit("error", error, {
1455
- plugin,
1456
- hookName,
1457
- strategy,
1458
- duration: Math.round(performance.now() - startTime)
1459
- });
1460
- return null;
1461
- }
1462
- })();
1463
- }
1464
- /**
1465
- * Run a sync plugin hook and return the result.
1466
- * @param hookName Name of the plugin hook. Must be in `PluginHooks`.
1467
- * @param args Arguments passed to the plugin hook.
1468
- * @param plugin The actual plugin
1469
- */
1470
- #executeSync({ strategy, hookName, parameters, plugin }) {
1471
- const hook = plugin[hookName];
1472
- if (!hook) return null;
1473
- this.events.emit("plugins:hook:processing:start", {
1474
- strategy,
1475
- hookName,
1476
- parameters,
1477
- plugin
1478
- });
1479
- const startTime = performance.now();
1480
- try {
1481
- const output = typeof hook === "function" ? hook.apply(this.getContext(plugin), parameters) : hook;
1482
- this.#emitProcessingEnd({
1483
- startTime,
1484
- output,
1485
- strategy,
1486
- hookName,
1487
- plugin,
1488
- parameters
1489
- });
1490
- return output;
1491
- } catch (error) {
1492
- this.events.emit("error", error, {
1493
- plugin,
1494
- hookName,
1495
- strategy,
1496
- duration: Math.round(performance.now() - startTime)
1497
- });
1498
- return null;
1499
- }
1500
- }
1501
- };
1502
- //#endregion
1503
- //#region src/renderNode.tsx
1504
- /**
1505
- * Handles the return value of a plugin AST hook or generator method.
1506
- *
1507
- * - React element → rendered via renderer-jsx, files stored in `driver.fileManager`
1508
- * - `Array<FileNode>` → upserted directly into `driver.fileManager`
1509
- * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
1510
- */
1511
- async function applyHookResult(result, driver) {
1512
- if (!result) return;
1513
- if (Array.isArray(result)) {
1514
- driver.fileManager.upsert(...result);
1515
- return;
1516
- }
1517
- const renderer = createRenderer();
1518
- await renderer.render(/* @__PURE__ */ jsx(Fragment, { children: result }));
1519
- driver.fileManager.upsert(...renderer.files);
1520
- renderer.unmount();
1521
- }
1522
- //#endregion
1523
515
  //#region src/createStorage.ts
1524
516
  /**
1525
517
  * Creates a storage factory. Call the returned function with optional options to get the storage instance.
@@ -1617,7 +609,7 @@ const fsStorage = createStorage(() => ({
1617
609
  }));
1618
610
  //#endregion
1619
611
  //#region package.json
1620
- var version$1 = "5.0.0-alpha.34";
612
+ var version$1 = "5.0.0-alpha.36";
1621
613
  //#endregion
1622
614
  //#region src/utils/diagnostics.ts
1623
615
  /**
@@ -1878,24 +870,14 @@ function isInputPath(config) {
1878
870
  return typeof config?.input === "object" && config.input !== null && "path" in config.input;
1879
871
  }
1880
872
  //#endregion
1881
- //#region src/build.ts
1882
- /**
1883
- * Initializes all Kubb infrastructure for a build without executing any plugins.
1884
- *
1885
- * - Validates the input path (when applicable).
1886
- * - Applies config defaults (`root`, `output.*`, `devtools`).
1887
- * - Runs the adapter (if configured) to produce the universal `InputNode`.
1888
- * When no adapter is supplied and `@kubb/adapter-oas` is installed as an
1889
- *
1890
- * Pass the returned {@link SetupResult} directly to {@link safeBuild} or {@link build}
1891
- * via the `overrides` argument to reuse the same infrastructure across multiple runs.
1892
- */
873
+ //#region src/createKubb.ts
1893
874
  async function setup(options) {
1894
- const { config: userConfig, events = new AsyncEventEmitter() } = options;
875
+ const { config: userConfig } = options;
876
+ const hooks = options.hooks ?? new AsyncEventEmitter();
1895
877
  const sources = /* @__PURE__ */ new Map();
1896
878
  const diagnosticInfo = getDiagnosticInfo();
1897
- if (Array.isArray(userConfig.input)) await events.emit("warn", "This feature is still under development — use with caution");
1898
- await events.emit("debug", {
879
+ if (Array.isArray(userConfig.input)) await hooks.emit("kubb:warn", "This feature is still under development — use with caution");
880
+ await hooks.emit("kubb:debug", {
1899
881
  date: /* @__PURE__ */ new Date(),
1900
882
  logs: [
1901
883
  "Configuration:",
@@ -1914,7 +896,7 @@ async function setup(options) {
1914
896
  try {
1915
897
  if (isInputPath(userConfig) && !new URLPath(userConfig.input.path).isURL) {
1916
898
  await exists(userConfig.input.path);
1917
- await events.emit("debug", {
899
+ await hooks.emit("kubb:debug", {
1918
900
  date: /* @__PURE__ */ new Date(),
1919
901
  logs: [`✓ Input file validated: ${userConfig.input.path}`]
1920
902
  });
@@ -1927,8 +909,8 @@ async function setup(options) {
1927
909
  }
1928
910
  if (!userConfig.adapter) throw new Error("Adapter should be defined");
1929
911
  const config = {
1930
- root: userConfig.root || process.cwd(),
1931
912
  ...userConfig,
913
+ root: userConfig.root || process.cwd(),
1932
914
  parsers: userConfig.parsers ?? [],
1933
915
  adapter: userConfig.adapter,
1934
916
  output: {
@@ -1946,26 +928,26 @@ async function setup(options) {
1946
928
  };
1947
929
  const storage = config.output.write === false ? null : config.output.storage ?? fsStorage();
1948
930
  if (config.output.clean) {
1949
- await events.emit("debug", {
931
+ await hooks.emit("kubb:debug", {
1950
932
  date: /* @__PURE__ */ new Date(),
1951
933
  logs: ["Cleaning output directories", ` • Output: ${config.output.path}`]
1952
934
  });
1953
935
  await storage?.clear(resolve(config.root, config.output.path));
1954
936
  }
1955
937
  const driver = new PluginDriver(config, {
1956
- events,
938
+ hooks,
1957
939
  concurrency: 15
1958
940
  });
1959
941
  const adapter = config.adapter;
1960
942
  if (!adapter) throw new Error("No adapter configured. Please provide an adapter in your kubb.config.ts.");
1961
943
  const source = inputToAdapterSource(config);
1962
- await events.emit("debug", {
944
+ await hooks.emit("kubb:debug", {
1963
945
  date: /* @__PURE__ */ new Date(),
1964
946
  logs: [`Running adapter: ${adapter.name}`]
1965
947
  });
1966
948
  driver.adapter = adapter;
1967
949
  driver.inputNode = await adapter.parse(source);
1968
- await events.emit("debug", {
950
+ await hooks.emit("kubb:debug", {
1969
951
  date: /* @__PURE__ */ new Date(),
1970
952
  logs: [
1971
953
  `✓ Adapter '${adapter.name}' resolved InputNode`,
@@ -1975,53 +957,32 @@ async function setup(options) {
1975
957
  });
1976
958
  return {
1977
959
  config,
1978
- events,
960
+ hooks,
1979
961
  driver,
1980
962
  sources,
1981
963
  storage
1982
964
  };
1983
965
  }
1984
966
  /**
1985
- * Runs a full Kubb build and throws on any error or plugin failure.
1986
- *
1987
- * Internally delegates to {@link safeBuild} and rethrows collected errors.
1988
- * Pass an existing {@link SetupResult} via `overrides` to skip the setup phase.
1989
- */
1990
- async function build(options, overrides) {
1991
- const { files, driver, failedPlugins, pluginTimings, error, sources } = await safeBuild(options, overrides);
1992
- if (error) throw error;
1993
- if (failedPlugins.size > 0) {
1994
- const errors = [...failedPlugins].map(({ error }) => error);
1995
- throw new BuildError(`Build Error with ${failedPlugins.size} failed plugins`, { errors });
1996
- }
1997
- return {
1998
- failedPlugins,
1999
- files,
2000
- driver,
2001
- pluginTimings,
2002
- error: void 0,
2003
- sources
2004
- };
2005
- }
2006
- /**
2007
967
  * Walks the AST and dispatches nodes to a plugin's direct AST hooks
2008
968
  * (`schema`, `operation`, `operations`).
2009
- *
2010
- * - Each hook accepts a single handler **or an array** — all entries are called in sequence.
2011
- * - Nodes that are excluded by `exclude`/`include` plugin options are skipped automatically.
2012
- * - Return values are handled via `applyHookResult`: React elements are rendered,
2013
- * `FileNode[]` are written via upsert, and `void` is a no-op (manual handling).
2014
- * - Barrel files are generated automatically when `output.barrelType` is set.
2015
969
  */
2016
970
  async function runPluginAstHooks(plugin, context) {
2017
971
  const { adapter, inputNode, resolver, driver } = context;
2018
972
  const { exclude, include, override } = plugin.options;
2019
973
  if (!adapter || !inputNode) throw new Error(`[${plugin.name}] No adapter found. Add an OAS adapter (e.g. pluginOas()) before this plugin in your Kubb config.`);
974
+ function resolveRenderer(gen) {
975
+ return gen.renderer === null ? void 0 : gen.renderer ?? plugin.renderer ?? context.config.renderer;
976
+ }
977
+ const generators = plugin.generators ?? [];
2020
978
  const collectedOperations = [];
979
+ const generatorContext = {
980
+ ...context,
981
+ resolver: driver.getResolver(plugin.name)
982
+ };
2021
983
  await walk(inputNode, {
2022
984
  depth: "shallow",
2023
985
  async schema(node) {
2024
- if (!plugin.schema) return;
2025
986
  const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
2026
987
  const options = resolver.resolveOptions(transformedNode, {
2027
988
  options: plugin.options,
@@ -2030,7 +991,15 @@ async function runPluginAstHooks(plugin, context) {
2030
991
  override
2031
992
  });
2032
993
  if (options === null) return;
2033
- await applyHookResult(await plugin.schema.call(context, transformedNode, options), driver);
994
+ const ctx = {
995
+ ...generatorContext,
996
+ options
997
+ };
998
+ for (const gen of generators) {
999
+ if (!gen.schema) continue;
1000
+ await applyHookResult(await gen.schema(transformedNode, ctx), driver, resolveRenderer(gen));
1001
+ }
1002
+ await driver.hooks.emit("kubb:generate:schema", transformedNode, ctx);
2034
1003
  },
2035
1004
  async operation(node) {
2036
1005
  const transformedNode = plugin.transformer ? transform(node, plugin.transformer) : node;
@@ -2042,28 +1011,43 @@ async function runPluginAstHooks(plugin, context) {
2042
1011
  });
2043
1012
  if (options !== null) {
2044
1013
  collectedOperations.push(transformedNode);
2045
- if (plugin.operation) await applyHookResult(await plugin.operation.call(context, transformedNode, options), driver);
1014
+ const ctx = {
1015
+ ...generatorContext,
1016
+ options
1017
+ };
1018
+ for (const gen of generators) {
1019
+ if (!gen.operation) continue;
1020
+ await applyHookResult(await gen.operation(transformedNode, ctx), driver, resolveRenderer(gen));
1021
+ }
1022
+ await driver.hooks.emit("kubb:generate:operation", transformedNode, ctx);
2046
1023
  }
2047
1024
  }
2048
1025
  });
2049
- if (plugin.operations && collectedOperations.length > 0) await applyHookResult(await plugin.operations.call(context, collectedOperations, plugin.options), driver);
1026
+ if (collectedOperations.length > 0) {
1027
+ const ctx = {
1028
+ ...generatorContext,
1029
+ options: plugin.options
1030
+ };
1031
+ for (const gen of generators) {
1032
+ if (!gen.operations) continue;
1033
+ await applyHookResult(await gen.operations(collectedOperations, ctx), driver, resolveRenderer(gen));
1034
+ }
1035
+ await driver.hooks.emit("kubb:generate:operations", collectedOperations, ctx);
1036
+ }
2050
1037
  }
2051
- /**
2052
- * Runs a full Kubb build and captures errors instead of throwing.
2053
- *
2054
- * - Installs each plugin in order, recording failures in `failedPlugins`.
2055
- * - Generates the root barrel file when `output.barrelType` is set.
2056
- * - Writes all files through the driver's FileManager and FileProcessor.
2057
- *
2058
- * Returns a {@link BuildOutput} even on failure — inspect `error` and
2059
- * `failedPlugins` to determine whether the build succeeded.
2060
- */
2061
- async function safeBuild(options, overrides) {
2062
- const { driver, events, sources, storage } = overrides ? overrides : await setup(options);
1038
+ async function safeBuild(setupResult) {
1039
+ const { driver, hooks, sources, storage } = setupResult;
2063
1040
  const failedPlugins = /* @__PURE__ */ new Set();
2064
1041
  const pluginTimings = /* @__PURE__ */ new Map();
2065
1042
  const config = driver.config;
2066
1043
  try {
1044
+ await driver.emitSetupHooks();
1045
+ if (driver.adapter && driver.inputNode) await hooks.emit("kubb:build:start", {
1046
+ config,
1047
+ adapter: driver.adapter,
1048
+ inputNode: driver.inputNode,
1049
+ getPlugin: (name) => driver.getPlugin(name)
1050
+ });
2067
1051
  for (const plugin of driver.plugins.values()) {
2068
1052
  const context = driver.getContext(plugin);
2069
1053
  const hrStart = process.hrtime();
@@ -2071,13 +1055,13 @@ async function safeBuild(options, overrides) {
2071
1055
  const root = resolve(config.root, config.output.path);
2072
1056
  try {
2073
1057
  const timestamp = /* @__PURE__ */ new Date();
2074
- await events.emit("plugin:start", plugin);
2075
- await events.emit("debug", {
1058
+ await hooks.emit("kubb:plugin:start", plugin);
1059
+ await hooks.emit("kubb:debug", {
2076
1060
  date: timestamp,
2077
1061
  logs: ["Starting plugin...", ` • Plugin Name: ${plugin.name}`]
2078
1062
  });
2079
1063
  await plugin.buildStart.call(context);
2080
- if (plugin.schema || plugin.operation || plugin.operations) await runPluginAstHooks(plugin, context);
1064
+ if (plugin.generators?.length || driver.hasRegisteredGenerators(plugin.name)) await runPluginAstHooks(plugin, context);
2081
1065
  if (output) {
2082
1066
  const barrelFiles = await getBarrelFiles(driver.fileManager.files, {
2083
1067
  type: output.barrelType ?? "named",
@@ -2089,11 +1073,11 @@ async function safeBuild(options, overrides) {
2089
1073
  }
2090
1074
  const duration = getElapsedMs(hrStart);
2091
1075
  pluginTimings.set(plugin.name, duration);
2092
- await events.emit("plugin:end", plugin, {
1076
+ await hooks.emit("kubb:plugin:end", plugin, {
2093
1077
  duration,
2094
1078
  success: true
2095
1079
  });
2096
- await events.emit("debug", {
1080
+ await hooks.emit("kubb:debug", {
2097
1081
  date: /* @__PURE__ */ new Date(),
2098
1082
  logs: [`✓ Plugin started successfully (${formatMs(duration)})`]
2099
1083
  });
@@ -2101,12 +1085,12 @@ async function safeBuild(options, overrides) {
2101
1085
  const error = caughtError;
2102
1086
  const errorTimestamp = /* @__PURE__ */ new Date();
2103
1087
  const duration = getElapsedMs(hrStart);
2104
- await events.emit("plugin:end", plugin, {
1088
+ await hooks.emit("kubb:plugin:end", plugin, {
2105
1089
  duration,
2106
1090
  success: false,
2107
1091
  error
2108
1092
  });
2109
- await events.emit("debug", {
1093
+ await hooks.emit("kubb:debug", {
2110
1094
  date: errorTimestamp,
2111
1095
  logs: [
2112
1096
  "✗ Plugin start failed",
@@ -2125,7 +1109,7 @@ async function safeBuild(options, overrides) {
2125
1109
  if (config.output.barrelType) {
2126
1110
  const rootPath = resolve(resolve(config.root), config.output.path, BARREL_FILENAME);
2127
1111
  const rootDir = dirname(rootPath);
2128
- await events.emit("debug", {
1112
+ await hooks.emit("kubb:debug", {
2129
1113
  date: /* @__PURE__ */ new Date(),
2130
1114
  logs: [
2131
1115
  "Generating barrel file",
@@ -2136,7 +1120,7 @@ async function safeBuild(options, overrides) {
2136
1120
  const barrelFiles = driver.fileManager.files.filter((file) => {
2137
1121
  return file.sources.some((source) => source.isIndexable);
2138
1122
  });
2139
- await events.emit("debug", {
1123
+ await hooks.emit("kubb:debug", {
2140
1124
  date: /* @__PURE__ */ new Date(),
2141
1125
  logs: [`Found ${barrelFiles.length} indexable files for barrel export`]
2142
1126
  });
@@ -2156,7 +1140,7 @@ async function safeBuild(options, overrides) {
2156
1140
  meta: {}
2157
1141
  });
2158
1142
  driver.fileManager.upsert(rootFile);
2159
- await events.emit("debug", {
1143
+ await hooks.emit("kubb:debug", {
2160
1144
  date: /* @__PURE__ */ new Date(),
2161
1145
  logs: [`✓ Generated barrel file (${rootFile.exports?.length || 0} exports)`]
2162
1146
  });
@@ -2165,7 +1149,7 @@ async function safeBuild(options, overrides) {
2165
1149
  const parsersMap = /* @__PURE__ */ new Map();
2166
1150
  for (const parser of config.parsers) if (parser.extNames) for (const extname of parser.extNames) parsersMap.set(extname, parser);
2167
1151
  const fileProcessor = new FileProcessor();
2168
- await events.emit("debug", {
1152
+ await hooks.emit("kubb:debug", {
2169
1153
  date: /* @__PURE__ */ new Date(),
2170
1154
  logs: [`Writing ${files.length} files...`]
2171
1155
  });
@@ -2173,10 +1157,10 @@ async function safeBuild(options, overrides) {
2173
1157
  parsers: parsersMap,
2174
1158
  extension: config.output.extension,
2175
1159
  onStart: async (processingFiles) => {
2176
- await events.emit("files:processing:start", processingFiles);
1160
+ await hooks.emit("kubb:files:processing:start", processingFiles);
2177
1161
  },
2178
1162
  onUpdate: async ({ file, source, processed, total, percentage }) => {
2179
- await events.emit("file:processing:update", {
1163
+ await hooks.emit("kubb:file:processing:update", {
2180
1164
  file,
2181
1165
  source,
2182
1166
  processed,
@@ -2190,8 +1174,8 @@ async function safeBuild(options, overrides) {
2190
1174
  }
2191
1175
  },
2192
1176
  onEnd: async (processedFiles) => {
2193
- await events.emit("files:processing:end", processedFiles);
2194
- await events.emit("debug", {
1177
+ await hooks.emit("kubb:files:processing:end", processedFiles);
1178
+ await hooks.emit("kubb:debug", {
2195
1179
  date: /* @__PURE__ */ new Date(),
2196
1180
  logs: [`✓ File write process completed for ${processedFiles.length} files`]
2197
1181
  });
@@ -2201,6 +1185,11 @@ async function safeBuild(options, overrides) {
2201
1185
  const context = driver.getContext(plugin);
2202
1186
  await plugin.buildEnd.call(context);
2203
1187
  }
1188
+ await hooks.emit("kubb:build:end", {
1189
+ files,
1190
+ config,
1191
+ outputDir: resolve(config.root, config.output.path)
1192
+ });
2204
1193
  return {
2205
1194
  failedPlugins,
2206
1195
  files,
@@ -2217,7 +1206,25 @@ async function safeBuild(options, overrides) {
2217
1206
  error,
2218
1207
  sources
2219
1208
  };
1209
+ } finally {
1210
+ driver.dispose();
1211
+ }
1212
+ }
1213
+ async function build(setupResult) {
1214
+ const { files, driver, failedPlugins, pluginTimings, error, sources } = await safeBuild(setupResult);
1215
+ if (error) throw error;
1216
+ if (failedPlugins.size > 0) {
1217
+ const errors = [...failedPlugins].map(({ error }) => error);
1218
+ throw new BuildError(`Build Error with ${failedPlugins.size} failed plugins`, { errors });
2220
1219
  }
1220
+ return {
1221
+ failedPlugins,
1222
+ files,
1223
+ driver,
1224
+ pluginTimings,
1225
+ error: void 0,
1226
+ sources
1227
+ };
2221
1228
  }
2222
1229
  function buildBarrelExports({ barrelFiles, rootDir, existingExports, config, driver }) {
2223
1230
  const pluginNameMap = /* @__PURE__ */ new Map();
@@ -2239,10 +1246,6 @@ function buildBarrelExports({ barrelFiles, rootDir, existingExports, config, dri
2239
1246
  });
2240
1247
  });
2241
1248
  }
2242
- /**
2243
- * Maps the resolved `Config['input']` shape into an `AdapterSource` that
2244
- * the adapter's `parse()` can consume.
2245
- */
2246
1249
  function inputToAdapterSource(config) {
2247
1250
  if (Array.isArray(config.input)) return {
2248
1251
  type: "paths",
@@ -2261,25 +1264,56 @@ function inputToAdapterSource(config) {
2261
1264
  path: resolve(config.root, config.input.path)
2262
1265
  };
2263
1266
  }
2264
- //#endregion
2265
- //#region src/createAdapter.ts
2266
1267
  /**
2267
- * Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
1268
+ * Creates a Kubb instance bound to a single config entry.
1269
+ *
1270
+ * The instance holds shared state (`hooks`, `sources`, `driver`, `config`) across the
1271
+ * `setup → build` lifecycle. Attach event listeners to `kubb.hooks` before
1272
+ * calling `setup()` or `build()`.
2268
1273
  *
2269
1274
  * @example
2270
- * export const myAdapter = createAdapter<MyAdapter>((options) => {
2271
- * return {
2272
- * name: 'my-adapter',
2273
- * options,
2274
- * async parse(source) { ... },
2275
- * }
1275
+ * ```ts
1276
+ * const kubb = createKubb({ config })
1277
+ *
1278
+ * kubb.hooks.on('kubb:plugin:end', (plugin, { duration }) => {
1279
+ * console.log(`${plugin.name} completed in ${duration}ms`)
2276
1280
  * })
2277
1281
  *
2278
- * // instantiate
2279
- * const adapter = myAdapter({ validate: true })
1282
+ * const { files, failedPlugins } = await kubb.safeBuild()
1283
+ * ```
2280
1284
  */
2281
- function createAdapter(build) {
2282
- return (options) => build(options ?? {});
1285
+ function createKubb(options) {
1286
+ const hooks = options.hooks ?? new AsyncEventEmitter();
1287
+ let setupResult;
1288
+ const instance = {
1289
+ get hooks() {
1290
+ return hooks;
1291
+ },
1292
+ get sources() {
1293
+ return setupResult?.sources ?? /* @__PURE__ */ new Map();
1294
+ },
1295
+ get driver() {
1296
+ return setupResult?.driver;
1297
+ },
1298
+ get config() {
1299
+ return setupResult?.config;
1300
+ },
1301
+ async setup() {
1302
+ setupResult = await setup({
1303
+ config: options.config,
1304
+ hooks
1305
+ });
1306
+ },
1307
+ async build() {
1308
+ if (!setupResult) await instance.setup();
1309
+ return build(setupResult);
1310
+ },
1311
+ async safeBuild() {
1312
+ if (!setupResult) await instance.setup();
1313
+ return safeBuild(setupResult);
1314
+ }
1315
+ };
1316
+ return instance;
2283
1317
  }
2284
1318
  //#endregion
2285
1319
  //#region src/createPlugin.ts
@@ -2300,69 +1334,53 @@ function createAdapter(build) {
2300
1334
  * // instantiate
2301
1335
  * const plugin = myPlugin({ output: { path: 'src/gen' } })
2302
1336
  * ```
1337
+ * @deprecated use definePlugin instead
2303
1338
  */
2304
1339
  function createPlugin(build) {
2305
1340
  return (options) => build(options ?? {});
2306
1341
  }
2307
1342
  //#endregion
2308
- //#region src/defineConfig.ts
2309
- function defineConfig(config) {
2310
- return config;
2311
- }
2312
- //#endregion
2313
- //#region src/defineGenerator.ts
2314
- /**
2315
- * Defines a generator. Returns the object as-is with correct `this` typings.
2316
- * No type discrimination (`type: 'react' | 'core'`) needed — `applyHookResult`
2317
- * handles React elements and `File[]` uniformly.
2318
- */
2319
- function defineGenerator(generator) {
2320
- return generator;
2321
- }
1343
+ //#region src/createRenderer.ts
2322
1344
  /**
2323
- * Merges an array of generators into a single generator.
2324
- *
2325
- * The merged generator's `schema`, `operation`, and `operations` methods run
2326
- * the corresponding method from each input generator in sequence, applying each
2327
- * result via `applyHookResult`. This eliminates the need to write the loop
2328
- * manually in each plugin.
1345
+ * Creates a renderer factory for use in generator definitions.
2329
1346
  *
2330
- * @param generators - Array of generators to merge into a single generator.
1347
+ * Wrap your renderer factory function with this helper to register it as the
1348
+ * renderer for a generator. Core will call this factory once per render cycle
1349
+ * to obtain a fresh renderer instance.
2331
1350
  *
2332
1351
  * @example
2333
1352
  * ```ts
2334
- * const merged = mergeGenerators(generators)
1353
+ * // packages/renderer-jsx/src/index.ts
1354
+ * export const jsxRenderer = createRenderer(() => {
1355
+ * const runtime = new Runtime()
1356
+ * return {
1357
+ * async render(element) { await runtime.render(element) },
1358
+ * get files() { return runtime.nodes },
1359
+ * unmount(error) { runtime.unmount(error) },
1360
+ * }
1361
+ * })
2335
1362
  *
2336
- * return {
2337
- * name: pluginName,
2338
- * schema: merged.schema,
2339
- * operation: merged.operation,
2340
- * operations: merged.operations,
2341
- * }
1363
+ * // packages/plugin-zod/src/generators/zodGenerator.tsx
1364
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
1365
+ * export const zodGenerator = defineGenerator<PluginZod>({
1366
+ * name: 'zod',
1367
+ * renderer: jsxRenderer,
1368
+ * schema(node, options) { return <File ...>...</File> },
1369
+ * })
2342
1370
  * ```
2343
1371
  */
2344
- function mergeGenerators(generators) {
2345
- return {
2346
- name: generators.length > 0 ? generators.map((g) => g.name).join("+") : "merged",
2347
- async schema(node, options) {
2348
- for (const gen of generators) {
2349
- if (!gen.schema) continue;
2350
- await applyHookResult(await gen.schema.call(this, node, options), this.driver);
2351
- }
2352
- },
2353
- async operation(node, options) {
2354
- for (const gen of generators) {
2355
- if (!gen.operation) continue;
2356
- await applyHookResult(await gen.operation.call(this, node, options), this.driver);
2357
- }
2358
- },
2359
- async operations(nodes, options) {
2360
- for (const gen of generators) {
2361
- if (!gen.operations) continue;
2362
- await applyHookResult(await gen.operations.call(this, nodes, options), this.driver);
2363
- }
2364
- }
2365
- };
1372
+ function createRenderer(factory) {
1373
+ return factory;
1374
+ }
1375
+ //#endregion
1376
+ //#region src/defineGenerator.ts
1377
+ /**
1378
+ * Defines a generator. Returns the object as-is with correct `this` typings.
1379
+ * `applyHookResult` handles renderer elements and `File[]` uniformly using
1380
+ * the generator's declared `renderer` factory.
1381
+ */
1382
+ function defineGenerator(generator) {
1383
+ return generator;
2366
1384
  }
2367
1385
  //#endregion
2368
1386
  //#region src/defineLogger.ts
@@ -2373,8 +1391,8 @@ function mergeGenerators(generators) {
2373
1391
  * export const myLogger = defineLogger({
2374
1392
  * name: 'my-logger',
2375
1393
  * install(context, options) {
2376
- * context.on('info', (message) => console.log('ℹ', message))
2377
- * context.on('error', (error) => console.error('✗', error.message))
1394
+ * context.on('kubb:info', (message) => console.log('ℹ', message))
1395
+ * context.on('kubb:error', (error) => console.error('✗', error.message))
2378
1396
  * },
2379
1397
  * })
2380
1398
  */
@@ -2407,359 +1425,6 @@ function defineParser(parser) {
2407
1425
  return parser;
2408
1426
  }
2409
1427
  //#endregion
2410
- //#region src/definePresets.ts
2411
- /**
2412
- * Creates a typed presets registry object — a named collection of {@link Preset} entries.
2413
- *
2414
- * @example
2415
- * import { definePreset, definePresets } from '@kubb/core'
2416
- * import { resolverTsLegacy } from '@kubb/plugin-ts'
2417
- *
2418
- * export const myPresets = definePresets({
2419
- * kubbV4: definePreset('kubbV4', { resolvers: [resolverTsLegacy] }),
2420
- * })
2421
- */
2422
- function definePresets(presets) {
2423
- return presets;
2424
- }
2425
- //#endregion
2426
- //#region src/defineResolver.ts
2427
- /**
2428
- * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
2429
- */
2430
- function matchesOperationPattern(node, type, pattern) {
2431
- switch (type) {
2432
- case "tag": return node.tags.some((tag) => !!tag.match(pattern));
2433
- case "operationId": return !!node.operationId.match(pattern);
2434
- case "path": return !!node.path.match(pattern);
2435
- case "method": return !!node.method.toLowerCase().match(pattern);
2436
- case "contentType": return !!node.requestBody?.contentType?.match(pattern);
2437
- default: return false;
2438
- }
2439
- }
2440
- /**
2441
- * Checks if a schema matches a pattern for a given filter type (`schemaName`).
2442
- *
2443
- * Returns `null` when the filter type doesn't apply to schemas.
2444
- */
2445
- function matchesSchemaPattern(node, type, pattern) {
2446
- switch (type) {
2447
- case "schemaName": return node.name ? !!node.name.match(pattern) : false;
2448
- default: return null;
2449
- }
2450
- }
2451
- /**
2452
- * Default name resolver used by `defineResolver`.
2453
- *
2454
- * - `camelCase` for `function` and `file` types.
2455
- * - `PascalCase` for `type`.
2456
- * - `camelCase` for everything else.
2457
- */
2458
- function defaultResolver(name, type) {
2459
- let resolvedName = camelCase(name);
2460
- if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
2461
- if (type === "type") resolvedName = pascalCase(name);
2462
- return resolvedName;
2463
- }
2464
- /**
2465
- * Default option resolver — applies include/exclude filters and merges matching override options.
2466
- *
2467
- * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
2468
- *
2469
- * @example Include/exclude filtering
2470
- * ```ts
2471
- * const options = defaultResolveOptions(operationNode, {
2472
- * options: { output: 'types' },
2473
- * exclude: [{ type: 'tag', pattern: 'internal' }],
2474
- * })
2475
- * // → null when node has tag 'internal'
2476
- * ```
2477
- *
2478
- * @example Override merging
2479
- * ```ts
2480
- * const options = defaultResolveOptions(operationNode, {
2481
- * options: { enumType: 'asConst' },
2482
- * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
2483
- * })
2484
- * // → { enumType: 'enum' } when operationId matches
2485
- * ```
2486
- */
2487
- function defaultResolveOptions(node, { options, exclude = [], include, override = [] }) {
2488
- if (isOperationNode(node)) {
2489
- if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
2490
- if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
2491
- const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
2492
- return {
2493
- ...options,
2494
- ...overrideOptions
2495
- };
2496
- }
2497
- if (isSchemaNode(node)) {
2498
- if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
2499
- if (include) {
2500
- const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
2501
- if (applicable.length > 0 && !applicable.includes(true)) return null;
2502
- }
2503
- const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
2504
- return {
2505
- ...options,
2506
- ...overrideOptions
2507
- };
2508
- }
2509
- return options;
2510
- }
2511
- /**
2512
- * Default path resolver used by `defineResolver`.
2513
- *
2514
- * - Returns the output directory in `single` mode.
2515
- * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
2516
- * - Falls back to a flat `output/baseName` path otherwise.
2517
- *
2518
- * A custom `group.name` function overrides the default subdirectory naming.
2519
- * For `tag` groups the default is `${camelCase(tag)}Controller`.
2520
- * For `path` groups the default is the first path segment after `/`.
2521
- *
2522
- * @example Flat output
2523
- * ```ts
2524
- * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
2525
- * // → '/src/types/petTypes.ts'
2526
- * ```
2527
- *
2528
- * @example Tag-based grouping
2529
- * ```ts
2530
- * defaultResolvePath(
2531
- * { baseName: 'petTypes.ts', tag: 'pets' },
2532
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
2533
- * )
2534
- * // → '/src/types/petsController/petTypes.ts'
2535
- * ```
2536
- *
2537
- * @example Path-based grouping
2538
- * ```ts
2539
- * defaultResolvePath(
2540
- * { baseName: 'petTypes.ts', path: '/pets/list' },
2541
- * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
2542
- * )
2543
- * // → '/src/types/pets/petTypes.ts'
2544
- * ```
2545
- *
2546
- * @example Single-file mode
2547
- * ```ts
2548
- * defaultResolvePath(
2549
- * { baseName: 'petTypes.ts', pathMode: 'single' },
2550
- * { root: '/src', output: { path: 'types' } },
2551
- * )
2552
- * // → '/src/types'
2553
- * ```
2554
- */
2555
- function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }, { root, output, group }) {
2556
- if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single") return path.resolve(root, output.path);
2557
- if (group && (groupPath || tag)) return path.resolve(root, output.path, group.name({ group: group.type === "path" ? groupPath : tag }), baseName);
2558
- return path.resolve(root, output.path, baseName);
2559
- }
2560
- /**
2561
- * Default file resolver used by `defineResolver`.
2562
- *
2563
- * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
2564
- * path resolution (`resolver.resolvePath`). The resolved file always has empty
2565
- * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
2566
- *
2567
- * In `single` mode the name is omitted and the file sits directly in the output directory.
2568
- *
2569
- * @example Resolve a schema file
2570
- * ```ts
2571
- * const file = defaultResolveFile.call(resolver,
2572
- * { name: 'pet', extname: '.ts' },
2573
- * { root: '/src', output: { path: 'types' } },
2574
- * )
2575
- * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
2576
- * ```
2577
- *
2578
- * @example Resolve an operation file with tag grouping
2579
- * ```ts
2580
- * const file = defaultResolveFile.call(resolver,
2581
- * { name: 'listPets', extname: '.ts', tag: 'pets' },
2582
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
2583
- * )
2584
- * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
2585
- * ```
2586
- */
2587
- function defaultResolveFile({ name, extname, tag, path: groupPath }, context) {
2588
- const pathMode = getMode(path.resolve(context.root, context.output.path));
2589
- const baseName = `${pathMode === "single" ? "" : this.default(name, "file")}${extname}`;
2590
- const filePath = this.resolvePath({
2591
- baseName,
2592
- pathMode,
2593
- tag,
2594
- path: groupPath
2595
- }, context);
2596
- return createFile({
2597
- path: filePath,
2598
- baseName: path.basename(filePath),
2599
- meta: { pluginName: this.pluginName },
2600
- sources: [],
2601
- imports: [],
2602
- exports: []
2603
- });
2604
- }
2605
- /**
2606
- * Generates the default "Generated by Kubb" banner from config and optional node metadata.
2607
- */
2608
- function buildDefaultBanner({ title, description, version, config }) {
2609
- try {
2610
- let source = "";
2611
- if (Array.isArray(config.input)) {
2612
- const first = config.input[0];
2613
- if (first && "path" in first) source = path.basename(first.path);
2614
- } else if ("path" in config.input) source = path.basename(config.input.path);
2615
- else if ("data" in config.input) source = "text content";
2616
- let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
2617
- if (config.output.defaultBanner === "simple") {
2618
- banner += "*/\n";
2619
- return banner;
2620
- }
2621
- if (source) banner += `* Source: ${source}\n`;
2622
- if (title) banner += `* Title: ${title}\n`;
2623
- if (description) {
2624
- const formattedDescription = description.replace(/\n/gm, "\n* ");
2625
- banner += `* Description: ${formattedDescription}\n`;
2626
- }
2627
- if (version) banner += `* OpenAPI spec version: ${version}\n`;
2628
- banner += "*/\n";
2629
- return banner;
2630
- } catch (_error) {
2631
- return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
2632
- }
2633
- }
2634
- /**
2635
- * Default banner resolver — returns the banner string for a generated file.
2636
- *
2637
- * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
2638
- * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
2639
- * from the OAS spec when a `node` is provided).
2640
- *
2641
- * - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
2642
- * - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
2643
- * - When `output.banner` is a string, returns it directly.
2644
- * - When `config.output.defaultBanner` is `false`, returns `undefined`.
2645
- * - Otherwise returns the Kubb "Generated by Kubb" notice.
2646
- *
2647
- * @example String banner overrides default
2648
- * ```ts
2649
- * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
2650
- * // → '// my banner'
2651
- * ```
2652
- *
2653
- * @example Function banner with node
2654
- * ```ts
2655
- * defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
2656
- * // → '// v3.0.0'
2657
- * ```
2658
- *
2659
- * @example No user banner — Kubb notice with OAS metadata
2660
- * ```ts
2661
- * defaultResolveBanner(inputNode, { config })
2662
- * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
2663
- * ```
2664
- *
2665
- * @example Disabled default banner
2666
- * ```ts
2667
- * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
2668
- * // → undefined
2669
- * ```
2670
- */
2671
- function defaultResolveBanner(node, { output, config }) {
2672
- if (typeof output?.banner === "function") return output.banner(node);
2673
- if (typeof output?.banner === "string") return output.banner;
2674
- if (config.output.defaultBanner === false) return;
2675
- return buildDefaultBanner({
2676
- title: node?.meta?.title,
2677
- version: node?.meta?.version,
2678
- config
2679
- });
2680
- }
2681
- /**
2682
- * Default footer resolver — returns the footer string for a generated file.
2683
- *
2684
- * - When `output.footer` is a function and `node` is provided, calls it with the node.
2685
- * - When `output.footer` is a function and `node` is absent, returns `undefined`.
2686
- * - When `output.footer` is a string, returns it directly.
2687
- * - Otherwise returns `undefined`.
2688
- *
2689
- * @example String footer
2690
- * ```ts
2691
- * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
2692
- * // → '// end of file'
2693
- * ```
2694
- *
2695
- * @example Function footer with node
2696
- * ```ts
2697
- * defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
2698
- * // → '// Pet Store'
2699
- * ```
2700
- */
2701
- function defaultResolveFooter(node, { output }) {
2702
- if (typeof output?.footer === "function") return node ? output.footer(node) : void 0;
2703
- if (typeof output?.footer === "string") return output.footer;
2704
- }
2705
- /**
2706
- * Defines a resolver for a plugin, injecting built-in defaults for name casing,
2707
- * include/exclude/override filtering, path resolution, and file construction.
2708
- *
2709
- * All four defaults can be overridden by providing them in the builder function:
2710
- * - `default` — name casing strategy (camelCase / PascalCase)
2711
- * - `resolveOptions` — include/exclude/override filtering
2712
- * - `resolvePath` — output path computation
2713
- * - `resolveFile` — full `FileNode` construction
2714
- *
2715
- * Methods in the builder have access to `this` (the full resolver object), so they
2716
- * can call other resolver methods without circular imports.
2717
- *
2718
- * @example Basic resolver with naming helpers
2719
- * ```ts
2720
- * export const resolver = defineResolver<PluginTs>(() => ({
2721
- * name: 'default',
2722
- * resolveName(node) {
2723
- * return this.default(node.name, 'function')
2724
- * },
2725
- * resolveTypedName(node) {
2726
- * return this.default(node.name, 'type')
2727
- * },
2728
- * }))
2729
- * ```
2730
- *
2731
- * @example Override resolvePath for a custom output structure
2732
- * ```ts
2733
- * export const resolver = defineResolver<PluginTs>(() => ({
2734
- * name: 'custom',
2735
- * resolvePath({ baseName }, { root, output }) {
2736
- * return path.resolve(root, output.path, 'generated', baseName)
2737
- * },
2738
- * }))
2739
- * ```
2740
- *
2741
- * @example Use this.default inside a helper
2742
- * ```ts
2743
- * export const resolver = defineResolver<PluginTs>(() => ({
2744
- * name: 'default',
2745
- * resolveParamName(node, param) {
2746
- * return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
2747
- * },
2748
- * }))
2749
- * ```
2750
- */
2751
- function defineResolver(build) {
2752
- return {
2753
- default: defaultResolver,
2754
- resolveOptions: defaultResolveOptions,
2755
- resolvePath: defaultResolvePath,
2756
- resolveFile: defaultResolveFile,
2757
- resolveBanner: defaultResolveBanner,
2758
- resolveFooter: defaultResolveFooter,
2759
- ...build()
2760
- };
2761
- }
2762
- //#endregion
2763
1428
  //#region src/storages/memoryStorage.ts
2764
1429
  /**
2765
1430
  * In-memory storage driver. Useful for testing and dry-run scenarios where
@@ -2847,22 +1512,6 @@ async function detectFormatter() {
2847
1512
  return null;
2848
1513
  }
2849
1514
  //#endregion
2850
- //#region src/utils/getConfigs.ts
2851
- /**
2852
- * Resolves a {@link ConfigInput} into a normalized array of {@link Config} objects.
2853
- *
2854
- * - Awaits the config when it is a `Promise`.
2855
- * - Calls the factory function with `args` when the config is a function.
2856
- * - Wraps a single config object in an array for uniform downstream handling.
2857
- */
2858
- async function getConfigs(config, args) {
2859
- const resolved = await (typeof config === "function" ? config(args) : config);
2860
- return (Array.isArray(resolved) ? resolved : [resolved]).map((item) => ({
2861
- plugins: [],
2862
- ...item
2863
- }));
2864
- }
2865
- //#endregion
2866
1515
  //#region src/utils/getFunctionParams.ts
2867
1516
  function order(items) {
2868
1517
  return sortBy(items.filter(Boolean), ([_key, item]) => {
@@ -2981,47 +1630,6 @@ var FunctionParams = class FunctionParams {
2981
1630
  }
2982
1631
  };
2983
1632
  //#endregion
2984
- //#region src/utils/getPreset.ts
2985
- /**
2986
- * Returns a copy of `defaults` where each function in `userOverrides` is wrapped
2987
- * so a `null`/`undefined` return falls back to the original. Non-function values
2988
- * are assigned directly. All calls use the merged object as `this`.
2989
- */
2990
- function withFallback(defaults, userOverrides) {
2991
- const merged = { ...defaults };
2992
- for (const key of Object.keys(userOverrides)) {
2993
- const userVal = userOverrides[key];
2994
- const defaultVal = defaults[key];
2995
- if (typeof userVal === "function" && typeof defaultVal === "function") merged[key] = (...args) => userVal.apply(merged, args) ?? defaultVal.apply(merged, args);
2996
- else if (userVal !== void 0) merged[key] = userVal;
2997
- }
2998
- return merged;
2999
- }
3000
- /**
3001
- * Resolves a named preset into a resolver, transformer, and generators.
3002
- *
3003
- * - Selects the preset resolver; wraps it with user overrides using null/undefined fallback.
3004
- * - Composes the preset's transformers into a single visitor; wraps it with the user transformer using null/undefined fallback.
3005
- * - Combines preset generators with user-supplied generators; falls back to the `default` preset's generators when neither provides any.
3006
- */
3007
- function getPreset(params) {
3008
- const { preset: presetName, presets, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = params;
3009
- const preset = presets[presetName];
3010
- const presetResolver = preset?.resolver ?? presets["default"].resolver;
3011
- const resolver = userResolver ? withFallback(presetResolver, userResolver) : presetResolver;
3012
- const presetTransformers = preset?.transformers ?? [];
3013
- const presetTransformer = presetTransformers.length > 0 ? composeTransformers$1(...presetTransformers) : void 0;
3014
- const transformer = presetTransformer && userTransformer ? withFallback(presetTransformer, userTransformer) : userTransformer ?? presetTransformer;
3015
- const presetGenerators = preset?.generators ?? [];
3016
- const defaultGenerators = presets["default"]?.generators ?? [];
3017
- return {
3018
- resolver,
3019
- transformer,
3020
- generators: presetGenerators.length > 0 || userGenerators.length > 0 ? [...presetGenerators, ...userGenerators] : defaultGenerators,
3021
- preset
3022
- };
3023
- }
3024
- //#endregion
3025
1633
  //#region src/utils/linters.ts
3026
1634
  /**
3027
1635
  * Returns `true` when the given linter is installed and callable.
@@ -3063,7 +1671,7 @@ async function detectLinter() {
3063
1671
  //#endregion
3064
1672
  //#region src/utils/packageJSON.ts
3065
1673
  function getPackageJSONSync(cwd) {
3066
- const pkgPath = pkg.up({ cwd });
1674
+ const pkgPath = findPackageJSON(cwd);
3067
1675
  if (!pkgPath) return null;
3068
1676
  return JSON.parse(readSync(pkgPath));
3069
1677
  }
@@ -3104,6 +1712,6 @@ function satisfiesDependency(dependency, version, cwd) {
3104
1712
  return satisfies(semVer, version);
3105
1713
  }
3106
1714
  //#endregion
3107
- export { AsyncEventEmitter, FunctionParams, PluginDriver, URLPath, build, build as default, buildDefaultBanner, composeTransformers, createAdapter, createFunctionParams, createPlugin, createStorage, defaultResolveBanner, defaultResolveFile, defaultResolveFooter, defaultResolveOptions, defaultResolvePath, defineConfig, defineGenerator, defineLogger, defineParser, definePresets, definePrinter, defineResolver, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getConfigs, getFunctionParams, getMode, getPreset, isInputPath, linters, logLevel, memoryStorage, mergeGenerators, safeBuild, satisfiesDependency, setup };
1715
+ export { AsyncEventEmitter, FileManager, FileProcessor, FunctionParams, PluginDriver, URLPath, ast, buildDefaultBanner, composeTransformers, createAdapter, createFunctionParams, createKubb, createPlugin, createRenderer, createStorage, defaultResolveBanner, defaultResolveFile, defaultResolveFooter, defaultResolveOptions, defaultResolvePath, defineGenerator, defineLogger, defineParser, definePlugin, definePrinter, defineResolver, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getFunctionParams, getMode, isInputPath, linters, logLevel, memoryStorage, satisfiesDependency };
3108
1716
 
3109
1717
  //# sourceMappingURL=index.js.map