@kubb/core 5.0.0-alpha.36 → 5.0.0-alpha.39

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 (42) hide show
  1. package/dist/{PluginDriver-CCdkwR14.cjs → PluginDriver-BQwm8hDd.cjs} +70 -147
  2. package/dist/PluginDriver-BQwm8hDd.cjs.map +1 -0
  3. package/dist/{PluginDriver-B_65W4fv.js → PluginDriver-CgXFtmNP.js} +36 -96
  4. package/dist/PluginDriver-CgXFtmNP.js.map +1 -0
  5. package/dist/index.cjs +23 -341
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +5 -317
  8. package/dist/index.js +23 -311
  9. package/dist/index.js.map +1 -1
  10. package/dist/mocks.cjs +2 -3
  11. package/dist/mocks.cjs.map +1 -1
  12. package/dist/mocks.d.ts +2 -2
  13. package/dist/mocks.js +2 -2
  14. package/dist/mocks.js.map +1 -1
  15. package/dist/{PluginDriver-C9iBgYbk.d.ts → types-DUc5lEUp.d.ts} +596 -714
  16. package/package.json +4 -11
  17. package/src/PluginDriver.ts +18 -17
  18. package/src/constants.ts +0 -48
  19. package/src/createKubb.ts +1 -1
  20. package/src/defineResolver.ts +3 -3
  21. package/src/index.ts +3 -20
  22. package/src/mocks.ts +3 -3
  23. package/src/storages/fsStorage.ts +27 -7
  24. package/src/types.ts +3 -11
  25. package/src/utils/TreeNode.ts +3 -3
  26. package/src/utils/executeStrategies.ts +0 -16
  27. package/dist/PluginDriver-B_65W4fv.js.map +0 -1
  28. package/dist/PluginDriver-CCdkwR14.cjs.map +0 -1
  29. package/dist/chunk-ByKO4r7w.cjs +0 -38
  30. package/dist/hooks.cjs +0 -32
  31. package/dist/hooks.cjs.map +0 -1
  32. package/dist/hooks.d.ts +0 -23
  33. package/dist/hooks.js +0 -29
  34. package/dist/hooks.js.map +0 -1
  35. package/src/hooks/index.ts +0 -3
  36. package/src/hooks/useDriver.ts +0 -9
  37. package/src/hooks/useMode.ts +0 -8
  38. package/src/hooks/usePlugin.ts +0 -9
  39. package/src/utils/FunctionParams.ts +0 -155
  40. package/src/utils/formatters.ts +0 -45
  41. package/src/utils/getFunctionParams.ts +0 -254
  42. package/src/utils/linters.ts +0 -45
package/dist/index.cjs CHANGED
@@ -1,17 +1,12 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_chunk = require("./chunk-ByKO4r7w.cjs");
3
- const require_PluginDriver = require("./PluginDriver-CCdkwR14.cjs");
2
+ const require_PluginDriver = require("./PluginDriver-BQwm8hDd.cjs");
4
3
  let node_events = require("node:events");
5
- let node_fs = require("node:fs");
6
4
  let node_fs_promises = require("node:fs/promises");
7
5
  let node_path = require("node:path");
8
- node_path = require_chunk.__toESM(node_path, 1);
6
+ node_path = require_PluginDriver.__toESM(node_path, 1);
9
7
  let _kubb_ast = require("@kubb/ast");
10
- _kubb_ast = require_chunk.__toESM(_kubb_ast, 1);
11
- let tinyexec = require("tinyexec");
8
+ _kubb_ast = require_PluginDriver.__toESM(_kubb_ast, 1);
12
9
  let node_process = require("node:process");
13
- let remeda = require("remeda");
14
- let semver = require("semver");
15
10
  //#region ../../internals/utils/src/errors.ts
16
11
  /**
17
12
  * Thrown when one or more errors occur during a Kubb build.
@@ -187,26 +182,6 @@ function formatMs(ms) {
187
182
  //#endregion
188
183
  //#region ../../internals/utils/src/fs.ts
189
184
  /**
190
- * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and
191
- * returns the absolute path of the nearest `package.json`, or `null` when none
192
- * is found before reaching the filesystem root.
193
- *
194
- * @example
195
- * ```ts
196
- * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'
197
- * ```
198
- */
199
- function findPackageJSON(cwd) {
200
- let dir = cwd ? (0, node_path.resolve)(cwd) : process.cwd();
201
- while (true) {
202
- const pkgPath = (0, node_path.join)(dir, "package.json");
203
- if ((0, node_fs.existsSync)(pkgPath)) return pkgPath;
204
- const parent = (0, node_path.dirname)(dir);
205
- if (parent === dir) return null;
206
- dir = parent;
207
- }
208
- }
209
- /**
210
185
  * Converts all backslashes to forward slashes.
211
186
  * Extended-length Windows paths (`\\?\...`) are left unchanged.
212
187
  */
@@ -245,17 +220,6 @@ async function exists(path) {
245
220
  return (0, node_fs_promises.access)(path).then(() => true, () => false);
246
221
  }
247
222
  /**
248
- * Synchronous counterpart of `read`.
249
- *
250
- * @example
251
- * ```ts
252
- * const source = readSync('./src/Pet.ts')
253
- * ```
254
- */
255
- function readSync(path) {
256
- return (0, node_fs.readFileSync)(path, { encoding: "utf8" });
257
- }
258
- /**
259
223
  * Writes `data` to `path`, trimming leading/trailing whitespace before saving.
260
224
  * Skips the write when the trimmed content is empty or identical to what is already on disk.
261
225
  * Creates any missing parent directories automatically.
@@ -541,6 +505,12 @@ function createStorage(build) {
541
505
  //#endregion
542
506
  //#region src/storages/fsStorage.ts
543
507
  /**
508
+ * Detects the filesystem error used to indicate that a path does not exist.
509
+ */
510
+ function isMissingPathError(error) {
511
+ return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
512
+ }
513
+ /**
544
514
  * Built-in filesystem storage driver.
545
515
  *
546
516
  * This is the default storage when no `storage` option is configured in `output`.
@@ -569,15 +539,17 @@ const fsStorage = createStorage(() => ({
569
539
  try {
570
540
  await (0, node_fs_promises.access)((0, node_path.resolve)(key));
571
541
  return true;
572
- } catch {
573
- return false;
542
+ } catch (error) {
543
+ if (isMissingPathError(error)) return false;
544
+ throw new Error(`Failed to access storage item "${key}"`, { cause: error });
574
545
  }
575
546
  },
576
547
  async getItem(key) {
577
548
  try {
578
549
  return await (0, node_fs_promises.readFile)((0, node_path.resolve)(key), "utf8");
579
- } catch {
580
- return null;
550
+ } catch (error) {
551
+ if (isMissingPathError(error)) return null;
552
+ throw new Error(`Failed to read storage item "${key}"`, { cause: error });
581
553
  }
582
554
  },
583
555
  async setItem(key, value) {
@@ -588,12 +560,14 @@ const fsStorage = createStorage(() => ({
588
560
  },
589
561
  async getKeys(base) {
590
562
  const keys = [];
563
+ const resolvedBase = (0, node_path.resolve)(base ?? process.cwd());
591
564
  async function walk(dir, prefix) {
592
565
  let entries;
593
566
  try {
594
567
  entries = await (0, node_fs_promises.readdir)(dir, { withFileTypes: true });
595
- } catch {
596
- return;
568
+ } catch (error) {
569
+ if (isMissingPathError(error)) return;
570
+ throw new Error(`Failed to list storage keys under "${resolvedBase}"`, { cause: error });
597
571
  }
598
572
  for (const entry of entries) {
599
573
  const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
@@ -601,7 +575,7 @@ const fsStorage = createStorage(() => ({
601
575
  else keys.push(rel);
602
576
  }
603
577
  }
604
- await walk((0, node_path.resolve)(base ?? process.cwd()), "");
578
+ await walk(resolvedBase, "");
605
579
  return keys;
606
580
  },
607
581
  async clear(base) {
@@ -611,7 +585,7 @@ const fsStorage = createStorage(() => ({
611
585
  }));
612
586
  //#endregion
613
587
  //#region package.json
614
- var version = "5.0.0-alpha.36";
588
+ var version = "5.0.0-alpha.39";
615
589
  //#endregion
616
590
  //#region src/utils/diagnostics.ts
617
591
  /**
@@ -726,14 +700,14 @@ var TreeNode = class TreeNode {
726
700
  name: filteredTree.name,
727
701
  path: filteredTree.path,
728
702
  file: filteredTree.file,
729
- type: require_PluginDriver.getMode(filteredTree.path)
703
+ type: require_PluginDriver.PluginDriver.getMode(filteredTree.path)
730
704
  });
731
705
  const recurse = (node, item) => {
732
706
  const subNode = node.addChild({
733
707
  name: item.name,
734
708
  path: item.path,
735
709
  file: item.file,
736
- type: require_PluginDriver.getMode(item.path)
710
+ type: require_PluginDriver.PluginDriver.getMode(item.path)
737
711
  });
738
712
  if (item.children?.length) item.children?.forEach((child) => {
739
713
  recurse(subNode, child);
@@ -1318,30 +1292,6 @@ function createKubb(options) {
1318
1292
  return instance;
1319
1293
  }
1320
1294
  //#endregion
1321
- //#region src/createPlugin.ts
1322
- /**
1323
- * Creates a plugin factory. Call the returned function with optional options to get the plugin instance.
1324
- *
1325
- * @example
1326
- * ```ts
1327
- * export const myPlugin = createPlugin<MyPlugin>((options) => {
1328
- * return {
1329
- * name: 'my-plugin',
1330
- * get options() { return options },
1331
- * resolvePath(baseName) { ... },
1332
- * resolveName(name, type) { ... },
1333
- * }
1334
- * })
1335
- *
1336
- * // instantiate
1337
- * const plugin = myPlugin({ output: { path: 'src/gen' } })
1338
- * ```
1339
- * @deprecated use definePlugin instead
1340
- */
1341
- function createPlugin(build) {
1342
- return (options) => build(options ?? {});
1343
- }
1344
- //#endregion
1345
1295
  //#region src/createRenderer.ts
1346
1296
  /**
1347
1297
  * Creates a renderer factory for use in generator definitions.
@@ -1475,249 +1425,9 @@ const memoryStorage = createStorage(() => {
1475
1425
  };
1476
1426
  });
1477
1427
  //#endregion
1478
- //#region src/utils/formatters.ts
1479
- /**
1480
- * Returns `true` when the given formatter is installed and callable.
1481
- *
1482
- * Availability is detected by running `<formatter> --version` and checking
1483
- * that the process exits without error.
1484
- */
1485
- async function isFormatterAvailable(formatter) {
1486
- try {
1487
- await (0, tinyexec.x)(formatter, ["--version"], { nodeOptions: { stdio: "ignore" } });
1488
- return true;
1489
- } catch {
1490
- return false;
1491
- }
1492
- }
1493
- /**
1494
- * Detects the first available code formatter on the current system.
1495
- *
1496
- * - Checks in preference order: `biome`, `oxfmt`, `prettier`.
1497
- * - Returns `null` when none are found.
1498
- *
1499
- * @example
1500
- * ```ts
1501
- * const formatter = await detectFormatter()
1502
- * if (formatter) {
1503
- * console.log(`Using ${formatter} for formatting`)
1504
- * }
1505
- * ```
1506
- */
1507
- async function detectFormatter() {
1508
- const formatterNames = new Set([
1509
- "biome",
1510
- "oxfmt",
1511
- "prettier"
1512
- ]);
1513
- for (const formatter of formatterNames) if (await isFormatterAvailable(formatter)) return formatter;
1514
- return null;
1515
- }
1516
- //#endregion
1517
- //#region src/utils/getFunctionParams.ts
1518
- function order(items) {
1519
- return (0, remeda.sortBy)(items.filter(Boolean), ([_key, item]) => {
1520
- if (item?.children) return 0;
1521
- if (item?.optional) return 1;
1522
- if (item?.default) return 2;
1523
- return 0;
1524
- });
1525
- }
1526
- function parseChild(key, item, options) {
1527
- const entries = order(Object.entries(item.children));
1528
- const types = [];
1529
- const names = [];
1530
- const optional = entries.every(([_key, item]) => item?.optional || !!item?.default);
1531
- entries.forEach(([key, entryItem]) => {
1532
- if (entryItem) {
1533
- const name = parseItem(key, {
1534
- ...entryItem,
1535
- type: void 0
1536
- }, options);
1537
- if (entryItem.children) {
1538
- const subTypes = Object.entries(entryItem.children).map(([key]) => {
1539
- return key;
1540
- }).join(", ");
1541
- if (subTypes) names.push(`${name}: { ${subTypes} }`);
1542
- else names.push(name);
1543
- } else if (options.type === "call" && options.transformName) names.push(`${key}: ${name}`);
1544
- else names.push(name);
1545
- if (entries.some(([_key, item]) => item?.type)) types.push(parseItem(key, {
1546
- ...entryItem,
1547
- default: void 0
1548
- }, options));
1549
- }
1550
- });
1551
- const name = item.mode === "inline" ? key : names.length ? `{ ${names.join(", ")} }` : void 0;
1552
- const type = item.type ? item.type : types.length ? `{ ${types.join("; ")} }` : void 0;
1553
- if (!name) return null;
1554
- return parseItem(name, {
1555
- type,
1556
- default: item.default,
1557
- optional: !item.default ? optional : void 0
1558
- }, options);
1559
- }
1560
- function parseItem(name, item, options) {
1561
- const acc = [];
1562
- const transformedName = options.transformName ? options.transformName(name) : name;
1563
- const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type;
1564
- if (options.type === "object") return transformedName;
1565
- if (options.type === "objectValue") return item.value ? `${transformedName}: ${item.value}` : transformedName;
1566
- if (item.type && options.type === "constructor") if (item.optional) if (transformedName.startsWith("{")) acc.push(`${transformedName}: ${transformedType} = {}`);
1567
- else acc.push(`${transformedName}?: ${transformedType}`);
1568
- else acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ""}`);
1569
- else if (item.default && options.type === "constructor") acc.push(`${transformedName} = ${item.default}`);
1570
- else if (item.value) acc.push(`${transformedName} : ${item.value}`);
1571
- else if (item.mode === "inlineSpread") acc.push(`... ${transformedName}`);
1572
- else acc.push(transformedName);
1573
- return acc[0];
1574
- }
1575
- function getFunctionParams(params, options) {
1576
- return order(Object.entries(params)).reduce((acc, [key, item]) => {
1577
- if (!item) return acc;
1578
- if (item.children) {
1579
- if (Object.keys(item.children).length === 0) return acc;
1580
- if (item.mode === "inlineSpread") return [...acc, getFunctionParams(item.children, options)];
1581
- const parsedItem = parseChild(key, item, options);
1582
- if (!parsedItem) return acc;
1583
- return [...acc, parsedItem];
1584
- }
1585
- const parsedItem = parseItem(key, item, options);
1586
- return [...acc, parsedItem];
1587
- }, []).join(", ");
1588
- }
1589
- /**
1590
- * @deprecated use @kubb/ast
1591
- */
1592
- function createFunctionParams(params) {
1593
- return params;
1594
- }
1595
- /**
1596
- * @deprecated use @kubb/ast
1597
- */
1598
- var FunctionParams = class FunctionParams {
1599
- #params;
1600
- static factory(params) {
1601
- return new FunctionParams(params);
1602
- }
1603
- constructor(params) {
1604
- this.#params = params;
1605
- }
1606
- get params() {
1607
- return this.#params;
1608
- }
1609
- get flatParams() {
1610
- const flatter = (acc, [key, item]) => {
1611
- if (item?.children) return Object.entries(item.children).reduce(flatter, acc);
1612
- if (item) acc[key] = item;
1613
- return acc;
1614
- };
1615
- return Object.entries(this.#params).reduce(flatter, {});
1616
- }
1617
- toCall({ transformName, transformType } = {}) {
1618
- return getFunctionParams(this.#params, {
1619
- type: "call",
1620
- transformName,
1621
- transformType
1622
- });
1623
- }
1624
- toObject() {
1625
- return getFunctionParams(this.#params, { type: "object" });
1626
- }
1627
- toObjectValue() {
1628
- return getFunctionParams(this.#params, { type: "objectValue" });
1629
- }
1630
- toConstructor() {
1631
- return getFunctionParams(this.#params, { type: "constructor" });
1632
- }
1633
- };
1634
- //#endregion
1635
- //#region src/utils/linters.ts
1636
- /**
1637
- * Returns `true` when the given linter is installed and callable.
1638
- *
1639
- * Availability is detected by running `<linter> --version` and checking
1640
- * that the process exits without error.
1641
- */
1642
- async function isLinterAvailable(linter) {
1643
- try {
1644
- await (0, tinyexec.x)(linter, ["--version"], { nodeOptions: { stdio: "ignore" } });
1645
- return true;
1646
- } catch {
1647
- return false;
1648
- }
1649
- }
1650
- /**
1651
- * Detects the first available linter on the current system.
1652
- *
1653
- * - Checks in preference order: `biome`, `oxlint`, `eslint`.
1654
- * - Returns `null` when none are found.
1655
- *
1656
- * @example
1657
- * ```ts
1658
- * const linter = await detectLinter()
1659
- * if (linter) {
1660
- * console.log(`Using ${linter} for linting`)
1661
- * }
1662
- * ```
1663
- */
1664
- async function detectLinter() {
1665
- const linterNames = new Set([
1666
- "biome",
1667
- "oxlint",
1668
- "eslint"
1669
- ]);
1670
- for (const linter of linterNames) if (await isLinterAvailable(linter)) return linter;
1671
- return null;
1672
- }
1673
- //#endregion
1674
- //#region src/utils/packageJSON.ts
1675
- function getPackageJSONSync(cwd) {
1676
- const pkgPath = findPackageJSON(cwd);
1677
- if (!pkgPath) return null;
1678
- return JSON.parse(readSync(pkgPath));
1679
- }
1680
- function match(packageJSON, dependency) {
1681
- const dependencies = {
1682
- ...packageJSON.dependencies || {},
1683
- ...packageJSON.devDependencies || {}
1684
- };
1685
- if (typeof dependency === "string" && dependencies[dependency]) return dependencies[dependency];
1686
- const matched = Object.keys(dependencies).find((dep) => dep.match(dependency));
1687
- return matched ? dependencies[matched] ?? null : null;
1688
- }
1689
- function getVersionSync(dependency, cwd) {
1690
- const packageJSON = getPackageJSONSync(cwd);
1691
- return packageJSON ? match(packageJSON, dependency) : null;
1692
- }
1693
- /**
1694
- * Returns `true` when the nearest `package.json` declares a dependency that
1695
- * satisfies the given semver range.
1696
- *
1697
- * - Searches both `dependencies` and `devDependencies`.
1698
- * - Accepts a string package name or a `RegExp` to match scoped/pattern packages.
1699
- * - Uses `semver.satisfies` for range comparison; returns `false` when the
1700
- * version string cannot be coerced into a valid semver.
1701
- *
1702
- * @example
1703
- * ```ts
1704
- * satisfiesDependency('react', '>=18') // true when react@18.x is installed
1705
- * satisfiesDependency(/^@tanstack\//, '>=5') // true when any @tanstack/* >=5 is found
1706
- * ```
1707
- */
1708
- function satisfiesDependency(dependency, version, cwd) {
1709
- const packageVersion = getVersionSync(dependency, cwd);
1710
- if (!packageVersion) return false;
1711
- if (packageVersion === version) return true;
1712
- const semVer = (0, semver.coerce)(packageVersion);
1713
- if (!semVer) return false;
1714
- return (0, semver.satisfies)(semVer, version);
1715
- }
1716
- //#endregion
1717
1428
  exports.AsyncEventEmitter = AsyncEventEmitter;
1718
1429
  exports.FileManager = require_PluginDriver.FileManager;
1719
1430
  exports.FileProcessor = FileProcessor;
1720
- exports.FunctionParams = FunctionParams;
1721
1431
  exports.PluginDriver = require_PluginDriver.PluginDriver;
1722
1432
  exports.URLPath = URLPath;
1723
1433
  Object.defineProperty(exports, "ast", {
@@ -1726,46 +1436,18 @@ Object.defineProperty(exports, "ast", {
1726
1436
  return _kubb_ast;
1727
1437
  }
1728
1438
  });
1729
- exports.buildDefaultBanner = require_PluginDriver.buildDefaultBanner;
1730
- Object.defineProperty(exports, "composeTransformers", {
1731
- enumerable: true,
1732
- get: function() {
1733
- return _kubb_ast.composeTransformers;
1734
- }
1735
- });
1736
1439
  exports.createAdapter = createAdapter;
1737
- exports.createFunctionParams = createFunctionParams;
1738
1440
  exports.createKubb = createKubb;
1739
- exports.createPlugin = createPlugin;
1740
1441
  exports.createRenderer = createRenderer;
1741
1442
  exports.createStorage = createStorage;
1742
- exports.defaultResolveBanner = require_PluginDriver.defaultResolveBanner;
1743
- exports.defaultResolveFile = require_PluginDriver.defaultResolveFile;
1744
- exports.defaultResolveFooter = require_PluginDriver.defaultResolveFooter;
1745
- exports.defaultResolveOptions = require_PluginDriver.defaultResolveOptions;
1746
- exports.defaultResolvePath = require_PluginDriver.defaultResolvePath;
1747
1443
  exports.defineGenerator = defineGenerator;
1748
1444
  exports.defineLogger = defineLogger;
1749
1445
  exports.defineParser = defineParser;
1750
1446
  exports.definePlugin = require_PluginDriver.definePlugin;
1751
- Object.defineProperty(exports, "definePrinter", {
1752
- enumerable: true,
1753
- get: function() {
1754
- return _kubb_ast.definePrinter;
1755
- }
1756
- });
1757
1447
  exports.defineResolver = require_PluginDriver.defineResolver;
1758
- exports.detectFormatter = detectFormatter;
1759
- exports.detectLinter = detectLinter;
1760
- exports.formatters = require_PluginDriver.formatters;
1761
1448
  exports.fsStorage = fsStorage;
1762
- exports.getBarrelFiles = getBarrelFiles;
1763
- exports.getFunctionParams = getFunctionParams;
1764
- exports.getMode = require_PluginDriver.getMode;
1765
1449
  exports.isInputPath = isInputPath;
1766
- exports.linters = require_PluginDriver.linters;
1767
1450
  exports.logLevel = require_PluginDriver.logLevel;
1768
1451
  exports.memoryStorage = memoryStorage;
1769
- exports.satisfiesDependency = satisfiesDependency;
1770
1452
 
1771
1453
  //# sourceMappingURL=index.cjs.map