@kubb/core 5.0.0-beta.36 → 5.0.0-beta.37

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.cjs CHANGED
@@ -1,27 +1,11 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_KubbDriver = require("./KubbDriver-Dil5m3NF.cjs");
2
+ const require_KubbDriver = require("./KubbDriver-CXoKVRxI.cjs");
3
3
  let node_fs_promises = require("node:fs/promises");
4
4
  let node_path = require("node:path");
5
5
  let _kubb_ast = require("@kubb/ast");
6
6
  _kubb_ast = require_KubbDriver.__toESM(_kubb_ast, 1);
7
- let node_process = require("node:process");
8
7
  //#region ../../internals/utils/src/fs.ts
9
8
  /**
10
- * Resolves to `true` when the file or directory at `path` exists.
11
- * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.
12
- *
13
- * @example
14
- * ```ts
15
- * if (await exists('./kubb.config.ts')) {
16
- * const content = await read('./kubb.config.ts')
17
- * }
18
- * ```
19
- */
20
- async function exists(path) {
21
- if (typeof Bun !== "undefined") return Bun.file(path).exists();
22
- return (0, node_fs_promises.access)(path).then(() => true, () => false);
23
- }
24
- /**
25
9
  * Writes `data` to `path`, trimming leading/trailing whitespace before saving.
26
10
  * Skips the write when the trimmed content is empty or identical to what is already on disk.
27
11
  * Creates any missing parent directories automatically.
@@ -106,14 +90,11 @@ function createAdapter(build) {
106
90
  return (options) => build(options ?? {});
107
91
  }
108
92
  //#endregion
109
- //#region package.json
110
- var version = "5.0.0-beta.36";
111
- //#endregion
112
93
  //#region src/createStorage.ts
113
94
  /**
114
95
  * Defines a custom storage backend. The builder receives user options and
115
96
  * returns a `Storage` implementation. Kubb ships with filesystem and
116
- * in-memory storages reach for this when you need to write generated files
97
+ * in-memory storages, reach for this when you need to write generated files
117
98
  * elsewhere (cloud storage, a database, a remote API).
118
99
  *
119
100
  * @example In-memory storage (the built-in implementation)
@@ -230,7 +211,7 @@ const fsStorage = createStorage(() => ({
230
211
  /**
231
212
  * Builds a `Storage` view scoped to the file paths produced by the current build.
232
213
  * Reads delegate to the underlying `storage` so source bytes stay where they were
233
- * written; writes register the key so subsequent reads and `getKeys` are scoped
214
+ * written. Writes register the key so subsequent reads and `getKeys` are scoped
234
215
  * to this build's output.
235
216
  */
236
217
  function createSourcesView(storage) {
@@ -271,8 +252,8 @@ function resolveConfig(userConfig) {
271
252
  output: {
272
253
  format: false,
273
254
  lint: false,
274
- extension: require_KubbDriver.DEFAULT_EXTENSION,
275
- defaultBanner: require_KubbDriver.DEFAULT_BANNER,
255
+ extension: { ".ts": ".ts" },
256
+ defaultBanner: "simple",
276
257
  ...userConfig.output
277
258
  },
278
259
  storage: userConfig.storage ?? fsStorage(),
@@ -283,21 +264,6 @@ function resolveConfig(userConfig) {
283
264
  plugins: userConfig.plugins ?? []
284
265
  };
285
266
  }
286
- /**
287
- * Returns a snapshot of the current runtime environment.
288
- *
289
- * Useful for attaching context to debug logs and error reports so that
290
- * issues can be reproduced without manual information gathering.
291
- */
292
- function getDiagnosticInfo() {
293
- return {
294
- nodeVersion: node_process.version,
295
- KubbVersion: version,
296
- platform: process.platform,
297
- arch: process.arch,
298
- cwd: process.cwd()
299
- };
300
- }
301
267
  function isInputPath(config) {
302
268
  return typeof config?.input === "object" && config.input !== null && "path" in config.input;
303
269
  }
@@ -312,7 +278,7 @@ function isInputPath(config) {
312
278
  * ```ts
313
279
  * const kubb = createKubb(userConfig)
314
280
  * kubb.hooks.on('kubb:plugin:end', ({ plugin, duration }) => console.log(plugin.name, duration))
315
- * const { files, failedPlugins } = await kubb.safeBuild()
281
+ * const { files, diagnostics } = await kubb.safeBuild()
316
282
  * ```
317
283
  */
318
284
  var Kubb = class {
@@ -344,26 +310,8 @@ var Kubb = class {
344
310
  const config = resolveConfig(this.#userConfig);
345
311
  const driver = new require_KubbDriver.KubbDriver(config, { hooks: this.hooks });
346
312
  const storage = createSourcesView(config.storage);
347
- await this.hooks.emit("kubb:debug", {
348
- date: /* @__PURE__ */ new Date(),
349
- logs: this.#configLogs(config)
350
- });
351
- if (isInputPath(this.#userConfig) && !new require_KubbDriver.URLPath(this.#userConfig.input.path).isURL) try {
352
- await exists(this.#userConfig.input.path);
353
- await this.hooks.emit("kubb:debug", {
354
- date: /* @__PURE__ */ new Date(),
355
- logs: [`✓ Input file validated: ${this.#userConfig.input.path}`]
356
- });
357
- } catch (caughtError) {
358
- throw new Error(`Cannot read file/URL defined in \`input.path\` or set with \`kubb generate PATH\` in the CLI of your Kubb config ${this.#userConfig.input.path}`, { cause: caughtError });
359
- }
360
- if (config.output.clean) {
361
- await this.hooks.emit("kubb:debug", {
362
- date: /* @__PURE__ */ new Date(),
363
- logs: ["Cleaning output directories", ` • Output: ${config.output.path}`]
364
- });
365
- await config.storage.clear((0, node_path.resolve)(config.root, config.output.path));
366
- }
313
+ this.hooks.setMaxListeners(Math.max(10, config.plugins.length * 4));
314
+ if (config.output.clean) await config.storage.clear((0, node_path.resolve)(config.root, config.output.path));
367
315
  await driver.setup();
368
316
  this.#config = config;
369
317
  this.#driver = driver;
@@ -375,10 +323,9 @@ var Kubb = class {
375
323
  */
376
324
  async build() {
377
325
  const out = await this.safeBuild();
378
- if (out.error) throw out.error;
379
- if (out.failedPlugins.size > 0) {
380
- const errors = [...out.failedPlugins].map(({ error }) => error);
381
- throw new require_KubbDriver.BuildError(`Build Error with ${out.failedPlugins.size} failed plugins`, { errors });
326
+ if (require_KubbDriver.Diagnostics.hasError(out.diagnostics)) {
327
+ const errors = out.diagnostics.filter(require_KubbDriver.isProblemDiagnostic).filter((diagnostic) => diagnostic.severity === "error").map((diagnostic) => diagnostic.cause ?? new require_KubbDriver.DiagnosticError(diagnostic));
328
+ throw new require_KubbDriver.BuildError(`Build failed with ${errors.length} ${errors.length === 1 ? "error" : "errors"}`, { errors });
382
329
  }
383
330
  return out;
384
331
  }
@@ -393,14 +340,12 @@ var Kubb = class {
393
340
  const cleanup = _usingCtx$1.u(this);
394
341
  const driver = cleanup.driver;
395
342
  const storage = cleanup.storage;
396
- const { failedPlugins, pluginTimings, error } = await driver.run({ storage });
343
+ const { diagnostics } = await driver.run({ storage });
397
344
  return {
398
- failedPlugins,
345
+ diagnostics,
399
346
  files: driver.fileManager.files,
400
347
  driver,
401
- pluginTimings,
402
- storage,
403
- ...error ? { error } : {}
348
+ storage
404
349
  };
405
350
  } catch (_) {
406
351
  _usingCtx$1.e = _;
@@ -414,24 +359,6 @@ var Kubb = class {
414
359
  [Symbol.dispose]() {
415
360
  this.dispose();
416
361
  }
417
- #configLogs(config) {
418
- const u = this.#userConfig;
419
- const diag = getDiagnosticInfo();
420
- return [
421
- "Configuration:",
422
- ` • Name: ${u.name || "unnamed"}`,
423
- ` • Root: ${u.root || process.cwd()}`,
424
- ` • Output: ${u.output?.path || "not specified"}`,
425
- ` • Plugins: ${u.plugins?.length || 0}`,
426
- "Output Settings:",
427
- ` • Storage: ${config.storage.name}`,
428
- ` • Formatter: ${u.output?.format || "none"}`,
429
- ` • Linter: ${u.output?.lint || "none"}`,
430
- `Running adapter: ${config.adapter?.name || "none"}`,
431
- "Environment:",
432
- Object.entries(diag).map(([key, value]) => ` • ${key}: ${value}`).join("\n")
433
- ];
434
- }
435
362
  };
436
363
  /**
437
364
  * Constructs a {@link Kubb} build orchestrator from a user config. Equivalent
@@ -457,13 +384,36 @@ function createKubb(userConfig, options = {}) {
457
384
  return new Kubb(userConfig, options);
458
385
  }
459
386
  //#endregion
387
+ //#region src/createReporter.ts
388
+ /**
389
+ * Defines a reporter. Returns the reporter unchanged at runtime. It exists for type inference and
390
+ * to mark the value as a reporter, mirroring {@link defineLogger}. Wiring the reporter onto the
391
+ * run's events is the host's job, so the reporter only ever deals with a {@link GenerationResult}.
392
+ *
393
+ * @example
394
+ * ```ts
395
+ * import { createReporter, Diagnostics } from '@kubb/core'
396
+ *
397
+ * export const jsonReporter = createReporter({
398
+ * name: 'json',
399
+ * report(result) {
400
+ * const status = Diagnostics.hasError(result.diagnostics) ? 'failed' : 'success'
401
+ * process.stdout.write(`${JSON.stringify({ status, diagnostics: result.diagnostics }, null, 2)}\n`)
402
+ * },
403
+ * })
404
+ * ```
405
+ */
406
+ function createReporter(reporter) {
407
+ return reporter;
408
+ }
409
+ //#endregion
460
410
  //#region src/createRenderer.ts
461
411
  /**
462
412
  * Defines a renderer factory. Renderers turn the generator's return value
463
413
  * (JSX, a template string, a tree of any shape) into `FileNode`s that get
464
414
  * written to disk.
465
415
  *
466
- * Use this to support output formats beyond JSX for instance, a Handlebars
416
+ * Use this to support output formats beyond JSX, for instance, a Handlebars
467
417
  * renderer, a string-template renderer, or a renderer that writes binary
468
418
  * files. Plugins and generators pick the renderer to use via the `renderer`
469
419
  * field on `defineGenerator`.
@@ -507,7 +457,7 @@ function createRenderer(factory) {
507
457
  * The returned object is the input as-is, but with `this` types preserved so
508
458
  * `schema`/`operation`/`operations` methods are correctly typed against the
509
459
  * plugin's `PluginFactoryOptions`. Renderer elements and `FileNode[]` returns
510
- * are both handled by the runtime pick whichever style fits.
460
+ * are both handled by the runtime, so pick whichever style fits.
511
461
  *
512
462
  * @example JSX-based schema generator
513
463
  * ```tsx
@@ -690,6 +640,8 @@ const memoryStorage = createStorage(() => {
690
640
  });
691
641
  //#endregion
692
642
  exports.AsyncEventEmitter = require_KubbDriver.AsyncEventEmitter;
643
+ exports.DiagnosticError = require_KubbDriver.DiagnosticError;
644
+ exports.Diagnostics = require_KubbDriver.Diagnostics;
693
645
  exports.FileManager = require_KubbDriver.FileManager;
694
646
  exports.FileProcessor = require_KubbDriver.FileProcessor;
695
647
  exports.KubbDriver = require_KubbDriver.KubbDriver;
@@ -703,6 +655,7 @@ Object.defineProperty(exports, "ast", {
703
655
  exports.createAdapter = createAdapter;
704
656
  exports.createKubb = createKubb;
705
657
  exports.createRenderer = createRenderer;
658
+ exports.createReporter = createReporter;
706
659
  exports.createStorage = createStorage;
707
660
  exports.defineGenerator = defineGenerator;
708
661
  exports.defineLogger = defineLogger;
@@ -710,9 +663,15 @@ exports.defineMiddleware = defineMiddleware;
710
663
  exports.defineParser = defineParser;
711
664
  exports.definePlugin = require_KubbDriver.definePlugin;
712
665
  exports.defineResolver = require_KubbDriver.defineResolver;
666
+ exports.diagnosticCatalog = require_KubbDriver.diagnosticCatalog;
667
+ exports.diagnosticCode = require_KubbDriver.diagnosticCode;
713
668
  exports.fsStorage = fsStorage;
714
669
  exports.isInputPath = isInputPath;
670
+ exports.isPerformanceDiagnostic = require_KubbDriver.isPerformanceDiagnostic;
671
+ exports.isProblemDiagnostic = require_KubbDriver.isProblemDiagnostic;
672
+ exports.isUpdateDiagnostic = require_KubbDriver.isUpdateDiagnostic;
715
673
  exports.logLevel = require_KubbDriver.logLevel;
716
674
  exports.memoryStorage = memoryStorage;
675
+ exports.narrowDiagnostic = require_KubbDriver.narrowDiagnostic;
717
676
 
718
677
  //# sourceMappingURL=index.cjs.map