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

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-BYBUfOZ8.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,54 @@ 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. When the definition has a `flush`, the returned reporter buffers each value
390
+ * `report` returns and hands the array to `flush` once, then clears it. Without a `flush`, nothing
391
+ * is buffered. Wiring the reporter onto the run's events is the host's job, so the reporter only
392
+ * ever deals with a {@link GenerationResult}.
393
+ *
394
+ * @example
395
+ * ```ts
396
+ * import { createReporter, Diagnostics } from '@kubb/core'
397
+ *
398
+ * export const jsonReporter = createReporter({
399
+ * name: 'json',
400
+ * report(result) {
401
+ * return { status: Diagnostics.hasError(result.diagnostics) ? 'failed' : 'success', diagnostics: result.diagnostics }
402
+ * },
403
+ * flush(context, reports) {
404
+ * process.stdout.write(`${JSON.stringify(reports, null, 2)}\n`)
405
+ * },
406
+ * })
407
+ * ```
408
+ */
409
+ function createReporter(reporter) {
410
+ const flush = reporter.flush;
411
+ if (!flush) return {
412
+ name: reporter.name,
413
+ report: reporter.report
414
+ };
415
+ const reports = [];
416
+ return {
417
+ name: reporter.name,
418
+ async report(result, context) {
419
+ reports.push(await reporter.report(result, context));
420
+ },
421
+ async flush(context) {
422
+ await flush(context, reports);
423
+ reports.length = 0;
424
+ }
425
+ };
426
+ }
427
+ //#endregion
460
428
  //#region src/createRenderer.ts
461
429
  /**
462
430
  * Defines a renderer factory. Renderers turn the generator's return value
463
431
  * (JSX, a template string, a tree of any shape) into `FileNode`s that get
464
432
  * written to disk.
465
433
  *
466
- * Use this to support output formats beyond JSX for instance, a Handlebars
434
+ * Use this to support output formats beyond JSX, for instance, a Handlebars
467
435
  * renderer, a string-template renderer, or a renderer that writes binary
468
436
  * files. Plugins and generators pick the renderer to use via the `renderer`
469
437
  * field on `defineGenerator`.
@@ -507,7 +475,7 @@ function createRenderer(factory) {
507
475
  * The returned object is the input as-is, but with `this` types preserved so
508
476
  * `schema`/`operation`/`operations` methods are correctly typed against the
509
477
  * plugin's `PluginFactoryOptions`. Renderer elements and `FileNode[]` returns
510
- * are both handled by the runtime pick whichever style fits.
478
+ * are both handled by the runtime, so pick whichever style fits.
511
479
  *
512
480
  * @example JSX-based schema generator
513
481
  * ```tsx
@@ -690,6 +658,8 @@ const memoryStorage = createStorage(() => {
690
658
  });
691
659
  //#endregion
692
660
  exports.AsyncEventEmitter = require_KubbDriver.AsyncEventEmitter;
661
+ exports.DiagnosticError = require_KubbDriver.DiagnosticError;
662
+ exports.Diagnostics = require_KubbDriver.Diagnostics;
693
663
  exports.FileManager = require_KubbDriver.FileManager;
694
664
  exports.FileProcessor = require_KubbDriver.FileProcessor;
695
665
  exports.KubbDriver = require_KubbDriver.KubbDriver;
@@ -703,6 +673,7 @@ Object.defineProperty(exports, "ast", {
703
673
  exports.createAdapter = createAdapter;
704
674
  exports.createKubb = createKubb;
705
675
  exports.createRenderer = createRenderer;
676
+ exports.createReporter = createReporter;
706
677
  exports.createStorage = createStorage;
707
678
  exports.defineGenerator = defineGenerator;
708
679
  exports.defineLogger = defineLogger;
@@ -710,9 +681,15 @@ exports.defineMiddleware = defineMiddleware;
710
681
  exports.defineParser = defineParser;
711
682
  exports.definePlugin = require_KubbDriver.definePlugin;
712
683
  exports.defineResolver = require_KubbDriver.defineResolver;
684
+ exports.diagnosticCatalog = require_KubbDriver.diagnosticCatalog;
685
+ exports.diagnosticCode = require_KubbDriver.diagnosticCode;
713
686
  exports.fsStorage = fsStorage;
714
687
  exports.isInputPath = isInputPath;
688
+ exports.isPerformanceDiagnostic = require_KubbDriver.isPerformanceDiagnostic;
689
+ exports.isProblemDiagnostic = require_KubbDriver.isProblemDiagnostic;
690
+ exports.isUpdateDiagnostic = require_KubbDriver.isUpdateDiagnostic;
715
691
  exports.logLevel = require_KubbDriver.logLevel;
716
692
  exports.memoryStorage = memoryStorage;
693
+ exports.narrowDiagnostic = require_KubbDriver.narrowDiagnostic;
717
694
 
718
695
  //# sourceMappingURL=index.cjs.map