@kubb/core 5.5.6 → 5.6.1
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 +698 -306
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +202 -4
- package/dist/index.js +645 -258
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +30 -47
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.js +24 -41
- package/dist/mocks.js.map +1 -1
- package/dist/{usingCtx-C_vhSf7M.cjs → nodeCache-CllHsCcy.cjs} +101 -222
- package/dist/nodeCache-CllHsCcy.cjs.map +1 -0
- package/dist/{usingCtx-DdG2edpx.js → nodeCache-CumrOPh-.js} +101 -216
- package/dist/nodeCache-CumrOPh-.js.map +1 -0
- package/dist/{types-DNE8KVDT.d.ts → types-B-w6zShf.d.ts} +66 -296
- package/package.json +6 -8
- package/dist/usingCtx-C_vhSf7M.cjs.map +0 -1
- package/dist/usingCtx-DdG2edpx.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_nodeCache = require("./nodeCache-CllHsCcy.cjs");
|
|
3
3
|
let node_async_hooks = require("node:async_hooks");
|
|
4
4
|
let node_util = require("node:util");
|
|
5
5
|
let node_process = require("node:process");
|
|
6
|
-
node_process =
|
|
7
|
-
let
|
|
6
|
+
node_process = require_nodeCache.__toESM(node_process, 1);
|
|
7
|
+
let tinyexec = require("tinyexec");
|
|
8
8
|
let node_fs_promises = require("node:fs/promises");
|
|
9
9
|
let node_path = require("node:path");
|
|
10
|
-
node_path =
|
|
10
|
+
node_path = require_nodeCache.__toESM(node_path, 1);
|
|
11
11
|
let _kubb_ast = require("@kubb/ast");
|
|
12
12
|
let node_crypto = require("node:crypto");
|
|
13
13
|
let node_fs = require("node:fs");
|
|
14
14
|
let node_os = require("node:os");
|
|
15
|
+
let node_readline = require("node:readline");
|
|
16
|
+
let unstorage = require("unstorage");
|
|
17
|
+
let unstorage_drivers_fs = require("unstorage/drivers/fs");
|
|
18
|
+
unstorage_drivers_fs = require_nodeCache.__toESM(unstorage_drivers_fs, 1);
|
|
15
19
|
let node_url = require("node:url");
|
|
16
20
|
//#region src/createAdapter.ts
|
|
17
21
|
/**
|
|
@@ -78,18 +82,131 @@ function isCIEnvironment() {
|
|
|
78
82
|
}
|
|
79
83
|
//#endregion
|
|
80
84
|
//#region ../../internals/utils/src/openInBrowser.ts
|
|
85
|
+
const ignore = () => {};
|
|
81
86
|
/**
|
|
82
87
|
* Opens a URL or local file in the default browser, unless running in CI.
|
|
83
88
|
*/
|
|
84
89
|
function openInBrowser(target) {
|
|
85
90
|
if (isCIEnvironment()) return;
|
|
86
|
-
if (node_process.default.platform === "win32")
|
|
87
|
-
"
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
if (node_process.default.platform === "win32") {
|
|
92
|
+
(0, tinyexec.x)("cmd", [
|
|
93
|
+
"/c",
|
|
94
|
+
"start",
|
|
95
|
+
"",
|
|
96
|
+
target
|
|
97
|
+
]).then(ignore, ignore);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
(0, tinyexec.x)(node_process.default.platform === "darwin" ? "open" : "xdg-open", [target]).then(ignore, ignore);
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region ../../internals/utils/src/tools.ts
|
|
104
|
+
/**
|
|
105
|
+
* CLI command descriptors for each supported code formatter.
|
|
106
|
+
*/
|
|
107
|
+
const formatters = {
|
|
108
|
+
prettier: {
|
|
109
|
+
command: "prettier",
|
|
110
|
+
args: (outputPath) => [
|
|
111
|
+
"--ignore-unknown",
|
|
112
|
+
"--write",
|
|
113
|
+
outputPath
|
|
114
|
+
],
|
|
115
|
+
errorMessage: "Prettier not found"
|
|
116
|
+
},
|
|
117
|
+
biome: {
|
|
118
|
+
command: "biome",
|
|
119
|
+
args: (outputPath) => [
|
|
120
|
+
"format",
|
|
121
|
+
"--write",
|
|
122
|
+
outputPath
|
|
123
|
+
],
|
|
124
|
+
errorMessage: "Biome not found"
|
|
125
|
+
},
|
|
126
|
+
oxfmt: {
|
|
127
|
+
command: "oxfmt",
|
|
128
|
+
args: (outputPath) => [outputPath],
|
|
129
|
+
errorMessage: "Oxfmt not found"
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* CLI command descriptors for each supported linter.
|
|
134
|
+
*/
|
|
135
|
+
const linters = {
|
|
136
|
+
eslint: {
|
|
137
|
+
command: "eslint",
|
|
138
|
+
args: (outputPath) => [outputPath, "--fix"],
|
|
139
|
+
errorMessage: "Eslint not found"
|
|
140
|
+
},
|
|
141
|
+
biome: {
|
|
142
|
+
command: "biome",
|
|
143
|
+
args: (outputPath) => [
|
|
144
|
+
"lint",
|
|
145
|
+
"--fix",
|
|
146
|
+
outputPath
|
|
147
|
+
],
|
|
148
|
+
errorMessage: "Biome not found"
|
|
149
|
+
},
|
|
150
|
+
oxlint: {
|
|
151
|
+
command: "oxlint",
|
|
152
|
+
args: (outputPath) => [
|
|
153
|
+
"--fix",
|
|
154
|
+
"--no-ignore",
|
|
155
|
+
outputPath
|
|
156
|
+
],
|
|
157
|
+
errorMessage: "Oxlint not found"
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Preference order for `format: 'auto'`, most-preferred first. Spelled out rather than taken from
|
|
162
|
+
* the table's key order, which is arbitrary and would silently change what `auto` picks.
|
|
163
|
+
*/
|
|
164
|
+
const FORMATTER_PREFERENCE = [
|
|
165
|
+
"oxfmt",
|
|
166
|
+
"biome",
|
|
167
|
+
"prettier"
|
|
168
|
+
];
|
|
169
|
+
/**
|
|
170
|
+
* Preference order for `lint: 'auto'`, most-preferred first.
|
|
171
|
+
*/
|
|
172
|
+
const LINTER_PREFERENCE = [
|
|
173
|
+
"oxlint",
|
|
174
|
+
"biome",
|
|
175
|
+
"eslint"
|
|
176
|
+
];
|
|
177
|
+
/**
|
|
178
|
+
* Whether `name` is on PATH or in a `node_modules/.bin` above `cwd` and answers `--version` with a zero exit.
|
|
179
|
+
*/
|
|
180
|
+
async function isToolAvailable(name, cwd) {
|
|
181
|
+
return (0, tinyexec.x)(name, ["--version"], {
|
|
182
|
+
throwOnError: false,
|
|
183
|
+
nodeOptions: {
|
|
184
|
+
cwd,
|
|
185
|
+
stdio: "ignore"
|
|
186
|
+
}
|
|
187
|
+
}).then(({ exitCode }) => exitCode === 0, () => false);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns the first installed executable from `candidates`, or `null` when none are found.
|
|
191
|
+
*
|
|
192
|
+
* Not memoized: a long-running host that probes repeatedly should cache the result itself, and a
|
|
193
|
+
* `--watch` build should keep noticing a tool installed mid-session.
|
|
194
|
+
*/
|
|
195
|
+
async function detectTool(candidates, cwd) {
|
|
196
|
+
for (const candidate of candidates) if (await isToolAvailable(candidate, cwd)) return candidate;
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Tokenizes a shell command string, respecting single and double quotes.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* tokenize('git commit -m "initial commit"')
|
|
205
|
+
* // → ['git', 'commit', '-m', 'initial commit']
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
function tokenize(command) {
|
|
209
|
+
return (command.match(/[^\s"']+|"([^"]*)"|'([^']*)'/g) ?? []).map((token) => token.replace(/^["']|["']$/g, ""));
|
|
93
210
|
}
|
|
94
211
|
//#endregion
|
|
95
212
|
//#region ../../internals/utils/src/time.ts
|
|
@@ -125,11 +242,134 @@ function formatMs(ms) {
|
|
|
125
242
|
return `${Math.round(ms)}ms`;
|
|
126
243
|
}
|
|
127
244
|
//#endregion
|
|
245
|
+
//#region package.json
|
|
246
|
+
var version = "5.6.1";
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/constants.ts
|
|
249
|
+
/**
|
|
250
|
+
* Stable codes Kubb attaches to a `Diagnostic`. Each maps to a known failure mode
|
|
251
|
+
* and stays stable so it can be referenced in tooling and (later) docs. Reference
|
|
252
|
+
* these instead of inlining the string at a throw site.
|
|
253
|
+
*/
|
|
254
|
+
const diagnosticCode = {
|
|
255
|
+
/**
|
|
256
|
+
* Fallback for an unstructured error with no specific code.
|
|
257
|
+
*/
|
|
258
|
+
unknown: "KUBB_UNKNOWN",
|
|
259
|
+
/**
|
|
260
|
+
* The file or URL set as `input` could not be read.
|
|
261
|
+
*/
|
|
262
|
+
inputNotFound: "KUBB_INPUT_NOT_FOUND",
|
|
263
|
+
/**
|
|
264
|
+
* A URL set as `input` (or referenced by a `$ref`) answered with a 4xx or 5xx status
|
|
265
|
+
* instead of the document.
|
|
266
|
+
*/
|
|
267
|
+
inputRequestFailed: "KUBB_INPUT_REQUEST_FAILED",
|
|
268
|
+
/**
|
|
269
|
+
* A URL set as `input` (or referenced by a `$ref`) never answered, so the request failed
|
|
270
|
+
* before a status was returned.
|
|
271
|
+
*/
|
|
272
|
+
inputUnreachable: "KUBB_INPUT_UNREACHABLE",
|
|
273
|
+
/**
|
|
274
|
+
* An adapter was configured without an `input`.
|
|
275
|
+
*/
|
|
276
|
+
inputRequired: "KUBB_INPUT_REQUIRED",
|
|
277
|
+
/**
|
|
278
|
+
* `input` uses the v4 `{ path }` / `{ data }` wrapper, which v5 reads as a parsed
|
|
279
|
+
* document instead of a pointer to one.
|
|
280
|
+
*/
|
|
281
|
+
legacyInput: "KUBB_LEGACY_INPUT",
|
|
282
|
+
/**
|
|
283
|
+
* The parsed `input` carries no `openapi` or `swagger` version, so it is not a
|
|
284
|
+
* document the adapter can read.
|
|
285
|
+
*/
|
|
286
|
+
invalidDocument: "KUBB_INVALID_DOCUMENT",
|
|
287
|
+
/** The document fails OpenAPI schema validation. Reported as a warning. */
|
|
288
|
+
invalidSpec: "KUBB_INVALID_SPEC",
|
|
289
|
+
/**
|
|
290
|
+
* A `$ref` (or equivalent reference) could not be resolved in the source document.
|
|
291
|
+
*/
|
|
292
|
+
refNotFound: "KUBB_REF_NOT_FOUND",
|
|
293
|
+
/**
|
|
294
|
+
* A server variable value is not allowed by its `enum`.
|
|
295
|
+
*/
|
|
296
|
+
invalidServerVariable: "KUBB_INVALID_SERVER_VARIABLE",
|
|
297
|
+
/**
|
|
298
|
+
* A required plugin is missing from the config.
|
|
299
|
+
*/
|
|
300
|
+
pluginNotFound: "KUBB_PLUGIN_NOT_FOUND",
|
|
301
|
+
/**
|
|
302
|
+
* A plugin threw while generating.
|
|
303
|
+
*/
|
|
304
|
+
pluginFailed: "KUBB_PLUGIN_FAILED",
|
|
305
|
+
/**
|
|
306
|
+
* A plugin reported a non-fatal warning through `ctx.warn`.
|
|
307
|
+
*/
|
|
308
|
+
pluginWarning: "KUBB_PLUGIN_WARNING",
|
|
309
|
+
/**
|
|
310
|
+
* A plugin reported an informational message through `ctx.info`.
|
|
311
|
+
*/
|
|
312
|
+
pluginInfo: "KUBB_PLUGIN_INFO",
|
|
313
|
+
/**
|
|
314
|
+
* A schema uses a `format` Kubb does not map to a specific type. Reserved for
|
|
315
|
+
* adapters to emit as a `warning`.
|
|
316
|
+
*/
|
|
317
|
+
unsupportedFormat: "KUBB_UNSUPPORTED_FORMAT",
|
|
318
|
+
/**
|
|
319
|
+
* A referenced schema or operation is marked `deprecated`. Reserved for adapters
|
|
320
|
+
* to emit as an `info`.
|
|
321
|
+
*/
|
|
322
|
+
deprecated: "KUBB_DEPRECATED",
|
|
323
|
+
/**
|
|
324
|
+
* An adapter is required but the config has none. The build cannot read the input
|
|
325
|
+
* without one.
|
|
326
|
+
*/
|
|
327
|
+
adapterRequired: "KUBB_ADAPTER_REQUIRED",
|
|
328
|
+
/**
|
|
329
|
+
* A resolved output path escapes the output directory, which can stem from a path
|
|
330
|
+
* traversal in the spec or a misconfigured `group.name`.
|
|
331
|
+
*/
|
|
332
|
+
pathTraversal: "KUBB_PATH_TRAVERSAL",
|
|
333
|
+
/**
|
|
334
|
+
* `output.clean` is enabled but `output.path` resolves to the project root or a parent of it,
|
|
335
|
+
* so cleaning would delete kubb.config and every source file.
|
|
336
|
+
*/
|
|
337
|
+
cleanRoot: "KUBB_CLEAN_ROOT",
|
|
338
|
+
/**
|
|
339
|
+
* A plugin's options are invalid, for example `output.mode: 'file'` paired with a `group` option.
|
|
340
|
+
*/
|
|
341
|
+
invalidPluginOptions: "KUBB_INVALID_PLUGIN_OPTIONS",
|
|
342
|
+
/**
|
|
343
|
+
* A post-generate command (`output.postGenerate`) exited with a failure.
|
|
344
|
+
*/
|
|
345
|
+
postGenerateFailed: "KUBB_POST_GENERATE_FAILED",
|
|
346
|
+
/**
|
|
347
|
+
* The formatter pass over the generated files failed.
|
|
348
|
+
*/
|
|
349
|
+
formatFailed: "KUBB_FORMAT_FAILED",
|
|
350
|
+
/**
|
|
351
|
+
* The linter pass over the generated files failed.
|
|
352
|
+
*/
|
|
353
|
+
lintFailed: "KUBB_LINT_FAILED",
|
|
354
|
+
/**
|
|
355
|
+
* A barrel tries to re-export the same name from more than one module.
|
|
356
|
+
*/
|
|
357
|
+
barrelDuplicateExport: "KUBB_BARREL_DUPLICATE_EXPORT",
|
|
358
|
+
/**
|
|
359
|
+
* Not a failure. Carries a plugin's elapsed time, summed into the run total.
|
|
360
|
+
*/
|
|
361
|
+
performance: "KUBB_PERFORMANCE",
|
|
362
|
+
/**
|
|
363
|
+
* Not a failure. A newer Kubb version is available on npm.
|
|
364
|
+
*/
|
|
365
|
+
updateAvailable: "KUBB_UPDATE_AVAILABLE"
|
|
366
|
+
};
|
|
367
|
+
//#endregion
|
|
128
368
|
//#region src/Diagnostics.ts
|
|
129
369
|
/**
|
|
130
370
|
* Docs major version, derived from the package version so the link tracks the published major.
|
|
131
371
|
*/
|
|
132
|
-
const docsMajor =
|
|
372
|
+
const docsMajor = version.split(".")[0] ?? "5";
|
|
133
373
|
/**
|
|
134
374
|
* Builds a type guard that narrows a {@link Diagnostic} to the variant for `kind`. A diagnostic
|
|
135
375
|
* with no `kind` is treated as a `problem`.
|
|
@@ -177,137 +417,142 @@ const severityStyle = {
|
|
|
177
417
|
warning: "yellow",
|
|
178
418
|
info: "blue"
|
|
179
419
|
};
|
|
420
|
+
const countKey = {
|
|
421
|
+
error: "errors",
|
|
422
|
+
warning: "warnings",
|
|
423
|
+
info: "infos"
|
|
424
|
+
};
|
|
180
425
|
/**
|
|
181
426
|
* Explanation for every {@link diagnosticCode}. Use {@link Diagnostics.explain} to look one up
|
|
182
427
|
* and `Diagnostics.docsUrl` for the matching kubb.dev page.
|
|
183
428
|
*/
|
|
184
429
|
const diagnosticCatalog = {
|
|
185
|
-
[
|
|
430
|
+
[diagnosticCode.unknown]: {
|
|
186
431
|
title: "Unknown error",
|
|
187
432
|
cause: "An error was thrown without a stable Kubb code, so it is reported as-is.",
|
|
188
433
|
fix: "Read the underlying message and stack. If it comes from a plugin or adapter, check its configuration; otherwise report it as a possible Kubb bug."
|
|
189
434
|
},
|
|
190
|
-
[
|
|
435
|
+
[diagnosticCode.inputNotFound]: {
|
|
191
436
|
title: "Input not found",
|
|
192
437
|
cause: "The file set as `input` (or passed as `kubb generate PATH`) could not be read. A URL reports `KUBB_INPUT_REQUEST_FAILED` or `KUBB_INPUT_UNREACHABLE` instead.",
|
|
193
438
|
fix: "Check that the path exists and is readable, then set it as `input` or pass it on the CLI."
|
|
194
439
|
},
|
|
195
|
-
[
|
|
440
|
+
[diagnosticCode.inputRequestFailed]: {
|
|
196
441
|
title: "Input request failed",
|
|
197
442
|
cause: "A URL set as `input` (or reached through a `$ref`) answered with a 4xx or 5xx status instead of the document.",
|
|
198
443
|
fix: "Open the URL to see what the server returns. A 401 or 403 needs credentials Kubb does not send, so download the document and point `input` at the local file. A 404 means the path is wrong, and a 5xx means the server itself failed."
|
|
199
444
|
},
|
|
200
|
-
[
|
|
445
|
+
[diagnosticCode.inputUnreachable]: {
|
|
201
446
|
title: "Input unreachable",
|
|
202
447
|
cause: "A URL set as `input` (or reached through a `$ref`) never answered, so the request failed before a status came back. A refused connection, an unknown host, an expired certificate, and a timeout all land here.",
|
|
203
448
|
fix: "Check that the host is running and reachable from this machine. For a local server, start it and confirm the port matches the one in `input`."
|
|
204
449
|
},
|
|
205
|
-
[
|
|
450
|
+
[diagnosticCode.inputRequired]: {
|
|
206
451
|
title: "Input required",
|
|
207
452
|
cause: "An adapter is configured but no `input` was provided.",
|
|
208
453
|
fix: "Set `input` to a file path, a URL, an inline spec (JSON/YAML string), or a parsed object in your Kubb config."
|
|
209
454
|
},
|
|
210
|
-
[
|
|
455
|
+
[diagnosticCode.legacyInput]: {
|
|
211
456
|
title: "Legacy input shape",
|
|
212
457
|
cause: "`input` is a `{ path }` or `{ data }` wrapper, which v4 used to point at a document and v5 reads as the document itself.",
|
|
213
458
|
fix: "Unwrap it: `input: { path: \"./petStore.yaml\" }` becomes `input: \"./petStore.yaml\"`, and `input: { data: spec }` becomes `input: spec`."
|
|
214
459
|
},
|
|
215
|
-
[
|
|
460
|
+
[diagnosticCode.invalidDocument]: {
|
|
216
461
|
title: "Invalid document",
|
|
217
462
|
cause: "The parsed `input` has no `openapi` or `swagger` version field, so it is not an OpenAPI or Swagger document.",
|
|
218
463
|
fix: "Point `input` at a document that declares `openapi` or `swagger`, and check that a passed object is the spec itself rather than a wrapper around it."
|
|
219
464
|
},
|
|
220
|
-
[
|
|
465
|
+
[diagnosticCode.invalidSpec]: {
|
|
221
466
|
title: "Invalid spec",
|
|
222
467
|
cause: "The document does not pass OpenAPI schema validation. Kubb still generates from it, but the output can be incomplete or wrong.",
|
|
223
468
|
fix: "Fix the reported problem in the spec. Run `kubb validate` to list every error, or set `validate: false` on the adapter to skip the check."
|
|
224
469
|
},
|
|
225
|
-
[
|
|
470
|
+
[diagnosticCode.refNotFound]: {
|
|
226
471
|
title: "Reference not found",
|
|
227
472
|
cause: "A `$ref` could not be resolved in the source document.",
|
|
228
473
|
fix: "Add the missing definition (for example under `components.schemas`) or fix the `$ref`. Run `kubb validate` to check the spec."
|
|
229
474
|
},
|
|
230
|
-
[
|
|
475
|
+
[diagnosticCode.invalidServerVariable]: {
|
|
231
476
|
title: "Invalid server variable",
|
|
232
477
|
cause: "A server variable value is not allowed by its `enum`.",
|
|
233
478
|
fix: "Use one of the values listed in the server variable `enum`, or update the spec."
|
|
234
479
|
},
|
|
235
|
-
[
|
|
480
|
+
[diagnosticCode.pluginNotFound]: {
|
|
236
481
|
title: "Plugin not found",
|
|
237
482
|
cause: "A plugin that another plugin depends on is missing from the config.",
|
|
238
483
|
fix: "Add the required plugin to the `plugins` array in kubb.config.ts, or remove the dependency on it."
|
|
239
484
|
},
|
|
240
|
-
[
|
|
485
|
+
[diagnosticCode.pluginFailed]: {
|
|
241
486
|
title: "Plugin failed",
|
|
242
487
|
cause: "A plugin threw while generating, or reported an error through `ctx.error`.",
|
|
243
488
|
fix: "Read the underlying error and check the plugin options and the schema or operation it failed on."
|
|
244
489
|
},
|
|
245
|
-
[
|
|
490
|
+
[diagnosticCode.pluginWarning]: {
|
|
246
491
|
title: "Plugin warning",
|
|
247
492
|
cause: "A plugin reported a non-fatal warning through `ctx.warn`.",
|
|
248
493
|
fix: "Review the message. It does not fail the build; adjust the plugin options or input if the warning is unwanted."
|
|
249
494
|
},
|
|
250
|
-
[
|
|
495
|
+
[diagnosticCode.pluginInfo]: {
|
|
251
496
|
title: "Plugin info",
|
|
252
497
|
cause: "A plugin reported an informational message through `ctx.info`.",
|
|
253
498
|
fix: "Informational only. No action is required."
|
|
254
499
|
},
|
|
255
|
-
[
|
|
500
|
+
[diagnosticCode.unsupportedFormat]: {
|
|
256
501
|
title: "Unsupported format",
|
|
257
502
|
cause: "A schema uses a `format` Kubb does not map to a specific type, so it falls back to the base type.",
|
|
258
503
|
fix: "Use a format Kubb supports, or handle the custom format with a parser or plugin."
|
|
259
504
|
},
|
|
260
|
-
[
|
|
505
|
+
[diagnosticCode.deprecated]: {
|
|
261
506
|
title: "Deprecated",
|
|
262
507
|
cause: "A referenced schema or operation is marked `deprecated`.",
|
|
263
508
|
fix: "Migrate off the deprecated definition if the warning is unwanted."
|
|
264
509
|
},
|
|
265
|
-
[
|
|
510
|
+
[diagnosticCode.adapterRequired]: {
|
|
266
511
|
title: "Adapter required",
|
|
267
512
|
cause: "An action needs an adapter but none is configured.",
|
|
268
513
|
fix: "Set `adapter` in kubb.config.ts, for example `adapterOas()`."
|
|
269
514
|
},
|
|
270
|
-
[
|
|
515
|
+
[diagnosticCode.pathTraversal]: {
|
|
271
516
|
title: "Path traversal",
|
|
272
517
|
cause: "A resolved output path escaped the output directory, which can stem from a path traversal in the spec or a misconfigured `group.name`.",
|
|
273
518
|
fix: "Keep generated paths within the output directory. Review the `group.name` function and the names coming from the spec."
|
|
274
519
|
},
|
|
275
|
-
[
|
|
520
|
+
[diagnosticCode.cleanRoot]: {
|
|
276
521
|
title: "Clean targets the project root",
|
|
277
522
|
cause: "`output.clean` is enabled and `output.path` resolves to the project root or a parent of it, so cleaning would delete `kubb.config` and every source file.",
|
|
278
523
|
fix: "Point `output.path` at a subdirectory such as `./src/gen` so clean only removes generated code, or disable `output.clean`."
|
|
279
524
|
},
|
|
280
|
-
[
|
|
525
|
+
[diagnosticCode.invalidPluginOptions]: {
|
|
281
526
|
title: "Invalid plugin options",
|
|
282
527
|
cause: "A plugin was configured with options that cannot be honored, for example `output.mode: 'file'` paired with a `group` option.",
|
|
283
528
|
fix: "Fix the plugin options. A single-file output has nothing to group, so remove the `group` option or use `output.mode: 'directory'`."
|
|
284
529
|
},
|
|
285
|
-
[
|
|
530
|
+
[diagnosticCode.postGenerateFailed]: {
|
|
286
531
|
title: "Post-generate command failed",
|
|
287
532
|
cause: "A post-generate command (`output.postGenerate`) exited with a non-zero status.",
|
|
288
533
|
fix: "Check the command is installed and correct, and run it manually to see the error."
|
|
289
534
|
},
|
|
290
|
-
[
|
|
535
|
+
[diagnosticCode.formatFailed]: {
|
|
291
536
|
title: "Format failed",
|
|
292
537
|
cause: "The formatter pass over the generated files failed.",
|
|
293
538
|
fix: "Check the formatter (oxfmt, biome, or prettier) is installed and its config is valid, then run it manually on the output."
|
|
294
539
|
},
|
|
295
|
-
[
|
|
540
|
+
[diagnosticCode.lintFailed]: {
|
|
296
541
|
title: "Lint failed",
|
|
297
542
|
cause: "The linter pass over the generated files failed.",
|
|
298
543
|
fix: "Check the linter (oxlint, biome, or eslint) is installed and its config is valid, then run it manually on the output."
|
|
299
544
|
},
|
|
300
|
-
[
|
|
545
|
+
[diagnosticCode.barrelDuplicateExport]: {
|
|
301
546
|
title: "Duplicate barrel export",
|
|
302
547
|
cause: "Two generated modules export the same name, so the barrel cannot re-export both.",
|
|
303
548
|
fix: "Rename one of the colliding declarations, or configure the plugin resolver to produce distinct names."
|
|
304
549
|
},
|
|
305
|
-
[
|
|
550
|
+
[diagnosticCode.performance]: {
|
|
306
551
|
title: "Performance",
|
|
307
552
|
cause: "Not a failure. Records a plugin’s elapsed time, summed into the run total.",
|
|
308
553
|
fix: "No action. This is an informational metric."
|
|
309
554
|
},
|
|
310
|
-
[
|
|
555
|
+
[diagnosticCode.updateAvailable]: {
|
|
311
556
|
title: "Update available",
|
|
312
557
|
cause: "A newer Kubb version is published on npm than the one running.",
|
|
313
558
|
fix: "Update the `@kubb/*` packages, for example `npm install -g @kubb/cli`, to get the latest fixes."
|
|
@@ -327,7 +572,7 @@ var Diagnostics = class Diagnostics {
|
|
|
327
572
|
/**
|
|
328
573
|
* The diagnostic code catalog, exposed as `Diagnostics.code` (e.g. `Diagnostics.code.refNotFound`).
|
|
329
574
|
*/
|
|
330
|
-
static code =
|
|
575
|
+
static code = diagnosticCode;
|
|
331
576
|
/**
|
|
332
577
|
* Type guard for a build {@link ProblemDiagnostic}.
|
|
333
578
|
*/
|
|
@@ -364,7 +609,9 @@ var Diagnostics = class Diagnostics {
|
|
|
364
609
|
*/
|
|
365
610
|
static isError(error) {
|
|
366
611
|
if (error instanceof Diagnostics.Error) return true;
|
|
367
|
-
|
|
612
|
+
if (!(error instanceof Error) || error.name !== "DiagnosticError") return false;
|
|
613
|
+
const { diagnostic } = error;
|
|
614
|
+
return typeof diagnostic === "object" && diagnostic !== null && typeof diagnostic.code === "string";
|
|
368
615
|
}
|
|
369
616
|
/**
|
|
370
617
|
* Runs `fn` with `sink` as the active diagnostic sink for the whole async
|
|
@@ -408,9 +655,9 @@ var Diagnostics = class Diagnostics {
|
|
|
408
655
|
current = current.cause;
|
|
409
656
|
}
|
|
410
657
|
return {
|
|
411
|
-
code:
|
|
658
|
+
code: diagnosticCode.unknown,
|
|
412
659
|
severity: "error",
|
|
413
|
-
message: root ? root.message :
|
|
660
|
+
message: root ? root.message : require_nodeCache.getErrorMessage(error),
|
|
414
661
|
cause: root
|
|
415
662
|
};
|
|
416
663
|
}
|
|
@@ -420,7 +667,7 @@ var Diagnostics = class Diagnostics {
|
|
|
420
667
|
static performance({ plugin, duration }) {
|
|
421
668
|
return {
|
|
422
669
|
kind: "performance",
|
|
423
|
-
code:
|
|
670
|
+
code: diagnosticCode.performance,
|
|
424
671
|
severity: "info",
|
|
425
672
|
message: `${plugin} generated in ${Math.round(duration)}ms`,
|
|
426
673
|
plugin,
|
|
@@ -433,7 +680,7 @@ var Diagnostics = class Diagnostics {
|
|
|
433
680
|
static update({ currentVersion, latestVersion }) {
|
|
434
681
|
return {
|
|
435
682
|
kind: "update",
|
|
436
|
-
code:
|
|
683
|
+
code: diagnosticCode.updateAvailable,
|
|
437
684
|
severity: "info",
|
|
438
685
|
message: `Update available: v${currentVersion} → v${latestVersion}. Run \`npm install -g @kubb/cli\` to update.`,
|
|
439
686
|
currentVersion,
|
|
@@ -452,35 +699,20 @@ var Diagnostics = class Diagnostics {
|
|
|
452
699
|
* that carry a `plugin`.
|
|
453
700
|
*/
|
|
454
701
|
static failedPlugins(diagnostics) {
|
|
455
|
-
|
|
456
|
-
for (const diagnostic of diagnostics) if (diagnostic.severity === "error" && diagnostic.plugin) names.add(diagnostic.plugin);
|
|
457
|
-
return [...names];
|
|
702
|
+
return [...new Set(diagnostics.flatMap((diagnostic) => diagnostic.severity === "error" && diagnostic.plugin ? [diagnostic.plugin] : []))];
|
|
458
703
|
}
|
|
459
704
|
/**
|
|
460
705
|
* Counts `problem` diagnostics by severity for the run summary. `performance` and
|
|
461
706
|
* `update` diagnostics are ignored.
|
|
462
707
|
*/
|
|
463
708
|
static count(diagnostics) {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
if (!isProblem(diagnostic)) continue;
|
|
469
|
-
if (diagnostic.severity === "error") {
|
|
470
|
-
errors += 1;
|
|
471
|
-
continue;
|
|
472
|
-
}
|
|
473
|
-
if (diagnostic.severity === "warning") {
|
|
474
|
-
warnings += 1;
|
|
475
|
-
continue;
|
|
476
|
-
}
|
|
477
|
-
infos += 1;
|
|
478
|
-
}
|
|
479
|
-
return {
|
|
480
|
-
errors,
|
|
481
|
-
warnings,
|
|
482
|
-
infos
|
|
709
|
+
const counts = {
|
|
710
|
+
errors: 0,
|
|
711
|
+
warnings: 0,
|
|
712
|
+
infos: 0
|
|
483
713
|
};
|
|
714
|
+
for (const diagnostic of diagnostics.filter(isProblem)) counts[countKey[diagnostic.severity]] += 1;
|
|
715
|
+
return counts;
|
|
484
716
|
}
|
|
485
717
|
/**
|
|
486
718
|
* Drops duplicate `problem` diagnostics that share a code, location pointer, and
|
|
@@ -532,7 +764,7 @@ var Diagnostics = class Diagnostics {
|
|
|
532
764
|
...problem?.location ? { location: problem.location } : {},
|
|
533
765
|
...problem?.help ? { help: problem.help } : {},
|
|
534
766
|
...problem?.plugin ? { plugin: problem.plugin } : {},
|
|
535
|
-
...diagnostic.code ===
|
|
767
|
+
...diagnostic.code === diagnosticCode.unknown ? {} : { docsUrl: Diagnostics.docsUrl(diagnostic.code) }
|
|
536
768
|
};
|
|
537
769
|
}
|
|
538
770
|
/**
|
|
@@ -552,7 +784,7 @@ var Diagnostics = class Diagnostics {
|
|
|
552
784
|
const details = [];
|
|
553
785
|
if (problem?.location && "pointer" in problem.location) details.push(` ${(0, node_util.styleText)("dim", "at:")} ${(0, node_util.styleText)("cyan", problem.location.pointer)}`);
|
|
554
786
|
if (problem?.help) details.push(` ${(0, node_util.styleText)("cyan", "fix:")} ${problem.help}`);
|
|
555
|
-
if (code !==
|
|
787
|
+
if (code !== diagnosticCode.unknown) details.push(` ${(0, node_util.styleText)("dim", "see:")} ${(0, node_util.styleText)("cyan", Diagnostics.docsUrl(code))}`);
|
|
556
788
|
return {
|
|
557
789
|
headline,
|
|
558
790
|
details
|
|
@@ -581,7 +813,7 @@ var Diagnostics = class Diagnostics {
|
|
|
581
813
|
function normalizeOutput({ output, group, pluginName }) {
|
|
582
814
|
const mode = output.mode ?? (node_path.default.extname(output.path) ? "file" : "directory");
|
|
583
815
|
if (mode === "file" && group) throw new Diagnostics.Error({
|
|
584
|
-
code:
|
|
816
|
+
code: Diagnostics.code.invalidPluginOptions,
|
|
585
817
|
severity: "error",
|
|
586
818
|
message: `Plugin "${pluginName}" resolves \`output.mode\` to 'file' but also configures a \`group\` option.`,
|
|
587
819
|
help: "A single-file output has nothing to group. Remove the `group` option, give `output.path` an extensionless directory name, or set `output.mode: 'directory'` explicitly.",
|
|
@@ -706,7 +938,7 @@ const resolverOptions = Symbol.for("@kubb/core/resolver/options");
|
|
|
706
938
|
/**
|
|
707
939
|
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
708
940
|
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
709
|
-
* every
|
|
941
|
+
* every segment, and joins with `/`.
|
|
710
942
|
*
|
|
711
943
|
* Empty segments are dropped before joining. They arise when the name starts with a dot
|
|
712
944
|
* followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to
|
|
@@ -715,16 +947,21 @@ const resolverOptions = Symbol.for("@kubb/core/resolver/options");
|
|
|
715
947
|
*
|
|
716
948
|
* @example Nested path from a dotted name
|
|
717
949
|
* `toFilePath('pet.petId') // 'pet/petId'`
|
|
718
|
-
*
|
|
719
|
-
* @example PascalCase the final segment
|
|
720
|
-
* `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`
|
|
721
|
-
*
|
|
722
|
-
* @example Suffix applied to the final segment only
|
|
723
|
-
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
724
950
|
*/
|
|
725
|
-
function toFilePath(name
|
|
726
|
-
|
|
727
|
-
|
|
951
|
+
function toFilePath(name) {
|
|
952
|
+
return name.split(/\.(?=[a-zA-Z])/).map((part) => require_nodeCache.camelCase(part)).filter(Boolean).join("/");
|
|
953
|
+
}
|
|
954
|
+
/** Throws `KUBB_PATH_TRAVERSAL` when `target` lies outside `parent`; `target === parent` stays allowed. */
|
|
955
|
+
function assertInside({ target, parent, message, help }) {
|
|
956
|
+
const parentWithSep = parent.endsWith(node_path.default.sep) ? parent : `${parent}${node_path.default.sep}`;
|
|
957
|
+
if (target === parent || target.startsWith(parentWithSep)) return;
|
|
958
|
+
throw new Diagnostics.Error({
|
|
959
|
+
code: Diagnostics.code.pathTraversal,
|
|
960
|
+
severity: "error",
|
|
961
|
+
message,
|
|
962
|
+
help,
|
|
963
|
+
location: { kind: "config" }
|
|
964
|
+
});
|
|
728
965
|
}
|
|
729
966
|
/**
|
|
730
967
|
* Built-in `file.baseName`: casts the identifier with `toFilePath` and appends the extension.
|
|
@@ -760,6 +997,8 @@ var Resolver = class Resolver {
|
|
|
760
997
|
static #patternCache = /* @__PURE__ */ new Map();
|
|
761
998
|
static #optionsCache = /* @__PURE__ */ new WeakMap();
|
|
762
999
|
pluginName;
|
|
1000
|
+
/** The built-in machinery, untouched by `name`/`file` overrides and built once since the driver reads it per node. */
|
|
1001
|
+
default;
|
|
763
1002
|
#options;
|
|
764
1003
|
#baseName;
|
|
765
1004
|
#filePath;
|
|
@@ -768,25 +1007,19 @@ var Resolver = class Resolver {
|
|
|
768
1007
|
this.#options = options;
|
|
769
1008
|
this.#baseName = options.file?.baseName ? options.file.baseName.bind(this) : toBaseName;
|
|
770
1009
|
this.#filePath = options.file?.path ? options.file.path.bind(this) : void 0;
|
|
771
|
-
this
|
|
772
|
-
|
|
773
|
-
/** Exposes the raw build options so `Resolver.merge` can read them across `@kubb/core` copies. */
|
|
774
|
-
get [resolverOptions]() {
|
|
775
|
-
return this.#options;
|
|
776
|
-
}
|
|
777
|
-
/**
|
|
778
|
-
* The built-in resolution machinery. Always reaches the untouched defaults, even when a
|
|
779
|
-
* plugin overrides the top-level `name` or `file`.
|
|
780
|
-
*/
|
|
781
|
-
get default() {
|
|
782
|
-
return {
|
|
783
|
-
name: require_usingCtx.camelCase,
|
|
1010
|
+
this.default = {
|
|
1011
|
+
name: require_nodeCache.camelCase,
|
|
784
1012
|
options: this.#resolveOptions.bind(this),
|
|
785
1013
|
path: this.#resolvePath.bind(this),
|
|
786
1014
|
file: this.#resolveFile.bind(this),
|
|
787
1015
|
banner: this.#resolveBanner.bind(this),
|
|
788
1016
|
footer: this.#resolveFooter.bind(this)
|
|
789
1017
|
};
|
|
1018
|
+
this.#apply(options);
|
|
1019
|
+
}
|
|
1020
|
+
/** Exposes the raw build options so `Resolver.merge` can read them across `@kubb/core` copies. */
|
|
1021
|
+
get [resolverOptions]() {
|
|
1022
|
+
return this.#options;
|
|
790
1023
|
}
|
|
791
1024
|
name(name) {
|
|
792
1025
|
return this.default.name(name);
|
|
@@ -933,9 +1166,9 @@ var Resolver = class Resolver {
|
|
|
933
1166
|
*/
|
|
934
1167
|
static #resolveGroupDir(group, groupValue) {
|
|
935
1168
|
if (group.name) return group.name({ group: groupValue });
|
|
936
|
-
if (group.type === "tag") return
|
|
1169
|
+
if (group.type === "tag") return require_nodeCache.camelCase(groupValue);
|
|
937
1170
|
const segment = groupValue.split("/").filter((part) => part !== "" && part !== "." && part !== "..")[0];
|
|
938
|
-
return segment ?
|
|
1171
|
+
return segment ? require_nodeCache.camelCase(segment) : "";
|
|
939
1172
|
}
|
|
940
1173
|
/**
|
|
941
1174
|
* `mode: 'file'` (default) resolves directly to `output.path`. `mode: 'directory'` resolves
|
|
@@ -946,13 +1179,11 @@ var Resolver = class Resolver {
|
|
|
946
1179
|
if (output.mode !== "directory") return node_path.default.resolve(root, output.path);
|
|
947
1180
|
const outputDir = node_path.default.resolve(root, output.path);
|
|
948
1181
|
const result = group && (groupPath || tag) ? node_path.default.resolve(outputDir, Resolver.#resolveGroupDir(group, group.type === "path" ? groupPath : tag), baseName) : node_path.default.resolve(outputDir, baseName);
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
severity: "error",
|
|
1182
|
+
assertInside({
|
|
1183
|
+
target: result,
|
|
1184
|
+
parent: outputDir,
|
|
953
1185
|
message: `Resolved path "${result}" is outside the output directory "${outputDir}".`,
|
|
954
|
-
help: "This can stem from a path traversal in the OpenAPI specification or a misconfigured `group.name` function. Keep generated paths within the output directory."
|
|
955
|
-
location: { kind: "config" }
|
|
1186
|
+
help: "This can stem from a path traversal in the OpenAPI specification or a misconfigured `group.name` function. Keep generated paths within the output directory."
|
|
956
1187
|
});
|
|
957
1188
|
return result;
|
|
958
1189
|
}
|
|
@@ -963,13 +1194,11 @@ var Resolver = class Resolver {
|
|
|
963
1194
|
*/
|
|
964
1195
|
#resolveOverridePath(filePath, root) {
|
|
965
1196
|
const resolved = node_path.default.resolve(root, filePath);
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
severity: "error",
|
|
1197
|
+
assertInside({
|
|
1198
|
+
target: resolved,
|
|
1199
|
+
parent: root,
|
|
970
1200
|
message: `Resolved path "${resolved}" is outside the project root "${root}".`,
|
|
971
|
-
help: "A resolver `file.path` must return a path inside the project root."
|
|
972
|
-
location: { kind: "config" }
|
|
1201
|
+
help: "A resolver `file.path` must return a path inside the project root."
|
|
973
1202
|
});
|
|
974
1203
|
return resolved;
|
|
975
1204
|
}
|
|
@@ -1194,21 +1423,27 @@ const ENFORCE_ORDER = {
|
|
|
1194
1423
|
pre: -1,
|
|
1195
1424
|
post: 1
|
|
1196
1425
|
};
|
|
1426
|
+
/** `include` filter types that select operations, and so prune the schemas they do not reach. */
|
|
1427
|
+
const OPERATION_FILTER_TYPES = /* @__PURE__ */ new Set([
|
|
1428
|
+
"tag",
|
|
1429
|
+
"operationId",
|
|
1430
|
+
"path",
|
|
1431
|
+
"method",
|
|
1432
|
+
"contentType"
|
|
1433
|
+
]);
|
|
1197
1434
|
const enforceWeight = (plugin) => plugin.enforce ? ENFORCE_ORDER[plugin.enforce] : 0;
|
|
1198
1435
|
/**
|
|
1199
1436
|
* The options bag a `NormalizedPlugin` starts with before a plugin refines it: a directory output
|
|
1200
1437
|
* at the plugin root and empty filter lists.
|
|
1201
1438
|
*/
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
};
|
|
1211
|
-
}
|
|
1439
|
+
const defaultPluginOptions = () => ({
|
|
1440
|
+
output: {
|
|
1441
|
+
path: ".",
|
|
1442
|
+
mode: "directory"
|
|
1443
|
+
},
|
|
1444
|
+
exclude: [],
|
|
1445
|
+
override: []
|
|
1446
|
+
});
|
|
1212
1447
|
/**
|
|
1213
1448
|
* Fills in the `output`, `exclude`, and `override` a `NormalizedPlugin` needs from a plugin's raw
|
|
1214
1449
|
* options, running `output` through `normalizeOutput`. Idempotent, so the driver can apply it after
|
|
@@ -1245,7 +1480,7 @@ var KubbDriver = class {
|
|
|
1245
1480
|
* Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
|
|
1246
1481
|
* add files. This property gives direct read/write access when needed.
|
|
1247
1482
|
*/
|
|
1248
|
-
fileManager = new
|
|
1483
|
+
fileManager = new require_nodeCache.FileManager();
|
|
1249
1484
|
plugins = /* @__PURE__ */ new Map();
|
|
1250
1485
|
/**
|
|
1251
1486
|
* Removers for every listener the driver added (plugin, generator) so `dispose()` can detach
|
|
@@ -1355,10 +1590,10 @@ var KubbDriver = class {
|
|
|
1355
1590
|
config: this.config,
|
|
1356
1591
|
options: plugin.options ?? {},
|
|
1357
1592
|
addGenerator: (...generators) => {
|
|
1358
|
-
|
|
1593
|
+
plugin.generators = [...plugin.generators ?? [], ...generators];
|
|
1359
1594
|
},
|
|
1360
1595
|
setResolver: (resolver) => {
|
|
1361
|
-
|
|
1596
|
+
plugin.resolver = Resolver.merge(createResolver({ pluginName: plugin.name }), resolver);
|
|
1362
1597
|
},
|
|
1363
1598
|
addMacro: (macro) => {
|
|
1364
1599
|
this.#transforms.add(plugin.name, macro);
|
|
@@ -1367,18 +1602,10 @@ var KubbDriver = class {
|
|
|
1367
1602
|
this.#transforms.set(plugin.name, macros);
|
|
1368
1603
|
},
|
|
1369
1604
|
setOptions: (opts) => {
|
|
1370
|
-
plugin.options = {
|
|
1605
|
+
plugin.options = normalizePluginOptions({
|
|
1371
1606
|
...plugin.options,
|
|
1372
1607
|
...opts
|
|
1373
|
-
};
|
|
1374
|
-
if (plugin.options.output) {
|
|
1375
|
-
const group = "group" in plugin.options ? plugin.options.group : void 0;
|
|
1376
|
-
plugin.options.output = normalizeOutput({
|
|
1377
|
-
output: plugin.options.output,
|
|
1378
|
-
group,
|
|
1379
|
-
pluginName: plugin.name
|
|
1380
|
-
});
|
|
1381
|
-
}
|
|
1608
|
+
}, plugin.name);
|
|
1382
1609
|
},
|
|
1383
1610
|
injectFile: (userFileNode) => {
|
|
1384
1611
|
this.fileManager.add(_kubb_ast.ast.factory.createFile(userFileNode));
|
|
@@ -1386,29 +1613,7 @@ var KubbDriver = class {
|
|
|
1386
1613
|
});
|
|
1387
1614
|
}
|
|
1388
1615
|
}
|
|
1389
|
-
/**
|
|
1390
|
-
* Appends a generator to its owning plugin so the generate loop can call it directly.
|
|
1391
|
-
*
|
|
1392
|
-
* The generator's `schema`, `operation`, and `operations` methods run per node during the AST
|
|
1393
|
-
* walk in `#runGenerators`, and their result is routed through `dispatch`. Because a generator is
|
|
1394
|
-
* bound to a plugin, generators from different plugins never cross-fire without a name check. The
|
|
1395
|
-
* renderer comes from `generator.renderer`; set it to `null` (or leave it unset) to opt out of
|
|
1396
|
-
* rendering.
|
|
1397
|
-
*
|
|
1398
|
-
* Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
|
|
1399
|
-
*/
|
|
1400
|
-
registerGenerator(pluginName, generator) {
|
|
1401
|
-
const plugin = this.plugins.get(pluginName);
|
|
1402
|
-
if (!plugin) return;
|
|
1403
|
-
plugin.generators = plugin.generators ? [...plugin.generators, generator] : [generator];
|
|
1404
|
-
}
|
|
1405
|
-
/**
|
|
1406
|
-
* Returns `true` when at least one generator was registered for the given plugin
|
|
1407
|
-
* via `addGenerator()` in `kubb:plugin:setup`.
|
|
1408
|
-
*
|
|
1409
|
-
* Used by the build loop to decide whether to walk the AST and run the generators
|
|
1410
|
-
* for a plugin.
|
|
1411
|
-
*/
|
|
1616
|
+
/** `true` once `addGenerator()` in `kubb:plugin:setup` registered a generator for the plugin. */
|
|
1412
1617
|
hasHookGenerators(pluginName) {
|
|
1413
1618
|
return (this.plugins.get(pluginName)?.generators?.length ?? 0) > 0;
|
|
1414
1619
|
}
|
|
@@ -1470,7 +1675,7 @@ var KubbDriver = class {
|
|
|
1470
1675
|
try {
|
|
1471
1676
|
await hooks.callHook("kubb:plugin:start", { plugin });
|
|
1472
1677
|
} catch (caughtError) {
|
|
1473
|
-
const error =
|
|
1678
|
+
const error = require_nodeCache.toError(caughtError);
|
|
1474
1679
|
const duration = getElapsedMs(hrStart);
|
|
1475
1680
|
await this.#emitPluginEnd({
|
|
1476
1681
|
plugin,
|
|
@@ -1591,7 +1796,7 @@ var KubbDriver = class {
|
|
|
1591
1796
|
const allowedSchemaNamesByPlugin = /* @__PURE__ */ new Map();
|
|
1592
1797
|
for (const { plugin } of entries) {
|
|
1593
1798
|
const { exclude, include, override } = plugin.options;
|
|
1594
|
-
if (!((include?.some(({ type }) =>
|
|
1799
|
+
if (!((include?.some(({ type }) => OPERATION_FILTER_TYPES.has(type)) ?? false) && !(include?.some(({ type }) => type === "schemaName") ?? false))) continue;
|
|
1595
1800
|
const resolver = this.getResolver(plugin.name);
|
|
1596
1801
|
const includedOps = operations.filter((operation) => resolver.default.options(operation, {
|
|
1597
1802
|
options: plugin.options,
|
|
@@ -1643,7 +1848,7 @@ var KubbDriver = class {
|
|
|
1643
1848
|
};
|
|
1644
1849
|
};
|
|
1645
1850
|
for (const node of schemas) {
|
|
1646
|
-
const cache =
|
|
1851
|
+
const cache = require_nodeCache.createNodeCache();
|
|
1647
1852
|
for (const state of states) {
|
|
1648
1853
|
if (state.error || !state.schemaGenerators.length) continue;
|
|
1649
1854
|
try {
|
|
@@ -1656,21 +1861,20 @@ var KubbDriver = class {
|
|
|
1656
1861
|
options,
|
|
1657
1862
|
cache
|
|
1658
1863
|
};
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
}
|
|
1864
|
+
await this.#dispatchNode({
|
|
1865
|
+
generators: state.schemaGenerators,
|
|
1866
|
+
node: transformedNode,
|
|
1867
|
+
ctx,
|
|
1868
|
+
generate: (generator) => generator.schema(transformedNode, ctx)
|
|
1869
|
+
});
|
|
1666
1870
|
await this.hooks.callHook("kubb:generate:schema", transformedNode, ctx);
|
|
1667
1871
|
} catch (caughtError) {
|
|
1668
|
-
state.error =
|
|
1872
|
+
state.error = require_nodeCache.toError(caughtError);
|
|
1669
1873
|
}
|
|
1670
1874
|
}
|
|
1671
1875
|
}
|
|
1672
1876
|
for (const node of operations) {
|
|
1673
|
-
const cache =
|
|
1877
|
+
const cache = require_nodeCache.createNodeCache();
|
|
1674
1878
|
for (const state of states) {
|
|
1675
1879
|
if (state.error || !state.operationGenerators.length && !state.operationsGenerators.length) continue;
|
|
1676
1880
|
try {
|
|
@@ -1678,22 +1882,22 @@ var KubbDriver = class {
|
|
|
1678
1882
|
if (!resolved) continue;
|
|
1679
1883
|
state.pluginOperations.push(resolved.transformedNode);
|
|
1680
1884
|
if (state.operationGenerators.length) {
|
|
1885
|
+
const { transformedNode } = resolved;
|
|
1681
1886
|
const ctx = {
|
|
1682
1887
|
...state.generatorContext,
|
|
1683
1888
|
options: resolved.options,
|
|
1684
1889
|
cache
|
|
1685
1890
|
};
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
await this.hooks.callHook("kubb:generate:operation", resolved.transformedNode, ctx);
|
|
1891
|
+
await this.#dispatchNode({
|
|
1892
|
+
generators: state.operationGenerators,
|
|
1893
|
+
node: transformedNode,
|
|
1894
|
+
ctx,
|
|
1895
|
+
generate: (generator) => generator.operation(transformedNode, ctx)
|
|
1896
|
+
});
|
|
1897
|
+
await this.hooks.callHook("kubb:generate:operation", transformedNode, ctx);
|
|
1694
1898
|
}
|
|
1695
1899
|
} catch (caughtError) {
|
|
1696
|
-
state.error =
|
|
1900
|
+
state.error = require_nodeCache.toError(caughtError);
|
|
1697
1901
|
}
|
|
1698
1902
|
}
|
|
1699
1903
|
}
|
|
@@ -1703,7 +1907,7 @@ var KubbDriver = class {
|
|
|
1703
1907
|
const ctx = {
|
|
1704
1908
|
...state.generatorContext,
|
|
1705
1909
|
options: state.plugin.options,
|
|
1706
|
-
cache:
|
|
1910
|
+
cache: require_nodeCache.createNodeCache()
|
|
1707
1911
|
};
|
|
1708
1912
|
for (const generator of state.operationsGenerators) await this.dispatch({
|
|
1709
1913
|
result: await generator.operations(state.pluginOperations, ctx),
|
|
@@ -1711,7 +1915,7 @@ var KubbDriver = class {
|
|
|
1711
1915
|
});
|
|
1712
1916
|
await this.hooks.callHook("kubb:generate:operations", state.pluginOperations, ctx);
|
|
1713
1917
|
} catch (caughtError) {
|
|
1714
|
-
state.error =
|
|
1918
|
+
state.error = require_nodeCache.toError(caughtError);
|
|
1715
1919
|
}
|
|
1716
1920
|
}
|
|
1717
1921
|
for (const state of states) {
|
|
@@ -1733,36 +1937,20 @@ var KubbDriver = class {
|
|
|
1733
1937
|
}
|
|
1734
1938
|
return diagnostics;
|
|
1735
1939
|
}
|
|
1736
|
-
/**
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
*
|
|
1745
|
-
* Pass `renderer` when the result may be a renderer element. Generators that only return
|
|
1746
|
-
* `Array<FileNode>` do not need one.
|
|
1747
|
-
*/
|
|
1748
|
-
async dispatch({ result, renderer }) {
|
|
1749
|
-
try {
|
|
1750
|
-
var _usingCtx$2 = require_usingCtx._usingCtx();
|
|
1751
|
-
if (!result) return;
|
|
1752
|
-
if (Array.isArray(result)) {
|
|
1753
|
-
this.fileManager.upsert(...result);
|
|
1754
|
-
return;
|
|
1755
|
-
}
|
|
1756
|
-
if (!renderer) return;
|
|
1757
|
-
const instance = _usingCtx$2.u(renderer());
|
|
1758
|
-
await instance.render(result);
|
|
1759
|
-
this.fileManager.upsert(...instance.files);
|
|
1760
|
-
} catch (_) {
|
|
1761
|
-
_usingCtx$2.e = _;
|
|
1762
|
-
} finally {
|
|
1763
|
-
_usingCtx$2.d();
|
|
1940
|
+
/** Runs `node` through the generators whose `match` accepts it; `generate` picks the method for the node kind. */
|
|
1941
|
+
async #dispatchNode({ generators, node, ctx, generate }) {
|
|
1942
|
+
for (const generator of generators) {
|
|
1943
|
+
if (!(generator.match ? await generator.match(node, ctx) : true)) continue;
|
|
1944
|
+
await this.dispatch({
|
|
1945
|
+
result: await generate(generator),
|
|
1946
|
+
renderer: generator.renderer
|
|
1947
|
+
});
|
|
1764
1948
|
}
|
|
1765
1949
|
}
|
|
1950
|
+
/** Stores whatever a generator method or `kubb:generate:*` hook returned, see {@link FileManager.dispatch}. */
|
|
1951
|
+
dispatch(params) {
|
|
1952
|
+
return this.fileManager.dispatch(params);
|
|
1953
|
+
}
|
|
1766
1954
|
/**
|
|
1767
1955
|
* Removes every listener the driver added. Listeners attached directly to `hooks` from outside
|
|
1768
1956
|
* the driver survive. Called at the end of a build to prevent leaks across repeated builds.
|
|
@@ -1780,15 +1968,6 @@ var KubbDriver = class {
|
|
|
1780
1968
|
[Symbol.dispose]() {
|
|
1781
1969
|
this.dispose();
|
|
1782
1970
|
}
|
|
1783
|
-
/**
|
|
1784
|
-
* Merges `partial` onto a fresh default resolver and stores the result on `plugin.resolver`,
|
|
1785
|
-
* which is the single source `getResolver` and `getPlugin(name).resolver` both read.
|
|
1786
|
-
*/
|
|
1787
|
-
setPluginResolver(pluginName, partial) {
|
|
1788
|
-
const plugin = this.plugins.get(pluginName);
|
|
1789
|
-
if (!plugin) return;
|
|
1790
|
-
plugin.resolver = Resolver.merge(createResolver({ pluginName }), partial);
|
|
1791
|
-
}
|
|
1792
1971
|
getResolver(pluginName) {
|
|
1793
1972
|
return this.plugins.get(pluginName)?.resolver ?? createResolver({ pluginName });
|
|
1794
1973
|
}
|
|
@@ -1916,7 +2095,7 @@ async function createOutputManifest({ storage, cache }) {
|
|
|
1916
2095
|
async commit() {
|
|
1917
2096
|
try {
|
|
1918
2097
|
const next = { ...entries };
|
|
1919
|
-
await
|
|
2098
|
+
await require_nodeCache.inParallel({
|
|
1920
2099
|
items: [...tracked],
|
|
1921
2100
|
limit: 50,
|
|
1922
2101
|
run: async ([key, source]) => {
|
|
@@ -1981,29 +2160,6 @@ function createStorage(build) {
|
|
|
1981
2160
|
}
|
|
1982
2161
|
//#endregion
|
|
1983
2162
|
//#region src/storages/fsStorage.ts
|
|
1984
|
-
const WRITE_CONCURRENCY = 50;
|
|
1985
|
-
function createLimiter(concurrency) {
|
|
1986
|
-
let active = 0;
|
|
1987
|
-
const queue = [];
|
|
1988
|
-
function next() {
|
|
1989
|
-
if (active >= concurrency) return;
|
|
1990
|
-
const run = queue.shift();
|
|
1991
|
-
if (!run) return;
|
|
1992
|
-
active++;
|
|
1993
|
-
run();
|
|
1994
|
-
}
|
|
1995
|
-
return function limit(task) {
|
|
1996
|
-
return new Promise((resolve, reject) => {
|
|
1997
|
-
queue.push(() => {
|
|
1998
|
-
task().then(resolve, reject).finally(() => {
|
|
1999
|
-
active--;
|
|
2000
|
-
next();
|
|
2001
|
-
});
|
|
2002
|
-
});
|
|
2003
|
-
next();
|
|
2004
|
-
});
|
|
2005
|
-
};
|
|
2006
|
-
}
|
|
2007
2163
|
/**
|
|
2008
2164
|
* Built-in filesystem storage driver.
|
|
2009
2165
|
*
|
|
@@ -2017,8 +2173,7 @@ function createLimiter(concurrency) {
|
|
|
2017
2173
|
* a formatter left behind
|
|
2018
2174
|
* - missing parent directories are created automatically
|
|
2019
2175
|
* - Bun's native file API is used when running under Bun
|
|
2020
|
-
* -
|
|
2021
|
-
* can fire every file's write without pacing itself
|
|
2176
|
+
* - `FileManager.write` bounds in-flight writes, so this driver does not pace itself
|
|
2022
2177
|
*
|
|
2023
2178
|
* @example
|
|
2024
2179
|
* ```ts
|
|
@@ -2032,51 +2187,43 @@ function createLimiter(concurrency) {
|
|
|
2032
2187
|
* })
|
|
2033
2188
|
* ```
|
|
2034
2189
|
*/
|
|
2035
|
-
const fsStorage = createStorage(() => {
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
}
|
|
2046
|
-
},
|
|
2047
|
-
async readItem(key) {
|
|
2048
|
-
try {
|
|
2049
|
-
return await (0, node_fs_promises.readFile)((0, node_path.resolve)(key), "utf8");
|
|
2050
|
-
} catch (_error) {
|
|
2051
|
-
return null;
|
|
2052
|
-
}
|
|
2053
|
-
},
|
|
2054
|
-
async writeItem(key, value, options) {
|
|
2055
|
-
await limit(() => require_usingCtx.write((0, node_path.resolve)(key), value, {
|
|
2056
|
-
sanity: false,
|
|
2057
|
-
stored: options?.stored
|
|
2058
|
-
}));
|
|
2059
|
-
},
|
|
2060
|
-
async removeItem(key) {
|
|
2061
|
-
await (0, node_fs_promises.rm)((0, node_path.resolve)(key), { force: true });
|
|
2062
|
-
},
|
|
2063
|
-
async readKeys(base) {
|
|
2064
|
-
const resolvedBase = (0, node_path.resolve)(base ?? process.cwd());
|
|
2065
|
-
const keys = [];
|
|
2066
|
-
try {
|
|
2067
|
-
for await (const entry of (0, node_fs_promises.glob)("**/*", {
|
|
2068
|
-
cwd: resolvedBase,
|
|
2069
|
-
withFileTypes: true
|
|
2070
|
-
})) if (entry.isFile()) keys.push(require_usingCtx.toPosixPath((0, node_path.relative)(resolvedBase, (0, node_path.join)(entry.parentPath, entry.name))));
|
|
2071
|
-
} catch (_error) {}
|
|
2072
|
-
return keys;
|
|
2073
|
-
},
|
|
2074
|
-
async empty(base) {
|
|
2075
|
-
if (!base) return;
|
|
2076
|
-
await require_usingCtx.clean((0, node_path.resolve)(base));
|
|
2190
|
+
const fsStorage = createStorage(() => ({
|
|
2191
|
+
name: "fs",
|
|
2192
|
+
async existsItem(key) {
|
|
2193
|
+
return require_nodeCache.exists((0, node_path.resolve)(key));
|
|
2194
|
+
},
|
|
2195
|
+
async readItem(key) {
|
|
2196
|
+
try {
|
|
2197
|
+
return await (0, node_fs_promises.readFile)((0, node_path.resolve)(key), "utf8");
|
|
2198
|
+
} catch (_error) {
|
|
2199
|
+
return null;
|
|
2077
2200
|
}
|
|
2078
|
-
}
|
|
2079
|
-
|
|
2201
|
+
},
|
|
2202
|
+
async writeItem(key, value, options) {
|
|
2203
|
+
await require_nodeCache.write((0, node_path.resolve)(key), value, {
|
|
2204
|
+
sanity: false,
|
|
2205
|
+
stored: options?.stored
|
|
2206
|
+
});
|
|
2207
|
+
},
|
|
2208
|
+
async removeItem(key) {
|
|
2209
|
+
await (0, node_fs_promises.rm)((0, node_path.resolve)(key), { force: true });
|
|
2210
|
+
},
|
|
2211
|
+
async readKeys(base) {
|
|
2212
|
+
const resolvedBase = (0, node_path.resolve)(base ?? process.cwd());
|
|
2213
|
+
const keys = [];
|
|
2214
|
+
try {
|
|
2215
|
+
for await (const entry of (0, node_fs_promises.glob)("**/*", {
|
|
2216
|
+
cwd: resolvedBase,
|
|
2217
|
+
withFileTypes: true
|
|
2218
|
+
})) if (entry.isFile()) keys.push(require_nodeCache.toPosixPath((0, node_path.relative)(resolvedBase, (0, node_path.join)(entry.parentPath, entry.name))));
|
|
2219
|
+
} catch (_error) {}
|
|
2220
|
+
return keys;
|
|
2221
|
+
},
|
|
2222
|
+
async empty(base) {
|
|
2223
|
+
if (!base) return;
|
|
2224
|
+
await require_nodeCache.clean((0, node_path.resolve)(base));
|
|
2225
|
+
}
|
|
2226
|
+
}));
|
|
2080
2227
|
//#endregion
|
|
2081
2228
|
//#region src/storages/cacheStorage.ts
|
|
2082
2229
|
/**
|
|
@@ -2132,7 +2279,202 @@ const cacheStorage = createStorage(({ root = process.cwd() }) => {
|
|
|
2132
2279
|
};
|
|
2133
2280
|
});
|
|
2134
2281
|
//#endregion
|
|
2282
|
+
//#region src/output/runHook.ts
|
|
2283
|
+
const KILL_GRACE_MS = 2e3;
|
|
2284
|
+
async function settle(proc, hooks, id, commandWithArgs) {
|
|
2285
|
+
let childError = null;
|
|
2286
|
+
let lineError = null;
|
|
2287
|
+
let lines = Promise.resolve();
|
|
2288
|
+
let killTimer;
|
|
2289
|
+
proc.process?.once("error", (error) => {
|
|
2290
|
+
childError = error;
|
|
2291
|
+
killTimer = setTimeout(() => proc.kill("SIGKILL"), KILL_GRACE_MS).unref();
|
|
2292
|
+
});
|
|
2293
|
+
if (hooks.listenerCount("kubb:hook:line") > 0) for (const input of [proc.process?.stdout, proc.process?.stderr]) {
|
|
2294
|
+
if (!input) continue;
|
|
2295
|
+
(0, node_readline.createInterface)({ input }).on("line", (line) => {
|
|
2296
|
+
lines = lines.then(() => hooks.callHook("kubb:hook:line", {
|
|
2297
|
+
id,
|
|
2298
|
+
line
|
|
2299
|
+
})).catch((error) => {
|
|
2300
|
+
lineError ??= require_nodeCache.toError(error);
|
|
2301
|
+
});
|
|
2302
|
+
});
|
|
2303
|
+
}
|
|
2304
|
+
const output = await Promise.resolve(proc).catch(() => null);
|
|
2305
|
+
clearTimeout(killTimer);
|
|
2306
|
+
await lines;
|
|
2307
|
+
const error = childError ?? lineError;
|
|
2308
|
+
if (error) return {
|
|
2309
|
+
success: false,
|
|
2310
|
+
error
|
|
2311
|
+
};
|
|
2312
|
+
if (output?.exitCode === 0) return {
|
|
2313
|
+
success: true,
|
|
2314
|
+
error: null
|
|
2315
|
+
};
|
|
2316
|
+
return {
|
|
2317
|
+
success: false,
|
|
2318
|
+
error: /* @__PURE__ */ new Error(`Hook execute failed: ${commandWithArgs}`),
|
|
2319
|
+
stdout: output?.stdout,
|
|
2320
|
+
stderr: output?.stderr
|
|
2321
|
+
};
|
|
2322
|
+
}
|
|
2323
|
+
/**
|
|
2324
|
+
* Spawns a command and returns its outcome, announcing it through `kubb:hook:start`,
|
|
2325
|
+
* `kubb:hook:line` (one per stdout or stderr line, only while a listener is attached) and
|
|
2326
|
+
* `kubb:hook:end`. A non-zero exit, a spawn failure, an abort or a throwing line listener returns
|
|
2327
|
+
* `success: false` instead of throwing. The failure travels on the result and `kubb:hook:end`
|
|
2328
|
+
* only, and the caller emits `kubb:success` or a diagnostic, so nothing is reported twice.
|
|
2329
|
+
*/
|
|
2330
|
+
async function runHook({ hooks, command, args = [], name, cwd = node_process.default.cwd(), signal }) {
|
|
2331
|
+
const id = (0, node_crypto.randomUUID)();
|
|
2332
|
+
await hooks.callHook("kubb:hook:start", {
|
|
2333
|
+
id,
|
|
2334
|
+
command,
|
|
2335
|
+
name,
|
|
2336
|
+
args
|
|
2337
|
+
});
|
|
2338
|
+
const result = await settle((0, tinyexec.x)(command, [...args], {
|
|
2339
|
+
signal,
|
|
2340
|
+
throwOnError: false,
|
|
2341
|
+
nodeOptions: {
|
|
2342
|
+
cwd,
|
|
2343
|
+
windowsHide: true,
|
|
2344
|
+
stdio: [
|
|
2345
|
+
"ignore",
|
|
2346
|
+
"pipe",
|
|
2347
|
+
"pipe"
|
|
2348
|
+
],
|
|
2349
|
+
detached: node_process.default.platform !== "win32"
|
|
2350
|
+
}
|
|
2351
|
+
}), hooks, id, [command, ...args].join(" "));
|
|
2352
|
+
await hooks.callHook("kubb:hook:end", {
|
|
2353
|
+
id,
|
|
2354
|
+
command,
|
|
2355
|
+
name,
|
|
2356
|
+
args,
|
|
2357
|
+
...result
|
|
2358
|
+
});
|
|
2359
|
+
return result;
|
|
2360
|
+
}
|
|
2361
|
+
//#endregion
|
|
2362
|
+
//#region src/output/runOutputPasses.ts
|
|
2363
|
+
/**
|
|
2364
|
+
* Format and lint are one pass over the output directory that differs only in these values.
|
|
2365
|
+
* `label` and `verb` are spelled out: deriving them from `kind` gives "lintter" and "lintting".
|
|
2366
|
+
*/
|
|
2367
|
+
const TOOL_PASSES = [{
|
|
2368
|
+
kind: "format",
|
|
2369
|
+
label: "formatter",
|
|
2370
|
+
verb: "Formatting",
|
|
2371
|
+
tools: formatters,
|
|
2372
|
+
preference: FORMATTER_PREFERENCE,
|
|
2373
|
+
code: Diagnostics.code.formatFailed
|
|
2374
|
+
}, {
|
|
2375
|
+
kind: "lint",
|
|
2376
|
+
label: "linter",
|
|
2377
|
+
verb: "Linting",
|
|
2378
|
+
tools: linters,
|
|
2379
|
+
preference: LINTER_PREFERENCE,
|
|
2380
|
+
code: Diagnostics.code.lintFailed
|
|
2381
|
+
}];
|
|
2382
|
+
/**
|
|
2383
|
+
* Resolves `auto` to an installed tool and runs it. Returns the failure instead of throwing.
|
|
2384
|
+
*/
|
|
2385
|
+
async function runToolPass({ pass, setting, outputPath, hooks, cwd, signal }) {
|
|
2386
|
+
const detected = setting === "auto" ? await detectTool(pass.preference, cwd) : setting;
|
|
2387
|
+
if (!detected) {
|
|
2388
|
+
await hooks.callHook("kubb:warn", { message: `No ${pass.label} found (${pass.preference.join(", ")}). Skipping ${pass.verb.toLowerCase()}.` });
|
|
2389
|
+
return null;
|
|
2390
|
+
}
|
|
2391
|
+
if (setting === "auto") await hooks.callHook("kubb:info", { message: `Auto-detected ${pass.label}: ${(0, node_util.styleText)("dim", detected)}` });
|
|
2392
|
+
const tool = pass.tools[detected];
|
|
2393
|
+
if (!tool || !(0, node_fs.existsSync)(outputPath)) return null;
|
|
2394
|
+
const result = await runHook({
|
|
2395
|
+
hooks,
|
|
2396
|
+
command: tool.command,
|
|
2397
|
+
args: tool.args(outputPath),
|
|
2398
|
+
cwd,
|
|
2399
|
+
signal
|
|
2400
|
+
});
|
|
2401
|
+
if (result.success) {
|
|
2402
|
+
await hooks.callHook("kubb:success", { message: `${pass.verb} with ${(0, node_util.styleText)("dim", detected)} on ${(0, node_util.styleText)("dim", outputPath)} successfully` });
|
|
2403
|
+
return null;
|
|
2404
|
+
}
|
|
2405
|
+
return result.error;
|
|
2406
|
+
}
|
|
2407
|
+
/**
|
|
2408
|
+
* Runs `output.format`, `output.lint` and `output.postGenerate` over the generated output and
|
|
2409
|
+
* returns the diagnostics they produced. Each failure is emitted through `Diagnostics.emit` and
|
|
2410
|
+
* returned, so it reaches the summary, the reporters and the exit code; the passes keep going so
|
|
2411
|
+
* one failing tool does not hide the next. `Kubb.generate()` runs this after an error-free build
|
|
2412
|
+
* unless the host passes its own `processOutput`.
|
|
2413
|
+
*
|
|
2414
|
+
* @example
|
|
2415
|
+
* ```ts
|
|
2416
|
+
* const diagnostics = await runOutputPasses({ config, outputPath: '/repo/src/gen', hooks })
|
|
2417
|
+
* ```
|
|
2418
|
+
*/
|
|
2419
|
+
async function runOutputPasses({ config, outputPath, hooks, signal }) {
|
|
2420
|
+
const diagnostics = [];
|
|
2421
|
+
const report = async (code, label, error) => {
|
|
2422
|
+
const diagnostic = {
|
|
2423
|
+
code,
|
|
2424
|
+
severity: "error",
|
|
2425
|
+
message: `${label} failed: ${error.message}`,
|
|
2426
|
+
help: "Check that the tool is installed and that the command and its config are correct.",
|
|
2427
|
+
location: { kind: "config" },
|
|
2428
|
+
cause: error
|
|
2429
|
+
};
|
|
2430
|
+
diagnostics.push(diagnostic);
|
|
2431
|
+
await Diagnostics.emit(hooks, diagnostic);
|
|
2432
|
+
};
|
|
2433
|
+
for (const pass of TOOL_PASSES) {
|
|
2434
|
+
const setting = config.output[pass.kind];
|
|
2435
|
+
if (!setting) continue;
|
|
2436
|
+
await hooks.callHook(`kubb:${pass.kind}:start`);
|
|
2437
|
+
const error = await runToolPass({
|
|
2438
|
+
pass,
|
|
2439
|
+
setting,
|
|
2440
|
+
outputPath,
|
|
2441
|
+
hooks,
|
|
2442
|
+
cwd: config.root,
|
|
2443
|
+
signal
|
|
2444
|
+
});
|
|
2445
|
+
if (error) await report(pass.code, pass.label, error);
|
|
2446
|
+
await hooks.callHook(`kubb:${pass.kind}:end`);
|
|
2447
|
+
signal?.throwIfAborted();
|
|
2448
|
+
}
|
|
2449
|
+
const commands = config.output.postGenerate ?? [];
|
|
2450
|
+
if (commands.length === 0) return diagnostics;
|
|
2451
|
+
await hooks.callHook("kubb:hooks:start");
|
|
2452
|
+
for (const entry of commands) {
|
|
2453
|
+
const { command, name } = typeof entry === "string" ? {
|
|
2454
|
+
command: entry,
|
|
2455
|
+
name: void 0
|
|
2456
|
+
} : entry;
|
|
2457
|
+
const [executable, ...args] = tokenize(command);
|
|
2458
|
+
if (!executable) continue;
|
|
2459
|
+
const result = await runHook({
|
|
2460
|
+
hooks,
|
|
2461
|
+
command: executable,
|
|
2462
|
+
args,
|
|
2463
|
+
name,
|
|
2464
|
+
cwd: config.root,
|
|
2465
|
+
signal
|
|
2466
|
+
});
|
|
2467
|
+
if (result.success) await hooks.callHook("kubb:success", { message: `${(0, node_util.styleText)("dim", name ?? command)} successfully executed` });
|
|
2468
|
+
if (!result.success) await report(Diagnostics.code.postGenerateFailed, "Post-generate command", result.error);
|
|
2469
|
+
signal?.throwIfAborted();
|
|
2470
|
+
}
|
|
2471
|
+
await hooks.callHook("kubb:hooks:end");
|
|
2472
|
+
return diagnostics;
|
|
2473
|
+
}
|
|
2474
|
+
//#endregion
|
|
2135
2475
|
//#region src/createKubb.ts
|
|
2476
|
+
/** Upper bound of listeners one plugin adds to a hook, sizing the emitter ceiling for large plugin sets. */
|
|
2477
|
+
const HOOK_LISTENERS_PER_PLUGIN = 4;
|
|
2136
2478
|
function resolveConfig(userConfig) {
|
|
2137
2479
|
return {
|
|
2138
2480
|
...userConfig,
|
|
@@ -2150,11 +2492,18 @@ function resolveConfig(userConfig) {
|
|
|
2150
2492
|
};
|
|
2151
2493
|
}
|
|
2152
2494
|
/**
|
|
2495
|
+
* Only the fs storage writes `output.path`, so only then do the passes have fresh files to run
|
|
2496
|
+
* over; a memory or custom storage would leave them formatting whatever an earlier run left there.
|
|
2497
|
+
*/
|
|
2498
|
+
function writesOutputDirectory(storage) {
|
|
2499
|
+
return storage.name === "fs";
|
|
2500
|
+
}
|
|
2501
|
+
/**
|
|
2153
2502
|
* Whether anything runs over the output directory after the files are written. Only then can the
|
|
2154
2503
|
* bytes on disk stop matching what Kubb wrote, which is what the manifest exists to track.
|
|
2155
2504
|
*/
|
|
2156
|
-
function hasOutputPasses(output) {
|
|
2157
|
-
return Boolean(output.format || output.lint || output.postGenerate?.length);
|
|
2505
|
+
function hasOutputPasses({ output, storage }) {
|
|
2506
|
+
return writesOutputDirectory(storage) && Boolean(output.format || output.lint || output.postGenerate?.length);
|
|
2158
2507
|
}
|
|
2159
2508
|
/**
|
|
2160
2509
|
* Kubb code-generation instance bound to a single config entry. Resolves the user
|
|
@@ -2177,17 +2526,15 @@ var Kubb = class {
|
|
|
2177
2526
|
hooks;
|
|
2178
2527
|
config;
|
|
2179
2528
|
#driver = null;
|
|
2180
|
-
#storage = null;
|
|
2181
2529
|
#manifest = null;
|
|
2182
2530
|
#signal;
|
|
2183
2531
|
constructor(userConfig, options = {}) {
|
|
2184
2532
|
this.config = resolveConfig(userConfig);
|
|
2185
|
-
this.hooks = options.hooks ?? new
|
|
2533
|
+
this.hooks = options.hooks ?? new require_nodeCache.Hookable();
|
|
2186
2534
|
this.#signal = options.signal;
|
|
2187
2535
|
}
|
|
2188
2536
|
get storage() {
|
|
2189
|
-
|
|
2190
|
-
return this.#storage;
|
|
2537
|
+
return this.config.storage;
|
|
2191
2538
|
}
|
|
2192
2539
|
get driver() {
|
|
2193
2540
|
if (!this.#driver) throw new Error("[kubb] setup() must be called before accessing driver");
|
|
@@ -2200,7 +2547,7 @@ var Kubb = class {
|
|
|
2200
2547
|
const signal = this.#signal;
|
|
2201
2548
|
signal?.throwIfAborted();
|
|
2202
2549
|
const config = this.config;
|
|
2203
|
-
const manifest = hasOutputPasses(config
|
|
2550
|
+
const manifest = hasOutputPasses(config) ? await createOutputManifest({
|
|
2204
2551
|
storage: config.storage,
|
|
2205
2552
|
cache: cacheStorage({ root: config.root })
|
|
2206
2553
|
}) : void 0;
|
|
@@ -2209,10 +2556,10 @@ var Kubb = class {
|
|
|
2209
2556
|
manifest,
|
|
2210
2557
|
signal
|
|
2211
2558
|
});
|
|
2212
|
-
this.hooks.setMaxListeners(Math.max(10, config.plugins.length *
|
|
2559
|
+
this.hooks.setMaxListeners(Math.max(10, config.plugins.length * HOOK_LISTENERS_PER_PLUGIN));
|
|
2213
2560
|
if (config.output.clean) {
|
|
2214
2561
|
const cleanPath = (0, node_path.resolve)(config.root, config.output.path);
|
|
2215
|
-
if (
|
|
2562
|
+
if (require_nodeCache.isPathInside(config.root, cleanPath)) throw new Diagnostics.Error({
|
|
2216
2563
|
code: Diagnostics.code.cleanRoot,
|
|
2217
2564
|
severity: "error",
|
|
2218
2565
|
message: `output.clean cannot delete "${cleanPath}" because it is the project root or a parent of it.`,
|
|
@@ -2223,7 +2570,6 @@ var Kubb = class {
|
|
|
2223
2570
|
}
|
|
2224
2571
|
await driver.setup();
|
|
2225
2572
|
this.#driver = driver;
|
|
2226
|
-
this.#storage = config.storage;
|
|
2227
2573
|
this.#manifest = manifest ?? null;
|
|
2228
2574
|
}
|
|
2229
2575
|
/**
|
|
@@ -2245,7 +2591,7 @@ var Kubb = class {
|
|
|
2245
2591
|
*/
|
|
2246
2592
|
async safeBuild() {
|
|
2247
2593
|
try {
|
|
2248
|
-
var _usingCtx$1 =
|
|
2594
|
+
var _usingCtx$1 = require_nodeCache._usingCtx();
|
|
2249
2595
|
this.#signal?.throwIfAborted();
|
|
2250
2596
|
if (!this.#driver) await this.setup();
|
|
2251
2597
|
const self = _usingCtx$1.u(this);
|
|
@@ -2310,9 +2656,12 @@ var Kubb = class {
|
|
|
2310
2656
|
diagnostics
|
|
2311
2657
|
};
|
|
2312
2658
|
}
|
|
2313
|
-
const
|
|
2659
|
+
const processOutput = options.processOutput ?? runOutputPasses;
|
|
2660
|
+
const outputDiagnostics = writesOutputDirectory(config.storage) ? await processOutput({
|
|
2314
2661
|
config,
|
|
2315
|
-
outputPath: (0, node_path.resolve)(config.root, config.output.path)
|
|
2662
|
+
outputPath: (0, node_path.resolve)(config.root, config.output.path),
|
|
2663
|
+
hooks,
|
|
2664
|
+
signal
|
|
2316
2665
|
}) : [];
|
|
2317
2666
|
const finalDiagnostics = [...diagnostics, ...outputDiagnostics];
|
|
2318
2667
|
const failed = Diagnostics.hasError(outputDiagnostics);
|
|
@@ -2553,6 +2902,44 @@ function createCliReporter({ render = consoleRenderer } = {}) {
|
|
|
2553
2902
|
*/
|
|
2554
2903
|
const cliReporter = createCliReporter();
|
|
2555
2904
|
//#endregion
|
|
2905
|
+
//#region src/storages/kubbStorage.ts
|
|
2906
|
+
function ensurePrivateDir(dir) {
|
|
2907
|
+
(0, node_fs.mkdirSync)(dir, {
|
|
2908
|
+
recursive: true,
|
|
2909
|
+
mode: 448
|
|
2910
|
+
});
|
|
2911
|
+
(0, node_fs.chmodSync)(dir, 448);
|
|
2912
|
+
return dir;
|
|
2913
|
+
}
|
|
2914
|
+
/**
|
|
2915
|
+
* Folder Kubb keeps its own files in, created with mode 0700. `base` resolves inside `KUBB_HOME`
|
|
2916
|
+
* (default `~/.kubb`). When that folder is not writable, it falls back to a folder in the OS temp directory.
|
|
2917
|
+
*
|
|
2918
|
+
* @example
|
|
2919
|
+
* `resolveKubbDir('cache') // '/home/me/.kubb/cache'`
|
|
2920
|
+
*/
|
|
2921
|
+
function resolveKubbDir(base = "") {
|
|
2922
|
+
const dir = (0, node_path.resolve)(node_process.default.env.KUBB_HOME ?? (0, node_path.join)((0, node_os.homedir)(), ".kubb"), base);
|
|
2923
|
+
try {
|
|
2924
|
+
return ensurePrivateDir(dir);
|
|
2925
|
+
} catch {
|
|
2926
|
+
return ensurePrivateDir((0, node_path.join)((0, node_os.tmpdir)(), "kubb", (0, node_crypto.hash)("sha256", dir).slice(0, 16)));
|
|
2927
|
+
}
|
|
2928
|
+
}
|
|
2929
|
+
/**
|
|
2930
|
+
* unstorage on the fs driver, rooted at {@link resolveKubbDir}. The fs driver cannot set a file
|
|
2931
|
+
* mode, so the 0700 folder keeps files such as a stored token private.
|
|
2932
|
+
*
|
|
2933
|
+
* @example
|
|
2934
|
+
* ```ts
|
|
2935
|
+
* const storage = kubbStorage({ base: 'cache' })
|
|
2936
|
+
* await storage.setItem('machine-secret', secret)
|
|
2937
|
+
* ```
|
|
2938
|
+
*/
|
|
2939
|
+
function kubbStorage({ base } = {}) {
|
|
2940
|
+
return (0, unstorage.createStorage)({ driver: (0, unstorage_drivers_fs.default)({ base: resolveKubbDir(base) }) });
|
|
2941
|
+
}
|
|
2942
|
+
//#endregion
|
|
2556
2943
|
//#region src/reporters/fileReporter.ts
|
|
2557
2944
|
/**
|
|
2558
2945
|
* Builds the `## Summary` section: the same counts the cli and json reporters expose, as a list of
|
|
@@ -2629,12 +3016,12 @@ const fileReporter = createReporter({
|
|
|
2629
3016
|
const content = (0, node_util.stripVTControlCharacters)([header, ...sections.map((section) => section.join("\n"))].join("\n\n"));
|
|
2630
3017
|
const baseName = `${[
|
|
2631
3018
|
"kubb",
|
|
2632
|
-
config.name,
|
|
3019
|
+
config.name?.replace(/[^a-zA-Z0-9_-]+/g, "-").replace(/^-+|-+$/g, ""),
|
|
2633
3020
|
Date.now()
|
|
2634
3021
|
].filter(Boolean).join("-")}.log`;
|
|
2635
|
-
const
|
|
2636
|
-
await
|
|
2637
|
-
console.error(`Debug log written to ${(0, node_path.relative)(node_process.default.cwd(),
|
|
3022
|
+
const dir = resolveKubbDir((0, node_path.resolve)(node_process.default.cwd(), ".kubb"));
|
|
3023
|
+
await kubbStorage({ base: dir }).setItem(baseName, `${content}\n`);
|
|
3024
|
+
console.error(`Debug log written to ${(0, node_path.relative)(node_process.default.cwd(), (0, node_path.join)(dir, baseName))}`);
|
|
2638
3025
|
}
|
|
2639
3026
|
});
|
|
2640
3027
|
//#endregion
|
|
@@ -2676,15 +3063,16 @@ const htmlReporter = createReporter({
|
|
|
2676
3063
|
result.config.name?.replace(/[^a-zA-Z0-9_-]+/g, "-").replace(/^-+|-+$/g, ""),
|
|
2677
3064
|
Date.now()
|
|
2678
3065
|
].filter(Boolean).join("-");
|
|
2679
|
-
const
|
|
3066
|
+
const dir = resolveKubbDir((0, node_path.resolve)(process.cwd(), ".kubb"));
|
|
3067
|
+
const storage = kubbStorage({ base: dir });
|
|
2680
3068
|
const assets = new URL("./html/", require("url").pathToFileURL(__filename).href);
|
|
2681
|
-
const [html, ui] = await Promise.all([
|
|
3069
|
+
const [html, ui] = await Promise.all([require_nodeCache.read((0, node_url.fileURLToPath)(new URL("index.html", assets))), require_nodeCache.read((0, node_url.fileURLToPath)(new URL("ui.iife.js", assets)))]);
|
|
2682
3070
|
await Promise.all([
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
3071
|
+
storage.setItem(`${baseName}/index.html`, html),
|
|
3072
|
+
storage.setItem(`${baseName}/ui.iife.js`, ui),
|
|
3073
|
+
storage.setItem(`${baseName}/data.js`, serializeReport(result))
|
|
2686
3074
|
]);
|
|
2687
|
-
const reportPath = (0, node_path.
|
|
3075
|
+
const reportPath = (0, node_path.join)(dir, baseName, "index.html");
|
|
2688
3076
|
console.error(`HTML report written to ${(0, node_path.relative)(process.cwd(), reportPath)}`);
|
|
2689
3077
|
openInBrowser(reportPath);
|
|
2690
3078
|
}
|
|
@@ -2840,7 +3228,7 @@ const memoryStorage = createStorage(() => {
|
|
|
2840
3228
|
});
|
|
2841
3229
|
//#endregion
|
|
2842
3230
|
exports.Diagnostics = Diagnostics;
|
|
2843
|
-
exports.Hookable =
|
|
3231
|
+
exports.Hookable = require_nodeCache.Hookable;
|
|
2844
3232
|
exports.KubbDriver = KubbDriver;
|
|
2845
3233
|
exports.Resolver = Resolver;
|
|
2846
3234
|
exports.applyConfigDefaults = applyConfigDefaults;
|
|
@@ -2861,8 +3249,12 @@ exports.fsStorage = fsStorage;
|
|
|
2861
3249
|
exports.getInputKind = getInputKind;
|
|
2862
3250
|
exports.htmlReporter = htmlReporter;
|
|
2863
3251
|
exports.jsonReporter = jsonReporter;
|
|
3252
|
+
exports.kubbStorage = kubbStorage;
|
|
2864
3253
|
exports.logLevel = logLevel;
|
|
2865
3254
|
exports.memoryStorage = memoryStorage;
|
|
2866
3255
|
exports.resolveCacheDir = resolveCacheDir;
|
|
3256
|
+
exports.resolveKubbDir = resolveKubbDir;
|
|
3257
|
+
exports.runHook = runHook;
|
|
3258
|
+
exports.runOutputPasses = runOutputPasses;
|
|
2867
3259
|
|
|
2868
3260
|
//# sourceMappingURL=index.cjs.map
|