@kubb/core 2.0.0-alpha.3 → 2.0.0-alpha.5

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 (53) hide show
  1. package/dist/index.cjs +70 -75
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +198 -215
  4. package/dist/index.d.ts +198 -215
  5. package/dist/index.js +70 -73
  6. package/dist/index.js.map +1 -1
  7. package/dist/utils.cjs +19 -8
  8. package/dist/utils.cjs.map +1 -1
  9. package/dist/utils.d.cts +6 -2
  10. package/dist/utils.d.ts +6 -2
  11. package/dist/utils.js +19 -8
  12. package/dist/utils.js.map +1 -1
  13. package/package.json +7 -5
  14. package/src/BarrelManager.ts +123 -0
  15. package/src/FileManager.ts +483 -0
  16. package/src/Generator.ts +34 -0
  17. package/src/PackageManager.ts +163 -0
  18. package/src/PluginManager.ts +644 -0
  19. package/src/PromiseManager.ts +47 -0
  20. package/src/SchemaGenerator.ts +8 -0
  21. package/src/build.ts +207 -0
  22. package/src/config.ts +22 -0
  23. package/src/errors.ts +12 -0
  24. package/src/index.ts +28 -0
  25. package/src/plugin.ts +80 -0
  26. package/src/types.ts +346 -0
  27. package/src/utils/EventEmitter.ts +24 -0
  28. package/src/utils/FunctionParams.ts +85 -0
  29. package/src/utils/Queue.ts +110 -0
  30. package/src/utils/TreeNode.ts +122 -0
  31. package/src/utils/URLPath.ts +128 -0
  32. package/src/utils/cache.ts +35 -0
  33. package/src/utils/clean.ts +5 -0
  34. package/src/utils/executeStrategies.ts +71 -0
  35. package/src/utils/index.ts +19 -0
  36. package/src/utils/logger.ts +76 -0
  37. package/src/utils/promise.ts +13 -0
  38. package/src/utils/randomColour.ts +39 -0
  39. package/src/utils/read.ts +68 -0
  40. package/src/utils/renderTemplate.ts +31 -0
  41. package/src/utils/throttle.ts +30 -0
  42. package/src/utils/timeout.ts +7 -0
  43. package/src/utils/transformers/combineCodes.ts +3 -0
  44. package/src/utils/transformers/createJSDocBlockText.ts +15 -0
  45. package/src/utils/transformers/escape.ts +31 -0
  46. package/src/utils/transformers/indent.ts +3 -0
  47. package/src/utils/transformers/index.ts +22 -0
  48. package/src/utils/transformers/nameSorter.ts +9 -0
  49. package/src/utils/transformers/searchAndReplace.ts +25 -0
  50. package/src/utils/transformers/transformReservedWord.ts +97 -0
  51. package/src/utils/transformers/trim.ts +3 -0
  52. package/src/utils/uniqueName.ts +20 -0
  53. package/src/utils/write.ts +63 -0
package/dist/index.js CHANGED
@@ -85,7 +85,7 @@ function createLogger({ logLevel, name, spinner }) {
85
85
  }
86
86
  };
87
87
  const info = (message) => {
88
- if (message && spinner) {
88
+ if (message && spinner && logLevel !== LogLevel.silent) {
89
89
  spinner.info(message);
90
90
  logs.push(message);
91
91
  }
@@ -270,14 +270,19 @@ function combineCodes(codes) {
270
270
  }
271
271
 
272
272
  // src/utils/transformers/createJSDocBlockText.ts
273
- function createJSDocBlockText({ comments }) {
273
+ function createJSDocBlockText({ comments, newLine }) {
274
274
  const filteredComments = comments.filter(Boolean);
275
275
  if (!filteredComments.length) {
276
276
  return "";
277
277
  }
278
- return `/**
278
+ const source = `/**
279
279
  * ${filteredComments.join("\n * ")}
280
280
  */`;
281
+ if (newLine) {
282
+ return `${source}
283
+ `;
284
+ }
285
+ return source;
281
286
  }
282
287
 
283
288
  // src/utils/transformers/escape.ts
@@ -431,6 +436,11 @@ function transformReservedWord(word) {
431
436
  return word;
432
437
  }
433
438
 
439
+ // src/utils/transformers/trim.ts
440
+ function trim(text) {
441
+ return text.replaceAll(/\n/g, "").trim();
442
+ }
443
+
434
444
  // src/utils/transformers/index.ts
435
445
  var transformers = {
436
446
  combineCodes,
@@ -440,6 +450,7 @@ var transformers = {
440
450
  transformReservedWord,
441
451
  nameSorter,
442
452
  searchAndReplace,
453
+ trim,
443
454
  JSDoc: {
444
455
  createJSDocBlockText
445
456
  }
@@ -621,12 +632,12 @@ var BarrelManager = class {
621
632
  if (currentTree.children?.length > 1) {
622
633
  const indexPath = path.resolve(currentTree.data.path, "index.ts");
623
634
  const exports = currentTree.children.filter(Boolean).map((file) => {
624
- const importPath = file.data.type === "directory" ? `./${file.data.name}` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
625
- if (importPath.includes("index") && indexPath.includes("index")) {
635
+ const importPath = file.data.type === "directory" ? `./${file.data.name}/index` : `./${file.data.name.replace(/\.[^.]*$/, "")}`;
636
+ if (importPath.includes("index") && file.data.type === "file") {
626
637
  return void 0;
627
638
  }
628
639
  return {
629
- path: includeExt ? file.data.type === "directory" ? `${importPath}/index${extName}` : `${importPath}${extName}` : importPath,
640
+ path: includeExt ? `${importPath}${extName}` : importPath,
630
641
  isTypeOnly
631
642
  };
632
643
  }).filter(Boolean);
@@ -641,10 +652,10 @@ var BarrelManager = class {
641
652
  } else {
642
653
  currentTree.children?.forEach((child) => {
643
654
  const indexPath = path.resolve(currentTree.data.path, "index.ts");
644
- const importPath = child.data.type === "directory" ? `./${child.data.name}` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
655
+ const importPath = child.data.type === "directory" ? `./${child.data.name}/index` : `./${child.data.name.replace(/\.[^.]*$/, "")}`;
645
656
  const exports = [
646
657
  {
647
- path: includeExt ? child.data.type === "directory" ? `${importPath}/index${extName}` : `${importPath}${extName}` : importPath,
658
+ path: includeExt ? `${importPath}${extName}` : importPath,
648
659
  isTypeOnly
649
660
  }
650
661
  ];
@@ -1064,44 +1075,6 @@ function setUniqueName(originalName, data) {
1064
1075
  }
1065
1076
 
1066
1077
  // src/errors.ts
1067
- var PluginError = class extends Error {
1068
- constructor(message, options) {
1069
- super(message, { cause: options.cause });
1070
- this.name = "PluginError";
1071
- this.cause = options.cause;
1072
- this.pluginManager = options.pluginManager;
1073
- }
1074
- };
1075
- var ParallelPluginError = class extends Error {
1076
- constructor(message, options) {
1077
- super(message, { cause: options.cause });
1078
- this.errors = [];
1079
- this.name = "ParallelPluginError";
1080
- this.errors = options.errors;
1081
- this.pluginManager = options.pluginManager;
1082
- }
1083
- findError(searchError) {
1084
- if (!searchError) {
1085
- return void 0;
1086
- }
1087
- return this.errors.find((error) => {
1088
- if (error.cause) {
1089
- if (error.cause.name == searchError.name) {
1090
- return true;
1091
- }
1092
- return !!this.findError(error.cause);
1093
- }
1094
- return error.name === searchError.name;
1095
- })?.cause;
1096
- }
1097
- };
1098
- var SummaryError = class extends Error {
1099
- constructor(message, options) {
1100
- super(message, { cause: options.cause });
1101
- this.name = "SummaryError";
1102
- this.summary = options.summary || [];
1103
- }
1104
- };
1105
1078
  var Warning = class extends Error {
1106
1079
  constructor(message, options) {
1107
1080
  super(message, { cause: options?.cause });
@@ -1318,6 +1291,7 @@ Names: ${JSON.stringify(names, void 0, 2)}`
1318
1291
  }).result;
1319
1292
  return transformReservedWord(name);
1320
1293
  };
1294
+ this.config = config;
1321
1295
  this.logger = options.logger;
1322
1296
  this.queue = new Queue(100, this.logger.logLevel === LogLevel.debug);
1323
1297
  this.fileManager = new FileManager({ task: options.task, queue: this.queue, timeout: options.writeTimeout });
@@ -1446,15 +1420,12 @@ Names: ${JSON.stringify(names, void 0, 2)}`
1446
1420
  }
1447
1421
  }
1448
1422
  const results = await Promise.allSettled(parallelPromises);
1449
- const errors = results.map((result) => {
1450
- if (isPromiseRejectedResult(result) && result.reason instanceof PluginError) {
1451
- return result.reason;
1423
+ results.forEach((result, index) => {
1424
+ if (isPromiseRejectedResult(result)) {
1425
+ const plugin = __privateMethod(this, _getSortedPlugins, getSortedPlugins_fn).call(this)[index];
1426
+ __privateMethod(this, _catcher, catcher_fn).call(this, result.reason, plugin, hookName);
1452
1427
  }
1453
- return void 0;
1454
- }).filter(Boolean);
1455
- if (errors.length) {
1456
- throw new ParallelPluginError("Error", { errors, pluginManager: this });
1457
- }
1428
+ });
1458
1429
  return results.filter((result) => result.status === "fulfilled").map((result) => result.value);
1459
1430
  }
1460
1431
  /**
@@ -1588,11 +1559,6 @@ execute_fn = function({
1588
1559
  return hook;
1589
1560
  }).then((result) => {
1590
1561
  output = result;
1591
- return result;
1592
- }).catch((e) => {
1593
- __privateMethod(this, _catcher, catcher_fn).call(this, e, plugin, hookName);
1594
- return null;
1595
- }).finally(() => {
1596
1562
  __privateMethod(this, _addExecutedToCallStack, addExecutedToCallStack_fn).call(this, {
1597
1563
  parameters,
1598
1564
  output,
@@ -1600,6 +1566,10 @@ execute_fn = function({
1600
1566
  hookName,
1601
1567
  plugin
1602
1568
  });
1569
+ return result;
1570
+ }).catch((e) => {
1571
+ __privateMethod(this, _catcher, catcher_fn).call(this, e, plugin, hookName);
1572
+ return null;
1603
1573
  });
1604
1574
  return task;
1605
1575
  };
@@ -1623,11 +1593,6 @@ executeSync_fn = function({
1623
1593
  return fn;
1624
1594
  }
1625
1595
  output = hook;
1626
- return hook;
1627
- } catch (e) {
1628
- __privateMethod(this, _catcher, catcher_fn).call(this, e, plugin, hookName);
1629
- return null;
1630
- } finally {
1631
1596
  __privateMethod(this, _addExecutedToCallStack, addExecutedToCallStack_fn).call(this, {
1632
1597
  parameters,
1633
1598
  output,
@@ -1635,15 +1600,18 @@ executeSync_fn = function({
1635
1600
  hookName,
1636
1601
  plugin
1637
1602
  });
1603
+ return hook;
1604
+ } catch (e) {
1605
+ __privateMethod(this, _catcher, catcher_fn).call(this, e, plugin, hookName);
1606
+ return null;
1638
1607
  }
1639
1608
  };
1640
1609
  _catcher = new WeakSet();
1641
1610
  catcher_fn = function(e, plugin, hookName) {
1642
- const text = `${e.message} (plugin: ${plugin.name}, hook: ${hookName})
1611
+ const text = `${e.message} (plugin: ${plugin?.name || "unknown"}, hook: ${hookName || "unknown"})
1643
1612
  `;
1644
- const pluginError = new PluginError(text, { cause: e, pluginManager: this });
1645
- this.eventEmitter.emit("error", pluginError);
1646
- throw pluginError;
1613
+ this.logger.error(text);
1614
+ this.eventEmitter.emit("error", e);
1647
1615
  };
1648
1616
  _parse = new WeakSet();
1649
1617
  parse_fn = function(plugin, pluginManager, context) {
@@ -1676,7 +1644,7 @@ parse_fn = function(plugin, pluginManager, context) {
1676
1644
  async function transformReducer(_previousCode, result, _plugin) {
1677
1645
  return result;
1678
1646
  }
1679
- async function build(options) {
1647
+ async function setup(options) {
1680
1648
  const { config, logger = createLogger({ logLevel: LogLevel.silent }) } = options;
1681
1649
  try {
1682
1650
  if (isInputPath(config) && !new URLPath(config.input.path).isURL) {
@@ -1730,7 +1698,6 @@ async function build(options) {
1730
1698
  }
1731
1699
  };
1732
1700
  const pluginManager = new PluginManager(config, { logger, task: queueTask, writeTimeout: 0 });
1733
- const { plugins, fileManager } = pluginManager;
1734
1701
  pluginManager.on("execute", (executer) => {
1735
1702
  const { hookName, parameters, plugin } = executer;
1736
1703
  if (hookName === "writeFile" && logger.spinner) {
@@ -1768,13 +1735,18 @@ ${code}`);
1768
1735
  console.log(logs.join("\n"));
1769
1736
  }
1770
1737
  });
1738
+ return pluginManager;
1739
+ }
1740
+ async function build(options) {
1741
+ const pluginManager = await setup(options);
1742
+ const { fileManager, logger } = pluginManager;
1771
1743
  await pluginManager.hookParallel({
1772
1744
  hookName: "validate",
1773
- parameters: [plugins]
1745
+ parameters: [pluginManager.plugins]
1774
1746
  });
1775
1747
  await pluginManager.hookParallel({
1776
1748
  hookName: "buildStart",
1777
- parameters: [config]
1749
+ parameters: [options.config]
1778
1750
  });
1779
1751
  await pluginManager.hookParallel({ hookName: "buildEnd" });
1780
1752
  if (!fileManager.isExecuting && logger.spinner) {
@@ -1783,6 +1755,28 @@ ${code}`);
1783
1755
  }
1784
1756
  return { files: fileManager.files.map((file) => ({ ...file, source: FileManager.getSource(file) })), pluginManager };
1785
1757
  }
1758
+ async function safeBuild(options) {
1759
+ const pluginManager = await setup(options);
1760
+ const { fileManager, logger } = pluginManager;
1761
+ try {
1762
+ await pluginManager.hookParallel({
1763
+ hookName: "validate",
1764
+ parameters: [pluginManager.plugins]
1765
+ });
1766
+ await pluginManager.hookParallel({
1767
+ hookName: "buildStart",
1768
+ parameters: [options.config]
1769
+ });
1770
+ await pluginManager.hookParallel({ hookName: "buildEnd" });
1771
+ if (!fileManager.isExecuting && logger.spinner) {
1772
+ logger.spinner.suffixText = "";
1773
+ logger.spinner.succeed(`\u{1F4BE} Writing completed`);
1774
+ }
1775
+ } catch (e) {
1776
+ return { files: fileManager.files.map((file) => ({ ...file, source: FileManager.getSource(file) })), pluginManager, error: e };
1777
+ }
1778
+ return { files: fileManager.files.map((file) => ({ ...file, source: FileManager.getSource(file) })), pluginManager };
1779
+ }
1786
1780
 
1787
1781
  // src/Generator.ts
1788
1782
  var _options3, _context;
@@ -1899,6 +1893,9 @@ var _PackageManager = class _PackageManager {
1899
1893
  if (!packageVersion) {
1900
1894
  return false;
1901
1895
  }
1896
+ if (packageVersion === version) {
1897
+ return true;
1898
+ }
1902
1899
  const semVer = coerce(packageVersion);
1903
1900
  if (!semVer) {
1904
1901
  throw new Error(`${packageVersion} is not valid`);
@@ -1930,6 +1927,6 @@ var SchemaGenerator = class extends Generator {
1930
1927
  // src/index.ts
1931
1928
  var src_default = build;
1932
1929
 
1933
- export { FileManager, Generator, KubbFile, PackageManager, ParallelPluginError, PluginError, PluginManager, PromiseManager, SchemaGenerator, SummaryError, ValidationPluginError, Warning, build, combineExports, combineImports, createPlugin, src_default as default, defineConfig, isInputPath, pluginName as name, pluginName };
1930
+ export { FileManager, Generator, KubbFile, PackageManager, PluginManager, PromiseManager, SchemaGenerator, ValidationPluginError, Warning, build, combineExports, combineImports, createPlugin, src_default as default, defineConfig, isInputPath, pluginName as name, pluginName, safeBuild };
1934
1931
  //# sourceMappingURL=out.js.map
1935
1932
  //# sourceMappingURL=index.js.map