@kubb/core 5.0.0-alpha.35 → 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.
package/dist/index.js CHANGED
@@ -1,12 +1,11 @@
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
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 path, { dirname, join, posix, resolve } from "node:path";
6
7
  import * as ast from "@kubb/ast";
7
- import { composeTransformers, createExport, createFile, createSource, definePrinter, extractStringsFromNodes, isOperationNode, isSchemaNode, transform, walk } from "@kubb/ast";
8
- import { performance } from "node:perf_hooks";
9
- import { deflateSync } from "fflate";
8
+ import { composeTransformers, createExport, createFile, createSource, definePrinter, extractStringsFromNodes, transform, walk } from "@kubb/ast";
10
9
  import { x } from "tinyexec";
11
10
  import { version } from "node:process";
12
11
  import { sortBy } from "remeda";
@@ -151,64 +150,6 @@ var AsyncEventEmitter = class {
151
150
  }
152
151
  };
153
152
  //#endregion
154
- //#region ../../internals/utils/src/casing.ts
155
- /**
156
- * Shared implementation for camelCase and PascalCase conversion.
157
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
158
- * and capitalizes each word according to `pascal`.
159
- *
160
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
161
- */
162
- function toCamelOrPascal(text, pascal) {
163
- 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) => {
164
- if (word.length > 1 && word === word.toUpperCase()) return word;
165
- if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
166
- return word.charAt(0).toUpperCase() + word.slice(1);
167
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
168
- }
169
- /**
170
- * Splits `text` on `.` and applies `transformPart` to each segment.
171
- * The last segment receives `isLast = true`, all earlier segments receive `false`.
172
- * Segments are joined with `/` to form a file path.
173
- *
174
- * Only splits on dots followed by a letter so that version numbers
175
- * embedded in operationIds (e.g. `v2025.0`) are kept intact.
176
- */
177
- function applyToFileParts(text, transformPart) {
178
- const parts = text.split(/\.(?=[a-zA-Z])/);
179
- return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
180
- }
181
- /**
182
- * Converts `text` to camelCase.
183
- * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
184
- *
185
- * @example
186
- * camelCase('hello-world') // 'helloWorld'
187
- * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
188
- */
189
- function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
190
- if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
191
- prefix,
192
- suffix
193
- } : {}));
194
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
195
- }
196
- /**
197
- * Converts `text` to PascalCase.
198
- * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
199
- *
200
- * @example
201
- * pascalCase('hello-world') // 'HelloWorld'
202
- * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
203
- */
204
- function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
205
- if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
206
- prefix,
207
- suffix
208
- }) : camelCase(part));
209
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
210
- }
211
- //#endregion
212
153
  //#region ../../internals/utils/src/time.ts
213
154
  /**
214
155
  * Calculates elapsed time in milliseconds from a high-resolution `process.hrtime` start time.
@@ -362,159 +303,6 @@ async function clean(path) {
362
303
  });
363
304
  }
364
305
  //#endregion
365
- //#region ../../internals/utils/src/string.ts
366
- /**
367
- * Strips the file extension from a path or file name.
368
- * Only removes the last `.ext` segment when the dot is not part of a directory name.
369
- *
370
- * @example
371
- * trimExtName('petStore.ts') // 'petStore'
372
- * trimExtName('/src/models/pet.ts') // '/src/models/pet'
373
- * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
374
- * trimExtName('noExtension') // 'noExtension'
375
- */
376
- function trimExtName$1(text) {
377
- const dotIndex = text.lastIndexOf(".");
378
- if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
379
- return text;
380
- }
381
- __name(trimExtName$1, "trimExtName");
382
- //#endregion
383
- //#region ../../internals/utils/src/promise.ts
384
- /** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.
385
- *
386
- * @example
387
- * ```ts
388
- * const results = await Promise.allSettled([p1, p2])
389
- * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)
390
- * ```
391
- */
392
- function isPromiseRejectedResult(result) {
393
- return result.status === "rejected";
394
- }
395
- //#endregion
396
- //#region ../../internals/utils/src/reserved.ts
397
- /**
398
- * JavaScript and Java reserved words.
399
- * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
400
- */
401
- const reservedWords = new Set([
402
- "abstract",
403
- "arguments",
404
- "boolean",
405
- "break",
406
- "byte",
407
- "case",
408
- "catch",
409
- "char",
410
- "class",
411
- "const",
412
- "continue",
413
- "debugger",
414
- "default",
415
- "delete",
416
- "do",
417
- "double",
418
- "else",
419
- "enum",
420
- "eval",
421
- "export",
422
- "extends",
423
- "false",
424
- "final",
425
- "finally",
426
- "float",
427
- "for",
428
- "function",
429
- "goto",
430
- "if",
431
- "implements",
432
- "import",
433
- "in",
434
- "instanceof",
435
- "int",
436
- "interface",
437
- "let",
438
- "long",
439
- "native",
440
- "new",
441
- "null",
442
- "package",
443
- "private",
444
- "protected",
445
- "public",
446
- "return",
447
- "short",
448
- "static",
449
- "super",
450
- "switch",
451
- "synchronized",
452
- "this",
453
- "throw",
454
- "throws",
455
- "transient",
456
- "true",
457
- "try",
458
- "typeof",
459
- "var",
460
- "void",
461
- "volatile",
462
- "while",
463
- "with",
464
- "yield",
465
- "Array",
466
- "Date",
467
- "hasOwnProperty",
468
- "Infinity",
469
- "isFinite",
470
- "isNaN",
471
- "isPrototypeOf",
472
- "length",
473
- "Math",
474
- "name",
475
- "NaN",
476
- "Number",
477
- "Object",
478
- "prototype",
479
- "String",
480
- "toString",
481
- "undefined",
482
- "valueOf"
483
- ]);
484
- /**
485
- * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.
486
- *
487
- * @example
488
- * ```ts
489
- * transformReservedWord('class') // '_class'
490
- * transformReservedWord('42foo') // '_42foo'
491
- * transformReservedWord('status') // 'status'
492
- * ```
493
- */
494
- function transformReservedWord(word) {
495
- const firstChar = word.charCodeAt(0);
496
- if (word && (reservedWords.has(word) || firstChar >= 48 && firstChar <= 57)) return `_${word}`;
497
- return word;
498
- }
499
- /**
500
- * Returns `true` when `name` is a syntactically valid JavaScript variable name.
501
- *
502
- * @example
503
- * ```ts
504
- * isValidVarName('status') // true
505
- * isValidVarName('class') // false (reserved word)
506
- * isValidVarName('42foo') // false (starts with digit)
507
- * ```
508
- */
509
- function isValidVarName(name) {
510
- try {
511
- new Function(`var ${name}`);
512
- } catch {
513
- return false;
514
- }
515
- return true;
516
- }
517
- //#endregion
518
306
  //#region ../../internals/utils/src/urlPath.ts
519
307
  /**
520
308
  * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
@@ -661,97 +449,6 @@ var URLPath = class {
661
449
  }
662
450
  };
663
451
  //#endregion
664
- //#region src/constants.ts
665
- /**
666
- * Base URL for the Kubb Studio web app.
667
- */
668
- const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
669
- /**
670
- * File name used for generated barrel (index) files.
671
- */
672
- const BARREL_FILENAME = "index.ts";
673
- /**
674
- * Default banner style written at the top of every generated file.
675
- */
676
- const DEFAULT_BANNER = "simple";
677
- /**
678
- * Default file-extension mapping used when no explicit mapping is configured.
679
- */
680
- const DEFAULT_EXTENSION = { ".ts": ".ts" };
681
- /**
682
- * Numeric log-level thresholds used internally to compare verbosity.
683
- *
684
- * Higher numbers are more verbose.
685
- */
686
- const logLevel = {
687
- silent: Number.NEGATIVE_INFINITY,
688
- error: 0,
689
- warn: 1,
690
- info: 3,
691
- verbose: 4,
692
- debug: 5
693
- };
694
- /**
695
- * CLI command descriptors for each supported linter.
696
- *
697
- * Each entry contains the executable `command`, an `args` factory that maps an
698
- * output path to the correct argument list, and an `errorMessage` shown when
699
- * the linter is not found.
700
- */
701
- const linters = {
702
- eslint: {
703
- command: "eslint",
704
- args: (outputPath) => [outputPath, "--fix"],
705
- errorMessage: "Eslint not found"
706
- },
707
- biome: {
708
- command: "biome",
709
- args: (outputPath) => [
710
- "lint",
711
- "--fix",
712
- outputPath
713
- ],
714
- errorMessage: "Biome not found"
715
- },
716
- oxlint: {
717
- command: "oxlint",
718
- args: (outputPath) => ["--fix", outputPath],
719
- errorMessage: "Oxlint not found"
720
- }
721
- };
722
- /**
723
- * CLI command descriptors for each supported code formatter.
724
- *
725
- * Each entry contains the executable `command`, an `args` factory that maps an
726
- * output path to the correct argument list, and an `errorMessage` shown when
727
- * the formatter is not found.
728
- */
729
- const formatters = {
730
- prettier: {
731
- command: "prettier",
732
- args: (outputPath) => [
733
- "--ignore-unknown",
734
- "--write",
735
- outputPath
736
- ],
737
- errorMessage: "Prettier not found"
738
- },
739
- biome: {
740
- command: "biome",
741
- args: (outputPath) => [
742
- "format",
743
- "--write",
744
- outputPath
745
- ],
746
- errorMessage: "Biome not found"
747
- },
748
- oxfmt: {
749
- command: "oxfmt",
750
- args: (outputPath) => [outputPath],
751
- errorMessage: "Oxfmt not found"
752
- }
753
- };
754
- //#endregion
755
452
  //#region src/createAdapter.ts
756
453
  /**
757
454
  * Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
@@ -772,136 +469,6 @@ function createAdapter(build) {
772
469
  return (options) => build(options ?? {});
773
470
  }
774
471
  //#endregion
775
- //#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
776
- var Node$1 = class {
777
- static {
778
- __name(this, "Node");
779
- }
780
- value;
781
- next;
782
- constructor(value) {
783
- this.value = value;
784
- }
785
- };
786
- var Queue = class {
787
- #head;
788
- #tail;
789
- #size;
790
- constructor() {
791
- this.clear();
792
- }
793
- enqueue(value) {
794
- const node = new Node$1(value);
795
- if (this.#head) {
796
- this.#tail.next = node;
797
- this.#tail = node;
798
- } else {
799
- this.#head = node;
800
- this.#tail = node;
801
- }
802
- this.#size++;
803
- }
804
- dequeue() {
805
- const current = this.#head;
806
- if (!current) return;
807
- this.#head = this.#head.next;
808
- this.#size--;
809
- if (!this.#head) this.#tail = void 0;
810
- return current.value;
811
- }
812
- peek() {
813
- if (!this.#head) return;
814
- return this.#head.value;
815
- }
816
- clear() {
817
- this.#head = void 0;
818
- this.#tail = void 0;
819
- this.#size = 0;
820
- }
821
- get size() {
822
- return this.#size;
823
- }
824
- *[Symbol.iterator]() {
825
- let current = this.#head;
826
- while (current) {
827
- yield current.value;
828
- current = current.next;
829
- }
830
- }
831
- *drain() {
832
- while (this.#head) yield this.dequeue();
833
- }
834
- };
835
- //#endregion
836
- //#region ../../node_modules/.pnpm/p-limit@7.3.0/node_modules/p-limit/index.js
837
- function pLimit(concurrency) {
838
- let rejectOnClear = false;
839
- if (typeof concurrency === "object") ({concurrency, rejectOnClear = false} = concurrency);
840
- validateConcurrency(concurrency);
841
- if (typeof rejectOnClear !== "boolean") throw new TypeError("Expected `rejectOnClear` to be a boolean");
842
- const queue = new Queue();
843
- let activeCount = 0;
844
- const resumeNext = () => {
845
- if (activeCount < concurrency && queue.size > 0) {
846
- activeCount++;
847
- queue.dequeue().run();
848
- }
849
- };
850
- const next = () => {
851
- activeCount--;
852
- resumeNext();
853
- };
854
- const run = async (function_, resolve, arguments_) => {
855
- const result = (async () => function_(...arguments_))();
856
- resolve(result);
857
- try {
858
- await result;
859
- } catch {}
860
- next();
861
- };
862
- const enqueue = (function_, resolve, reject, arguments_) => {
863
- const queueItem = { reject };
864
- new Promise((internalResolve) => {
865
- queueItem.run = internalResolve;
866
- queue.enqueue(queueItem);
867
- }).then(run.bind(void 0, function_, resolve, arguments_));
868
- if (activeCount < concurrency) resumeNext();
869
- };
870
- const generator = (function_, ...arguments_) => new Promise((resolve, reject) => {
871
- enqueue(function_, resolve, reject, arguments_);
872
- });
873
- Object.defineProperties(generator, {
874
- activeCount: { get: () => activeCount },
875
- pendingCount: { get: () => queue.size },
876
- clearQueue: { value() {
877
- if (!rejectOnClear) {
878
- queue.clear();
879
- return;
880
- }
881
- const abortError = AbortSignal.abort().reason;
882
- while (queue.size > 0) queue.dequeue().reject(abortError);
883
- } },
884
- concurrency: {
885
- get: () => concurrency,
886
- set(newConcurrency) {
887
- validateConcurrency(newConcurrency);
888
- concurrency = newConcurrency;
889
- queueMicrotask(() => {
890
- while (activeCount < concurrency && queue.size > 0) resumeNext();
891
- });
892
- }
893
- },
894
- map: { async value(iterable, function_) {
895
- const promises = Array.from(iterable, (value, index) => this(function_, value, index));
896
- return Promise.all(promises);
897
- } }
898
- });
899
- return generator;
900
- }
901
- function validateConcurrency(concurrency) {
902
- if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
903
- }
904
- //#endregion
905
472
  //#region src/FileProcessor.ts
906
473
  function joinSources(file) {
907
474
  return file.sources.map((item) => extractStringsFromNodes(item.nodes)).filter(Boolean).join("\n\n");
@@ -945,1243 +512,6 @@ var FileProcessor = class {
945
512
  }
946
513
  };
947
514
  //#endregion
948
- //#region src/definePlugin.ts
949
- /**
950
- * Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
951
- *
952
- * Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
953
- * so it can normalize them and register their handlers on the `AsyncEventEmitter`.
954
- */
955
- function isHookStylePlugin(plugin) {
956
- return typeof plugin === "object" && plugin !== null && "hooks" in plugin;
957
- }
958
- /**
959
- * Creates a plugin factory using the new hook-style (`hooks:`) API.
960
- *
961
- * The returned factory is called with optional options and produces a `HookStylePlugin`
962
- * that coexists with plugins created via the legacy `createPlugin` API in the same
963
- * `kubb.config.ts`.
964
- *
965
- * Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
966
- * both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
967
- *
968
- * @example
969
- * ```ts
970
- * // With PluginFactoryOptions (recommended for real plugins)
971
- * export const pluginTs = definePlugin<PluginTs>((options) => ({
972
- * name: 'plugin-ts',
973
- * hooks: {
974
- * 'kubb:plugin:setup'(ctx) {
975
- * ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
976
- * },
977
- * },
978
- * }))
979
- * ```
980
- */
981
- function definePlugin(factory) {
982
- return (options) => factory(options ?? {});
983
- }
984
- //#endregion
985
- //#region src/defineResolver.ts
986
- /**
987
- * Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
988
- */
989
- function matchesOperationPattern(node, type, pattern) {
990
- switch (type) {
991
- case "tag": return node.tags.some((tag) => !!tag.match(pattern));
992
- case "operationId": return !!node.operationId.match(pattern);
993
- case "path": return !!node.path.match(pattern);
994
- case "method": return !!node.method.toLowerCase().match(pattern);
995
- case "contentType": return !!node.requestBody?.contentType?.match(pattern);
996
- default: return false;
997
- }
998
- }
999
- /**
1000
- * Checks if a schema matches a pattern for a given filter type (`schemaName`).
1001
- *
1002
- * Returns `null` when the filter type doesn't apply to schemas.
1003
- */
1004
- function matchesSchemaPattern(node, type, pattern) {
1005
- switch (type) {
1006
- case "schemaName": return node.name ? !!node.name.match(pattern) : false;
1007
- default: return null;
1008
- }
1009
- }
1010
- /**
1011
- * Default name resolver used by `defineResolver`.
1012
- *
1013
- * - `camelCase` for `function` and `file` types.
1014
- * - `PascalCase` for `type`.
1015
- * - `camelCase` for everything else.
1016
- */
1017
- function defaultResolver(name, type) {
1018
- let resolvedName = camelCase(name);
1019
- if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
1020
- if (type === "type") resolvedName = pascalCase(name);
1021
- return resolvedName;
1022
- }
1023
- /**
1024
- * Default option resolver — applies include/exclude filters and merges matching override options.
1025
- *
1026
- * Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
1027
- *
1028
- * @example Include/exclude filtering
1029
- * ```ts
1030
- * const options = defaultResolveOptions(operationNode, {
1031
- * options: { output: 'types' },
1032
- * exclude: [{ type: 'tag', pattern: 'internal' }],
1033
- * })
1034
- * // → null when node has tag 'internal'
1035
- * ```
1036
- *
1037
- * @example Override merging
1038
- * ```ts
1039
- * const options = defaultResolveOptions(operationNode, {
1040
- * options: { enumType: 'asConst' },
1041
- * override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
1042
- * })
1043
- * // → { enumType: 'enum' } when operationId matches
1044
- * ```
1045
- */
1046
- function defaultResolveOptions(node, { options, exclude = [], include, override = [] }) {
1047
- if (isOperationNode(node)) {
1048
- if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
1049
- if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
1050
- const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
1051
- return {
1052
- ...options,
1053
- ...overrideOptions
1054
- };
1055
- }
1056
- if (isSchemaNode(node)) {
1057
- if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
1058
- if (include) {
1059
- const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
1060
- if (applicable.length > 0 && !applicable.includes(true)) return null;
1061
- }
1062
- const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
1063
- return {
1064
- ...options,
1065
- ...overrideOptions
1066
- };
1067
- }
1068
- return options;
1069
- }
1070
- /**
1071
- * Default path resolver used by `defineResolver`.
1072
- *
1073
- * - Returns the output directory in `single` mode.
1074
- * - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
1075
- * - Falls back to a flat `output/baseName` path otherwise.
1076
- *
1077
- * A custom `group.name` function overrides the default subdirectory naming.
1078
- * For `tag` groups the default is `${camelCase(tag)}Controller`.
1079
- * For `path` groups the default is the first path segment after `/`.
1080
- *
1081
- * @example Flat output
1082
- * ```ts
1083
- * defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
1084
- * // → '/src/types/petTypes.ts'
1085
- * ```
1086
- *
1087
- * @example Tag-based grouping
1088
- * ```ts
1089
- * defaultResolvePath(
1090
- * { baseName: 'petTypes.ts', tag: 'pets' },
1091
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
1092
- * )
1093
- * // → '/src/types/petsController/petTypes.ts'
1094
- * ```
1095
- *
1096
- * @example Path-based grouping
1097
- * ```ts
1098
- * defaultResolvePath(
1099
- * { baseName: 'petTypes.ts', path: '/pets/list' },
1100
- * { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
1101
- * )
1102
- * // → '/src/types/pets/petTypes.ts'
1103
- * ```
1104
- *
1105
- * @example Single-file mode
1106
- * ```ts
1107
- * defaultResolvePath(
1108
- * { baseName: 'petTypes.ts', pathMode: 'single' },
1109
- * { root: '/src', output: { path: 'types' } },
1110
- * )
1111
- * // → '/src/types'
1112
- * ```
1113
- */
1114
- function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }, { root, output, group }) {
1115
- if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single") return path.resolve(root, output.path);
1116
- if (group && (groupPath || tag)) return path.resolve(root, output.path, group.name({ group: group.type === "path" ? groupPath : tag }), baseName);
1117
- return path.resolve(root, output.path, baseName);
1118
- }
1119
- /**
1120
- * Default file resolver used by `defineResolver`.
1121
- *
1122
- * Resolves a `FileNode` by combining name resolution (`resolver.default`) with
1123
- * path resolution (`resolver.resolvePath`). The resolved file always has empty
1124
- * `sources`, `imports`, and `exports` arrays — consumers populate those separately.
1125
- *
1126
- * In `single` mode the name is omitted and the file sits directly in the output directory.
1127
- *
1128
- * @example Resolve a schema file
1129
- * ```ts
1130
- * const file = defaultResolveFile.call(resolver,
1131
- * { name: 'pet', extname: '.ts' },
1132
- * { root: '/src', output: { path: 'types' } },
1133
- * )
1134
- * // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
1135
- * ```
1136
- *
1137
- * @example Resolve an operation file with tag grouping
1138
- * ```ts
1139
- * const file = defaultResolveFile.call(resolver,
1140
- * { name: 'listPets', extname: '.ts', tag: 'pets' },
1141
- * { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
1142
- * )
1143
- * // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
1144
- * ```
1145
- */
1146
- function defaultResolveFile({ name, extname, tag, path: groupPath }, context) {
1147
- const pathMode = getMode(path.resolve(context.root, context.output.path));
1148
- const baseName = `${pathMode === "single" ? "" : this.default(name, "file")}${extname}`;
1149
- const filePath = this.resolvePath({
1150
- baseName,
1151
- pathMode,
1152
- tag,
1153
- path: groupPath
1154
- }, context);
1155
- return createFile({
1156
- path: filePath,
1157
- baseName: path.basename(filePath),
1158
- meta: { pluginName: this.pluginName },
1159
- sources: [],
1160
- imports: [],
1161
- exports: []
1162
- });
1163
- }
1164
- /**
1165
- * Generates the default "Generated by Kubb" banner from config and optional node metadata.
1166
- */
1167
- function buildDefaultBanner({ title, description, version, config }) {
1168
- try {
1169
- let source = "";
1170
- if (Array.isArray(config.input)) {
1171
- const first = config.input[0];
1172
- if (first && "path" in first) source = path.basename(first.path);
1173
- } else if ("path" in config.input) source = path.basename(config.input.path);
1174
- else if ("data" in config.input) source = "text content";
1175
- let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
1176
- if (config.output.defaultBanner === "simple") {
1177
- banner += "*/\n";
1178
- return banner;
1179
- }
1180
- if (source) banner += `* Source: ${source}\n`;
1181
- if (title) banner += `* Title: ${title}\n`;
1182
- if (description) {
1183
- const formattedDescription = description.replace(/\n/gm, "\n* ");
1184
- banner += `* Description: ${formattedDescription}\n`;
1185
- }
1186
- if (version) banner += `* OpenAPI spec version: ${version}\n`;
1187
- banner += "*/\n";
1188
- return banner;
1189
- } catch (_error) {
1190
- return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
1191
- }
1192
- }
1193
- /**
1194
- * Default banner resolver — returns the banner string for a generated file.
1195
- *
1196
- * A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
1197
- * When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
1198
- * from the OAS spec when a `node` is provided).
1199
- *
1200
- * - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
1201
- * - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
1202
- * - When `output.banner` is a string, returns it directly.
1203
- * - When `config.output.defaultBanner` is `false`, returns `undefined`.
1204
- * - Otherwise returns the Kubb "Generated by Kubb" notice.
1205
- *
1206
- * @example String banner overrides default
1207
- * ```ts
1208
- * defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
1209
- * // → '// my banner'
1210
- * ```
1211
- *
1212
- * @example Function banner with node
1213
- * ```ts
1214
- * defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
1215
- * // → '// v3.0.0'
1216
- * ```
1217
- *
1218
- * @example No user banner — Kubb notice with OAS metadata
1219
- * ```ts
1220
- * defaultResolveBanner(inputNode, { config })
1221
- * // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
1222
- * ```
1223
- *
1224
- * @example Disabled default banner
1225
- * ```ts
1226
- * defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
1227
- * // → undefined
1228
- * ```
1229
- */
1230
- function defaultResolveBanner(node, { output, config }) {
1231
- if (typeof output?.banner === "function") return output.banner(node);
1232
- if (typeof output?.banner === "string") return output.banner;
1233
- if (config.output.defaultBanner === false) return;
1234
- return buildDefaultBanner({
1235
- title: node?.meta?.title,
1236
- version: node?.meta?.version,
1237
- config
1238
- });
1239
- }
1240
- /**
1241
- * Default footer resolver — returns the footer string for a generated file.
1242
- *
1243
- * - When `output.footer` is a function and `node` is provided, calls it with the node.
1244
- * - When `output.footer` is a function and `node` is absent, returns `undefined`.
1245
- * - When `output.footer` is a string, returns it directly.
1246
- * - Otherwise returns `undefined`.
1247
- *
1248
- * @example String footer
1249
- * ```ts
1250
- * defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
1251
- * // → '// end of file'
1252
- * ```
1253
- *
1254
- * @example Function footer with node
1255
- * ```ts
1256
- * defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
1257
- * // → '// Pet Store'
1258
- * ```
1259
- */
1260
- function defaultResolveFooter(node, { output }) {
1261
- if (typeof output?.footer === "function") return node ? output.footer(node) : void 0;
1262
- if (typeof output?.footer === "string") return output.footer;
1263
- }
1264
- /**
1265
- * Defines a resolver for a plugin, injecting built-in defaults for name casing,
1266
- * include/exclude/override filtering, path resolution, and file construction.
1267
- *
1268
- * All four defaults can be overridden by providing them in the builder function:
1269
- * - `default` — name casing strategy (camelCase / PascalCase)
1270
- * - `resolveOptions` — include/exclude/override filtering
1271
- * - `resolvePath` — output path computation
1272
- * - `resolveFile` — full `FileNode` construction
1273
- *
1274
- * Methods in the builder have access to `this` (the full resolver object), so they
1275
- * can call other resolver methods without circular imports.
1276
- *
1277
- * @example Basic resolver with naming helpers
1278
- * ```ts
1279
- * export const resolver = defineResolver<PluginTs>(() => ({
1280
- * name: 'default',
1281
- * resolveName(node) {
1282
- * return this.default(node.name, 'function')
1283
- * },
1284
- * resolveTypedName(node) {
1285
- * return this.default(node.name, 'type')
1286
- * },
1287
- * }))
1288
- * ```
1289
- *
1290
- * @example Override resolvePath for a custom output structure
1291
- * ```ts
1292
- * export const resolver = defineResolver<PluginTs>(() => ({
1293
- * name: 'custom',
1294
- * resolvePath({ baseName }, { root, output }) {
1295
- * return path.resolve(root, output.path, 'generated', baseName)
1296
- * },
1297
- * }))
1298
- * ```
1299
- *
1300
- * @example Use this.default inside a helper
1301
- * ```ts
1302
- * export const resolver = defineResolver<PluginTs>(() => ({
1303
- * name: 'default',
1304
- * resolveParamName(node, param) {
1305
- * return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
1306
- * },
1307
- * }))
1308
- * ```
1309
- */
1310
- function defineResolver(build) {
1311
- return {
1312
- default: defaultResolver,
1313
- resolveOptions: defaultResolveOptions,
1314
- resolvePath: defaultResolvePath,
1315
- resolveFile: defaultResolveFile,
1316
- resolveBanner: defaultResolveBanner,
1317
- resolveFooter: defaultResolveFooter,
1318
- ...build()
1319
- };
1320
- }
1321
- //#endregion
1322
- //#region src/devtools.ts
1323
- /**
1324
- * Encodes an `InputNode` as a compressed, URL-safe string.
1325
- *
1326
- * The JSON representation is deflate-compressed with {@link deflateSync} before
1327
- * base64url encoding, which typically reduces payload size by 70–80 % and
1328
- * keeps URLs well within browser and server path-length limits.
1329
- *
1330
- * Use {@link decodeAst} to reverse.
1331
- */
1332
- function encodeAst(input) {
1333
- const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(input)));
1334
- return Buffer.from(compressed).toString("base64url");
1335
- }
1336
- /**
1337
- * Constructs the Kubb Studio URL for the given `InputNode`.
1338
- * When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
1339
- * The `input` is encoded and attached as the `?root=` query parameter so Studio
1340
- * can decode and render it without a round-trip to any server.
1341
- */
1342
- function getStudioUrl(input, studioUrl, options = {}) {
1343
- return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(input)}`;
1344
- }
1345
- /**
1346
- * Opens the Kubb Studio URL for the given `InputNode` in the default browser —
1347
- *
1348
- * Falls back to printing the URL if the browser cannot be launched.
1349
- */
1350
- async function openInStudio(input, studioUrl, options = {}) {
1351
- const url = getStudioUrl(input, studioUrl, options);
1352
- const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
1353
- const args = process.platform === "win32" ? [
1354
- "/c",
1355
- "start",
1356
- "",
1357
- url
1358
- ] : [url];
1359
- try {
1360
- await x(cmd, args);
1361
- } catch {
1362
- console.log(`\n ${url}\n`);
1363
- }
1364
- }
1365
- //#endregion
1366
- //#region src/FileManager.ts
1367
- function mergeFile(a, b) {
1368
- return {
1369
- ...a,
1370
- sources: [...a.sources || [], ...b.sources || []],
1371
- imports: [...a.imports || [], ...b.imports || []],
1372
- exports: [...a.exports || [], ...b.exports || []]
1373
- };
1374
- }
1375
- /**
1376
- * In-memory file store for generated files.
1377
- *
1378
- * Files with the same `path` are merged — sources, imports, and exports are concatenated.
1379
- * The `files` getter returns all stored files sorted by path length (shortest first).
1380
- *
1381
- * @example
1382
- * ```ts
1383
- * import { FileManager } from '@kubb/core'
1384
- *
1385
- * const manager = new FileManager()
1386
- * manager.upsert(myFile)
1387
- * console.log(manager.files) // all stored files
1388
- * ```
1389
- */
1390
- var FileManager = class {
1391
- #cache = /* @__PURE__ */ new Map();
1392
- #filesCache = null;
1393
- /**
1394
- * Adds one or more files. Files with the same path are merged — sources, imports,
1395
- * and exports from all calls with the same path are concatenated together.
1396
- */
1397
- add(...files) {
1398
- const resolvedFiles = [];
1399
- const mergedFiles = /* @__PURE__ */ new Map();
1400
- files.forEach((file) => {
1401
- const existing = mergedFiles.get(file.path);
1402
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
1403
- else mergedFiles.set(file.path, file);
1404
- });
1405
- for (const file of mergedFiles.values()) {
1406
- const resolvedFile = createFile(file);
1407
- this.#cache.set(resolvedFile.path, resolvedFile);
1408
- this.#filesCache = null;
1409
- resolvedFiles.push(resolvedFile);
1410
- }
1411
- return resolvedFiles;
1412
- }
1413
- /**
1414
- * Adds or merges one or more files.
1415
- * If a file with the same path already exists, its sources/imports/exports are merged together.
1416
- */
1417
- upsert(...files) {
1418
- const resolvedFiles = [];
1419
- const mergedFiles = /* @__PURE__ */ new Map();
1420
- files.forEach((file) => {
1421
- const existing = mergedFiles.get(file.path);
1422
- if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
1423
- else mergedFiles.set(file.path, file);
1424
- });
1425
- for (const file of mergedFiles.values()) {
1426
- const existing = this.#cache.get(file.path);
1427
- const resolvedFile = createFile(existing ? mergeFile(existing, file) : file);
1428
- this.#cache.set(resolvedFile.path, resolvedFile);
1429
- this.#filesCache = null;
1430
- resolvedFiles.push(resolvedFile);
1431
- }
1432
- return resolvedFiles;
1433
- }
1434
- getByPath(path) {
1435
- return this.#cache.get(path) ?? null;
1436
- }
1437
- deleteByPath(path) {
1438
- this.#cache.delete(path);
1439
- this.#filesCache = null;
1440
- }
1441
- clear() {
1442
- this.#cache.clear();
1443
- this.#filesCache = null;
1444
- }
1445
- /**
1446
- * All stored files, sorted by path length (shorter paths first).
1447
- * Barrel/index files (e.g. index.ts) are sorted last within each length bucket.
1448
- */
1449
- get files() {
1450
- if (this.#filesCache) return this.#filesCache;
1451
- const keys = [...this.#cache.keys()].sort((a, b) => {
1452
- if (a.length !== b.length) return a.length - b.length;
1453
- const aIsIndex = trimExtName$1(a).endsWith("index");
1454
- if (aIsIndex !== trimExtName$1(b).endsWith("index")) return aIsIndex ? 1 : -1;
1455
- return 0;
1456
- });
1457
- const files = [];
1458
- for (const key of keys) {
1459
- const file = this.#cache.get(key);
1460
- if (file) files.push(file);
1461
- }
1462
- this.#filesCache = files;
1463
- return files;
1464
- }
1465
- };
1466
- //#endregion
1467
- //#region src/renderNode.ts
1468
- /**
1469
- * Handles the return value of a plugin AST hook or generator method.
1470
- *
1471
- * - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
1472
- * - `Array<FileNode>` → added directly into `driver.fileManager`
1473
- * - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
1474
- *
1475
- * Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
1476
- * may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
1477
- */
1478
- async function applyHookResult(result, driver, rendererFactory) {
1479
- if (!result) return;
1480
- if (Array.isArray(result)) {
1481
- driver.fileManager.upsert(...result);
1482
- return;
1483
- }
1484
- if (!rendererFactory) return;
1485
- const renderer = rendererFactory();
1486
- await renderer.render(result);
1487
- driver.fileManager.upsert(...renderer.files);
1488
- renderer.unmount();
1489
- }
1490
- //#endregion
1491
- //#region src/utils/executeStrategies.ts
1492
- /**
1493
- * Runs promise functions in sequence, threading each result into the next call.
1494
- *
1495
- * - Each function receives the accumulated state from the previous call.
1496
- * - Skips functions that return a falsy value (acts as a no-op for that step).
1497
- * - Returns an array of all individual results.
1498
- * @deprecated
1499
- */
1500
- function hookSeq(promises) {
1501
- return promises.filter(Boolean).reduce((promise, func) => {
1502
- if (typeof func !== "function") throw new Error("HookSeq needs a function that returns a promise `() => Promise<unknown>`");
1503
- return promise.then((state) => {
1504
- const calledFunc = func(state);
1505
- if (calledFunc) return calledFunc.then(Array.prototype.concat.bind(state));
1506
- return state;
1507
- });
1508
- }, Promise.resolve([]));
1509
- }
1510
- /**
1511
- * Runs promise functions in sequence and returns the first non-null result.
1512
- *
1513
- * - Stops as soon as `nullCheck` passes for a result (default: `!== null`).
1514
- * - Subsequent functions are skipped once a match is found.
1515
- * @deprecated
1516
- */
1517
- function hookFirst(promises, nullCheck = (state) => state !== null) {
1518
- let promise = Promise.resolve(null);
1519
- for (const func of promises.filter(Boolean)) promise = promise.then((state) => {
1520
- if (nullCheck(state)) return state;
1521
- return func(state);
1522
- });
1523
- return promise;
1524
- }
1525
- /**
1526
- * Runs promise functions concurrently and returns all settled results.
1527
- *
1528
- * - Limits simultaneous executions to `concurrency` (default: unlimited).
1529
- * - Uses `Promise.allSettled` so individual failures do not cancel other tasks.
1530
- * @deprecated
1531
- */
1532
- function hookParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
1533
- const limit = pLimit(concurrency);
1534
- const tasks = promises.filter(Boolean).map((promise) => limit(() => promise()));
1535
- return Promise.allSettled(tasks);
1536
- }
1537
- //#endregion
1538
- //#region src/PluginDriver.ts
1539
- /**
1540
- * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
1541
- *
1542
- * @example
1543
- * ```ts
1544
- * getMode('src/gen/types.ts') // 'single'
1545
- * getMode('src/gen/types') // 'split'
1546
- * ```
1547
- */
1548
- function getMode(fileOrFolder) {
1549
- if (!fileOrFolder) return "split";
1550
- return extname(fileOrFolder) ? "single" : "split";
1551
- }
1552
- const hookFirstNullCheck = (state) => !!state?.result;
1553
- var PluginDriver = class {
1554
- config;
1555
- options;
1556
- /**
1557
- * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
1558
- * the build pipeline after the adapter's `parse()` resolves.
1559
- */
1560
- inputNode = void 0;
1561
- adapter = void 0;
1562
- #studioIsOpen = false;
1563
- /**
1564
- * Central file store for all generated files.
1565
- * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
1566
- * add files; this property gives direct read/write access when needed.
1567
- */
1568
- fileManager = new FileManager();
1569
- plugins = /* @__PURE__ */ new Map();
1570
- /**
1571
- * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
1572
- * Used by the build loop to decide whether to emit generator events for a given plugin.
1573
- */
1574
- #pluginsWithEventGenerators = /* @__PURE__ */ new Set();
1575
- #resolvers = /* @__PURE__ */ new Map();
1576
- #defaultResolvers = /* @__PURE__ */ new Map();
1577
- #hookListeners = /* @__PURE__ */ new Map();
1578
- constructor(config, options) {
1579
- this.config = config;
1580
- this.options = {
1581
- ...options,
1582
- hooks: options.hooks
1583
- };
1584
- config.plugins.map((rawPlugin) => {
1585
- if (isHookStylePlugin(rawPlugin)) return this.#normalizeHookStylePlugin(rawPlugin);
1586
- return {
1587
- ...rawPlugin,
1588
- buildStart: rawPlugin.buildStart ?? (() => {}),
1589
- buildEnd: rawPlugin.buildEnd ?? (() => {})
1590
- };
1591
- }).filter((plugin) => {
1592
- if (typeof plugin.apply === "function") return plugin.apply(config);
1593
- return true;
1594
- }).sort((a, b) => {
1595
- if (b.dependencies?.includes(a.name)) return -1;
1596
- if (a.dependencies?.includes(b.name)) return 1;
1597
- return 0;
1598
- }).forEach((plugin) => {
1599
- this.plugins.set(plugin.name, plugin);
1600
- });
1601
- }
1602
- get hooks() {
1603
- if (!this.options.hooks) throw new Error("hooks are not defined");
1604
- return this.options.hooks;
1605
- }
1606
- /**
1607
- * Creates a `Plugin`-compatible object from a hook-style plugin and registers
1608
- * its lifecycle handlers on the `AsyncEventEmitter`.
1609
- *
1610
- * The normalized plugin has an empty `buildStart` — generators registered via
1611
- * `addGenerator()` in `kubb:plugin:setup` are stored on `normalizedPlugin.generators`
1612
- * and used by `runPluginAstHooks` during the build.
1613
- */
1614
- #normalizeHookStylePlugin(hookPlugin) {
1615
- const generators = [];
1616
- const driver = this;
1617
- const normalizedPlugin = {
1618
- name: hookPlugin.name,
1619
- dependencies: hookPlugin.dependencies,
1620
- options: {
1621
- output: { path: "." },
1622
- exclude: [],
1623
- override: []
1624
- },
1625
- generators,
1626
- inject: () => void 0,
1627
- resolveName(name, type) {
1628
- return driver.getResolver(hookPlugin.name).default(name, type);
1629
- },
1630
- resolvePath(baseName, pathMode, resolveOptions) {
1631
- const resolver = driver.getResolver(hookPlugin.name);
1632
- const opts = normalizedPlugin.options;
1633
- const group = resolveOptions?.group;
1634
- return resolver.resolvePath({
1635
- baseName,
1636
- pathMode,
1637
- tag: group?.tag,
1638
- path: group?.path
1639
- }, {
1640
- root: resolve(driver.config.root, driver.config.output.path),
1641
- output: opts.output,
1642
- group: opts.group
1643
- });
1644
- },
1645
- buildStart() {},
1646
- buildEnd() {}
1647
- };
1648
- this.registerPluginHooks(hookPlugin, normalizedPlugin);
1649
- return normalizedPlugin;
1650
- }
1651
- /**
1652
- * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
1653
- *
1654
- * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
1655
- * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
1656
- * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
1657
- *
1658
- * All other hooks are iterated and registered directly as pass-through listeners.
1659
- * Any event key present in the global `KubbHooks` interface can be subscribed to.
1660
- *
1661
- * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
1662
- * the plugin lifecycle without modifying plugin behavior.
1663
- */
1664
- registerPluginHooks(hookPlugin, normalizedPlugin) {
1665
- const { hooks } = hookPlugin;
1666
- if (hooks["kubb:plugin:setup"]) {
1667
- const setupHandler = (globalCtx) => {
1668
- const pluginCtx = {
1669
- ...globalCtx,
1670
- options: hookPlugin.options ?? {},
1671
- addGenerator: (gen) => {
1672
- this.registerGenerator(normalizedPlugin.name, gen);
1673
- },
1674
- setResolver: (resolver) => {
1675
- this.setPluginResolver(normalizedPlugin.name, resolver);
1676
- },
1677
- setTransformer: (visitor) => {
1678
- normalizedPlugin.transformer = visitor;
1679
- },
1680
- setRenderer: (renderer) => {
1681
- normalizedPlugin.renderer = renderer;
1682
- },
1683
- setOptions: (opts) => {
1684
- normalizedPlugin.options = {
1685
- ...normalizedPlugin.options,
1686
- ...opts
1687
- };
1688
- },
1689
- injectFile: (file) => {
1690
- const fileNode = createFile({
1691
- baseName: file.baseName,
1692
- path: file.path,
1693
- sources: file.sources ?? [],
1694
- imports: [],
1695
- exports: []
1696
- });
1697
- this.fileManager.add(fileNode);
1698
- }
1699
- };
1700
- return hooks["kubb:plugin:setup"](pluginCtx);
1701
- };
1702
- this.hooks.on("kubb:plugin:setup", setupHandler);
1703
- this.#trackHookListener("kubb:plugin:setup", setupHandler);
1704
- }
1705
- for (const [event, handler] of Object.entries(hooks)) {
1706
- if (event === "kubb:plugin:setup" || !handler) continue;
1707
- this.hooks.on(event, handler);
1708
- this.#trackHookListener(event, handler);
1709
- }
1710
- }
1711
- /**
1712
- * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
1713
- * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
1714
- *
1715
- * Call this once from `safeBuild` before the plugin execution loop begins.
1716
- */
1717
- async emitSetupHooks() {
1718
- await this.hooks.emit("kubb:plugin:setup", {
1719
- config: this.config,
1720
- addGenerator: () => {},
1721
- setResolver: () => {},
1722
- setTransformer: () => {},
1723
- setRenderer: () => {},
1724
- setOptions: () => {},
1725
- injectFile: () => {},
1726
- updateConfig: () => {},
1727
- options: {}
1728
- });
1729
- }
1730
- /**
1731
- * Registers a generator for the given plugin on the shared event emitter.
1732
- *
1733
- * The generator's `schema`, `operation`, and `operations` methods are registered as
1734
- * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
1735
- * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
1736
- * so that generators from different plugins do not cross-fire.
1737
- *
1738
- * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
1739
- * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
1740
- * declares a renderer.
1741
- *
1742
- * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
1743
- */
1744
- registerGenerator(pluginName, gen) {
1745
- const resolveRenderer = () => {
1746
- const plugin = this.plugins.get(pluginName);
1747
- return gen.renderer === null ? void 0 : gen.renderer ?? plugin?.renderer ?? this.config.renderer;
1748
- };
1749
- if (gen.schema) {
1750
- const schemaHandler = async (node, ctx) => {
1751
- if (ctx.plugin.name !== pluginName) return;
1752
- await applyHookResult(await gen.schema(node, ctx), this, resolveRenderer());
1753
- };
1754
- this.hooks.on("kubb:generate:schema", schemaHandler);
1755
- this.#trackHookListener("kubb:generate:schema", schemaHandler);
1756
- }
1757
- if (gen.operation) {
1758
- const operationHandler = async (node, ctx) => {
1759
- if (ctx.plugin.name !== pluginName) return;
1760
- await applyHookResult(await gen.operation(node, ctx), this, resolveRenderer());
1761
- };
1762
- this.hooks.on("kubb:generate:operation", operationHandler);
1763
- this.#trackHookListener("kubb:generate:operation", operationHandler);
1764
- }
1765
- if (gen.operations) {
1766
- const operationsHandler = async (nodes, ctx) => {
1767
- if (ctx.plugin.name !== pluginName) return;
1768
- await applyHookResult(await gen.operations(nodes, ctx), this, resolveRenderer());
1769
- };
1770
- this.hooks.on("kubb:generate:operations", operationsHandler);
1771
- this.#trackHookListener("kubb:generate:operations", operationsHandler);
1772
- }
1773
- this.#pluginsWithEventGenerators.add(pluginName);
1774
- }
1775
- /**
1776
- * Returns `true` when at least one generator was registered for the given plugin
1777
- * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
1778
- *
1779
- * Used by the build loop to decide whether to walk the AST and emit generator events
1780
- * for a plugin that has no static `plugin.generators`.
1781
- */
1782
- hasRegisteredGenerators(pluginName) {
1783
- return this.#pluginsWithEventGenerators.has(pluginName);
1784
- }
1785
- dispose() {
1786
- for (const [event, handlers] of this.#hookListeners) for (const handler of handlers) this.hooks.off(event, handler);
1787
- this.#hookListeners.clear();
1788
- this.#pluginsWithEventGenerators.clear();
1789
- }
1790
- #trackHookListener(event, handler) {
1791
- let handlers = this.#hookListeners.get(event);
1792
- if (!handlers) {
1793
- handlers = /* @__PURE__ */ new Set();
1794
- this.#hookListeners.set(event, handlers);
1795
- }
1796
- handlers.add(handler);
1797
- }
1798
- #createDefaultResolver(pluginName) {
1799
- const existingResolver = this.#defaultResolvers.get(pluginName);
1800
- if (existingResolver) return existingResolver;
1801
- const resolver = defineResolver(() => ({
1802
- name: "default",
1803
- pluginName
1804
- }));
1805
- this.#defaultResolvers.set(pluginName, resolver);
1806
- return resolver;
1807
- }
1808
- setPluginResolver(pluginName, partial) {
1809
- const merged = {
1810
- ...this.#createDefaultResolver(pluginName),
1811
- ...partial
1812
- };
1813
- this.#resolvers.set(pluginName, merged);
1814
- const plugin = this.plugins.get(pluginName);
1815
- if (plugin) plugin.resolver = merged;
1816
- }
1817
- getResolver(pluginName) {
1818
- const dynamicResolver = this.#resolvers.get(pluginName);
1819
- if (dynamicResolver) return dynamicResolver;
1820
- const pluginResolver = this.plugins.get(pluginName)?.resolver;
1821
- if (pluginResolver) return pluginResolver;
1822
- return this.#createDefaultResolver(pluginName);
1823
- }
1824
- getContext(plugin) {
1825
- const driver = this;
1826
- const baseContext = {
1827
- config: driver.config,
1828
- get root() {
1829
- return resolve(driver.config.root, driver.config.output.path);
1830
- },
1831
- getMode(output) {
1832
- return getMode(resolve(driver.config.root, driver.config.output.path, output.path));
1833
- },
1834
- hooks: driver.hooks,
1835
- plugin,
1836
- getPlugin: driver.getPlugin.bind(driver),
1837
- requirePlugin: driver.requirePlugin.bind(driver),
1838
- driver,
1839
- addFile: async (...files) => {
1840
- driver.fileManager.add(...files);
1841
- },
1842
- upsertFile: async (...files) => {
1843
- driver.fileManager.upsert(...files);
1844
- },
1845
- get inputNode() {
1846
- return driver.inputNode;
1847
- },
1848
- get adapter() {
1849
- return driver.adapter;
1850
- },
1851
- get resolver() {
1852
- return driver.getResolver(plugin.name);
1853
- },
1854
- get transformer() {
1855
- return plugin.transformer;
1856
- },
1857
- warn(message) {
1858
- driver.hooks.emit("kubb:warn", message);
1859
- },
1860
- error(error) {
1861
- driver.hooks.emit("kubb:error", typeof error === "string" ? new Error(error) : error);
1862
- },
1863
- info(message) {
1864
- driver.hooks.emit("kubb:info", message);
1865
- },
1866
- openInStudio(options) {
1867
- if (!driver.config.devtools || driver.#studioIsOpen) return;
1868
- if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
1869
- if (!driver.inputNode || !driver.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
1870
- driver.#studioIsOpen = true;
1871
- const studioUrl = driver.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
1872
- return openInStudio(driver.inputNode, studioUrl, options);
1873
- }
1874
- };
1875
- let mergedExtras = {};
1876
- for (const p of this.plugins.values()) if (typeof p.inject === "function") {
1877
- const result = p.inject.call(baseContext);
1878
- if (result !== null && typeof result === "object") mergedExtras = {
1879
- ...mergedExtras,
1880
- ...result
1881
- };
1882
- }
1883
- return {
1884
- ...baseContext,
1885
- ...mergedExtras
1886
- };
1887
- }
1888
- /**
1889
- * @deprecated use resolvers context instead
1890
- */
1891
- getFile({ name, mode, extname, pluginName, options }) {
1892
- const resolvedName = mode ? mode === "single" ? "" : this.resolveName({
1893
- name,
1894
- pluginName,
1895
- type: "file"
1896
- }) : name;
1897
- const path = this.resolvePath({
1898
- baseName: `${resolvedName}${extname}`,
1899
- mode,
1900
- pluginName,
1901
- options
1902
- });
1903
- if (!path) throw new Error(`Filepath should be defined for resolvedName "${resolvedName}" and pluginName "${pluginName}"`);
1904
- return createFile({
1905
- path,
1906
- baseName: basename(path),
1907
- meta: { pluginName },
1908
- sources: [],
1909
- imports: [],
1910
- exports: []
1911
- });
1912
- }
1913
- /**
1914
- * @deprecated use resolvers context instead
1915
- */
1916
- resolvePath = (params) => {
1917
- const defaultPath = resolve(resolve(this.config.root, this.config.output.path), params.baseName);
1918
- if (params.pluginName) return this.hookForPluginSync({
1919
- pluginName: params.pluginName,
1920
- hookName: "resolvePath",
1921
- parameters: [
1922
- params.baseName,
1923
- params.mode,
1924
- params.options
1925
- ]
1926
- })?.at(0) || defaultPath;
1927
- return this.hookFirstSync({
1928
- hookName: "resolvePath",
1929
- parameters: [
1930
- params.baseName,
1931
- params.mode,
1932
- params.options
1933
- ]
1934
- })?.result || defaultPath;
1935
- };
1936
- /**
1937
- * @deprecated use resolvers context instead
1938
- */
1939
- resolveName = (params) => {
1940
- if (params.pluginName) return transformReservedWord(this.hookForPluginSync({
1941
- pluginName: params.pluginName,
1942
- hookName: "resolveName",
1943
- parameters: [params.name.trim(), params.type]
1944
- })?.at(0) ?? params.name);
1945
- const name = this.hookFirstSync({
1946
- hookName: "resolveName",
1947
- parameters: [params.name.trim(), params.type]
1948
- })?.result;
1949
- return transformReservedWord(name ?? params.name);
1950
- };
1951
- /**
1952
- * Run a specific hookName for plugin x.
1953
- */
1954
- async hookForPlugin({ pluginName, hookName, parameters }) {
1955
- const plugin = this.plugins.get(pluginName);
1956
- if (!plugin) return [null];
1957
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1958
- hookName,
1959
- plugins: [plugin]
1960
- });
1961
- const result = await this.#execute({
1962
- strategy: "hookFirst",
1963
- hookName,
1964
- parameters,
1965
- plugin
1966
- });
1967
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
1968
- return [result];
1969
- }
1970
- /**
1971
- * Run a specific hookName for plugin x.
1972
- */
1973
- hookForPluginSync({ pluginName, hookName, parameters }) {
1974
- const plugin = this.plugins.get(pluginName);
1975
- if (!plugin) return null;
1976
- const result = this.#executeSync({
1977
- strategy: "hookFirst",
1978
- hookName,
1979
- parameters,
1980
- plugin
1981
- });
1982
- return result !== null ? [result] : [];
1983
- }
1984
- /**
1985
- * Returns the first non-null result.
1986
- */
1987
- async hookFirst({ hookName, parameters, skipped }) {
1988
- const plugins = [];
1989
- for (const plugin of this.plugins.values()) if (hookName in plugin && (skipped ? !skipped.has(plugin) : true)) plugins.push(plugin);
1990
- this.hooks.emit("kubb:plugins:hook:progress:start", {
1991
- hookName,
1992
- plugins
1993
- });
1994
- const result = await hookFirst(plugins.map((plugin) => {
1995
- return async () => {
1996
- const value = await this.#execute({
1997
- strategy: "hookFirst",
1998
- hookName,
1999
- parameters,
2000
- plugin
2001
- });
2002
- return Promise.resolve({
2003
- plugin,
2004
- result: value
2005
- });
2006
- };
2007
- }), hookFirstNullCheck);
2008
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
2009
- return result;
2010
- }
2011
- /**
2012
- * Returns the first non-null result.
2013
- */
2014
- hookFirstSync({ hookName, parameters, skipped }) {
2015
- let parseResult = null;
2016
- for (const plugin of this.plugins.values()) {
2017
- if (!(hookName in plugin)) continue;
2018
- if (skipped?.has(plugin)) continue;
2019
- parseResult = {
2020
- result: this.#executeSync({
2021
- strategy: "hookFirst",
2022
- hookName,
2023
- parameters,
2024
- plugin
2025
- }),
2026
- plugin
2027
- };
2028
- if (parseResult.result != null) break;
2029
- }
2030
- return parseResult;
2031
- }
2032
- /**
2033
- * Runs all plugins in parallel based on `this.plugin` order and `dependencies` settings.
2034
- */
2035
- async hookParallel({ hookName, parameters }) {
2036
- const plugins = [];
2037
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
2038
- this.hooks.emit("kubb:plugins:hook:progress:start", {
2039
- hookName,
2040
- plugins
2041
- });
2042
- const pluginStartTimes = /* @__PURE__ */ new Map();
2043
- const results = await hookParallel(plugins.map((plugin) => {
2044
- return () => {
2045
- pluginStartTimes.set(plugin, performance.now());
2046
- return this.#execute({
2047
- strategy: "hookParallel",
2048
- hookName,
2049
- parameters,
2050
- plugin
2051
- });
2052
- };
2053
- }), this.options.concurrency);
2054
- results.forEach((result, index) => {
2055
- if (isPromiseRejectedResult(result)) {
2056
- const plugin = plugins[index];
2057
- if (plugin) {
2058
- const startTime = pluginStartTimes.get(plugin) ?? performance.now();
2059
- this.hooks.emit("kubb:error", result.reason, {
2060
- plugin,
2061
- hookName,
2062
- strategy: "hookParallel",
2063
- duration: Math.round(performance.now() - startTime),
2064
- parameters
2065
- });
2066
- }
2067
- }
2068
- });
2069
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
2070
- return results.reduce((acc, result) => {
2071
- if (result.status === "fulfilled") acc.push(result.value);
2072
- return acc;
2073
- }, []);
2074
- }
2075
- /**
2076
- * Execute a lifecycle hook sequentially for all plugins that implement it.
2077
- */
2078
- async hookSeq({ hookName, parameters }) {
2079
- const plugins = [];
2080
- for (const plugin of this.plugins.values()) if (hookName in plugin) plugins.push(plugin);
2081
- this.hooks.emit("kubb:plugins:hook:progress:start", {
2082
- hookName,
2083
- plugins
2084
- });
2085
- await hookSeq(plugins.map((plugin) => {
2086
- return () => this.#execute({
2087
- strategy: "hookSeq",
2088
- hookName,
2089
- parameters,
2090
- plugin
2091
- });
2092
- }));
2093
- this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
2094
- }
2095
- getPlugin(pluginName) {
2096
- return this.plugins.get(pluginName);
2097
- }
2098
- requirePlugin(pluginName) {
2099
- const plugin = this.plugins.get(pluginName);
2100
- if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
2101
- return plugin;
2102
- }
2103
- /**
2104
- * Emit hook-processing completion metadata after a plugin hook resolves.
2105
- */
2106
- #emitProcessingEnd({ startTime, output, strategy, hookName, plugin, parameters }) {
2107
- this.hooks.emit("kubb:plugins:hook:processing:end", {
2108
- duration: Math.round(performance.now() - startTime),
2109
- parameters,
2110
- output,
2111
- strategy,
2112
- hookName,
2113
- plugin
2114
- });
2115
- }
2116
- #execute({ strategy, hookName, parameters, plugin }) {
2117
- const hook = plugin[hookName];
2118
- if (!hook) return null;
2119
- this.hooks.emit("kubb:plugins:hook:processing:start", {
2120
- strategy,
2121
- hookName,
2122
- parameters,
2123
- plugin
2124
- });
2125
- const startTime = performance.now();
2126
- return (async () => {
2127
- try {
2128
- const output = typeof hook === "function" ? await Promise.resolve(hook.apply(this.getContext(plugin), parameters ?? [])) : hook;
2129
- this.#emitProcessingEnd({
2130
- startTime,
2131
- output,
2132
- strategy,
2133
- hookName,
2134
- plugin,
2135
- parameters
2136
- });
2137
- return output;
2138
- } catch (error) {
2139
- this.hooks.emit("kubb:error", error, {
2140
- plugin,
2141
- hookName,
2142
- strategy,
2143
- duration: Math.round(performance.now() - startTime)
2144
- });
2145
- return null;
2146
- }
2147
- })();
2148
- }
2149
- /**
2150
- * Execute a plugin lifecycle hook synchronously and return its output.
2151
- */
2152
- #executeSync({ strategy, hookName, parameters, plugin }) {
2153
- const hook = plugin[hookName];
2154
- if (!hook) return null;
2155
- this.hooks.emit("kubb:plugins:hook:processing:start", {
2156
- strategy,
2157
- hookName,
2158
- parameters,
2159
- plugin
2160
- });
2161
- const startTime = performance.now();
2162
- try {
2163
- const output = typeof hook === "function" ? hook.apply(this.getContext(plugin), parameters) : hook;
2164
- this.#emitProcessingEnd({
2165
- startTime,
2166
- output,
2167
- strategy,
2168
- hookName,
2169
- plugin,
2170
- parameters
2171
- });
2172
- return output;
2173
- } catch (error) {
2174
- this.hooks.emit("kubb:error", error, {
2175
- plugin,
2176
- hookName,
2177
- strategy,
2178
- duration: Math.round(performance.now() - startTime)
2179
- });
2180
- return null;
2181
- }
2182
- }
2183
- };
2184
- //#endregion
2185
515
  //#region src/createStorage.ts
2186
516
  /**
2187
517
  * Creates a storage factory. Call the returned function with optional options to get the storage instance.
@@ -2279,7 +609,7 @@ const fsStorage = createStorage(() => ({
2279
609
  }));
2280
610
  //#endregion
2281
611
  //#region package.json
2282
- var version$1 = "5.0.0-alpha.35";
612
+ var version$1 = "5.0.0-alpha.36";
2283
613
  //#endregion
2284
614
  //#region src/utils/diagnostics.ts
2285
615
  /**
@@ -3382,6 +1712,6 @@ function satisfiesDependency(dependency, version, cwd) {
3382
1712
  return satisfies(semVer, version);
3383
1713
  }
3384
1714
  //#endregion
3385
- export { AsyncEventEmitter, 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 };
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 };
3386
1716
 
3387
1717
  //# sourceMappingURL=index.js.map