@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.js CHANGED
@@ -1,15 +1,11 @@
1
1
  import "./chunk--u3MIqq1.js";
2
- import { S as camelCase, _ as DEFAULT_STUDIO_URL, a as buildDefaultBanner, b as logLevel, c as defaultResolveFooter, d as defineResolver, f as definePlugin, g as DEFAULT_EXTENSION, h as DEFAULT_BANNER, i as FileManager, l as defaultResolveOptions, m as BARREL_FILENAME, n as getMode, o as defaultResolveBanner, p as pLimit, r as applyHookResult, s as defaultResolveFile, t as PluginDriver, u as defaultResolvePath, v as formatters, x as isValidVarName, y as linters } from "./PluginDriver-B_65W4fv.js";
2
+ import { a as definePlugin, c as DEFAULT_BANNER, d as logLevel, f as isValidVarName, i as defineResolver, l as DEFAULT_EXTENSION, n as applyHookResult, o as pLimit, p as camelCase, r as FileManager, s as BARREL_FILENAME, t as PluginDriver, u as DEFAULT_STUDIO_URL } from "./PluginDriver-CgXFtmNP.js";
3
3
  import { EventEmitter } from "node:events";
4
- import { existsSync, readFileSync } from "node:fs";
5
4
  import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
6
5
  import path, { dirname, join, posix, resolve } from "node:path";
7
6
  import * as ast from "@kubb/ast";
8
- import { composeTransformers, createExport, createFile, createSource, definePrinter, extractStringsFromNodes, transform, walk } from "@kubb/ast";
9
- import { x } from "tinyexec";
7
+ import { createExport, createFile, createSource, extractStringsFromNodes, transform, walk } from "@kubb/ast";
10
8
  import { version } from "node:process";
11
- import { sortBy } from "remeda";
12
- import { coerce, satisfies } from "semver";
13
9
  //#region ../../internals/utils/src/errors.ts
14
10
  /**
15
11
  * Thrown when one or more errors occur during a Kubb build.
@@ -185,26 +181,6 @@ function formatMs(ms) {
185
181
  //#endregion
186
182
  //#region ../../internals/utils/src/fs.ts
187
183
  /**
188
- * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and
189
- * returns the absolute path of the nearest `package.json`, or `null` when none
190
- * is found before reaching the filesystem root.
191
- *
192
- * @example
193
- * ```ts
194
- * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'
195
- * ```
196
- */
197
- function findPackageJSON(cwd) {
198
- let dir = cwd ? resolve(cwd) : process.cwd();
199
- while (true) {
200
- const pkgPath = join(dir, "package.json");
201
- if (existsSync(pkgPath)) return pkgPath;
202
- const parent = dirname(dir);
203
- if (parent === dir) return null;
204
- dir = parent;
205
- }
206
- }
207
- /**
208
184
  * Converts all backslashes to forward slashes.
209
185
  * Extended-length Windows paths (`\\?\...`) are left unchanged.
210
186
  */
@@ -243,17 +219,6 @@ async function exists(path) {
243
219
  return access(path).then(() => true, () => false);
244
220
  }
245
221
  /**
246
- * Synchronous counterpart of `read`.
247
- *
248
- * @example
249
- * ```ts
250
- * const source = readSync('./src/Pet.ts')
251
- * ```
252
- */
253
- function readSync(path) {
254
- return readFileSync(path, { encoding: "utf8" });
255
- }
256
- /**
257
222
  * Writes `data` to `path`, trimming leading/trailing whitespace before saving.
258
223
  * Skips the write when the trimmed content is empty or identical to what is already on disk.
259
224
  * Creates any missing parent directories automatically.
@@ -539,6 +504,12 @@ function createStorage(build) {
539
504
  //#endregion
540
505
  //#region src/storages/fsStorage.ts
541
506
  /**
507
+ * Detects the filesystem error used to indicate that a path does not exist.
508
+ */
509
+ function isMissingPathError(error) {
510
+ return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
511
+ }
512
+ /**
542
513
  * Built-in filesystem storage driver.
543
514
  *
544
515
  * This is the default storage when no `storage` option is configured in `output`.
@@ -567,15 +538,17 @@ const fsStorage = createStorage(() => ({
567
538
  try {
568
539
  await access(resolve(key));
569
540
  return true;
570
- } catch {
571
- return false;
541
+ } catch (error) {
542
+ if (isMissingPathError(error)) return false;
543
+ throw new Error(`Failed to access storage item "${key}"`, { cause: error });
572
544
  }
573
545
  },
574
546
  async getItem(key) {
575
547
  try {
576
548
  return await readFile(resolve(key), "utf8");
577
- } catch {
578
- return null;
549
+ } catch (error) {
550
+ if (isMissingPathError(error)) return null;
551
+ throw new Error(`Failed to read storage item "${key}"`, { cause: error });
579
552
  }
580
553
  },
581
554
  async setItem(key, value) {
@@ -586,12 +559,14 @@ const fsStorage = createStorage(() => ({
586
559
  },
587
560
  async getKeys(base) {
588
561
  const keys = [];
562
+ const resolvedBase = resolve(base ?? process.cwd());
589
563
  async function walk(dir, prefix) {
590
564
  let entries;
591
565
  try {
592
566
  entries = await readdir(dir, { withFileTypes: true });
593
- } catch {
594
- return;
567
+ } catch (error) {
568
+ if (isMissingPathError(error)) return;
569
+ throw new Error(`Failed to list storage keys under "${resolvedBase}"`, { cause: error });
595
570
  }
596
571
  for (const entry of entries) {
597
572
  const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
@@ -599,7 +574,7 @@ const fsStorage = createStorage(() => ({
599
574
  else keys.push(rel);
600
575
  }
601
576
  }
602
- await walk(resolve(base ?? process.cwd()), "");
577
+ await walk(resolvedBase, "");
603
578
  return keys;
604
579
  },
605
580
  async clear(base) {
@@ -609,7 +584,7 @@ const fsStorage = createStorage(() => ({
609
584
  }));
610
585
  //#endregion
611
586
  //#region package.json
612
- var version$1 = "5.0.0-alpha.36";
587
+ var version$1 = "5.0.0-alpha.39";
613
588
  //#endregion
614
589
  //#region src/utils/diagnostics.ts
615
590
  /**
@@ -724,14 +699,14 @@ var TreeNode = class TreeNode {
724
699
  name: filteredTree.name,
725
700
  path: filteredTree.path,
726
701
  file: filteredTree.file,
727
- type: getMode(filteredTree.path)
702
+ type: PluginDriver.getMode(filteredTree.path)
728
703
  });
729
704
  const recurse = (node, item) => {
730
705
  const subNode = node.addChild({
731
706
  name: item.name,
732
707
  path: item.path,
733
708
  file: item.file,
734
- type: getMode(item.path)
709
+ type: PluginDriver.getMode(item.path)
735
710
  });
736
711
  if (item.children?.length) item.children?.forEach((child) => {
737
712
  recurse(subNode, child);
@@ -1316,30 +1291,6 @@ function createKubb(options) {
1316
1291
  return instance;
1317
1292
  }
1318
1293
  //#endregion
1319
- //#region src/createPlugin.ts
1320
- /**
1321
- * Creates a plugin factory. Call the returned function with optional options to get the plugin instance.
1322
- *
1323
- * @example
1324
- * ```ts
1325
- * export const myPlugin = createPlugin<MyPlugin>((options) => {
1326
- * return {
1327
- * name: 'my-plugin',
1328
- * get options() { return options },
1329
- * resolvePath(baseName) { ... },
1330
- * resolveName(name, type) { ... },
1331
- * }
1332
- * })
1333
- *
1334
- * // instantiate
1335
- * const plugin = myPlugin({ output: { path: 'src/gen' } })
1336
- * ```
1337
- * @deprecated use definePlugin instead
1338
- */
1339
- function createPlugin(build) {
1340
- return (options) => build(options ?? {});
1341
- }
1342
- //#endregion
1343
1294
  //#region src/createRenderer.ts
1344
1295
  /**
1345
1296
  * Creates a renderer factory for use in generator definitions.
@@ -1473,245 +1424,6 @@ const memoryStorage = createStorage(() => {
1473
1424
  };
1474
1425
  });
1475
1426
  //#endregion
1476
- //#region src/utils/formatters.ts
1477
- /**
1478
- * Returns `true` when the given formatter is installed and callable.
1479
- *
1480
- * Availability is detected by running `<formatter> --version` and checking
1481
- * that the process exits without error.
1482
- */
1483
- async function isFormatterAvailable(formatter) {
1484
- try {
1485
- await x(formatter, ["--version"], { nodeOptions: { stdio: "ignore" } });
1486
- return true;
1487
- } catch {
1488
- return false;
1489
- }
1490
- }
1491
- /**
1492
- * Detects the first available code formatter on the current system.
1493
- *
1494
- * - Checks in preference order: `biome`, `oxfmt`, `prettier`.
1495
- * - Returns `null` when none are found.
1496
- *
1497
- * @example
1498
- * ```ts
1499
- * const formatter = await detectFormatter()
1500
- * if (formatter) {
1501
- * console.log(`Using ${formatter} for formatting`)
1502
- * }
1503
- * ```
1504
- */
1505
- async function detectFormatter() {
1506
- const formatterNames = new Set([
1507
- "biome",
1508
- "oxfmt",
1509
- "prettier"
1510
- ]);
1511
- for (const formatter of formatterNames) if (await isFormatterAvailable(formatter)) return formatter;
1512
- return null;
1513
- }
1514
- //#endregion
1515
- //#region src/utils/getFunctionParams.ts
1516
- function order(items) {
1517
- return sortBy(items.filter(Boolean), ([_key, item]) => {
1518
- if (item?.children) return 0;
1519
- if (item?.optional) return 1;
1520
- if (item?.default) return 2;
1521
- return 0;
1522
- });
1523
- }
1524
- function parseChild(key, item, options) {
1525
- const entries = order(Object.entries(item.children));
1526
- const types = [];
1527
- const names = [];
1528
- const optional = entries.every(([_key, item]) => item?.optional || !!item?.default);
1529
- entries.forEach(([key, entryItem]) => {
1530
- if (entryItem) {
1531
- const name = parseItem(key, {
1532
- ...entryItem,
1533
- type: void 0
1534
- }, options);
1535
- if (entryItem.children) {
1536
- const subTypes = Object.entries(entryItem.children).map(([key]) => {
1537
- return key;
1538
- }).join(", ");
1539
- if (subTypes) names.push(`${name}: { ${subTypes} }`);
1540
- else names.push(name);
1541
- } else if (options.type === "call" && options.transformName) names.push(`${key}: ${name}`);
1542
- else names.push(name);
1543
- if (entries.some(([_key, item]) => item?.type)) types.push(parseItem(key, {
1544
- ...entryItem,
1545
- default: void 0
1546
- }, options));
1547
- }
1548
- });
1549
- const name = item.mode === "inline" ? key : names.length ? `{ ${names.join(", ")} }` : void 0;
1550
- const type = item.type ? item.type : types.length ? `{ ${types.join("; ")} }` : void 0;
1551
- if (!name) return null;
1552
- return parseItem(name, {
1553
- type,
1554
- default: item.default,
1555
- optional: !item.default ? optional : void 0
1556
- }, options);
1557
- }
1558
- function parseItem(name, item, options) {
1559
- const acc = [];
1560
- const transformedName = options.transformName ? options.transformName(name) : name;
1561
- const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type;
1562
- if (options.type === "object") return transformedName;
1563
- if (options.type === "objectValue") return item.value ? `${transformedName}: ${item.value}` : transformedName;
1564
- if (item.type && options.type === "constructor") if (item.optional) if (transformedName.startsWith("{")) acc.push(`${transformedName}: ${transformedType} = {}`);
1565
- else acc.push(`${transformedName}?: ${transformedType}`);
1566
- else acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ""}`);
1567
- else if (item.default && options.type === "constructor") acc.push(`${transformedName} = ${item.default}`);
1568
- else if (item.value) acc.push(`${transformedName} : ${item.value}`);
1569
- else if (item.mode === "inlineSpread") acc.push(`... ${transformedName}`);
1570
- else acc.push(transformedName);
1571
- return acc[0];
1572
- }
1573
- function getFunctionParams(params, options) {
1574
- return order(Object.entries(params)).reduce((acc, [key, item]) => {
1575
- if (!item) return acc;
1576
- if (item.children) {
1577
- if (Object.keys(item.children).length === 0) return acc;
1578
- if (item.mode === "inlineSpread") return [...acc, getFunctionParams(item.children, options)];
1579
- const parsedItem = parseChild(key, item, options);
1580
- if (!parsedItem) return acc;
1581
- return [...acc, parsedItem];
1582
- }
1583
- const parsedItem = parseItem(key, item, options);
1584
- return [...acc, parsedItem];
1585
- }, []).join(", ");
1586
- }
1587
- /**
1588
- * @deprecated use @kubb/ast
1589
- */
1590
- function createFunctionParams(params) {
1591
- return params;
1592
- }
1593
- /**
1594
- * @deprecated use @kubb/ast
1595
- */
1596
- var FunctionParams = class FunctionParams {
1597
- #params;
1598
- static factory(params) {
1599
- return new FunctionParams(params);
1600
- }
1601
- constructor(params) {
1602
- this.#params = params;
1603
- }
1604
- get params() {
1605
- return this.#params;
1606
- }
1607
- get flatParams() {
1608
- const flatter = (acc, [key, item]) => {
1609
- if (item?.children) return Object.entries(item.children).reduce(flatter, acc);
1610
- if (item) acc[key] = item;
1611
- return acc;
1612
- };
1613
- return Object.entries(this.#params).reduce(flatter, {});
1614
- }
1615
- toCall({ transformName, transformType } = {}) {
1616
- return getFunctionParams(this.#params, {
1617
- type: "call",
1618
- transformName,
1619
- transformType
1620
- });
1621
- }
1622
- toObject() {
1623
- return getFunctionParams(this.#params, { type: "object" });
1624
- }
1625
- toObjectValue() {
1626
- return getFunctionParams(this.#params, { type: "objectValue" });
1627
- }
1628
- toConstructor() {
1629
- return getFunctionParams(this.#params, { type: "constructor" });
1630
- }
1631
- };
1632
- //#endregion
1633
- //#region src/utils/linters.ts
1634
- /**
1635
- * Returns `true` when the given linter is installed and callable.
1636
- *
1637
- * Availability is detected by running `<linter> --version` and checking
1638
- * that the process exits without error.
1639
- */
1640
- async function isLinterAvailable(linter) {
1641
- try {
1642
- await x(linter, ["--version"], { nodeOptions: { stdio: "ignore" } });
1643
- return true;
1644
- } catch {
1645
- return false;
1646
- }
1647
- }
1648
- /**
1649
- * Detects the first available linter on the current system.
1650
- *
1651
- * - Checks in preference order: `biome`, `oxlint`, `eslint`.
1652
- * - Returns `null` when none are found.
1653
- *
1654
- * @example
1655
- * ```ts
1656
- * const linter = await detectLinter()
1657
- * if (linter) {
1658
- * console.log(`Using ${linter} for linting`)
1659
- * }
1660
- * ```
1661
- */
1662
- async function detectLinter() {
1663
- const linterNames = new Set([
1664
- "biome",
1665
- "oxlint",
1666
- "eslint"
1667
- ]);
1668
- for (const linter of linterNames) if (await isLinterAvailable(linter)) return linter;
1669
- return null;
1670
- }
1671
- //#endregion
1672
- //#region src/utils/packageJSON.ts
1673
- function getPackageJSONSync(cwd) {
1674
- const pkgPath = findPackageJSON(cwd);
1675
- if (!pkgPath) return null;
1676
- return JSON.parse(readSync(pkgPath));
1677
- }
1678
- function match(packageJSON, dependency) {
1679
- const dependencies = {
1680
- ...packageJSON.dependencies || {},
1681
- ...packageJSON.devDependencies || {}
1682
- };
1683
- if (typeof dependency === "string" && dependencies[dependency]) return dependencies[dependency];
1684
- const matched = Object.keys(dependencies).find((dep) => dep.match(dependency));
1685
- return matched ? dependencies[matched] ?? null : null;
1686
- }
1687
- function getVersionSync(dependency, cwd) {
1688
- const packageJSON = getPackageJSONSync(cwd);
1689
- return packageJSON ? match(packageJSON, dependency) : null;
1690
- }
1691
- /**
1692
- * Returns `true` when the nearest `package.json` declares a dependency that
1693
- * satisfies the given semver range.
1694
- *
1695
- * - Searches both `dependencies` and `devDependencies`.
1696
- * - Accepts a string package name or a `RegExp` to match scoped/pattern packages.
1697
- * - Uses `semver.satisfies` for range comparison; returns `false` when the
1698
- * version string cannot be coerced into a valid semver.
1699
- *
1700
- * @example
1701
- * ```ts
1702
- * satisfiesDependency('react', '>=18') // true when react@18.x is installed
1703
- * satisfiesDependency(/^@tanstack\//, '>=5') // true when any @tanstack/* >=5 is found
1704
- * ```
1705
- */
1706
- function satisfiesDependency(dependency, version, cwd) {
1707
- const packageVersion = getVersionSync(dependency, cwd);
1708
- if (!packageVersion) return false;
1709
- if (packageVersion === version) return true;
1710
- const semVer = coerce(packageVersion);
1711
- if (!semVer) return false;
1712
- return satisfies(semVer, version);
1713
- }
1714
- //#endregion
1715
- export { AsyncEventEmitter, FileManager, FileProcessor, FunctionParams, PluginDriver, URLPath, ast, buildDefaultBanner, composeTransformers, createAdapter, createFunctionParams, createKubb, createPlugin, createRenderer, createStorage, defaultResolveBanner, defaultResolveFile, defaultResolveFooter, defaultResolveOptions, defaultResolvePath, defineGenerator, defineLogger, defineParser, definePlugin, definePrinter, defineResolver, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getFunctionParams, getMode, isInputPath, linters, logLevel, memoryStorage, satisfiesDependency };
1427
+ export { AsyncEventEmitter, FileManager, FileProcessor, PluginDriver, URLPath, ast, createAdapter, createKubb, createRenderer, createStorage, defineGenerator, defineLogger, defineParser, definePlugin, defineResolver, fsStorage, isInputPath, logLevel, memoryStorage };
1716
1428
 
1717
1429
  //# sourceMappingURL=index.js.map