@kubb/core 5.0.0-alpha.6 → 5.0.0-alpha.7

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
@@ -453,6 +453,10 @@ function setUniqueName(originalName, data) {
453
453
  data[originalName] = 1;
454
454
  return originalName;
455
455
  }
456
+ /** Type guard for a rejected `Promise.allSettled` result with a typed `reason`. */
457
+ function isPromiseRejectedResult(result) {
458
+ return result.status === "rejected";
459
+ }
456
460
  /**
457
461
  * JavaScript and Java reserved words.
458
462
  * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
@@ -939,29 +943,13 @@ function hookParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
939
943
  return Promise.allSettled(tasks);
940
944
  }
941
945
  //#endregion
942
- //#region src/PromiseManager.ts
943
- var PromiseManager = class {
944
- #options = {};
945
- constructor(options = {}) {
946
- this.#options = options;
947
- }
948
- run(strategy, promises, { concurrency = Number.POSITIVE_INFINITY } = {}) {
949
- if (strategy === "seq") return hookSeq(promises);
950
- if (strategy === "first") return hookFirst(promises, this.#options.nullCheck);
951
- if (strategy === "parallel") return hookParallel(promises, concurrency);
952
- throw new Error(`${strategy} not implemented`);
953
- }
954
- };
955
- function isPromiseRejectedResult(result) {
956
- return result.status === "rejected";
957
- }
958
- //#endregion
959
- //#region src/PluginManager.ts
946
+ //#region src/PluginDriver.ts
960
947
  function getMode(fileOrFolder) {
961
948
  if (!fileOrFolder) return "split";
962
949
  return (0, node_path.extname)(fileOrFolder) ? "single" : "split";
963
950
  }
964
- var PluginManager = class {
951
+ const hookFirstNullCheck = (state) => !!state?.result;
952
+ var PluginDriver = class {
965
953
  config;
966
954
  options;
967
955
  /**
@@ -973,11 +961,9 @@ var PluginManager = class {
973
961
  #studioIsOpen = false;
974
962
  #plugins = /* @__PURE__ */ new Set();
975
963
  #usedPluginNames = {};
976
- #promiseManager;
977
964
  constructor(config, options) {
978
965
  this.config = config;
979
966
  this.options = options;
980
- this.#promiseManager = new PromiseManager({ nullCheck: (state) => !!state?.result });
981
967
  [...config.plugins || []].forEach((plugin) => {
982
968
  const parsedPlugin = this.#parse(plugin);
983
969
  this.#plugins.add(parsedPlugin);
@@ -988,13 +974,13 @@ var PluginManager = class {
988
974
  }
989
975
  getContext(plugin) {
990
976
  const plugins = [...this.#plugins];
991
- const pluginManager = this;
977
+ const driver = this;
992
978
  const baseContext = {
993
979
  fabric: this.options.fabric,
994
980
  config: this.config,
995
981
  plugin,
996
982
  events: this.options.events,
997
- pluginManager: this,
983
+ driver: this,
998
984
  mode: getMode((0, node_path.resolve)(this.config.root, this.config.output.path)),
999
985
  addFile: async (...files) => {
1000
986
  await this.options.fabric.addFile(...files);
@@ -1003,18 +989,18 @@ var PluginManager = class {
1003
989
  await this.options.fabric.upsertFile(...files);
1004
990
  },
1005
991
  get rootNode() {
1006
- return pluginManager.rootNode;
992
+ return driver.rootNode;
1007
993
  },
1008
994
  get adapter() {
1009
- return pluginManager.adapter;
995
+ return driver.adapter;
1010
996
  },
1011
997
  openInStudio(options) {
1012
- if (!pluginManager.config.devtools || pluginManager.#studioIsOpen) return;
1013
- if (typeof pluginManager.config.devtools !== "object") throw new Error("Devtools must be an object");
1014
- if (!pluginManager.rootNode || !pluginManager.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
1015
- pluginManager.#studioIsOpen = true;
1016
- const studioUrl = pluginManager.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
1017
- return openInStudio(pluginManager.rootNode, studioUrl, options);
998
+ if (!driver.config.devtools || driver.#studioIsOpen) return;
999
+ if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
1000
+ if (!driver.rootNode || !driver.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
1001
+ driver.#studioIsOpen = true;
1002
+ const studioUrl = driver.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
1003
+ return openInStudio(driver.rootNode, studioUrl, options);
1018
1004
  }
1019
1005
  };
1020
1006
  const mergedExtras = {};
@@ -1133,7 +1119,7 @@ var PluginManager = class {
1133
1119
  hookName,
1134
1120
  plugins
1135
1121
  });
1136
- const promises = plugins.map((plugin) => {
1122
+ const result = await hookFirst(plugins.map((plugin) => {
1137
1123
  return async () => {
1138
1124
  const value = await this.#execute({
1139
1125
  strategy: "hookFirst",
@@ -1146,8 +1132,7 @@ var PluginManager = class {
1146
1132
  result: value
1147
1133
  });
1148
1134
  };
1149
- });
1150
- const result = await this.#promiseManager.run("first", promises);
1135
+ }), hookFirstNullCheck);
1151
1136
  this.events.emit("plugins:hook:progress:end", { hookName });
1152
1137
  return result;
1153
1138
  }
@@ -1183,7 +1168,7 @@ var PluginManager = class {
1183
1168
  plugins
1184
1169
  });
1185
1170
  const pluginStartTimes = /* @__PURE__ */ new Map();
1186
- const promises = plugins.map((plugin) => {
1171
+ const results = await hookParallel(plugins.map((plugin) => {
1187
1172
  return () => {
1188
1173
  pluginStartTimes.set(plugin, node_perf_hooks.performance.now());
1189
1174
  return this.#execute({
@@ -1193,8 +1178,7 @@ var PluginManager = class {
1193
1178
  plugin
1194
1179
  });
1195
1180
  };
1196
- });
1197
- const results = await this.#promiseManager.run("parallel", promises, { concurrency: this.options.concurrency });
1181
+ }), this.options.concurrency);
1198
1182
  results.forEach((result, index) => {
1199
1183
  if (isPromiseRejectedResult(result)) {
1200
1184
  const plugin = this.#getSortedPlugins(hookName)[index];
@@ -1225,15 +1209,14 @@ var PluginManager = class {
1225
1209
  hookName,
1226
1210
  plugins
1227
1211
  });
1228
- const promises = plugins.map((plugin) => {
1212
+ await hookSeq(plugins.map((plugin) => {
1229
1213
  return () => this.#execute({
1230
1214
  strategy: "hookSeq",
1231
1215
  hookName,
1232
1216
  parameters,
1233
1217
  plugin
1234
1218
  });
1235
- });
1236
- await this.#promiseManager.run("seq", promises);
1219
+ }));
1237
1220
  this.events.emit("plugins:hook:progress:end", { hookName });
1238
1221
  }
1239
1222
  #getSortedPlugins(hookName) {
@@ -1464,7 +1447,7 @@ const fsStorage = defineStorage(() => ({
1464
1447
  }));
1465
1448
  //#endregion
1466
1449
  //#region package.json
1467
- var version = "5.0.0-alpha.6";
1450
+ var version = "5.0.0-alpha.7";
1468
1451
  //#endregion
1469
1452
  //#region src/utils/diagnostics.ts
1470
1453
  /**
@@ -1578,7 +1561,7 @@ async function setup(options) {
1578
1561
  ` • Barrel type: ${definedConfig.output.barrelType || "none"}`
1579
1562
  ]
1580
1563
  });
1581
- const pluginManager = new PluginManager(definedConfig, {
1564
+ const pluginDriver = new PluginDriver(definedConfig, {
1582
1565
  fabric,
1583
1566
  events,
1584
1567
  concurrency: 15
@@ -1589,26 +1572,26 @@ async function setup(options) {
1589
1572
  date: /* @__PURE__ */ new Date(),
1590
1573
  logs: [`Running adapter: ${definedConfig.adapter.name}`]
1591
1574
  });
1592
- pluginManager.adapter = definedConfig.adapter;
1593
- pluginManager.rootNode = await definedConfig.adapter.parse(source);
1575
+ pluginDriver.adapter = definedConfig.adapter;
1576
+ pluginDriver.rootNode = await definedConfig.adapter.parse(source);
1594
1577
  await events.emit("debug", {
1595
1578
  date: /* @__PURE__ */ new Date(),
1596
1579
  logs: [
1597
1580
  `✓ Adapter '${definedConfig.adapter.name}' resolved RootNode`,
1598
- ` • Schemas: ${pluginManager.rootNode.schemas.length}`,
1599
- ` • Operations: ${pluginManager.rootNode.operations.length}`
1581
+ ` • Schemas: ${pluginDriver.rootNode.schemas.length}`,
1582
+ ` • Operations: ${pluginDriver.rootNode.operations.length}`
1600
1583
  ]
1601
1584
  });
1602
1585
  }
1603
1586
  return {
1604
1587
  events,
1605
1588
  fabric,
1606
- pluginManager,
1589
+ driver: pluginDriver,
1607
1590
  sources
1608
1591
  };
1609
1592
  }
1610
1593
  async function build(options, overrides) {
1611
- const { fabric, files, pluginManager, failedPlugins, pluginTimings, error, sources } = await safeBuild(options, overrides);
1594
+ const { fabric, files, driver, failedPlugins, pluginTimings, error, sources } = await safeBuild(options, overrides);
1612
1595
  if (error) throw error;
1613
1596
  if (failedPlugins.size > 0) {
1614
1597
  const errors = [...failedPlugins].map(({ error }) => error);
@@ -1618,20 +1601,20 @@ async function build(options, overrides) {
1618
1601
  failedPlugins,
1619
1602
  fabric,
1620
1603
  files,
1621
- pluginManager,
1604
+ driver,
1622
1605
  pluginTimings,
1623
1606
  error: void 0,
1624
1607
  sources
1625
1608
  };
1626
1609
  }
1627
1610
  async function safeBuild(options, overrides) {
1628
- const { fabric, pluginManager, events, sources } = overrides ? overrides : await setup(options);
1611
+ const { fabric, driver, events, sources } = overrides ? overrides : await setup(options);
1629
1612
  const failedPlugins = /* @__PURE__ */ new Set();
1630
1613
  const pluginTimings = /* @__PURE__ */ new Map();
1631
- const config = pluginManager.config;
1614
+ const config = driver.config;
1632
1615
  try {
1633
- for (const plugin of pluginManager.plugins) {
1634
- const context = pluginManager.getContext(plugin);
1616
+ for (const plugin of driver.plugins) {
1617
+ const context = driver.getContext(plugin);
1635
1618
  const hrStart = process.hrtime();
1636
1619
  const installer = plugin.install.bind(context);
1637
1620
  try {
@@ -1704,7 +1687,7 @@ async function safeBuild(options, overrides) {
1704
1687
  rootDir,
1705
1688
  existingExports: new Set(existingBarrel?.exports?.flatMap((e) => Array.isArray(e.name) ? e.name : [e.name]).filter((n) => Boolean(n)) ?? []),
1706
1689
  config,
1707
- pluginManager
1690
+ driver
1708
1691
  }),
1709
1692
  sources: [],
1710
1693
  imports: [],
@@ -1722,7 +1705,7 @@ async function safeBuild(options, overrides) {
1722
1705
  failedPlugins,
1723
1706
  fabric,
1724
1707
  files,
1725
- pluginManager,
1708
+ driver,
1726
1709
  pluginTimings,
1727
1710
  sources
1728
1711
  };
@@ -1731,16 +1714,16 @@ async function safeBuild(options, overrides) {
1731
1714
  failedPlugins,
1732
1715
  fabric,
1733
1716
  files: [],
1734
- pluginManager,
1717
+ driver,
1735
1718
  pluginTimings,
1736
1719
  error,
1737
1720
  sources
1738
1721
  };
1739
1722
  }
1740
1723
  }
1741
- function buildBarrelExports({ barrelFiles, rootDir, existingExports, config, pluginManager }) {
1724
+ function buildBarrelExports({ barrelFiles, rootDir, existingExports, config, driver }) {
1742
1725
  const pluginNameMap = /* @__PURE__ */ new Map();
1743
- for (const plugin of pluginManager.plugins) pluginNameMap.set(plugin.name, plugin);
1726
+ for (const plugin of driver.plugins) pluginNameMap.set(plugin.name, plugin);
1744
1727
  return barrelFiles.flatMap((file) => {
1745
1728
  const containsOnlyTypes = file.sources?.every((source) => source.isTypeOnly);
1746
1729
  return (file.sources ?? []).flatMap((source) => {
@@ -2214,50 +2197,46 @@ function buildDirectoryTree(files, rootFolder = "") {
2214
2197
  return root;
2215
2198
  }
2216
2199
  //#endregion
2217
- //#region src/BarrelManager.ts
2200
+ //#region src/utils/getBarrelFiles.ts
2218
2201
  /** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */
2219
- var BarrelManager = class {
2220
- getFiles({ files: generatedFiles, root }) {
2221
- const cachedFiles = /* @__PURE__ */ new Map();
2222
- TreeNode.build(generatedFiles, root)?.forEach((treeNode) => {
2223
- if (!treeNode || !treeNode.children || !treeNode.parent?.data.path) return;
2224
- const barrelFile = {
2225
- path: (0, node_path.join)(treeNode.parent?.data.path, "index.ts"),
2226
- baseName: "index.ts",
2227
- exports: [],
2228
- imports: [],
2229
- sources: []
2230
- };
2231
- const previousBarrelFile = cachedFiles.get(barrelFile.path);
2232
- treeNode.leaves.forEach((item) => {
2233
- if (!item.data.name) return;
2234
- (item.data.file?.sources || []).forEach((source) => {
2235
- if (!item.data.file?.path || !source.isIndexable || !source.name) return;
2236
- if (previousBarrelFile?.sources.some((item) => item.name === source.name && item.isTypeOnly === source.isTypeOnly)) return;
2237
- barrelFile.exports.push({
2238
- name: [source.name],
2239
- path: getRelativePath(treeNode.parent?.data.path, item.data.path),
2240
- isTypeOnly: source.isTypeOnly
2241
- });
2242
- barrelFile.sources.push({
2243
- name: source.name,
2244
- isTypeOnly: source.isTypeOnly,
2245
- value: "",
2246
- isExportable: false,
2247
- isIndexable: false
2248
- });
2202
+ function getBarrelFilesByRoot(root, files) {
2203
+ const cachedFiles = /* @__PURE__ */ new Map();
2204
+ TreeNode.build(files, root)?.forEach((treeNode) => {
2205
+ if (!treeNode || !treeNode.children || !treeNode.parent?.data.path) return;
2206
+ const barrelFile = {
2207
+ path: (0, node_path.join)(treeNode.parent?.data.path, "index.ts"),
2208
+ baseName: "index.ts",
2209
+ exports: [],
2210
+ imports: [],
2211
+ sources: []
2212
+ };
2213
+ const previousBarrelFile = cachedFiles.get(barrelFile.path);
2214
+ treeNode.leaves.forEach((item) => {
2215
+ if (!item.data.name) return;
2216
+ (item.data.file?.sources || []).forEach((source) => {
2217
+ if (!item.data.file?.path || !source.isIndexable || !source.name) return;
2218
+ if (previousBarrelFile?.sources.some((item) => item.name === source.name && item.isTypeOnly === source.isTypeOnly)) return;
2219
+ barrelFile.exports.push({
2220
+ name: [source.name],
2221
+ path: getRelativePath(treeNode.parent?.data.path, item.data.path),
2222
+ isTypeOnly: source.isTypeOnly
2223
+ });
2224
+ barrelFile.sources.push({
2225
+ name: source.name,
2226
+ isTypeOnly: source.isTypeOnly,
2227
+ value: "",
2228
+ isExportable: false,
2229
+ isIndexable: false
2249
2230
  });
2250
2231
  });
2251
- if (previousBarrelFile) {
2252
- previousBarrelFile.sources.push(...barrelFile.sources);
2253
- previousBarrelFile.exports?.push(...barrelFile.exports || []);
2254
- } else cachedFiles.set(barrelFile.path, barrelFile);
2255
2232
  });
2256
- return [...cachedFiles.values()];
2257
- }
2258
- };
2259
- //#endregion
2260
- //#region src/utils/getBarrelFiles.ts
2233
+ if (previousBarrelFile) {
2234
+ previousBarrelFile.sources.push(...barrelFile.sources);
2235
+ previousBarrelFile.exports?.push(...barrelFile.exports || []);
2236
+ } else cachedFiles.set(barrelFile.path, barrelFile);
2237
+ });
2238
+ return [...cachedFiles.values()];
2239
+ }
2261
2240
  function trimExtName(text) {
2262
2241
  const dotIndex = text.lastIndexOf(".");
2263
2242
  if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
@@ -2265,14 +2244,9 @@ function trimExtName(text) {
2265
2244
  }
2266
2245
  async function getBarrelFiles(files, { type, meta = {}, root, output }) {
2267
2246
  if (!type || type === "propagate") return [];
2268
- const barrelManager = new BarrelManager();
2269
2247
  const pathToBuildFrom = (0, node_path.join)(root, output.path);
2270
2248
  if (trimExtName(pathToBuildFrom).endsWith("index")) return [];
2271
- const barrelFiles = barrelManager.getFiles({
2272
- files,
2273
- root: pathToBuildFrom,
2274
- meta
2275
- });
2249
+ const barrelFiles = getBarrelFilesByRoot(pathToBuildFrom, files);
2276
2250
  if (type === "all") return barrelFiles.map((file) => {
2277
2251
  return {
2278
2252
  ...file,
@@ -2398,8 +2372,7 @@ function resolveOptions(node, { options, exclude = [], include, override = [] })
2398
2372
  exports.AsyncEventEmitter = AsyncEventEmitter;
2399
2373
  exports.FunctionParams = FunctionParams;
2400
2374
  exports.PackageManager = PackageManager;
2401
- exports.PluginManager = PluginManager;
2402
- exports.PromiseManager = PromiseManager;
2375
+ exports.PluginDriver = PluginDriver;
2403
2376
  exports.URLPath = URLPath;
2404
2377
  exports.build = build;
2405
2378
  exports.default = build;