@kubb/core 3.0.0-alpha.6 → 3.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/dist/{FileManager-wyXRkgIk.d.cts → FileManager-B0GJlthB.d.cts} +30 -16
  2. package/dist/{FileManager-Ceski1W9.d.ts → FileManager-BFycBDqG.d.ts} +30 -16
  3. package/dist/chunk-27CPVXAT.js +945 -0
  4. package/dist/chunk-27CPVXAT.js.map +1 -0
  5. package/dist/{chunk-34BPAXR2.cjs → chunk-5E2I6KH4.cjs} +7 -7
  6. package/dist/{chunk-34BPAXR2.cjs.map → chunk-5E2I6KH4.cjs.map} +1 -1
  7. package/dist/chunk-5IGANEGE.cjs +1564 -0
  8. package/dist/chunk-5IGANEGE.cjs.map +1 -0
  9. package/dist/{chunk-67C6RBGQ.cjs → chunk-A6PCLWEY.cjs} +5 -5
  10. package/dist/{chunk-67C6RBGQ.cjs.map → chunk-A6PCLWEY.cjs.map} +1 -1
  11. package/dist/{chunk-5HN4LW3R.cjs → chunk-DID47EQD.cjs} +3 -3
  12. package/dist/{chunk-5HN4LW3R.cjs.map → chunk-DID47EQD.cjs.map} +1 -1
  13. package/dist/{chunk-25NKJ3DV.js → chunk-HBQM723K.js} +6 -6
  14. package/dist/{chunk-LOLEBOMW.cjs → chunk-IPZQXBA2.cjs} +16 -21
  15. package/dist/chunk-IPZQXBA2.cjs.map +1 -0
  16. package/dist/{chunk-76UEJFFE.js → chunk-SCR3LUXT.js} +9 -16
  17. package/dist/{chunk-76UEJFFE.js.map → chunk-SCR3LUXT.js.map} +1 -1
  18. package/dist/{chunk-XCPFG6DO.cjs → chunk-SEH6NUCX.cjs} +4 -28
  19. package/dist/chunk-SEH6NUCX.cjs.map +1 -0
  20. package/dist/index.cjs +380 -378
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +2 -2
  23. package/dist/index.d.ts +2 -2
  24. package/dist/index.js +325 -319
  25. package/dist/index.js.map +1 -1
  26. package/dist/logger.cjs +4 -4
  27. package/dist/logger.js +2 -3
  28. package/dist/mocks.cjs +20 -15
  29. package/dist/mocks.cjs.map +1 -1
  30. package/dist/mocks.d.cts +6 -5
  31. package/dist/mocks.d.ts +6 -5
  32. package/dist/mocks.js +15 -11
  33. package/dist/mocks.js.map +1 -1
  34. package/dist/{prompt-6FWP747F.cjs → prompt-DVQN7JTN.cjs} +61 -61
  35. package/dist/{prompt-6FWP747F.cjs.map → prompt-DVQN7JTN.cjs.map} +1 -1
  36. package/dist/{prompt-HK3MWREM.js → prompt-WQQUN22Z.js} +5 -6
  37. package/dist/{prompt-HK3MWREM.js.map → prompt-WQQUN22Z.js.map} +1 -1
  38. package/dist/transformers.cjs +19 -19
  39. package/dist/transformers.js +0 -1
  40. package/dist/transformers.js.map +1 -1
  41. package/dist/utils.cjs +8 -4
  42. package/dist/utils.cjs.map +1 -1
  43. package/dist/utils.d.cts +25 -24
  44. package/dist/utils.d.ts +25 -24
  45. package/dist/utils.js +5 -2
  46. package/package.json +7 -7
  47. package/src/BarrelManager.ts +90 -40
  48. package/src/FileManager.ts +89 -109
  49. package/src/PluginManager.ts +2 -1
  50. package/src/__snapshots__/barrel.json +91 -0
  51. package/src/__snapshots__/grouped.json +114 -0
  52. package/src/__snapshots__/ordered.json +62 -0
  53. package/src/build.ts +60 -23
  54. package/src/plugin.ts +2 -2
  55. package/src/types.ts +22 -2
  56. package/src/utils/TreeNode.ts +43 -1
  57. package/src/utils/index.ts +1 -1
  58. package/src/utils/parser.ts +128 -67
  59. package/dist/chunk-AWAZR3D5.js +0 -360
  60. package/dist/chunk-AWAZR3D5.js.map +0 -1
  61. package/dist/chunk-DCZQYCCO.cjs +0 -1155
  62. package/dist/chunk-DCZQYCCO.cjs.map +0 -1
  63. package/dist/chunk-HMLY7DHA.js +0 -16
  64. package/dist/chunk-HMLY7DHA.js.map +0 -1
  65. package/dist/chunk-L3JJLZ5Q.cjs +0 -371
  66. package/dist/chunk-L3JJLZ5Q.cjs.map +0 -1
  67. package/dist/chunk-LOLEBOMW.cjs.map +0 -1
  68. package/dist/chunk-RIEGCND2.js +0 -525
  69. package/dist/chunk-RIEGCND2.js.map +0 -1
  70. package/dist/chunk-XCPFG6DO.cjs.map +0 -1
  71. package/schema.json +0 -86
  72. /package/dist/{chunk-25NKJ3DV.js.map → chunk-HBQM723K.js.map} +0 -0
package/dist/index.js CHANGED
@@ -3,25 +3,16 @@ import {
3
3
  } from "./chunk-2EU7DMPM.js";
4
4
  import {
5
5
  EventEmitter,
6
- createLogger,
7
- p
8
- } from "./chunk-76UEJFFE.js";
6
+ createLogger
7
+ } from "./chunk-SCR3LUXT.js";
9
8
  import {
10
9
  FileManager,
11
- processFiles
12
- } from "./chunk-RIEGCND2.js";
13
- import {
14
10
  URLPath,
11
+ processFiles,
15
12
  setUniqueName
16
- } from "./chunk-AWAZR3D5.js";
13
+ } from "./chunk-27CPVXAT.js";
17
14
  import "./chunk-4X5FFJPJ.js";
18
- import "./chunk-25NKJ3DV.js";
19
- import {
20
- __privateAdd,
21
- __privateGet,
22
- __privateMethod,
23
- __privateSet
24
- } from "./chunk-HMLY7DHA.js";
15
+ import "./chunk-HBQM723K.js";
25
16
 
26
17
  // src/build.ts
27
18
  import { clean, read } from "@kubb/fs";
@@ -61,11 +52,10 @@ function hookParallel(promises) {
61
52
  }
62
53
 
63
54
  // src/PromiseManager.ts
64
- var _options;
65
55
  var PromiseManager = class {
56
+ #options = {};
66
57
  constructor(options = {}) {
67
- __privateAdd(this, _options, {});
68
- __privateSet(this, _options, options);
58
+ this.#options = options;
69
59
  return this;
70
60
  }
71
61
  run(strategy, promises) {
@@ -73,7 +63,7 @@ var PromiseManager = class {
73
63
  return hookSeq(promises);
74
64
  }
75
65
  if (strategy === "first") {
76
- return hookFirst(promises, __privateGet(this, _options).nullCheck);
66
+ return hookFirst(promises, this.#options.nullCheck);
77
67
  }
78
68
  if (strategy === "parallel") {
79
69
  return hookParallel(promises);
@@ -81,7 +71,6 @@ var PromiseManager = class {
81
71
  throw new Error(`${strategy} not implemented`);
82
72
  }
83
73
  };
84
- _options = new WeakMap();
85
74
  function isPromise(result) {
86
75
  return !!result && typeof result?.then === "function";
87
76
  }
@@ -94,7 +83,7 @@ var ValidationPluginError = class extends Error {
94
83
  };
95
84
 
96
85
  // src/plugin.ts
97
- import path from "path";
86
+ import path from "node:path";
98
87
 
99
88
  // src/utils/cache.ts
100
89
  function createPluginCache(Store = /* @__PURE__ */ Object.create(null)) {
@@ -163,7 +152,7 @@ var pluginCore = createPlugin((options) => {
163
152
  };
164
153
  },
165
154
  resolvePath(baseName) {
166
- const root = path.resolve(this.config.root, this.config.output.path);
155
+ const root = path.resolve(options.config.root, options.config.output.path);
167
156
  return path.resolve(root, baseName);
168
157
  },
169
158
  resolveName(name) {
@@ -173,75 +162,25 @@ var pluginCore = createPlugin((options) => {
173
162
  });
174
163
 
175
164
  // src/PluginManager.ts
176
- var _core, _usedPluginNames, _promiseManager, _PluginManager_instances, getSortedPlugins_fn, addExecutedToCallStack_fn, execute_fn, executeSync_fn, catcher_fn, parse_fn;
177
165
  var PluginManager = class {
166
+ plugins;
167
+ fileManager;
168
+ events = new EventEmitter();
169
+ config;
170
+ executed = [];
171
+ logger;
172
+ options;
173
+ #core;
174
+ #usedPluginNames = {};
175
+ #promiseManager;
178
176
  constructor(config, options) {
179
- __privateAdd(this, _PluginManager_instances);
180
- this.events = new EventEmitter();
181
- this.executed = [];
182
- __privateAdd(this, _core);
183
- __privateAdd(this, _usedPluginNames, {});
184
- __privateAdd(this, _promiseManager);
185
- this.resolvePath = (params) => {
186
- if (params.pluginKey) {
187
- const paths = this.hookForPluginSync({
188
- pluginKey: params.pluginKey,
189
- hookName: "resolvePath",
190
- parameters: [params.baseName, params.mode, params.options]
191
- });
192
- if (paths && paths?.length > 1) {
193
- this.logger.emit("debug", {
194
- logs: [
195
- `Cannot return a path where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough
196
-
197
- Paths: ${JSON.stringify(paths, void 0, 2)}
198
-
199
- Falling back on the first item.
200
- `
201
- ]
202
- });
203
- }
204
- return paths?.at(0);
205
- }
206
- return this.hookFirstSync({
207
- hookName: "resolvePath",
208
- parameters: [params.baseName, params.mode, params.options]
209
- }).result;
210
- };
211
- this.resolveName = (params) => {
212
- if (params.pluginKey) {
213
- const names = this.hookForPluginSync({
214
- pluginKey: params.pluginKey,
215
- hookName: "resolveName",
216
- parameters: [params.name, params.type]
217
- });
218
- if (names && names?.length > 1) {
219
- this.logger.emit("debug", {
220
- logs: [
221
- `Cannot return a name where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough
222
-
223
- Names: ${JSON.stringify(names, void 0, 2)}
224
-
225
- Falling back on the first item.
226
- `
227
- ]
228
- });
229
- }
230
- return transformReservedWord(names?.at(0) || params.name);
231
- }
232
- const name = this.hookFirstSync({
233
- hookName: "resolveName",
234
- parameters: [params.name, params.type]
235
- }).result;
236
- return transformReservedWord(name);
237
- };
238
177
  this.config = config;
239
178
  this.options = options;
240
179
  this.logger = options.logger;
241
180
  this.fileManager = new FileManager();
242
- __privateSet(this, _promiseManager, new PromiseManager({
181
+ this.#promiseManager = new PromiseManager({
243
182
  nullCheck: (state) => !!state?.result
244
- }));
183
+ });
245
184
  const plugins = config.plugins || [];
246
185
  const core = pluginCore({
247
186
  config,
@@ -250,11 +189,11 @@ Falling back on the first item.
250
189
  fileManager: this.fileManager,
251
190
  resolvePath: this.resolvePath.bind(this),
252
191
  resolveName: this.resolveName.bind(this),
253
- getPlugins: __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).bind(this)
192
+ getPlugins: this.#getSortedPlugins.bind(this)
254
193
  });
255
- __privateSet(this, _core, __privateMethod(this, _PluginManager_instances, parse_fn).call(this, core, this, core.context.call(null)));
256
- this.plugins = [__privateGet(this, _core), ...plugins].map((plugin) => {
257
- return __privateMethod(this, _PluginManager_instances, parse_fn).call(this, plugin, this, __privateGet(this, _core).context);
194
+ this.#core = this.#parse(core, this, core.context.call(null));
195
+ this.plugins = [this.#core, ...plugins].map((plugin) => {
196
+ return this.#parse(plugin, this, this.#core.context);
258
197
  });
259
198
  return this;
260
199
  }
@@ -273,6 +212,60 @@ Falling back on the first item.
273
212
  sources: []
274
213
  };
275
214
  }
215
+ resolvePath = (params) => {
216
+ if (params.pluginKey) {
217
+ const paths = this.hookForPluginSync({
218
+ pluginKey: params.pluginKey,
219
+ hookName: "resolvePath",
220
+ parameters: [params.baseName, params.mode, params.options]
221
+ });
222
+ if (paths && paths?.length > 1) {
223
+ this.logger.emit("debug", {
224
+ logs: [
225
+ `Cannot return a path where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough
226
+
227
+ Paths: ${JSON.stringify(paths, void 0, 2)}
228
+
229
+ Falling back on the first item.
230
+ `
231
+ ]
232
+ });
233
+ }
234
+ return paths?.at(0);
235
+ }
236
+ return this.hookFirstSync({
237
+ hookName: "resolvePath",
238
+ parameters: [params.baseName, params.mode, params.options]
239
+ }).result;
240
+ };
241
+ //TODO refactor by using the order of plugins and the cache of the fileManager instead of guessing and recreating the name/path
242
+ resolveName = (params) => {
243
+ if (params.pluginKey) {
244
+ const names = this.hookForPluginSync({
245
+ pluginKey: params.pluginKey,
246
+ hookName: "resolveName",
247
+ parameters: [params.name, params.type]
248
+ });
249
+ if (names && names?.length > 1) {
250
+ this.logger.emit("debug", {
251
+ logs: [
252
+ `Cannot return a name where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough
253
+
254
+ Names: ${JSON.stringify(names, void 0, 2)}
255
+
256
+ Falling back on the first item.
257
+ `
258
+ ]
259
+ });
260
+ }
261
+ return transformReservedWord(names?.at(0) || params.name);
262
+ }
263
+ const name = this.hookFirstSync({
264
+ hookName: "resolveName",
265
+ parameters: [params.name, params.type]
266
+ }).result;
267
+ return transformReservedWord(name);
268
+ };
276
269
  /**
277
270
  * Instead of calling `pluginManager.events.on` you can use `pluginManager.on`. This one also has better types.
278
271
  */
@@ -290,7 +283,7 @@ Falling back on the first item.
290
283
  const plugins = this.getPluginsByKey(hookName, pluginKey);
291
284
  this.logger.emit("progress_start", { id: hookName, size: plugins.length });
292
285
  const promises = plugins.map((plugin) => {
293
- return __privateMethod(this, _PluginManager_instances, execute_fn).call(this, {
286
+ return this.#execute({
294
287
  strategy: "hookFirst",
295
288
  hookName,
296
289
  parameters,
@@ -311,7 +304,7 @@ Falling back on the first item.
311
304
  }) {
312
305
  const plugins = this.getPluginsByKey(hookName, pluginKey);
313
306
  const result = plugins.map((plugin) => {
314
- return __privateMethod(this, _PluginManager_instances, executeSync_fn).call(this, {
307
+ return this.#executeSync({
315
308
  strategy: "hookFirst",
316
309
  hookName,
317
310
  parameters,
@@ -328,13 +321,13 @@ Falling back on the first item.
328
321
  parameters,
329
322
  skipped
330
323
  }) {
331
- const plugins = __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).call(this, hookName).filter((plugin) => {
324
+ const plugins = this.#getSortedPlugins(hookName).filter((plugin) => {
332
325
  return skipped ? skipped.has(plugin) : true;
333
326
  });
334
327
  this.logger.emit("progress_start", { id: hookName, size: plugins.length });
335
328
  const promises = plugins.map((plugin) => {
336
329
  return async () => {
337
- const value = await __privateMethod(this, _PluginManager_instances, execute_fn).call(this, {
330
+ const value = await this.#execute({
338
331
  strategy: "hookFirst",
339
332
  hookName,
340
333
  parameters,
@@ -346,7 +339,7 @@ Falling back on the first item.
346
339
  });
347
340
  };
348
341
  });
349
- const result = await __privateGet(this, _promiseManager).run("first", promises);
342
+ const result = await this.#promiseManager.run("first", promises);
350
343
  this.logger.emit("progress_stop", { id: hookName });
351
344
  return result;
352
345
  }
@@ -359,12 +352,12 @@ Falling back on the first item.
359
352
  skipped
360
353
  }) {
361
354
  let parseResult = null;
362
- const plugins = __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).call(this, hookName).filter((plugin) => {
355
+ const plugins = this.#getSortedPlugins(hookName).filter((plugin) => {
363
356
  return skipped ? skipped.has(plugin) : true;
364
357
  });
365
358
  for (const plugin of plugins) {
366
359
  parseResult = {
367
- result: __privateMethod(this, _PluginManager_instances, executeSync_fn).call(this, {
360
+ result: this.#executeSync({
368
361
  strategy: "hookFirst",
369
362
  hookName,
370
363
  parameters,
@@ -385,21 +378,21 @@ Falling back on the first item.
385
378
  hookName,
386
379
  parameters
387
380
  }) {
388
- const plugins = __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).call(this, hookName);
381
+ const plugins = this.#getSortedPlugins(hookName);
389
382
  this.logger.emit("progress_start", { id: hookName, size: plugins.length });
390
383
  const promises = plugins.map((plugin) => {
391
- return () => __privateMethod(this, _PluginManager_instances, execute_fn).call(this, {
384
+ return () => this.#execute({
392
385
  strategy: "hookParallel",
393
386
  hookName,
394
387
  parameters,
395
388
  plugin
396
389
  });
397
390
  });
398
- const results = await __privateGet(this, _promiseManager).run("parallel", promises);
391
+ const results = await this.#promiseManager.run("parallel", promises);
399
392
  results.forEach((result, index) => {
400
393
  if (isPromiseRejectedResult(result)) {
401
- const plugin = __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).call(this, hookName)[index];
402
- __privateMethod(this, _PluginManager_instances, catcher_fn).call(this, result.reason, plugin, hookName);
394
+ const plugin = this.#getSortedPlugins(hookName)[index];
395
+ this.#catcher(result.reason, plugin, hookName);
403
396
  }
404
397
  });
405
398
  this.logger.emit("progress_stop", { id: hookName });
@@ -414,18 +407,18 @@ Falling back on the first item.
414
407
  reduce
415
408
  }) {
416
409
  const [argument0, ...rest] = parameters;
417
- const plugins = __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).call(this, hookName);
410
+ const plugins = this.#getSortedPlugins(hookName);
418
411
  let promise = Promise.resolve(argument0);
419
412
  for (const plugin of plugins) {
420
413
  promise = promise.then((arg0) => {
421
- const value = __privateMethod(this, _PluginManager_instances, execute_fn).call(this, {
414
+ const value = this.#execute({
422
415
  strategy: "hookReduceArg0",
423
416
  hookName,
424
417
  parameters: [arg0, ...rest],
425
418
  plugin
426
419
  });
427
420
  return value;
428
- }).then((result) => reduce.call(__privateGet(this, _core).context, argument0, result, plugin));
421
+ }).then((result) => reduce.call(this.#core.context, argument0, result, plugin));
429
422
  }
430
423
  return promise;
431
424
  }
@@ -433,19 +426,46 @@ Falling back on the first item.
433
426
  * Chains plugins
434
427
  */
435
428
  async hookSeq({ hookName, parameters }) {
436
- const plugins = __privateMethod(this, _PluginManager_instances, getSortedPlugins_fn).call(this, hookName);
429
+ const plugins = this.#getSortedPlugins(hookName);
437
430
  this.logger.emit("progress_start", { id: hookName, size: plugins.length });
438
431
  const promises = plugins.map((plugin) => {
439
- return () => __privateMethod(this, _PluginManager_instances, execute_fn).call(this, {
432
+ return () => this.#execute({
440
433
  strategy: "hookSeq",
441
434
  hookName,
442
435
  parameters,
443
436
  plugin
444
437
  });
445
438
  });
446
- await __privateGet(this, _promiseManager).run("seq", promises);
439
+ await this.#promiseManager.run("seq", promises);
447
440
  this.logger.emit("progress_stop", { id: hookName });
448
441
  }
442
+ #getSortedPlugins(hookName) {
443
+ const plugins = [...this.plugins].filter((plugin) => plugin.name !== "core");
444
+ if (hookName) {
445
+ const containsHookName = plugins.some((item) => item[hookName]);
446
+ if (!containsHookName) {
447
+ this.logger.emit("debug", { logs: [`No hook ${hookName} found`] });
448
+ }
449
+ return plugins.filter((item) => item[hookName]);
450
+ }
451
+ return plugins.map((plugin) => {
452
+ if (plugin.pre) {
453
+ const isValid = plugin.pre.every((pluginName) => plugins.find((pluginToFind) => pluginToFind.name === pluginName));
454
+ if (!isValid) {
455
+ throw new ValidationPluginError(`This plugin has a pre set that is not valid(${JSON.stringify(plugin.pre, void 0, 2)})`);
456
+ }
457
+ }
458
+ return plugin;
459
+ }).sort((a, b) => {
460
+ if (b.pre?.includes(a.name)) {
461
+ return 1;
462
+ }
463
+ if (b.post?.includes(a.name)) {
464
+ return -1;
465
+ }
466
+ return 0;
467
+ });
468
+ }
449
469
  getPluginsByKey(hookName, pluginKey) {
450
470
  const plugins = [...this.plugins];
451
471
  const [searchPluginName, searchIdentifier] = pluginKey;
@@ -473,6 +493,120 @@ Falling back on the first item.
473
493
  }
474
494
  return pluginByPluginName;
475
495
  }
496
+ #addExecutedToCallStack(executer) {
497
+ if (executer) {
498
+ this.events.emit("executed", executer);
499
+ this.executed.push(executer);
500
+ this.logger.emit("progress", { id: executer.hookName, data: `${executer.plugin.name} ${executer.parameters?.join(", ") || ""}` });
501
+ }
502
+ }
503
+ /**
504
+ * Run an async plugin hook and return the result.
505
+ * @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`.
506
+ * @param args Arguments passed to the plugin hook.
507
+ * @param plugin The actual pluginObject to run.
508
+ */
509
+ // Implementation signature
510
+ #execute({
511
+ strategy,
512
+ hookName,
513
+ parameters,
514
+ plugin
515
+ }) {
516
+ const hook = plugin[hookName];
517
+ let output;
518
+ if (!hook) {
519
+ return null;
520
+ }
521
+ this.events.emit("execute", { strategy, hookName, parameters, plugin });
522
+ const promise = new Promise((resolve2) => {
523
+ resolve2(void 0);
524
+ });
525
+ const task = promise.then(() => {
526
+ if (typeof hook === "function") {
527
+ const possiblePromiseResult = hook.apply({ ...this.#core.context, plugin }, parameters);
528
+ if (isPromise(possiblePromiseResult)) {
529
+ return Promise.resolve(possiblePromiseResult);
530
+ }
531
+ return possiblePromiseResult;
532
+ }
533
+ return hook;
534
+ }).then((result) => {
535
+ output = result;
536
+ this.#addExecutedToCallStack({
537
+ parameters,
538
+ output,
539
+ strategy,
540
+ hookName,
541
+ plugin
542
+ });
543
+ return result;
544
+ }).catch((e) => {
545
+ this.#catcher(e, plugin, hookName);
546
+ return null;
547
+ });
548
+ return task;
549
+ }
550
+ /**
551
+ * Run a sync plugin hook and return the result.
552
+ * @param hookName Name of the plugin hook. Must be in `PluginHooks`.
553
+ * @param args Arguments passed to the plugin hook.
554
+ * @param plugin The acutal plugin
555
+ * @param replaceContext When passed, the plugin context can be overridden.
556
+ */
557
+ #executeSync({
558
+ strategy,
559
+ hookName,
560
+ parameters,
561
+ plugin
562
+ }) {
563
+ const hook = plugin[hookName];
564
+ let output;
565
+ if (!hook) {
566
+ return null;
567
+ }
568
+ this.events.emit("execute", { strategy, hookName, parameters, plugin });
569
+ try {
570
+ if (typeof hook === "function") {
571
+ const fn = hook.apply({ ...this.#core.context, plugin }, parameters);
572
+ output = fn;
573
+ return fn;
574
+ }
575
+ output = hook;
576
+ this.#addExecutedToCallStack({
577
+ parameters,
578
+ output,
579
+ strategy,
580
+ hookName,
581
+ plugin
582
+ });
583
+ return hook;
584
+ } catch (e) {
585
+ this.#catcher(e, plugin, hookName);
586
+ return null;
587
+ }
588
+ }
589
+ #catcher(cause, plugin, hookName) {
590
+ const text = `${cause.message} (plugin: ${plugin?.name || "unknown"}, hook: ${hookName || "unknown"})`;
591
+ this.logger.emit("error", text, cause);
592
+ this.events.emit("error", cause);
593
+ }
594
+ #parse(plugin, pluginManager, context) {
595
+ const usedPluginNames = pluginManager.#usedPluginNames;
596
+ setUniqueName(plugin.name, usedPluginNames);
597
+ const key = [plugin.name, usedPluginNames[plugin.name]].filter(Boolean);
598
+ if (plugin.context && typeof plugin.context === "function") {
599
+ return {
600
+ ...plugin,
601
+ key,
602
+ context: plugin.context.call(context)
603
+ };
604
+ }
605
+ return {
606
+ ...plugin,
607
+ key
608
+ };
609
+ }
476
610
  static getDependedPlugins(plugins, dependedPluginNames) {
477
611
  let pluginNames = [];
478
612
  if (typeof dependedPluginNames === "string") {
@@ -492,151 +626,6 @@ Falling back on the first item.
492
626
  return ["buildStart", "resolvePath", "resolveName", "buildEnd"];
493
627
  }
494
628
  };
495
- _core = new WeakMap();
496
- _usedPluginNames = new WeakMap();
497
- _promiseManager = new WeakMap();
498
- _PluginManager_instances = new WeakSet();
499
- getSortedPlugins_fn = function(hookName) {
500
- const plugins = [...this.plugins].filter((plugin) => plugin.name !== "core");
501
- if (hookName) {
502
- const containsHookName = plugins.some((item) => item[hookName]);
503
- if (!containsHookName) {
504
- this.logger.emit("debug", { logs: [`No hook ${hookName} found`] });
505
- }
506
- return plugins.filter((item) => item[hookName]);
507
- }
508
- return plugins.map((plugin) => {
509
- if (plugin.pre) {
510
- const isValid = plugin.pre.every((pluginName) => plugins.find((pluginToFind) => pluginToFind.name === pluginName));
511
- if (!isValid) {
512
- throw new ValidationPluginError(`This plugin has a pre set that is not valid(${JSON.stringify(plugin.pre, void 0, 2)})`);
513
- }
514
- }
515
- return plugin;
516
- }).sort((a, b) => {
517
- if (b.pre?.includes(a.name)) {
518
- return 1;
519
- }
520
- if (b.post?.includes(a.name)) {
521
- return -1;
522
- }
523
- return 0;
524
- });
525
- };
526
- addExecutedToCallStack_fn = function(executer) {
527
- if (executer) {
528
- this.events.emit("executed", executer);
529
- this.executed.push(executer);
530
- this.logger.emit("progress", { id: executer.hookName, data: `${executer.plugin.name}` });
531
- }
532
- };
533
- /**
534
- * Run an async plugin hook and return the result.
535
- * @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`.
536
- * @param args Arguments passed to the plugin hook.
537
- * @param plugin The actual pluginObject to run.
538
- */
539
- // Implementation signature
540
- execute_fn = function({
541
- strategy,
542
- hookName,
543
- parameters,
544
- plugin
545
- }) {
546
- const hook = plugin[hookName];
547
- let output;
548
- if (!hook) {
549
- return null;
550
- }
551
- this.events.emit("execute", { strategy, hookName, parameters, plugin });
552
- const promise = new Promise((resolve) => {
553
- resolve(void 0);
554
- });
555
- const task = promise.then(() => {
556
- if (typeof hook === "function") {
557
- const possiblePromiseResult = hook.apply({ ...__privateGet(this, _core).context, plugin }, parameters);
558
- if (isPromise(possiblePromiseResult)) {
559
- return Promise.resolve(possiblePromiseResult);
560
- }
561
- return possiblePromiseResult;
562
- }
563
- return hook;
564
- }).then((result) => {
565
- output = result;
566
- __privateMethod(this, _PluginManager_instances, addExecutedToCallStack_fn).call(this, {
567
- parameters,
568
- output,
569
- strategy,
570
- hookName,
571
- plugin
572
- });
573
- return result;
574
- }).catch((e) => {
575
- __privateMethod(this, _PluginManager_instances, catcher_fn).call(this, e, plugin, hookName);
576
- return null;
577
- });
578
- return task;
579
- };
580
- /**
581
- * Run a sync plugin hook and return the result.
582
- * @param hookName Name of the plugin hook. Must be in `PluginHooks`.
583
- * @param args Arguments passed to the plugin hook.
584
- * @param plugin The acutal plugin
585
- * @param replaceContext When passed, the plugin context can be overridden.
586
- */
587
- executeSync_fn = function({
588
- strategy,
589
- hookName,
590
- parameters,
591
- plugin
592
- }) {
593
- const hook = plugin[hookName];
594
- let output;
595
- if (!hook) {
596
- return null;
597
- }
598
- this.events.emit("execute", { strategy, hookName, parameters, plugin });
599
- try {
600
- if (typeof hook === "function") {
601
- const fn = hook.apply({ ...__privateGet(this, _core).context, plugin }, parameters);
602
- output = fn;
603
- return fn;
604
- }
605
- output = hook;
606
- __privateMethod(this, _PluginManager_instances, addExecutedToCallStack_fn).call(this, {
607
- parameters,
608
- output,
609
- strategy,
610
- hookName,
611
- plugin
612
- });
613
- return hook;
614
- } catch (e) {
615
- __privateMethod(this, _PluginManager_instances, catcher_fn).call(this, e, plugin, hookName);
616
- return null;
617
- }
618
- };
619
- catcher_fn = function(cause, plugin, hookName) {
620
- const text = `${cause.message} (plugin: ${plugin?.name || "unknown"}, hook: ${hookName || "unknown"})`;
621
- this.logger.emit("error", text, cause);
622
- this.events.emit("error", cause);
623
- };
624
- parse_fn = function(plugin, pluginManager, context) {
625
- const usedPluginNames = __privateGet(pluginManager, _usedPluginNames);
626
- setUniqueName(plugin.name, usedPluginNames);
627
- const key = [plugin.name, usedPluginNames[plugin.name]].filter(Boolean);
628
- if (plugin.context && typeof plugin.context === "function") {
629
- return {
630
- ...plugin,
631
- key,
632
- context: plugin.context.call(context)
633
- };
634
- }
635
- return {
636
- ...plugin,
637
- key
638
- };
639
- };
640
629
 
641
630
  // src/config.ts
642
631
  function defineConfig(options) {
@@ -647,6 +636,8 @@ function isInputPath(result) {
647
636
  }
648
637
 
649
638
  // src/build.ts
639
+ import { resolve } from "node:path";
640
+ import { getRelativePath } from "@kubb/fs";
650
641
  async function setup(options) {
651
642
  const { config, logger = createLogger() } = options;
652
643
  try {
@@ -655,12 +646,9 @@ async function setup(options) {
655
646
  }
656
647
  } catch (e) {
657
648
  if (isInputPath(config)) {
658
- throw new Error(
659
- `Cannot read file/URL defined in \`input.path\` or set with \`kubb generate PATH\` in the CLI of your Kubb config ${p.dim(config.input.path)}`,
660
- {
661
- cause: e
662
- }
663
- );
649
+ throw new Error(`Cannot read file/URL defined in \`input.path\` or set with \`kubb generate PATH\` in the CLI of your Kubb config ${config.input.path}`, {
650
+ cause: e
651
+ });
664
652
  }
665
653
  }
666
654
  if (config.output.clean) {
@@ -669,21 +657,12 @@ async function setup(options) {
669
657
  return new PluginManager(config, { logger });
670
658
  }
671
659
  async function build(options) {
672
- const pluginManager = await setup(options);
673
- await pluginManager.hookParallel({
674
- hookName: "buildStart",
675
- parameters: [options.config]
676
- });
677
- const files = await processFiles({
678
- config: options.config,
679
- dryRun: !options.config.output.write,
680
- files: pluginManager.fileManager.files,
681
- logger: pluginManager.logger
682
- });
683
- await pluginManager.hookParallel({ hookName: "buildEnd" });
660
+ const { files, pluginManager, error } = await safeBuild(options);
661
+ if (error) throw error;
684
662
  return {
685
663
  files,
686
- pluginManager
664
+ pluginManager,
665
+ error
687
666
  };
688
667
  }
689
668
  async function safeBuild(options) {
@@ -694,6 +673,42 @@ async function safeBuild(options) {
694
673
  hookName: "buildStart",
695
674
  parameters: [options.config]
696
675
  });
676
+ const root = resolve(options.config.root);
677
+ const rootPath = resolve(root, options.config.output.path, "index.ts");
678
+ const barrelFiles = pluginManager.fileManager.files.filter((file) => {
679
+ return file.sources.some((source) => source.isIndexable);
680
+ });
681
+ const rootFile = {
682
+ path: rootPath,
683
+ baseName: "index.ts",
684
+ exports: barrelFiles.flatMap((file) => {
685
+ return file.sources?.map((source) => {
686
+ if (!file.path || !source.isIndexable) {
687
+ return void 0;
688
+ }
689
+ const plugin = pluginManager.plugins.find((item) => {
690
+ const meta = file.meta;
691
+ return item.name === meta?.pluginKey?.[0];
692
+ });
693
+ if (plugin?.output?.exportType === false) {
694
+ return void 0;
695
+ }
696
+ if (FileManager.getMode(plugin?.output?.path) === "single") {
697
+ return void 0;
698
+ }
699
+ return {
700
+ name: options.config.output.exportType === "barrel" ? void 0 : [source.name],
701
+ path: getRelativePath(rootPath, file.path),
702
+ isTypeOnly: source.isTypeOnly
703
+ };
704
+ }).filter(Boolean);
705
+ }).filter(Boolean),
706
+ sources: [],
707
+ meta: {}
708
+ };
709
+ if (options.config.output.exportType) {
710
+ await pluginManager.fileManager.add(rootFile);
711
+ }
697
712
  files = await processFiles({
698
713
  config: options.config,
699
714
  dryRun: !options.config.output.write,
@@ -701,6 +716,7 @@ async function safeBuild(options) {
701
716
  logger: pluginManager.logger
702
717
  });
703
718
  await pluginManager.hookParallel({ hookName: "buildEnd" });
719
+ pluginManager.fileManager.clear();
704
720
  } catch (e) {
705
721
  return {
706
722
  files: [],
@@ -715,66 +731,62 @@ async function safeBuild(options) {
715
731
  }
716
732
 
717
733
  // src/Generator.ts
718
- var _options2, _context;
719
734
  var Generator = class {
735
+ #options = {};
736
+ #context = {};
720
737
  constructor(options, context) {
721
- __privateAdd(this, _options2, {});
722
- __privateAdd(this, _context, {});
723
738
  if (context) {
724
- __privateSet(this, _context, context);
739
+ this.#context = context;
725
740
  }
726
741
  if (options) {
727
- __privateSet(this, _options2, options);
742
+ this.#options = options;
728
743
  }
729
744
  return this;
730
745
  }
731
746
  get options() {
732
- return __privateGet(this, _options2);
747
+ return this.#options;
733
748
  }
734
749
  get context() {
735
- return __privateGet(this, _context);
750
+ return this.#context;
736
751
  }
737
752
  set options(options) {
738
- __privateSet(this, _options2, { ...__privateGet(this, _options2), ...options });
753
+ this.#options = { ...this.#options, ...options };
739
754
  }
740
755
  };
741
- _options2 = new WeakMap();
742
- _context = new WeakMap();
743
756
 
744
757
  // src/PackageManager.ts
745
- import mod from "module";
746
- import os from "os";
747
- import { pathToFileURL } from "url";
758
+ import mod from "node:module";
759
+ import os from "node:os";
760
+ import { pathToFileURL } from "node:url";
748
761
  import { findUp, findUpSync } from "find-up";
749
762
  import { coerce, satisfies } from "semver";
750
763
  import { read as read2, readSync } from "@kubb/fs";
751
- var _cache, _cwd, _SLASHES, _PackageManager_instances, match_fn;
752
- var _PackageManager = class _PackageManager {
764
+ var PackageManager = class _PackageManager {
765
+ static #cache = {};
766
+ #cwd;
767
+ #SLASHES = /* @__PURE__ */ new Set(["/", "\\"]);
753
768
  constructor(workspace) {
754
- __privateAdd(this, _PackageManager_instances);
755
- __privateAdd(this, _cwd);
756
- __privateAdd(this, _SLASHES, /* @__PURE__ */ new Set(["/", "\\"]));
757
769
  if (workspace) {
758
- __privateSet(this, _cwd, workspace);
770
+ this.#cwd = workspace;
759
771
  }
760
772
  return this;
761
773
  }
762
774
  set workspace(workspace) {
763
- __privateSet(this, _cwd, workspace);
775
+ this.#cwd = workspace;
764
776
  }
765
777
  get workspace() {
766
- return __privateGet(this, _cwd);
778
+ return this.#cwd;
767
779
  }
768
780
  normalizeDirectory(directory) {
769
- if (!__privateGet(this, _SLASHES).has(directory[directory.length - 1])) {
781
+ if (!this.#SLASHES.has(directory[directory.length - 1])) {
770
782
  return `${directory}/`;
771
783
  }
772
784
  return directory;
773
785
  }
774
786
  getLocation(path2) {
775
787
  let location = path2;
776
- if (__privateGet(this, _cwd)) {
777
- const require2 = mod.createRequire(this.normalizeDirectory(__privateGet(this, _cwd)));
788
+ if (this.#cwd) {
789
+ const require2 = mod.createRequire(this.normalizeDirectory(this.#cwd));
778
790
  location = require2.resolve(path2);
779
791
  }
780
792
  return location;
@@ -794,7 +806,7 @@ var _PackageManager = class _PackageManager {
794
806
  }
795
807
  async getPackageJSON() {
796
808
  const pkgPath = await findUp(["package.json"], {
797
- cwd: __privateGet(this, _cwd)
809
+ cwd: this.#cwd
798
810
  });
799
811
  if (!pkgPath) {
800
812
  return void 0;
@@ -804,7 +816,7 @@ var _PackageManager = class _PackageManager {
804
816
  }
805
817
  getPackageJSONSync() {
806
818
  const pkgPath = findUpSync(["package.json"], {
807
- cwd: __privateGet(this, _cwd)
819
+ cwd: this.#cwd
808
820
  });
809
821
  if (!pkgPath) {
810
822
  return void 0;
@@ -813,27 +825,38 @@ var _PackageManager = class _PackageManager {
813
825
  return JSON.parse(json);
814
826
  }
815
827
  static setVersion(dependency, version) {
816
- __privateGet(_PackageManager, _cache)[dependency] = version;
828
+ _PackageManager.#cache[dependency] = version;
829
+ }
830
+ #match(packageJSON, dependency) {
831
+ const dependencies = {
832
+ ...packageJSON["dependencies"] || {},
833
+ ...packageJSON["devDependencies"] || {}
834
+ };
835
+ if (typeof dependency === "string" && dependencies[dependency]) {
836
+ return dependencies[dependency];
837
+ }
838
+ const matchedDependency = Object.keys(dependencies).find((dep) => dep.match(dependency));
839
+ return matchedDependency ? dependencies[matchedDependency] : void 0;
817
840
  }
818
841
  async getVersion(dependency) {
819
- if (typeof dependency === "string" && __privateGet(_PackageManager, _cache)[dependency]) {
820
- return __privateGet(_PackageManager, _cache)[dependency];
842
+ if (typeof dependency === "string" && _PackageManager.#cache[dependency]) {
843
+ return _PackageManager.#cache[dependency];
821
844
  }
822
845
  const packageJSON = await this.getPackageJSON();
823
846
  if (!packageJSON) {
824
847
  return void 0;
825
848
  }
826
- return __privateMethod(this, _PackageManager_instances, match_fn).call(this, packageJSON, dependency);
849
+ return this.#match(packageJSON, dependency);
827
850
  }
828
851
  getVersionSync(dependency) {
829
- if (typeof dependency === "string" && __privateGet(_PackageManager, _cache)[dependency]) {
830
- return __privateGet(_PackageManager, _cache)[dependency];
852
+ if (typeof dependency === "string" && _PackageManager.#cache[dependency]) {
853
+ return _PackageManager.#cache[dependency];
831
854
  }
832
855
  const packageJSON = this.getPackageJSONSync();
833
856
  if (!packageJSON) {
834
857
  return void 0;
835
858
  }
836
- return __privateMethod(this, _PackageManager_instances, match_fn).call(this, packageJSON, dependency);
859
+ return this.#match(packageJSON, dependency);
837
860
  }
838
861
  async isValid(dependency, version) {
839
862
  const packageVersion = await this.getVersion(dependency);
@@ -861,23 +884,6 @@ var _PackageManager = class _PackageManager {
861
884
  return satisfies(semVer, version);
862
885
  }
863
886
  };
864
- _cache = new WeakMap();
865
- _cwd = new WeakMap();
866
- _SLASHES = new WeakMap();
867
- _PackageManager_instances = new WeakSet();
868
- match_fn = function(packageJSON, dependency) {
869
- const dependencies = {
870
- ...packageJSON["dependencies"] || {},
871
- ...packageJSON["devDependencies"] || {}
872
- };
873
- if (typeof dependency === "string" && dependencies[dependency]) {
874
- return dependencies[dependency];
875
- }
876
- const matchedDependency = Object.keys(dependencies).find((dep) => dep.match(dependency));
877
- return matchedDependency ? dependencies[matchedDependency] : void 0;
878
- };
879
- __privateAdd(_PackageManager, _cache, {});
880
- var PackageManager = _PackageManager;
881
887
  export {
882
888
  FileManager,
883
889
  Generator,