@kubb/core 5.8.4 → 5.9.1

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.
@@ -33,7 +33,7 @@ node_path = __toESM(node_path, 1);
33
33
  let _kubb_ast = require("@kubb/ast");
34
34
  let node_events = require("node:events");
35
35
  //#region package.json
36
- var version = "5.8.4";
36
+ var version = "5.9.1";
37
37
  //#endregion
38
38
  //#region ../../internals/utils/src/casing.ts
39
39
  /**
@@ -91,74 +91,34 @@ function getErrorMessage(value) {
91
91
  }
92
92
  //#endregion
93
93
  //#region ../../internals/utils/src/runtime.ts
94
+ const isBun = typeof Bun !== "undefined";
95
+ const isDeno = "Deno" in globalThis;
94
96
  /**
95
- * Detects the JavaScript runtime executing the current process and exposes its name and version.
97
+ * The JavaScript runtime executing the current process: its name, version and whether it is Bun.
96
98
  *
97
- * Prefer the shared {@link runtime} instance over constructing your own.
99
+ * @example
100
+ * ```ts
101
+ * runtime.name // 'bun' when run with `bun kubb`, 'node' otherwise
102
+ * ```
98
103
  */
99
- var Runtime = class {
100
- /**
101
- * `true` when the current process is running under Bun.
102
- *
103
- * Detection keys off the global `Bun` object rather than `process.versions`,
104
- * because Bun polyfills `process.versions.node` for Node compatibility and would
105
- * otherwise look like Node.
106
- *
107
- * @example
108
- * ```ts
109
- * if (runtime.isBun) {
110
- * await Bun.write(path, data)
111
- * }
112
- * ```
113
- */
114
- get isBun() {
115
- return typeof Bun !== "undefined";
116
- }
117
- /**
118
- * `true` when the current process is running under Deno.
119
- */
120
- get isDeno() {
121
- return typeof globalThis.Deno !== "undefined";
122
- }
123
- /**
124
- * `true` when the current process is running under Node.
125
- *
126
- * Bun and Deno are excluded first so a polyfilled `process` does not register as Node.
127
- */
128
- get isNode() {
129
- return !this.isBun && !this.isDeno && typeof process !== "undefined" && process.versions?.node != null;
130
- }
131
- /**
132
- * Name of the runtime executing the current process.
133
- *
134
- * @example
135
- * ```ts
136
- * runtime.name // 'bun' when run with `bun kubb`, 'node' otherwise
137
- * ```
138
- */
139
- get name() {
140
- if (this.isBun) return "bun";
141
- if (this.isDeno) return "deno";
142
- return "node";
143
- }
144
- /**
145
- * Version of the active runtime, or an empty string when it cannot be read.
146
- *
147
- * @example
148
- * ```ts
149
- * runtime.version // '1.3.11' under Bun, '22.22.2' under Node
150
- * ```
151
- */
152
- get version() {
153
- if (this.isBun) return process.versions.bun ?? "";
154
- if (this.isDeno) return globalThis.Deno?.version?.deno ?? "";
155
- return process.versions?.node ?? "";
156
- }
104
+ const runtime = {
105
+ isBun,
106
+ ...detect()
157
107
  };
158
- /**
159
- * Shared {@link Runtime} instance describing the JavaScript runtime executing the current process.
160
- */
161
- const runtime = new Runtime();
108
+ function detect() {
109
+ if (isBun) return {
110
+ name: "bun",
111
+ version: process.versions.bun ?? ""
112
+ };
113
+ if (isDeno) return {
114
+ name: "deno",
115
+ version: globalThis.Deno?.version?.deno ?? ""
116
+ };
117
+ return {
118
+ name: "node",
119
+ version: process.versions?.node ?? ""
120
+ };
121
+ }
162
122
  //#endregion
163
123
  //#region ../../internals/utils/src/fs.ts
164
124
  /**
@@ -427,6 +387,10 @@ const diagnosticCode = {
427
387
  * A plugin threw while generating.
428
388
  */
429
389
  pluginFailed: "KUBB_PLUGIN_FAILED",
390
+ /** An adapter threw while parsing the input. */
391
+ adapterFailed: "KUBB_ADAPTER_FAILED",
392
+ /** A parser threw while printing a generated file. */
393
+ parserFailed: "KUBB_PARSER_FAILED",
430
394
  /**
431
395
  * A plugin reported a non-fatal warning through `ctx.warn`.
432
396
  */
@@ -546,6 +510,11 @@ const severityStyle = {
546
510
  warning: "yellow",
547
511
  info: "blue"
548
512
  };
513
+ const failedCode = {
514
+ plugin: diagnosticCode.pluginFailed,
515
+ adapter: diagnosticCode.adapterFailed,
516
+ parser: diagnosticCode.parserFailed
517
+ };
549
518
  const countKey = {
550
519
  error: "errors",
551
520
  warning: "warnings",
@@ -629,7 +598,17 @@ const diagnosticCatalog = {
629
598
  [diagnosticCode.pluginFailed]: {
630
599
  title: "Plugin failed",
631
600
  cause: "A plugin threw while generating, or reported an error through `ctx.error`.",
632
- fix: "Read the underlying error and check the plugin options and the schema or operation it failed on."
601
+ fix: "Read the underlying error and check the plugin options and the schema or operation it failed on. If the plugin crashed, rerun with `--verbose` for the stack trace and report it to the plugin maintainers, at the `report:` link when one is shown."
602
+ },
603
+ [diagnosticCode.adapterFailed]: {
604
+ title: "Adapter failed",
605
+ cause: "The adapter threw an unexpected error while parsing the input.",
606
+ fix: "This is likely a bug in the adapter. Rerun with `--verbose` for the stack trace and report it to the adapter maintainers, at the `report:` link when one is shown."
607
+ },
608
+ [diagnosticCode.parserFailed]: {
609
+ title: "Parser failed",
610
+ cause: "A parser threw an unexpected error while printing a generated file.",
611
+ fix: "This is likely a bug in the parser. Rerun with `--verbose` for the stack trace and report it to the parser maintainers, at the `report:` link when one is shown."
633
612
  },
634
613
  [diagnosticCode.pluginWarning]: {
635
614
  title: "Plugin warning",
@@ -811,6 +790,29 @@ var Diagnostics = class Diagnostics {
811
790
  };
812
791
  }
813
792
  /**
793
+ * Coerces an error thrown by a plugin, adapter, or parser into a diagnostic attributed to it.
794
+ * A structured {@link Diagnostics.Error} keeps its code. Any other error is a crash: it gets the
795
+ * kind's `*_FAILED` code and the extension's `meta.issues` link, so the user reports it to the
796
+ * extension's maintainers instead of to Kubb.
797
+ */
798
+ static fromExtension(error, { kind, extension }) {
799
+ const diagnostic = Diagnostics.from(error);
800
+ const name = extension.meta?.name ?? extension.name;
801
+ const owner = kind === "plugin" ? { plugin: diagnostic.plugin ?? name } : {};
802
+ if (diagnostic.code !== diagnosticCode.unknown || diagnostic.cause?.name === "AbortError") return {
803
+ ...diagnostic,
804
+ ...owner
805
+ };
806
+ const issues = extension.meta?.issues;
807
+ return {
808
+ ...diagnostic,
809
+ ...owner,
810
+ code: failedCode[kind],
811
+ message: kind === "plugin" ? diagnostic.message : `${kind} "${name}" crashed: ${diagnostic.message}`,
812
+ ...issues ? { issues } : {}
813
+ };
814
+ }
815
+ /**
814
816
  * Builds a per-plugin performance record. Reporters sum these into the run total.
815
817
  */
816
818
  static performance({ plugin, duration }) {
@@ -913,13 +915,14 @@ var Diagnostics = class Diagnostics {
913
915
  ...problem?.location ? { location: problem.location } : {},
914
916
  ...problem?.help ? { help: problem.help } : {},
915
917
  ...problem?.plugin ? { plugin: problem.plugin } : {},
918
+ ...problem?.issues ? { issues: problem.issues } : {},
916
919
  ...diagnostic.code === diagnosticCode.unknown ? {} : { docsUrl: Diagnostics.docsUrl(diagnostic.code) }
917
920
  };
918
921
  }
919
922
  /**
920
923
  * Renders a {@link Diagnostic} for terminal output as its parts: the `headline`
921
924
  * (`[CODE] plugin: message`, with the code in the severity color) and the indented `details`
922
- * rows (`at:` pointer, `fix:` help, `see:` docs link).
925
+ * rows (`at:` pointer, `fix:` help, `see:` docs link, `report:` issue tracker).
923
926
  *
924
927
  * Hosts compose these to fit their gutter: a clack logger passes `[headline, ...details]` as the
925
928
  * message with no gutter symbol, while plain text outputs use {@link Diagnostics.formatLines}.
@@ -934,6 +937,7 @@ var Diagnostics = class Diagnostics {
934
937
  if (problem?.location && "pointer" in problem.location) details.push(` ${(0, node_util.styleText)("dim", "at:")} ${(0, node_util.styleText)("cyan", problem.location.pointer)}`);
935
938
  if (problem?.help) details.push(` ${(0, node_util.styleText)("cyan", "fix:")} ${problem.help}`);
936
939
  if (code !== diagnosticCode.unknown) details.push(` ${(0, node_util.styleText)("dim", "see:")} ${(0, node_util.styleText)("cyan", Diagnostics.docsUrl(code))}`);
940
+ if (problem?.issues) details.push(` ${(0, node_util.styleText)("dim", "report:")} ${(0, node_util.styleText)("cyan", problem.issues)}`);
937
941
  return {
938
942
  headline,
939
943
  details
@@ -1493,7 +1497,7 @@ var Resolver = class Resolver {
1493
1497
  return null;
1494
1498
  }
1495
1499
  static #computeOptions(node, { options, exclude = [], include, override = [] }) {
1496
- if (_kubb_ast.operationDef.is(node)) {
1500
+ if (node.kind === "Operation") {
1497
1501
  if (exclude.some((filter) => Resolver.#matchesOperation(node, filter))) return null;
1498
1502
  if (include && !include.some((filter) => Resolver.#matchesOperation(node, filter))) return null;
1499
1503
  return {
@@ -1501,7 +1505,7 @@ var Resolver = class Resolver {
1501
1505
  ...override.find((filter) => Resolver.#matchesOperation(node, filter))?.options
1502
1506
  };
1503
1507
  }
1504
- if (_kubb_ast.schemaDef.is(node)) {
1508
+ if (node.kind === "Schema") {
1505
1509
  if (exclude.some((filter) => Resolver.#matchesSchema(node, filter) === true)) return null;
1506
1510
  if (include) {
1507
1511
  const applicable = include.map((filter) => Resolver.#matchesSchema(node, filter)).filter((result) => result !== null);
@@ -1896,6 +1900,17 @@ function _usingCtx() {
1896
1900
  function joinSources(file) {
1897
1901
  return file.sources.map((source) => (0, _kubb_ast.extractStringsFromNodes)(source.nodes)).filter(Boolean).join("\n\n");
1898
1902
  }
1903
+ /** Runs a parser call and turns a crash into a `KUBB_PARSER_FAILED` diagnostic that names the parser. */
1904
+ function runParser(parser, print) {
1905
+ try {
1906
+ return print();
1907
+ } catch (error) {
1908
+ throw new Diagnostics.Error(Diagnostics.fromExtension(error, {
1909
+ kind: "parser",
1910
+ extension: parser
1911
+ }));
1912
+ }
1913
+ }
1899
1914
  /**
1900
1915
  * Reads a `copy` file and prints it through the parser's `copy` hook, or verbatim with `banner`/`footer` when there is none.
1901
1916
  */
@@ -1906,7 +1921,7 @@ async function parseCopy(file, parser) {
1906
1921
  } catch (err) {
1907
1922
  throw new Error(`[kubb] Could not copy file into output: ${file.copy}`, { cause: err });
1908
1923
  }
1909
- if (parser?.copy) return parser.parse(_kubb_ast.ast.factory.createFile(parser.copy(file, content)));
1924
+ if (parser?.copy) return runParser(parser, () => parser.parse(_kubb_ast.ast.factory.createFile(parser.copy(file, content))));
1910
1925
  return [
1911
1926
  file.banner,
1912
1927
  content,
@@ -2028,7 +2043,7 @@ var FileManager = class {
2028
2043
  const parser = parsers?.get(file.extname);
2029
2044
  if (file.copy) return parseCopy(file, parser);
2030
2045
  if (!parser) return joinSources(file);
2031
- return parser.parse(file);
2046
+ return runParser(parser, () => parser.parse(file));
2032
2047
  }
2033
2048
  /**
2034
2049
  * Parses and writes every file through a bounded pool of workers. A small spec runs all its files
@@ -2334,7 +2349,14 @@ var KubbDriver = class {
2334
2349
  async #parseInput() {
2335
2350
  if (this.inputNode || !this.adapter || !this.#adapterSource) return;
2336
2351
  this.options.signal?.throwIfAborted();
2337
- this.inputNode = await this.adapter.parse(this.#adapterSource, { signal: this.options.signal });
2352
+ try {
2353
+ this.inputNode = await this.adapter.parse(this.#adapterSource, { signal: this.options.signal });
2354
+ } catch (caughtError) {
2355
+ throw new Diagnostics.Error(Diagnostics.fromExtension(caughtError, {
2356
+ kind: "adapter",
2357
+ extension: this.adapter
2358
+ }));
2359
+ }
2338
2360
  }
2339
2361
  /**
2340
2362
  * Registers a plugin's lifecycle hooks on the shared `Hookable` as pass-through listeners that
@@ -2392,18 +2414,13 @@ var KubbDriver = class {
2392
2414
  await setupHook?.(ctx);
2393
2415
  });
2394
2416
  } catch (caughtError) {
2395
- const diagnostic = Diagnostics.from(caughtError);
2396
- throw new Diagnostics.Error({
2397
- ...diagnostic,
2398
- plugin: diagnostic.plugin ?? plugin.name
2399
- });
2417
+ throw new Diagnostics.Error(Diagnostics.fromExtension(caughtError, {
2418
+ kind: "plugin",
2419
+ extension: plugin
2420
+ }));
2400
2421
  }
2401
2422
  }
2402
2423
  }
2403
- /** `true` once `addGenerator()` in `kubb:plugin:setup` registered a generator for the plugin. */
2404
- hasHookGenerators(pluginName) {
2405
- return (this.plugins.get(pluginName)?.generators?.length ?? 0) > 0;
2406
- }
2407
2424
  /**
2408
2425
  * Runs the full plugin pipeline. Returns the diagnostics collected so far even
2409
2426
  * when an outer hook throws, since the orchestrator preserves partial state by capturing
@@ -2470,16 +2487,16 @@ var KubbDriver = class {
2470
2487
  success: false,
2471
2488
  error
2472
2489
  });
2473
- diagnostics.push({
2474
- ...Diagnostics.from(error),
2475
- plugin: plugin.name
2476
- }, Diagnostics.performance({
2490
+ diagnostics.push(Diagnostics.fromExtension(error, {
2491
+ kind: "plugin",
2492
+ extension: plugin
2493
+ }), Diagnostics.performance({
2477
2494
  plugin: plugin.name,
2478
2495
  duration
2479
2496
  }));
2480
2497
  continue;
2481
2498
  }
2482
- if (this.hasHookGenerators(plugin.name)) {
2499
+ if ((plugin.generators?.length ?? 0) > 0) {
2483
2500
  generatorPlugins.push({
2484
2501
  plugin,
2485
2502
  context,
@@ -2697,7 +2714,7 @@ var KubbDriver = class {
2697
2714
  options: state.plugin.options,
2698
2715
  cache: createNodeCache()
2699
2716
  };
2700
- for (const generator of state.operationsGenerators) await this.dispatch({
2717
+ for (const generator of state.operationsGenerators) await this.fileManager.dispatch({
2701
2718
  result: await generator.operations(state.pluginOperations, ctx),
2702
2719
  renderer: generator.renderer
2703
2720
  });
@@ -2714,10 +2731,10 @@ var KubbDriver = class {
2714
2731
  success: !state.error,
2715
2732
  error: state.error ?? void 0
2716
2733
  });
2717
- if (state.error) diagnostics.push({
2718
- ...Diagnostics.from(state.error),
2719
- plugin: state.plugin.name
2720
- });
2734
+ if (state.error) diagnostics.push(Diagnostics.fromExtension(state.error, {
2735
+ kind: "plugin",
2736
+ extension: state.plugin
2737
+ }));
2721
2738
  diagnostics.push(Diagnostics.performance({
2722
2739
  plugin: state.plugin.name,
2723
2740
  duration
@@ -2729,16 +2746,12 @@ var KubbDriver = class {
2729
2746
  async #dispatchNode({ generators, node, ctx, generate }) {
2730
2747
  for (const generator of generators) {
2731
2748
  if (!(generator.match ? await generator.match(node, ctx) : true)) continue;
2732
- await this.dispatch({
2749
+ await this.fileManager.dispatch({
2733
2750
  result: await generate(generator),
2734
2751
  renderer: generator.renderer
2735
2752
  });
2736
2753
  }
2737
2754
  }
2738
- /** Stores whatever a generator method or `kubb:generate:*` hook returned, see {@link FileManager.dispatch}. */
2739
- dispatch(params) {
2740
- return this.fileManager.dispatch(params);
2741
- }
2742
2755
  /**
2743
2756
  * Removes every listener the driver added. Listeners attached directly to `hooks` from outside
2744
2757
  * the driver survive. Called at the end of a build to prevent leaks across repeated builds.
@@ -3095,4 +3108,4 @@ Object.defineProperty(exports, "write", {
3095
3108
  }
3096
3109
  });
3097
3110
 
3098
- //# sourceMappingURL=KubbDriver-Iv2StSPa.cjs.map
3111
+ //# sourceMappingURL=KubbDriver-Cd01FAa0.cjs.map