@kubb/core 5.8.0 → 5.8.2

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/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  ### Core engine for Kubb
26
26
 
27
- Core engine for Kubb's plugin-based code generation system. Provides the plugin driver, file manager, `defineConfig`, `definePlugin`, `defineMiddleware`, and the build orchestration layer used by every Kubb plugin.
27
+ Core engine for Kubb's plugin-based code generation system. Provides the plugin driver, file manager, `defineConfig`, `defineExtension`, `defineMiddleware`, and the build orchestration layer used by every Kubb plugin.
28
28
 
29
29
  > **Note:** `@kubb/core` is an internal library. Install the [`kubb`](https://npmx.dev/package/kubb) meta-package instead and author plugins, generators, resolvers, parsers, and adapters through its `kubb/kit` subpath, which re-exports the authoring surface of this package.
30
30
 
@@ -1,11 +1,14 @@
1
- import "./rolldown-runtime-C0LytTxp.js";
1
+ import "./rolldown-runtime-CRm0XQPb.js";
2
+ import { satisfies } from "verkit";
2
3
  import { AsyncLocalStorage } from "node:async_hooks";
3
4
  import { styleText } from "node:util";
4
5
  import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
5
6
  import path, { dirname, isAbsolute, relative, resolve } from "node:path";
6
7
  import { ast, collectImportedRefNames, collectUsedSchemaNames, composeMacros, extractStringsFromNodes, operationDef, schemaDef, transform } from "@kubb/ast";
7
- import { satisfies } from "verkit";
8
8
  import { EventEmitter } from "node:events";
9
+ //#region package.json
10
+ var version = "5.8.2";
11
+ //#endregion
9
12
  //#region ../../internals/utils/src/casing.ts
10
13
  /**
11
14
  * Shared implementation for camelCase and PascalCase conversion.
@@ -329,9 +332,6 @@ function formatMs(ms) {
329
332
  return `${Math.round(ms)}ms`;
330
333
  }
331
334
  //#endregion
332
- //#region package.json
333
- var version = "5.8.0";
334
- //#endregion
335
335
  //#region src/constants.ts
336
336
  /**
337
337
  * Stable codes Kubb attaches to a `Diagnostic`. Each maps to a known failure mode
@@ -443,10 +443,6 @@ const diagnosticCode = {
443
443
  */
444
444
  invalidPluginOptions: "KUBB_INVALID_PLUGIN_OPTIONS",
445
445
  /**
446
- * An extension's options fail its options `schema`.
447
- */
448
- invalidOptions: "KUBB_INVALID_OPTIONS",
449
- /**
450
446
  * A post-generate command (`output.postGenerate`) exited with a failure.
451
447
  */
452
448
  postGenerateFailed: "KUBB_POST_GENERATE_FAILED",
@@ -654,11 +650,6 @@ const diagnosticCatalog = {
654
650
  cause: "A plugin was configured with options that cannot be honored, for example `output.mode: 'file'` paired with a `group` option.",
655
651
  fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option or use `output.mode: 'directory'`."
656
652
  },
657
- [diagnosticCode.invalidOptions]: {
658
- title: "Invalid options",
659
- cause: "A plugin declares an options `schema`, and the options passed to it in kubb.config.ts do not match it.",
660
- fix: "Fix each option the message lists by its path, such as `enum.type`. The plugin documentation lists the accepted values."
661
- },
662
653
  [diagnosticCode.postGenerateFailed]: {
663
654
  title: "Post-generate command failed",
664
655
  cause: "A post-generate command (`output.postGenerate`) exited with a non-zero status.",
@@ -932,6 +923,134 @@ var Diagnostics = class Diagnostics {
932
923
  }
933
924
  };
934
925
  //#endregion
926
+ //#region src/defineExtension.ts
927
+ /**
928
+ * Returns `meta`, or `{ name }` for an extension that only sets the deprecated `name`. Throws `KUBB_INVALID_PLUGIN_OPTIONS` when it has neither.
929
+ */
930
+ function resolveMeta({ name, meta }) {
931
+ if (meta) return meta;
932
+ if (name) return { name };
933
+ throw new Diagnostics.Error({
934
+ code: Diagnostics.code.invalidPluginOptions,
935
+ severity: "error",
936
+ message: "An extension has neither `meta.name` nor `name`.",
937
+ help: "Set `meta: { name }` on the plugin or parser.",
938
+ location: { kind: "config" }
939
+ });
940
+ }
941
+ /**
942
+ * Throws `KUBB_INCOMPATIBLE_EXTENSION` when the running Kubb or Node.js version is outside `meta.compatibility`.
943
+ */
944
+ function checkCompatibility(meta) {
945
+ const checks = [{
946
+ target: "Kubb",
947
+ range: meta.compatibility?.kubb,
948
+ version
949
+ }, {
950
+ target: "Node.js",
951
+ range: meta.compatibility?.node,
952
+ version: process.versions.node
953
+ }];
954
+ for (const { target, range, version } of checks) {
955
+ if (!range || satisfies(version, range, { includePrerelease: true })) continue;
956
+ throw new Diagnostics.Error({
957
+ code: Diagnostics.code.incompatibleExtension,
958
+ severity: "error",
959
+ message: `"${meta.name}" requires ${target} ${range}, but ${version} is running.`,
960
+ help: `Use a ${target} version that satisfies ${range}, or a version of "${meta.name}" that supports ${target} ${version}.`,
961
+ location: { kind: "config" },
962
+ plugin: meta.name
963
+ });
964
+ }
965
+ }
966
+ /**
967
+ * Throws `KUBB_MISSING_DEPENDENCY` for a missing required entry in a `dependencies` record, and
968
+ * `KUBB_INCOMPATIBLE_DEPENDENCY` when an entry's `meta.version` is out of range. Arrays are not checked.
969
+ */
970
+ function checkDependencies({ name, dependencies, plugins }) {
971
+ if (!dependencies || Array.isArray(dependencies)) return;
972
+ for (const [dependencyName, dependency] of Object.entries(dependencies)) {
973
+ const target = plugins.get(dependencyName);
974
+ if (!target) {
975
+ if (dependency?.optional) continue;
976
+ throw new Diagnostics.Error({
977
+ code: Diagnostics.code.missingDependency,
978
+ severity: "error",
979
+ message: `Plugin "${dependencyName}" is required by "${name}" but not found.`,
980
+ help: `Add "${dependencyName}" to the \`plugins\` array in kubb.config.ts.`,
981
+ location: { kind: "config" },
982
+ plugin: name
983
+ });
984
+ }
985
+ const range = dependency?.version;
986
+ const version = target.meta?.version;
987
+ if (!range || !version || satisfies(version, range, { includePrerelease: true })) continue;
988
+ throw new Diagnostics.Error({
989
+ code: Diagnostics.code.incompatibleDependency,
990
+ severity: "error",
991
+ message: `"${name}" requires "${dependencyName}" ${range}, but ${version} is configured.`,
992
+ help: `Install a version of "${dependencyName}" that satisfies ${range}.`,
993
+ location: { kind: "config" },
994
+ plugin: name
995
+ });
996
+ }
997
+ }
998
+ function createExtensionFactory({ kind, meta, build }) {
999
+ const factory = (options) => build(options ?? {});
1000
+ return Object.assign(factory, meta ? {
1001
+ kind,
1002
+ meta
1003
+ } : { kind });
1004
+ }
1005
+ function defineExtension(definition) {
1006
+ switch (definition.kind) {
1007
+ case "plugin": {
1008
+ const { meta, dependencies, enforce, hooks = {}, setup } = definition;
1009
+ return createExtensionFactory({
1010
+ kind: "plugin",
1011
+ meta,
1012
+ build: (options) => ({
1013
+ name: meta.name,
1014
+ meta,
1015
+ options,
1016
+ dependencies,
1017
+ enforce,
1018
+ hooks,
1019
+ setup
1020
+ })
1021
+ });
1022
+ }
1023
+ case "adapter": {
1024
+ const { meta, setup, build } = definition;
1025
+ return createExtensionFactory({
1026
+ kind: "adapter",
1027
+ meta,
1028
+ build: (raw) => {
1029
+ const options = setup ? setup(raw) : raw;
1030
+ return Object.assign(build(options), {
1031
+ name: meta.name,
1032
+ meta,
1033
+ options
1034
+ });
1035
+ }
1036
+ });
1037
+ }
1038
+ case "parser": {
1039
+ const { meta, build } = definition;
1040
+ return createExtensionFactory({
1041
+ kind: "parser",
1042
+ meta,
1043
+ build: (options) => ({
1044
+ ...build(options),
1045
+ name: meta.name,
1046
+ meta,
1047
+ options
1048
+ })
1049
+ });
1050
+ }
1051
+ }
1052
+ }
1053
+ //#endregion
935
1054
  //#region src/composables.ts
936
1055
  const kubbContextKey = Symbol.for("kubb.context");
937
1056
  const host = globalThis;
@@ -999,7 +1118,7 @@ function injectFile(file) {
999
1118
  requireContext("injectFile").injectFile(file);
1000
1119
  }
1001
1120
  /**
1002
- * Returns the options of the plugin being set up, parsed by its options `schema` when it has one, like `ctx.options`.
1121
+ * Returns the options the user passed to the plugin being set up, like `ctx.options`.
1003
1122
  */
1004
1123
  function useOptions() {
1005
1124
  return requireContext("useOptions").options;
@@ -1023,123 +1142,6 @@ function requirePlugin(name) {
1023
1142
  return requireContext("requirePlugin").requirePlugin(name);
1024
1143
  }
1025
1144
  //#endregion
1026
- //#region src/defineExtension.ts
1027
- /**
1028
- * Returns `meta`, or `{ name }` for an extension that only sets the deprecated `name`. Throws `KUBB_INVALID_PLUGIN_OPTIONS` when it has neither.
1029
- */
1030
- function resolveMeta({ name, meta }) {
1031
- if (meta) return meta;
1032
- if (name) return { name };
1033
- throw new Diagnostics.Error({
1034
- code: Diagnostics.code.invalidPluginOptions,
1035
- severity: "error",
1036
- message: "An extension has neither `meta.name` nor `name`.",
1037
- help: "Set `meta: { name }` on the plugin or parser.",
1038
- location: { kind: "config" }
1039
- });
1040
- }
1041
- /**
1042
- * Throws `KUBB_INCOMPATIBLE_EXTENSION` when the running Kubb or Node.js version is outside `meta.compatibility`.
1043
- */
1044
- function checkCompatibility(meta) {
1045
- const checks = [{
1046
- target: "Kubb",
1047
- range: meta.compatibility?.kubb,
1048
- version
1049
- }, {
1050
- target: "Node.js",
1051
- range: meta.compatibility?.node,
1052
- version: process.versions.node
1053
- }];
1054
- for (const { target, range, version } of checks) {
1055
- if (!range || satisfies(version, range, { includePrerelease: true })) continue;
1056
- throw new Diagnostics.Error({
1057
- code: Diagnostics.code.incompatibleExtension,
1058
- severity: "error",
1059
- message: `"${meta.name}" requires ${target} ${range}, but ${version} is running.`,
1060
- help: `Use a ${target} version that satisfies ${range}, or a version of "${meta.name}" that supports ${target} ${version}.`,
1061
- location: { kind: "config" },
1062
- plugin: meta.name
1063
- });
1064
- }
1065
- }
1066
- /**
1067
- * Throws `KUBB_MISSING_DEPENDENCY` for a missing required entry in a `dependencies` record, and
1068
- * `KUBB_INCOMPATIBLE_DEPENDENCY` when an entry's `meta.version` is out of range. Arrays are not checked.
1069
- */
1070
- function checkDependencies({ name, dependencies, plugins }) {
1071
- if (!dependencies || Array.isArray(dependencies)) return;
1072
- for (const [dependencyName, dependency] of Object.entries(dependencies)) {
1073
- const target = plugins.get(dependencyName);
1074
- if (!target) {
1075
- if (dependency?.optional) continue;
1076
- throw new Diagnostics.Error({
1077
- code: Diagnostics.code.missingDependency,
1078
- severity: "error",
1079
- message: `Plugin "${dependencyName}" is required by "${name}" but not found.`,
1080
- help: `Add "${dependencyName}" to the \`plugins\` array in kubb.config.ts.`,
1081
- location: { kind: "config" },
1082
- plugin: name
1083
- });
1084
- }
1085
- const range = dependency?.version;
1086
- const version = target.meta?.version;
1087
- if (!range || !version || satisfies(version, range, { includePrerelease: true })) continue;
1088
- throw new Diagnostics.Error({
1089
- code: Diagnostics.code.incompatibleDependency,
1090
- severity: "error",
1091
- message: `"${name}" requires "${dependencyName}" ${range}, but ${version} is configured.`,
1092
- help: `Install a version of "${dependencyName}" that satisfies ${range}.`,
1093
- location: { kind: "config" },
1094
- plugin: name
1095
- });
1096
- }
1097
- }
1098
- function formatIssuePath(path) {
1099
- return (path ?? []).map((segment) => String(typeof segment === "object" ? segment.key : segment)).join(".");
1100
- }
1101
- /**
1102
- * Validates an extension's raw options against its `schema` and returns the parsed value, with the schema's defaults.
1103
- * Throws `KUBB_INVALID_OPTIONS`, naming the extension and each issue path, when the options are invalid.
1104
- * Refuses an async validator, because extension factories run synchronously while the config loads.
1105
- */
1106
- function parseExtensionOptions({ kind, meta, schema, input }) {
1107
- const validated = schema["~standard"].validate(input);
1108
- if (typeof validated.then === "function") {
1109
- Promise.resolve(validated).catch(() => void 0);
1110
- throw new Error(`The options schema of ${kind} "${meta.name}" validates asynchronously. Use a synchronous schema, because Kubb validates options while the config loads.`);
1111
- }
1112
- const result = validated;
1113
- if (!result.issues) return result.value;
1114
- const issues = result.issues.map((issue) => {
1115
- const path = formatIssuePath(issue.path);
1116
- return path ? `\`${path}\`: ${issue.message}` : issue.message;
1117
- });
1118
- throw new Diagnostics.Error({
1119
- code: Diagnostics.code.invalidOptions,
1120
- severity: "error",
1121
- message: `Invalid options for ${kind} "${meta.name}". ${issues.join("; ")}`,
1122
- help: `Fix the listed options of "${meta.name}" in kubb.config.ts. Its documentation lists the accepted values.`,
1123
- location: { kind: "config" },
1124
- ...kind === "plugin" && { plugin: meta.name }
1125
- });
1126
- }
1127
- function defineExtension({ kind, meta, schema, build }) {
1128
- const factory = (raw) => {
1129
- const options = raw ?? {};
1130
- return build({
1131
- options,
1132
- parsedOptions: schema && meta ? parseExtensionOptions({
1133
- kind,
1134
- meta,
1135
- schema,
1136
- input: options
1137
- }) : options
1138
- });
1139
- };
1140
- return Object.assign(factory, { kind }, meta && { meta }, schema && { schema });
1141
- }
1142
- //#endregion
1143
1145
  //#region src/definePlugin.ts
1144
1146
  /**
1145
1147
  * Merges the `output.mode` default into the output config and validates the combination.
@@ -1180,9 +1182,9 @@ function assertPluginOption({ plugin, option, value, allowed }) {
1180
1182
  });
1181
1183
  }
1182
1184
  function definePlugin(input) {
1183
- if (typeof input === "function") return defineExtension({
1185
+ if (typeof input === "function") return createExtensionFactory({
1184
1186
  kind: "plugin",
1185
- build: ({ options }) => {
1187
+ build: (options) => {
1186
1188
  const plugin = input(options);
1187
1189
  return {
1188
1190
  ...plugin,
@@ -1190,21 +1192,9 @@ function definePlugin(input) {
1190
1192
  };
1191
1193
  }
1192
1194
  });
1193
- const { meta, schema, dependencies, enforce, hooks = {}, setup } = input;
1194
1195
  return defineExtension({
1195
1196
  kind: "plugin",
1196
- meta,
1197
- schema,
1198
- build: ({ options, parsedOptions }) => ({
1199
- name: meta.name,
1200
- meta,
1201
- options,
1202
- ...schema && { parsedOptions },
1203
- dependencies,
1204
- enforce,
1205
- hooks,
1206
- setup
1207
- })
1197
+ ...input
1208
1198
  });
1209
1199
  }
1210
1200
  //#endregion
@@ -2242,26 +2232,25 @@ var KubbDriver = class {
2242
2232
  this.adapter = config.adapter ?? null;
2243
2233
  }
2244
2234
  /**
2245
- * Normalizes and checks every configured plugin, orders them, and registers their lifecycle handlers.
2235
+ * Checks the adapter's and parsers' `meta.compatibility`, then normalizes and checks every configured plugin, orders them, and registers their lifecycle handlers.
2246
2236
  * A plugin that another lists as a dependency runs first, then `enforce: 'pre'` before
2247
2237
  * `'post'`. When the config has an adapter, the adapter source is resolved from the input
2248
2238
  * so `run` can parse it later.
2249
2239
  */
2250
2240
  async setup() {
2251
2241
  this.options.signal?.throwIfAborted();
2252
- for (const parser of this.config.parsers) if (parser.meta) checkCompatibility(parser.meta);
2242
+ for (const extension of [this.config.adapter, ...this.config.parsers]) if (extension?.meta) checkCompatibility(extension.meta);
2253
2243
  const normalized = this.#sortPlugins(this.config.plugins.map((rawPlugin) => {
2254
2244
  if (rawPlugin.meta) checkCompatibility(rawPlugin.meta);
2255
2245
  const meta = resolveMeta(rawPlugin);
2256
- const options = rawPlugin.parsedOptions ?? rawPlugin.options;
2257
2246
  return {
2258
2247
  name: meta.name,
2259
2248
  meta,
2260
- dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2249
+ dependencies: typeof rawPlugin.dependencies === "function" ? rawPlugin.dependencies(rawPlugin.options ?? {}, { config: this.config }) : rawPlugin.dependencies,
2261
2250
  enforce: rawPlugin.enforce,
2262
2251
  hooks: rawPlugin.hooks,
2263
2252
  setup: rawPlugin.setup,
2264
- options: options ?? defaultPluginOptions(),
2253
+ options: rawPlugin.options ?? defaultPluginOptions(),
2265
2254
  resolver: createResolver({ pluginName: meta.name })
2266
2255
  };
2267
2256
  }));
@@ -2833,6 +2822,6 @@ function pluginNotFoundError({ pluginName, requiredBy }) {
2833
2822
  });
2834
2823
  }
2835
2824
  //#endregion
2836
- export { clean as A, useConfig as C, formatMs as D, Diagnostics as E, write as F, toError as I, camelCase as L, isPathInside as M, read as N, getElapsedMs as O, toPosixPath as P, tryUseKubb as S, useOptions as T, kubbContext as _, _usingCtx as a, setOptions as b, Resolver as c, definePlugin as d, defineExtension as f, injectFile as g, addMacro as h, FileManager as i, exists as j, inParallel as k, getInputKind as l, addGenerator as m, pluginNotFoundError as n, Hookable as o, resolveMeta as p, createNodeCache as r, createResolver as s, KubbDriver as t, assertPluginOption as u, requirePlugin as v, useKubb as w, setResolver as x, setMacros as y };
2825
+ export { getElapsedMs as A, useOptions as C, resolveMeta as D, defineExtension as E, read as F, toPosixPath as I, write as L, clean as M, exists as N, Diagnostics as O, isPathInside as P, toError as R, useKubb as S, createExtensionFactory as T, setMacros as _, _usingCtx as a, tryUseKubb as b, Resolver as c, definePlugin as d, addGenerator as f, requirePlugin as g, kubbContext as h, FileManager as i, inParallel as j, formatMs as k, getInputKind as l, injectFile as m, pluginNotFoundError as n, Hookable as o, addMacro as p, createNodeCache as r, createResolver as s, KubbDriver as t, assertPluginOption as u, setOptions as v, checkCompatibility as w, useConfig as x, setResolver as y, camelCase as z };
2837
2826
 
2838
- //# sourceMappingURL=KubbDriver-BrltQ0nL.js.map
2827
+ //# sourceMappingURL=KubbDriver-DBm4hRKA.js.map