@kubb/core 5.5.6 → 5.6.0
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 +649 -301
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +175 -4
- package/dist/index.js +593 -247
- 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 +3 -2
- package/dist/usingCtx-C_vhSf7M.cjs.map +0 -1
- package/dist/usingCtx-DdG2edpx.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import { a as
|
|
2
|
+
import { a as inParallel, c as isPathInside, d as write, f as getErrorMessage, i as Hookable, l as read, m as camelCase, n as FileManager, o as clean, p as toError, r as _usingCtx, s as exists, t as createNodeCache, u as toPosixPath } from "./nodeCache-CumrOPh-.js";
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
4
|
import { stripVTControlCharacters, styleText } from "node:util";
|
|
5
5
|
import process$1 from "node:process";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { x } from "tinyexec";
|
|
7
|
+
import { glob, readFile, rm } from "node:fs/promises";
|
|
8
8
|
import path, { join, relative, resolve } from "node:path";
|
|
9
9
|
import { ast, collectImportedRefNames, collectUsedSchemaNames, composeMacros, operationDef, schemaDef, transform } from "@kubb/ast";
|
|
10
|
-
import { hash } from "node:crypto";
|
|
10
|
+
import { hash, randomUUID } from "node:crypto";
|
|
11
11
|
import { existsSync } from "node:fs";
|
|
12
12
|
import { tmpdir } from "node:os";
|
|
13
|
+
import { createInterface } from "node:readline";
|
|
13
14
|
import { fileURLToPath } from "node:url";
|
|
14
15
|
//#region src/createAdapter.ts
|
|
15
16
|
/**
|
|
@@ -76,18 +77,131 @@ function isCIEnvironment() {
|
|
|
76
77
|
}
|
|
77
78
|
//#endregion
|
|
78
79
|
//#region ../../internals/utils/src/openInBrowser.ts
|
|
80
|
+
const ignore = () => {};
|
|
79
81
|
/**
|
|
80
82
|
* Opens a URL or local file in the default browser, unless running in CI.
|
|
81
83
|
*/
|
|
82
84
|
function openInBrowser(target) {
|
|
83
85
|
if (isCIEnvironment()) return;
|
|
84
|
-
if (process$1.platform === "win32")
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
86
|
+
if (process$1.platform === "win32") {
|
|
87
|
+
x("cmd", [
|
|
88
|
+
"/c",
|
|
89
|
+
"start",
|
|
90
|
+
"",
|
|
91
|
+
target
|
|
92
|
+
]).then(ignore, ignore);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
x(process$1.platform === "darwin" ? "open" : "xdg-open", [target]).then(ignore, ignore);
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region ../../internals/utils/src/tools.ts
|
|
99
|
+
/**
|
|
100
|
+
* CLI command descriptors for each supported code formatter.
|
|
101
|
+
*/
|
|
102
|
+
const formatters = {
|
|
103
|
+
prettier: {
|
|
104
|
+
command: "prettier",
|
|
105
|
+
args: (outputPath) => [
|
|
106
|
+
"--ignore-unknown",
|
|
107
|
+
"--write",
|
|
108
|
+
outputPath
|
|
109
|
+
],
|
|
110
|
+
errorMessage: "Prettier not found"
|
|
111
|
+
},
|
|
112
|
+
biome: {
|
|
113
|
+
command: "biome",
|
|
114
|
+
args: (outputPath) => [
|
|
115
|
+
"format",
|
|
116
|
+
"--write",
|
|
117
|
+
outputPath
|
|
118
|
+
],
|
|
119
|
+
errorMessage: "Biome not found"
|
|
120
|
+
},
|
|
121
|
+
oxfmt: {
|
|
122
|
+
command: "oxfmt",
|
|
123
|
+
args: (outputPath) => [outputPath],
|
|
124
|
+
errorMessage: "Oxfmt not found"
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* CLI command descriptors for each supported linter.
|
|
129
|
+
*/
|
|
130
|
+
const linters = {
|
|
131
|
+
eslint: {
|
|
132
|
+
command: "eslint",
|
|
133
|
+
args: (outputPath) => [outputPath, "--fix"],
|
|
134
|
+
errorMessage: "Eslint not found"
|
|
135
|
+
},
|
|
136
|
+
biome: {
|
|
137
|
+
command: "biome",
|
|
138
|
+
args: (outputPath) => [
|
|
139
|
+
"lint",
|
|
140
|
+
"--fix",
|
|
141
|
+
outputPath
|
|
142
|
+
],
|
|
143
|
+
errorMessage: "Biome not found"
|
|
144
|
+
},
|
|
145
|
+
oxlint: {
|
|
146
|
+
command: "oxlint",
|
|
147
|
+
args: (outputPath) => [
|
|
148
|
+
"--fix",
|
|
149
|
+
"--no-ignore",
|
|
150
|
+
outputPath
|
|
151
|
+
],
|
|
152
|
+
errorMessage: "Oxlint not found"
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Preference order for `format: 'auto'`, most-preferred first. Spelled out rather than taken from
|
|
157
|
+
* the table's key order, which is arbitrary and would silently change what `auto` picks.
|
|
158
|
+
*/
|
|
159
|
+
const FORMATTER_PREFERENCE = [
|
|
160
|
+
"oxfmt",
|
|
161
|
+
"biome",
|
|
162
|
+
"prettier"
|
|
163
|
+
];
|
|
164
|
+
/**
|
|
165
|
+
* Preference order for `lint: 'auto'`, most-preferred first.
|
|
166
|
+
*/
|
|
167
|
+
const LINTER_PREFERENCE = [
|
|
168
|
+
"oxlint",
|
|
169
|
+
"biome",
|
|
170
|
+
"eslint"
|
|
171
|
+
];
|
|
172
|
+
/**
|
|
173
|
+
* Whether `name` is on PATH or in a `node_modules/.bin` above `cwd` and answers `--version` with a zero exit.
|
|
174
|
+
*/
|
|
175
|
+
async function isToolAvailable(name, cwd) {
|
|
176
|
+
return x(name, ["--version"], {
|
|
177
|
+
throwOnError: false,
|
|
178
|
+
nodeOptions: {
|
|
179
|
+
cwd,
|
|
180
|
+
stdio: "ignore"
|
|
181
|
+
}
|
|
182
|
+
}).then(({ exitCode }) => exitCode === 0, () => false);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Returns the first installed executable from `candidates`, or `null` when none are found.
|
|
186
|
+
*
|
|
187
|
+
* Not memoized: a long-running host that probes repeatedly should cache the result itself, and a
|
|
188
|
+
* `--watch` build should keep noticing a tool installed mid-session.
|
|
189
|
+
*/
|
|
190
|
+
async function detectTool(candidates, cwd) {
|
|
191
|
+
for (const candidate of candidates) if (await isToolAvailable(candidate, cwd)) return candidate;
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Tokenizes a shell command string, respecting single and double quotes.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* tokenize('git commit -m "initial commit"')
|
|
200
|
+
* // → ['git', 'commit', '-m', 'initial commit']
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
function tokenize(command) {
|
|
204
|
+
return (command.match(/[^\s"']+|"([^"]*)"|'([^']*)'/g) ?? []).map((token) => token.replace(/^["']|["']$/g, ""));
|
|
91
205
|
}
|
|
92
206
|
//#endregion
|
|
93
207
|
//#region ../../internals/utils/src/time.ts
|
|
@@ -123,11 +237,134 @@ function formatMs(ms) {
|
|
|
123
237
|
return `${Math.round(ms)}ms`;
|
|
124
238
|
}
|
|
125
239
|
//#endregion
|
|
240
|
+
//#region package.json
|
|
241
|
+
var version = "5.6.0";
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/constants.ts
|
|
244
|
+
/**
|
|
245
|
+
* Stable codes Kubb attaches to a `Diagnostic`. Each maps to a known failure mode
|
|
246
|
+
* and stays stable so it can be referenced in tooling and (later) docs. Reference
|
|
247
|
+
* these instead of inlining the string at a throw site.
|
|
248
|
+
*/
|
|
249
|
+
const diagnosticCode = {
|
|
250
|
+
/**
|
|
251
|
+
* Fallback for an unstructured error with no specific code.
|
|
252
|
+
*/
|
|
253
|
+
unknown: "KUBB_UNKNOWN",
|
|
254
|
+
/**
|
|
255
|
+
* The file or URL set as `input` could not be read.
|
|
256
|
+
*/
|
|
257
|
+
inputNotFound: "KUBB_INPUT_NOT_FOUND",
|
|
258
|
+
/**
|
|
259
|
+
* A URL set as `input` (or referenced by a `$ref`) answered with a 4xx or 5xx status
|
|
260
|
+
* instead of the document.
|
|
261
|
+
*/
|
|
262
|
+
inputRequestFailed: "KUBB_INPUT_REQUEST_FAILED",
|
|
263
|
+
/**
|
|
264
|
+
* A URL set as `input` (or referenced by a `$ref`) never answered, so the request failed
|
|
265
|
+
* before a status was returned.
|
|
266
|
+
*/
|
|
267
|
+
inputUnreachable: "KUBB_INPUT_UNREACHABLE",
|
|
268
|
+
/**
|
|
269
|
+
* An adapter was configured without an `input`.
|
|
270
|
+
*/
|
|
271
|
+
inputRequired: "KUBB_INPUT_REQUIRED",
|
|
272
|
+
/**
|
|
273
|
+
* `input` uses the v4 `{ path }` / `{ data }` wrapper, which v5 reads as a parsed
|
|
274
|
+
* document instead of a pointer to one.
|
|
275
|
+
*/
|
|
276
|
+
legacyInput: "KUBB_LEGACY_INPUT",
|
|
277
|
+
/**
|
|
278
|
+
* The parsed `input` carries no `openapi` or `swagger` version, so it is not a
|
|
279
|
+
* document the adapter can read.
|
|
280
|
+
*/
|
|
281
|
+
invalidDocument: "KUBB_INVALID_DOCUMENT",
|
|
282
|
+
/** The document fails OpenAPI schema validation. Reported as a warning. */
|
|
283
|
+
invalidSpec: "KUBB_INVALID_SPEC",
|
|
284
|
+
/**
|
|
285
|
+
* A `$ref` (or equivalent reference) could not be resolved in the source document.
|
|
286
|
+
*/
|
|
287
|
+
refNotFound: "KUBB_REF_NOT_FOUND",
|
|
288
|
+
/**
|
|
289
|
+
* A server variable value is not allowed by its `enum`.
|
|
290
|
+
*/
|
|
291
|
+
invalidServerVariable: "KUBB_INVALID_SERVER_VARIABLE",
|
|
292
|
+
/**
|
|
293
|
+
* A required plugin is missing from the config.
|
|
294
|
+
*/
|
|
295
|
+
pluginNotFound: "KUBB_PLUGIN_NOT_FOUND",
|
|
296
|
+
/**
|
|
297
|
+
* A plugin threw while generating.
|
|
298
|
+
*/
|
|
299
|
+
pluginFailed: "KUBB_PLUGIN_FAILED",
|
|
300
|
+
/**
|
|
301
|
+
* A plugin reported a non-fatal warning through `ctx.warn`.
|
|
302
|
+
*/
|
|
303
|
+
pluginWarning: "KUBB_PLUGIN_WARNING",
|
|
304
|
+
/**
|
|
305
|
+
* A plugin reported an informational message through `ctx.info`.
|
|
306
|
+
*/
|
|
307
|
+
pluginInfo: "KUBB_PLUGIN_INFO",
|
|
308
|
+
/**
|
|
309
|
+
* A schema uses a `format` Kubb does not map to a specific type. Reserved for
|
|
310
|
+
* adapters to emit as a `warning`.
|
|
311
|
+
*/
|
|
312
|
+
unsupportedFormat: "KUBB_UNSUPPORTED_FORMAT",
|
|
313
|
+
/**
|
|
314
|
+
* A referenced schema or operation is marked `deprecated`. Reserved for adapters
|
|
315
|
+
* to emit as an `info`.
|
|
316
|
+
*/
|
|
317
|
+
deprecated: "KUBB_DEPRECATED",
|
|
318
|
+
/**
|
|
319
|
+
* An adapter is required but the config has none. The build cannot read the input
|
|
320
|
+
* without one.
|
|
321
|
+
*/
|
|
322
|
+
adapterRequired: "KUBB_ADAPTER_REQUIRED",
|
|
323
|
+
/**
|
|
324
|
+
* A resolved output path escapes the output directory, which can stem from a path
|
|
325
|
+
* traversal in the spec or a misconfigured `group.name`.
|
|
326
|
+
*/
|
|
327
|
+
pathTraversal: "KUBB_PATH_TRAVERSAL",
|
|
328
|
+
/**
|
|
329
|
+
* `output.clean` is enabled but `output.path` resolves to the project root or a parent of it,
|
|
330
|
+
* so cleaning would delete kubb.config and every source file.
|
|
331
|
+
*/
|
|
332
|
+
cleanRoot: "KUBB_CLEAN_ROOT",
|
|
333
|
+
/**
|
|
334
|
+
* A plugin's options are invalid, for example `output.mode: 'file'` paired with a `group` option.
|
|
335
|
+
*/
|
|
336
|
+
invalidPluginOptions: "KUBB_INVALID_PLUGIN_OPTIONS",
|
|
337
|
+
/**
|
|
338
|
+
* A post-generate command (`output.postGenerate`) exited with a failure.
|
|
339
|
+
*/
|
|
340
|
+
postGenerateFailed: "KUBB_POST_GENERATE_FAILED",
|
|
341
|
+
/**
|
|
342
|
+
* The formatter pass over the generated files failed.
|
|
343
|
+
*/
|
|
344
|
+
formatFailed: "KUBB_FORMAT_FAILED",
|
|
345
|
+
/**
|
|
346
|
+
* The linter pass over the generated files failed.
|
|
347
|
+
*/
|
|
348
|
+
lintFailed: "KUBB_LINT_FAILED",
|
|
349
|
+
/**
|
|
350
|
+
* A barrel tries to re-export the same name from more than one module.
|
|
351
|
+
*/
|
|
352
|
+
barrelDuplicateExport: "KUBB_BARREL_DUPLICATE_EXPORT",
|
|
353
|
+
/**
|
|
354
|
+
* Not a failure. Carries a plugin's elapsed time, summed into the run total.
|
|
355
|
+
*/
|
|
356
|
+
performance: "KUBB_PERFORMANCE",
|
|
357
|
+
/**
|
|
358
|
+
* Not a failure. A newer Kubb version is available on npm.
|
|
359
|
+
*/
|
|
360
|
+
updateAvailable: "KUBB_UPDATE_AVAILABLE"
|
|
361
|
+
};
|
|
362
|
+
//#endregion
|
|
126
363
|
//#region src/Diagnostics.ts
|
|
127
364
|
/**
|
|
128
365
|
* Docs major version, derived from the package version so the link tracks the published major.
|
|
129
366
|
*/
|
|
130
|
-
const docsMajor =
|
|
367
|
+
const docsMajor = version.split(".")[0] ?? "5";
|
|
131
368
|
/**
|
|
132
369
|
* Builds a type guard that narrows a {@link Diagnostic} to the variant for `kind`. A diagnostic
|
|
133
370
|
* with no `kind` is treated as a `problem`.
|
|
@@ -175,6 +412,11 @@ const severityStyle = {
|
|
|
175
412
|
warning: "yellow",
|
|
176
413
|
info: "blue"
|
|
177
414
|
};
|
|
415
|
+
const countKey = {
|
|
416
|
+
error: "errors",
|
|
417
|
+
warning: "warnings",
|
|
418
|
+
info: "infos"
|
|
419
|
+
};
|
|
178
420
|
/**
|
|
179
421
|
* Explanation for every {@link diagnosticCode}. Use {@link Diagnostics.explain} to look one up
|
|
180
422
|
* and `Diagnostics.docsUrl` for the matching kubb.dev page.
|
|
@@ -362,7 +604,9 @@ var Diagnostics = class Diagnostics {
|
|
|
362
604
|
*/
|
|
363
605
|
static isError(error) {
|
|
364
606
|
if (error instanceof Diagnostics.Error) return true;
|
|
365
|
-
|
|
607
|
+
if (!(error instanceof Error) || error.name !== "DiagnosticError") return false;
|
|
608
|
+
const { diagnostic } = error;
|
|
609
|
+
return typeof diagnostic === "object" && diagnostic !== null && typeof diagnostic.code === "string";
|
|
366
610
|
}
|
|
367
611
|
/**
|
|
368
612
|
* Runs `fn` with `sink` as the active diagnostic sink for the whole async
|
|
@@ -450,35 +694,20 @@ var Diagnostics = class Diagnostics {
|
|
|
450
694
|
* that carry a `plugin`.
|
|
451
695
|
*/
|
|
452
696
|
static failedPlugins(diagnostics) {
|
|
453
|
-
|
|
454
|
-
for (const diagnostic of diagnostics) if (diagnostic.severity === "error" && diagnostic.plugin) names.add(diagnostic.plugin);
|
|
455
|
-
return [...names];
|
|
697
|
+
return [...new Set(diagnostics.flatMap((diagnostic) => diagnostic.severity === "error" && diagnostic.plugin ? [diagnostic.plugin] : []))];
|
|
456
698
|
}
|
|
457
699
|
/**
|
|
458
700
|
* Counts `problem` diagnostics by severity for the run summary. `performance` and
|
|
459
701
|
* `update` diagnostics are ignored.
|
|
460
702
|
*/
|
|
461
703
|
static count(diagnostics) {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
if (!isProblem(diagnostic)) continue;
|
|
467
|
-
if (diagnostic.severity === "error") {
|
|
468
|
-
errors += 1;
|
|
469
|
-
continue;
|
|
470
|
-
}
|
|
471
|
-
if (diagnostic.severity === "warning") {
|
|
472
|
-
warnings += 1;
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
infos += 1;
|
|
476
|
-
}
|
|
477
|
-
return {
|
|
478
|
-
errors,
|
|
479
|
-
warnings,
|
|
480
|
-
infos
|
|
704
|
+
const counts = {
|
|
705
|
+
errors: 0,
|
|
706
|
+
warnings: 0,
|
|
707
|
+
infos: 0
|
|
481
708
|
};
|
|
709
|
+
for (const diagnostic of diagnostics.filter(isProblem)) counts[countKey[diagnostic.severity]] += 1;
|
|
710
|
+
return counts;
|
|
482
711
|
}
|
|
483
712
|
/**
|
|
484
713
|
* Drops duplicate `problem` diagnostics that share a code, location pointer, and
|
|
@@ -579,7 +808,7 @@ var Diagnostics = class Diagnostics {
|
|
|
579
808
|
function normalizeOutput({ output, group, pluginName }) {
|
|
580
809
|
const mode = output.mode ?? (path.extname(output.path) ? "file" : "directory");
|
|
581
810
|
if (mode === "file" && group) throw new Diagnostics.Error({
|
|
582
|
-
code:
|
|
811
|
+
code: Diagnostics.code.invalidPluginOptions,
|
|
583
812
|
severity: "error",
|
|
584
813
|
message: `Plugin "${pluginName}" resolves \`output.mode\` to 'file' but also configures a \`group\` option.`,
|
|
585
814
|
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.",
|
|
@@ -704,7 +933,7 @@ const resolverOptions = Symbol.for("@kubb/core/resolver/options");
|
|
|
704
933
|
/**
|
|
705
934
|
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
706
935
|
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
707
|
-
* every
|
|
936
|
+
* every segment, and joins with `/`.
|
|
708
937
|
*
|
|
709
938
|
* Empty segments are dropped before joining. They arise when the name starts with a dot
|
|
710
939
|
* followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to
|
|
@@ -713,16 +942,21 @@ const resolverOptions = Symbol.for("@kubb/core/resolver/options");
|
|
|
713
942
|
*
|
|
714
943
|
* @example Nested path from a dotted name
|
|
715
944
|
* `toFilePath('pet.petId') // 'pet/petId'`
|
|
716
|
-
*
|
|
717
|
-
* @example PascalCase the final segment
|
|
718
|
-
* `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`
|
|
719
|
-
*
|
|
720
|
-
* @example Suffix applied to the final segment only
|
|
721
|
-
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
722
945
|
*/
|
|
723
|
-
function toFilePath(name
|
|
724
|
-
|
|
725
|
-
|
|
946
|
+
function toFilePath(name) {
|
|
947
|
+
return name.split(/\.(?=[a-zA-Z])/).map((part) => camelCase(part)).filter(Boolean).join("/");
|
|
948
|
+
}
|
|
949
|
+
/** Throws `KUBB_PATH_TRAVERSAL` when `target` lies outside `parent`; `target === parent` stays allowed. */
|
|
950
|
+
function assertInside({ target, parent, message, help }) {
|
|
951
|
+
const parentWithSep = parent.endsWith(path.sep) ? parent : `${parent}${path.sep}`;
|
|
952
|
+
if (target === parent || target.startsWith(parentWithSep)) return;
|
|
953
|
+
throw new Diagnostics.Error({
|
|
954
|
+
code: Diagnostics.code.pathTraversal,
|
|
955
|
+
severity: "error",
|
|
956
|
+
message,
|
|
957
|
+
help,
|
|
958
|
+
location: { kind: "config" }
|
|
959
|
+
});
|
|
726
960
|
}
|
|
727
961
|
/**
|
|
728
962
|
* Built-in `file.baseName`: casts the identifier with `toFilePath` and appends the extension.
|
|
@@ -758,6 +992,8 @@ var Resolver = class Resolver {
|
|
|
758
992
|
static #patternCache = /* @__PURE__ */ new Map();
|
|
759
993
|
static #optionsCache = /* @__PURE__ */ new WeakMap();
|
|
760
994
|
pluginName;
|
|
995
|
+
/** The built-in machinery, untouched by `name`/`file` overrides and built once since the driver reads it per node. */
|
|
996
|
+
default;
|
|
761
997
|
#options;
|
|
762
998
|
#baseName;
|
|
763
999
|
#filePath;
|
|
@@ -766,18 +1002,7 @@ var Resolver = class Resolver {
|
|
|
766
1002
|
this.#options = options;
|
|
767
1003
|
this.#baseName = options.file?.baseName ? options.file.baseName.bind(this) : toBaseName;
|
|
768
1004
|
this.#filePath = options.file?.path ? options.file.path.bind(this) : void 0;
|
|
769
|
-
this
|
|
770
|
-
}
|
|
771
|
-
/** Exposes the raw build options so `Resolver.merge` can read them across `@kubb/core` copies. */
|
|
772
|
-
get [resolverOptions]() {
|
|
773
|
-
return this.#options;
|
|
774
|
-
}
|
|
775
|
-
/**
|
|
776
|
-
* The built-in resolution machinery. Always reaches the untouched defaults, even when a
|
|
777
|
-
* plugin overrides the top-level `name` or `file`.
|
|
778
|
-
*/
|
|
779
|
-
get default() {
|
|
780
|
-
return {
|
|
1005
|
+
this.default = {
|
|
781
1006
|
name: camelCase,
|
|
782
1007
|
options: this.#resolveOptions.bind(this),
|
|
783
1008
|
path: this.#resolvePath.bind(this),
|
|
@@ -785,6 +1010,11 @@ var Resolver = class Resolver {
|
|
|
785
1010
|
banner: this.#resolveBanner.bind(this),
|
|
786
1011
|
footer: this.#resolveFooter.bind(this)
|
|
787
1012
|
};
|
|
1013
|
+
this.#apply(options);
|
|
1014
|
+
}
|
|
1015
|
+
/** Exposes the raw build options so `Resolver.merge` can read them across `@kubb/core` copies. */
|
|
1016
|
+
get [resolverOptions]() {
|
|
1017
|
+
return this.#options;
|
|
788
1018
|
}
|
|
789
1019
|
name(name) {
|
|
790
1020
|
return this.default.name(name);
|
|
@@ -944,13 +1174,11 @@ var Resolver = class Resolver {
|
|
|
944
1174
|
if (output.mode !== "directory") return path.resolve(root, output.path);
|
|
945
1175
|
const outputDir = path.resolve(root, output.path);
|
|
946
1176
|
const result = group && (groupPath || tag) ? path.resolve(outputDir, Resolver.#resolveGroupDir(group, group.type === "path" ? groupPath : tag), baseName) : path.resolve(outputDir, baseName);
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
severity: "error",
|
|
1177
|
+
assertInside({
|
|
1178
|
+
target: result,
|
|
1179
|
+
parent: outputDir,
|
|
951
1180
|
message: `Resolved path "${result}" is outside the output directory "${outputDir}".`,
|
|
952
|
-
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."
|
|
953
|
-
location: { kind: "config" }
|
|
1181
|
+
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."
|
|
954
1182
|
});
|
|
955
1183
|
return result;
|
|
956
1184
|
}
|
|
@@ -961,13 +1189,11 @@ var Resolver = class Resolver {
|
|
|
961
1189
|
*/
|
|
962
1190
|
#resolveOverridePath(filePath, root) {
|
|
963
1191
|
const resolved = path.resolve(root, filePath);
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
severity: "error",
|
|
1192
|
+
assertInside({
|
|
1193
|
+
target: resolved,
|
|
1194
|
+
parent: root,
|
|
968
1195
|
message: `Resolved path "${resolved}" is outside the project root "${root}".`,
|
|
969
|
-
help: "A resolver `file.path` must return a path inside the project root."
|
|
970
|
-
location: { kind: "config" }
|
|
1196
|
+
help: "A resolver `file.path` must return a path inside the project root."
|
|
971
1197
|
});
|
|
972
1198
|
return resolved;
|
|
973
1199
|
}
|
|
@@ -1192,21 +1418,27 @@ const ENFORCE_ORDER = {
|
|
|
1192
1418
|
pre: -1,
|
|
1193
1419
|
post: 1
|
|
1194
1420
|
};
|
|
1421
|
+
/** `include` filter types that select operations, and so prune the schemas they do not reach. */
|
|
1422
|
+
const OPERATION_FILTER_TYPES = /* @__PURE__ */ new Set([
|
|
1423
|
+
"tag",
|
|
1424
|
+
"operationId",
|
|
1425
|
+
"path",
|
|
1426
|
+
"method",
|
|
1427
|
+
"contentType"
|
|
1428
|
+
]);
|
|
1195
1429
|
const enforceWeight = (plugin) => plugin.enforce ? ENFORCE_ORDER[plugin.enforce] : 0;
|
|
1196
1430
|
/**
|
|
1197
1431
|
* The options bag a `NormalizedPlugin` starts with before a plugin refines it: a directory output
|
|
1198
1432
|
* at the plugin root and empty filter lists.
|
|
1199
1433
|
*/
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
};
|
|
1209
|
-
}
|
|
1434
|
+
const defaultPluginOptions = () => ({
|
|
1435
|
+
output: {
|
|
1436
|
+
path: ".",
|
|
1437
|
+
mode: "directory"
|
|
1438
|
+
},
|
|
1439
|
+
exclude: [],
|
|
1440
|
+
override: []
|
|
1441
|
+
});
|
|
1210
1442
|
/**
|
|
1211
1443
|
* Fills in the `output`, `exclude`, and `override` a `NormalizedPlugin` needs from a plugin's raw
|
|
1212
1444
|
* options, running `output` through `normalizeOutput`. Idempotent, so the driver can apply it after
|
|
@@ -1353,10 +1585,10 @@ var KubbDriver = class {
|
|
|
1353
1585
|
config: this.config,
|
|
1354
1586
|
options: plugin.options ?? {},
|
|
1355
1587
|
addGenerator: (...generators) => {
|
|
1356
|
-
|
|
1588
|
+
plugin.generators = [...plugin.generators ?? [], ...generators];
|
|
1357
1589
|
},
|
|
1358
1590
|
setResolver: (resolver) => {
|
|
1359
|
-
|
|
1591
|
+
plugin.resolver = Resolver.merge(createResolver({ pluginName: plugin.name }), resolver);
|
|
1360
1592
|
},
|
|
1361
1593
|
addMacro: (macro) => {
|
|
1362
1594
|
this.#transforms.add(plugin.name, macro);
|
|
@@ -1365,18 +1597,10 @@ var KubbDriver = class {
|
|
|
1365
1597
|
this.#transforms.set(plugin.name, macros);
|
|
1366
1598
|
},
|
|
1367
1599
|
setOptions: (opts) => {
|
|
1368
|
-
plugin.options = {
|
|
1600
|
+
plugin.options = normalizePluginOptions({
|
|
1369
1601
|
...plugin.options,
|
|
1370
1602
|
...opts
|
|
1371
|
-
};
|
|
1372
|
-
if (plugin.options.output) {
|
|
1373
|
-
const group = "group" in plugin.options ? plugin.options.group : void 0;
|
|
1374
|
-
plugin.options.output = normalizeOutput({
|
|
1375
|
-
output: plugin.options.output,
|
|
1376
|
-
group,
|
|
1377
|
-
pluginName: plugin.name
|
|
1378
|
-
});
|
|
1379
|
-
}
|
|
1603
|
+
}, plugin.name);
|
|
1380
1604
|
},
|
|
1381
1605
|
injectFile: (userFileNode) => {
|
|
1382
1606
|
this.fileManager.add(ast.factory.createFile(userFileNode));
|
|
@@ -1384,29 +1608,7 @@ var KubbDriver = class {
|
|
|
1384
1608
|
});
|
|
1385
1609
|
}
|
|
1386
1610
|
}
|
|
1387
|
-
/**
|
|
1388
|
-
* Appends a generator to its owning plugin so the generate loop can call it directly.
|
|
1389
|
-
*
|
|
1390
|
-
* The generator's `schema`, `operation`, and `operations` methods run per node during the AST
|
|
1391
|
-
* walk in `#runGenerators`, and their result is routed through `dispatch`. Because a generator is
|
|
1392
|
-
* bound to a plugin, generators from different plugins never cross-fire without a name check. The
|
|
1393
|
-
* renderer comes from `generator.renderer`; set it to `null` (or leave it unset) to opt out of
|
|
1394
|
-
* rendering.
|
|
1395
|
-
*
|
|
1396
|
-
* Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
|
|
1397
|
-
*/
|
|
1398
|
-
registerGenerator(pluginName, generator) {
|
|
1399
|
-
const plugin = this.plugins.get(pluginName);
|
|
1400
|
-
if (!plugin) return;
|
|
1401
|
-
plugin.generators = plugin.generators ? [...plugin.generators, generator] : [generator];
|
|
1402
|
-
}
|
|
1403
|
-
/**
|
|
1404
|
-
* Returns `true` when at least one generator was registered for the given plugin
|
|
1405
|
-
* via `addGenerator()` in `kubb:plugin:setup`.
|
|
1406
|
-
*
|
|
1407
|
-
* Used by the build loop to decide whether to walk the AST and run the generators
|
|
1408
|
-
* for a plugin.
|
|
1409
|
-
*/
|
|
1611
|
+
/** `true` once `addGenerator()` in `kubb:plugin:setup` registered a generator for the plugin. */
|
|
1410
1612
|
hasHookGenerators(pluginName) {
|
|
1411
1613
|
return (this.plugins.get(pluginName)?.generators?.length ?? 0) > 0;
|
|
1412
1614
|
}
|
|
@@ -1654,13 +1856,12 @@ var KubbDriver = class {
|
|
|
1654
1856
|
options,
|
|
1655
1857
|
cache
|
|
1656
1858
|
};
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
}
|
|
1859
|
+
await this.#dispatchNode({
|
|
1860
|
+
generators: state.schemaGenerators,
|
|
1861
|
+
node: transformedNode,
|
|
1862
|
+
ctx,
|
|
1863
|
+
generate: (generator) => generator.schema(transformedNode, ctx)
|
|
1864
|
+
});
|
|
1664
1865
|
await this.hooks.callHook("kubb:generate:schema", transformedNode, ctx);
|
|
1665
1866
|
} catch (caughtError) {
|
|
1666
1867
|
state.error = toError(caughtError);
|
|
@@ -1676,19 +1877,19 @@ var KubbDriver = class {
|
|
|
1676
1877
|
if (!resolved) continue;
|
|
1677
1878
|
state.pluginOperations.push(resolved.transformedNode);
|
|
1678
1879
|
if (state.operationGenerators.length) {
|
|
1880
|
+
const { transformedNode } = resolved;
|
|
1679
1881
|
const ctx = {
|
|
1680
1882
|
...state.generatorContext,
|
|
1681
1883
|
options: resolved.options,
|
|
1682
1884
|
cache
|
|
1683
1885
|
};
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
await this.hooks.callHook("kubb:generate:operation", resolved.transformedNode, ctx);
|
|
1886
|
+
await this.#dispatchNode({
|
|
1887
|
+
generators: state.operationGenerators,
|
|
1888
|
+
node: transformedNode,
|
|
1889
|
+
ctx,
|
|
1890
|
+
generate: (generator) => generator.operation(transformedNode, ctx)
|
|
1891
|
+
});
|
|
1892
|
+
await this.hooks.callHook("kubb:generate:operation", transformedNode, ctx);
|
|
1692
1893
|
}
|
|
1693
1894
|
} catch (caughtError) {
|
|
1694
1895
|
state.error = toError(caughtError);
|
|
@@ -1731,36 +1932,20 @@ var KubbDriver = class {
|
|
|
1731
1932
|
}
|
|
1732
1933
|
return diagnostics;
|
|
1733
1934
|
}
|
|
1734
|
-
/**
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
*
|
|
1743
|
-
* Pass `renderer` when the result may be a renderer element. Generators that only return
|
|
1744
|
-
* `Array<FileNode>` do not need one.
|
|
1745
|
-
*/
|
|
1746
|
-
async dispatch({ result, renderer }) {
|
|
1747
|
-
try {
|
|
1748
|
-
var _usingCtx$2 = _usingCtx();
|
|
1749
|
-
if (!result) return;
|
|
1750
|
-
if (Array.isArray(result)) {
|
|
1751
|
-
this.fileManager.upsert(...result);
|
|
1752
|
-
return;
|
|
1753
|
-
}
|
|
1754
|
-
if (!renderer) return;
|
|
1755
|
-
const instance = _usingCtx$2.u(renderer());
|
|
1756
|
-
await instance.render(result);
|
|
1757
|
-
this.fileManager.upsert(...instance.files);
|
|
1758
|
-
} catch (_) {
|
|
1759
|
-
_usingCtx$2.e = _;
|
|
1760
|
-
} finally {
|
|
1761
|
-
_usingCtx$2.d();
|
|
1935
|
+
/** Runs `node` through the generators whose `match` accepts it; `generate` picks the method for the node kind. */
|
|
1936
|
+
async #dispatchNode({ generators, node, ctx, generate }) {
|
|
1937
|
+
for (const generator of generators) {
|
|
1938
|
+
if (!(generator.match ? await generator.match(node, ctx) : true)) continue;
|
|
1939
|
+
await this.dispatch({
|
|
1940
|
+
result: await generate(generator),
|
|
1941
|
+
renderer: generator.renderer
|
|
1942
|
+
});
|
|
1762
1943
|
}
|
|
1763
1944
|
}
|
|
1945
|
+
/** Stores whatever a generator method or `kubb:generate:*` hook returned, see {@link FileManager.dispatch}. */
|
|
1946
|
+
dispatch(params) {
|
|
1947
|
+
return this.fileManager.dispatch(params);
|
|
1948
|
+
}
|
|
1764
1949
|
/**
|
|
1765
1950
|
* Removes every listener the driver added. Listeners attached directly to `hooks` from outside
|
|
1766
1951
|
* the driver survive. Called at the end of a build to prevent leaks across repeated builds.
|
|
@@ -1778,15 +1963,6 @@ var KubbDriver = class {
|
|
|
1778
1963
|
[Symbol.dispose]() {
|
|
1779
1964
|
this.dispose();
|
|
1780
1965
|
}
|
|
1781
|
-
/**
|
|
1782
|
-
* Merges `partial` onto a fresh default resolver and stores the result on `plugin.resolver`,
|
|
1783
|
-
* which is the single source `getResolver` and `getPlugin(name).resolver` both read.
|
|
1784
|
-
*/
|
|
1785
|
-
setPluginResolver(pluginName, partial) {
|
|
1786
|
-
const plugin = this.plugins.get(pluginName);
|
|
1787
|
-
if (!plugin) return;
|
|
1788
|
-
plugin.resolver = Resolver.merge(createResolver({ pluginName }), partial);
|
|
1789
|
-
}
|
|
1790
1966
|
getResolver(pluginName) {
|
|
1791
1967
|
return this.plugins.get(pluginName)?.resolver ?? createResolver({ pluginName });
|
|
1792
1968
|
}
|
|
@@ -1979,29 +2155,6 @@ function createStorage(build) {
|
|
|
1979
2155
|
}
|
|
1980
2156
|
//#endregion
|
|
1981
2157
|
//#region src/storages/fsStorage.ts
|
|
1982
|
-
const WRITE_CONCURRENCY = 50;
|
|
1983
|
-
function createLimiter(concurrency) {
|
|
1984
|
-
let active = 0;
|
|
1985
|
-
const queue = [];
|
|
1986
|
-
function next() {
|
|
1987
|
-
if (active >= concurrency) return;
|
|
1988
|
-
const run = queue.shift();
|
|
1989
|
-
if (!run) return;
|
|
1990
|
-
active++;
|
|
1991
|
-
run();
|
|
1992
|
-
}
|
|
1993
|
-
return function limit(task) {
|
|
1994
|
-
return new Promise((resolve, reject) => {
|
|
1995
|
-
queue.push(() => {
|
|
1996
|
-
task().then(resolve, reject).finally(() => {
|
|
1997
|
-
active--;
|
|
1998
|
-
next();
|
|
1999
|
-
});
|
|
2000
|
-
});
|
|
2001
|
-
next();
|
|
2002
|
-
});
|
|
2003
|
-
};
|
|
2004
|
-
}
|
|
2005
2158
|
/**
|
|
2006
2159
|
* Built-in filesystem storage driver.
|
|
2007
2160
|
*
|
|
@@ -2015,8 +2168,7 @@ function createLimiter(concurrency) {
|
|
|
2015
2168
|
* a formatter left behind
|
|
2016
2169
|
* - missing parent directories are created automatically
|
|
2017
2170
|
* - Bun's native file API is used when running under Bun
|
|
2018
|
-
* -
|
|
2019
|
-
* can fire every file's write without pacing itself
|
|
2171
|
+
* - `FileManager.write` bounds in-flight writes, so this driver does not pace itself
|
|
2020
2172
|
*
|
|
2021
2173
|
* @example
|
|
2022
2174
|
* ```ts
|
|
@@ -2030,51 +2182,43 @@ function createLimiter(concurrency) {
|
|
|
2030
2182
|
* })
|
|
2031
2183
|
* ```
|
|
2032
2184
|
*/
|
|
2033
|
-
const fsStorage = createStorage(() => {
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
}
|
|
2044
|
-
},
|
|
2045
|
-
async readItem(key) {
|
|
2046
|
-
try {
|
|
2047
|
-
return await readFile(resolve(key), "utf8");
|
|
2048
|
-
} catch (_error) {
|
|
2049
|
-
return null;
|
|
2050
|
-
}
|
|
2051
|
-
},
|
|
2052
|
-
async writeItem(key, value, options) {
|
|
2053
|
-
await limit(() => write(resolve(key), value, {
|
|
2054
|
-
sanity: false,
|
|
2055
|
-
stored: options?.stored
|
|
2056
|
-
}));
|
|
2057
|
-
},
|
|
2058
|
-
async removeItem(key) {
|
|
2059
|
-
await rm(resolve(key), { force: true });
|
|
2060
|
-
},
|
|
2061
|
-
async readKeys(base) {
|
|
2062
|
-
const resolvedBase = resolve(base ?? process.cwd());
|
|
2063
|
-
const keys = [];
|
|
2064
|
-
try {
|
|
2065
|
-
for await (const entry of glob("**/*", {
|
|
2066
|
-
cwd: resolvedBase,
|
|
2067
|
-
withFileTypes: true
|
|
2068
|
-
})) if (entry.isFile()) keys.push(toPosixPath(relative(resolvedBase, join(entry.parentPath, entry.name))));
|
|
2069
|
-
} catch (_error) {}
|
|
2070
|
-
return keys;
|
|
2071
|
-
},
|
|
2072
|
-
async empty(base) {
|
|
2073
|
-
if (!base) return;
|
|
2074
|
-
await clean(resolve(base));
|
|
2185
|
+
const fsStorage = createStorage(() => ({
|
|
2186
|
+
name: "fs",
|
|
2187
|
+
async existsItem(key) {
|
|
2188
|
+
return exists(resolve(key));
|
|
2189
|
+
},
|
|
2190
|
+
async readItem(key) {
|
|
2191
|
+
try {
|
|
2192
|
+
return await readFile(resolve(key), "utf8");
|
|
2193
|
+
} catch (_error) {
|
|
2194
|
+
return null;
|
|
2075
2195
|
}
|
|
2076
|
-
}
|
|
2077
|
-
|
|
2196
|
+
},
|
|
2197
|
+
async writeItem(key, value, options) {
|
|
2198
|
+
await write(resolve(key), value, {
|
|
2199
|
+
sanity: false,
|
|
2200
|
+
stored: options?.stored
|
|
2201
|
+
});
|
|
2202
|
+
},
|
|
2203
|
+
async removeItem(key) {
|
|
2204
|
+
await rm(resolve(key), { force: true });
|
|
2205
|
+
},
|
|
2206
|
+
async readKeys(base) {
|
|
2207
|
+
const resolvedBase = resolve(base ?? process.cwd());
|
|
2208
|
+
const keys = [];
|
|
2209
|
+
try {
|
|
2210
|
+
for await (const entry of glob("**/*", {
|
|
2211
|
+
cwd: resolvedBase,
|
|
2212
|
+
withFileTypes: true
|
|
2213
|
+
})) if (entry.isFile()) keys.push(toPosixPath(relative(resolvedBase, join(entry.parentPath, entry.name))));
|
|
2214
|
+
} catch (_error) {}
|
|
2215
|
+
return keys;
|
|
2216
|
+
},
|
|
2217
|
+
async empty(base) {
|
|
2218
|
+
if (!base) return;
|
|
2219
|
+
await clean(resolve(base));
|
|
2220
|
+
}
|
|
2221
|
+
}));
|
|
2078
2222
|
//#endregion
|
|
2079
2223
|
//#region src/storages/cacheStorage.ts
|
|
2080
2224
|
/**
|
|
@@ -2130,7 +2274,202 @@ const cacheStorage = createStorage(({ root = process.cwd() }) => {
|
|
|
2130
2274
|
};
|
|
2131
2275
|
});
|
|
2132
2276
|
//#endregion
|
|
2277
|
+
//#region src/output/runHook.ts
|
|
2278
|
+
const KILL_GRACE_MS = 2e3;
|
|
2279
|
+
async function settle(proc, hooks, id, commandWithArgs) {
|
|
2280
|
+
let childError = null;
|
|
2281
|
+
let lineError = null;
|
|
2282
|
+
let lines = Promise.resolve();
|
|
2283
|
+
let killTimer;
|
|
2284
|
+
proc.process?.once("error", (error) => {
|
|
2285
|
+
childError = error;
|
|
2286
|
+
killTimer = setTimeout(() => proc.kill("SIGKILL"), KILL_GRACE_MS).unref();
|
|
2287
|
+
});
|
|
2288
|
+
if (hooks.listenerCount("kubb:hook:line") > 0) for (const input of [proc.process?.stdout, proc.process?.stderr]) {
|
|
2289
|
+
if (!input) continue;
|
|
2290
|
+
createInterface({ input }).on("line", (line) => {
|
|
2291
|
+
lines = lines.then(() => hooks.callHook("kubb:hook:line", {
|
|
2292
|
+
id,
|
|
2293
|
+
line
|
|
2294
|
+
})).catch((error) => {
|
|
2295
|
+
lineError ??= toError(error);
|
|
2296
|
+
});
|
|
2297
|
+
});
|
|
2298
|
+
}
|
|
2299
|
+
const output = await Promise.resolve(proc).catch(() => null);
|
|
2300
|
+
clearTimeout(killTimer);
|
|
2301
|
+
await lines;
|
|
2302
|
+
const error = childError ?? lineError;
|
|
2303
|
+
if (error) return {
|
|
2304
|
+
success: false,
|
|
2305
|
+
error
|
|
2306
|
+
};
|
|
2307
|
+
if (output?.exitCode === 0) return {
|
|
2308
|
+
success: true,
|
|
2309
|
+
error: null
|
|
2310
|
+
};
|
|
2311
|
+
return {
|
|
2312
|
+
success: false,
|
|
2313
|
+
error: /* @__PURE__ */ new Error(`Hook execute failed: ${commandWithArgs}`),
|
|
2314
|
+
stdout: output?.stdout,
|
|
2315
|
+
stderr: output?.stderr
|
|
2316
|
+
};
|
|
2317
|
+
}
|
|
2318
|
+
/**
|
|
2319
|
+
* Spawns a command and returns its outcome, announcing it through `kubb:hook:start`,
|
|
2320
|
+
* `kubb:hook:line` (one per stdout or stderr line, only while a listener is attached) and
|
|
2321
|
+
* `kubb:hook:end`. A non-zero exit, a spawn failure, an abort or a throwing line listener returns
|
|
2322
|
+
* `success: false` instead of throwing. The failure travels on the result and `kubb:hook:end`
|
|
2323
|
+
* only, and the caller emits `kubb:success` or a diagnostic, so nothing is reported twice.
|
|
2324
|
+
*/
|
|
2325
|
+
async function runHook({ hooks, command, args = [], name, cwd = process$1.cwd(), signal }) {
|
|
2326
|
+
const id = randomUUID();
|
|
2327
|
+
await hooks.callHook("kubb:hook:start", {
|
|
2328
|
+
id,
|
|
2329
|
+
command,
|
|
2330
|
+
name,
|
|
2331
|
+
args
|
|
2332
|
+
});
|
|
2333
|
+
const result = await settle(x(command, [...args], {
|
|
2334
|
+
signal,
|
|
2335
|
+
throwOnError: false,
|
|
2336
|
+
nodeOptions: {
|
|
2337
|
+
cwd,
|
|
2338
|
+
windowsHide: true,
|
|
2339
|
+
stdio: [
|
|
2340
|
+
"ignore",
|
|
2341
|
+
"pipe",
|
|
2342
|
+
"pipe"
|
|
2343
|
+
],
|
|
2344
|
+
detached: process$1.platform !== "win32"
|
|
2345
|
+
}
|
|
2346
|
+
}), hooks, id, [command, ...args].join(" "));
|
|
2347
|
+
await hooks.callHook("kubb:hook:end", {
|
|
2348
|
+
id,
|
|
2349
|
+
command,
|
|
2350
|
+
name,
|
|
2351
|
+
args,
|
|
2352
|
+
...result
|
|
2353
|
+
});
|
|
2354
|
+
return result;
|
|
2355
|
+
}
|
|
2356
|
+
//#endregion
|
|
2357
|
+
//#region src/output/runOutputPasses.ts
|
|
2358
|
+
/**
|
|
2359
|
+
* Format and lint are one pass over the output directory that differs only in these values.
|
|
2360
|
+
* `label` and `verb` are spelled out: deriving them from `kind` gives "lintter" and "lintting".
|
|
2361
|
+
*/
|
|
2362
|
+
const TOOL_PASSES = [{
|
|
2363
|
+
kind: "format",
|
|
2364
|
+
label: "formatter",
|
|
2365
|
+
verb: "Formatting",
|
|
2366
|
+
tools: formatters,
|
|
2367
|
+
preference: FORMATTER_PREFERENCE,
|
|
2368
|
+
code: Diagnostics.code.formatFailed
|
|
2369
|
+
}, {
|
|
2370
|
+
kind: "lint",
|
|
2371
|
+
label: "linter",
|
|
2372
|
+
verb: "Linting",
|
|
2373
|
+
tools: linters,
|
|
2374
|
+
preference: LINTER_PREFERENCE,
|
|
2375
|
+
code: Diagnostics.code.lintFailed
|
|
2376
|
+
}];
|
|
2377
|
+
/**
|
|
2378
|
+
* Resolves `auto` to an installed tool and runs it. Returns the failure instead of throwing.
|
|
2379
|
+
*/
|
|
2380
|
+
async function runToolPass({ pass, setting, outputPath, hooks, cwd, signal }) {
|
|
2381
|
+
const detected = setting === "auto" ? await detectTool(pass.preference, cwd) : setting;
|
|
2382
|
+
if (!detected) {
|
|
2383
|
+
await hooks.callHook("kubb:warn", { message: `No ${pass.label} found (${pass.preference.join(", ")}). Skipping ${pass.verb.toLowerCase()}.` });
|
|
2384
|
+
return null;
|
|
2385
|
+
}
|
|
2386
|
+
if (setting === "auto") await hooks.callHook("kubb:info", { message: `Auto-detected ${pass.label}: ${styleText("dim", detected)}` });
|
|
2387
|
+
const tool = pass.tools[detected];
|
|
2388
|
+
if (!tool || !existsSync(outputPath)) return null;
|
|
2389
|
+
const result = await runHook({
|
|
2390
|
+
hooks,
|
|
2391
|
+
command: tool.command,
|
|
2392
|
+
args: tool.args(outputPath),
|
|
2393
|
+
cwd,
|
|
2394
|
+
signal
|
|
2395
|
+
});
|
|
2396
|
+
if (result.success) {
|
|
2397
|
+
await hooks.callHook("kubb:success", { message: `${pass.verb} with ${styleText("dim", detected)} on ${styleText("dim", outputPath)} successfully` });
|
|
2398
|
+
return null;
|
|
2399
|
+
}
|
|
2400
|
+
return result.error;
|
|
2401
|
+
}
|
|
2402
|
+
/**
|
|
2403
|
+
* Runs `output.format`, `output.lint` and `output.postGenerate` over the generated output and
|
|
2404
|
+
* returns the diagnostics they produced. Each failure is emitted through `Diagnostics.emit` and
|
|
2405
|
+
* returned, so it reaches the summary, the reporters and the exit code; the passes keep going so
|
|
2406
|
+
* one failing tool does not hide the next. `Kubb.generate()` runs this after an error-free build
|
|
2407
|
+
* unless the host passes its own `processOutput`.
|
|
2408
|
+
*
|
|
2409
|
+
* @example
|
|
2410
|
+
* ```ts
|
|
2411
|
+
* const diagnostics = await runOutputPasses({ config, outputPath: '/repo/src/gen', hooks })
|
|
2412
|
+
* ```
|
|
2413
|
+
*/
|
|
2414
|
+
async function runOutputPasses({ config, outputPath, hooks, signal }) {
|
|
2415
|
+
const diagnostics = [];
|
|
2416
|
+
const report = async (code, label, error) => {
|
|
2417
|
+
const diagnostic = {
|
|
2418
|
+
code,
|
|
2419
|
+
severity: "error",
|
|
2420
|
+
message: `${label} failed: ${error.message}`,
|
|
2421
|
+
help: "Check that the tool is installed and that the command and its config are correct.",
|
|
2422
|
+
location: { kind: "config" },
|
|
2423
|
+
cause: error
|
|
2424
|
+
};
|
|
2425
|
+
diagnostics.push(diagnostic);
|
|
2426
|
+
await Diagnostics.emit(hooks, diagnostic);
|
|
2427
|
+
};
|
|
2428
|
+
for (const pass of TOOL_PASSES) {
|
|
2429
|
+
const setting = config.output[pass.kind];
|
|
2430
|
+
if (!setting) continue;
|
|
2431
|
+
await hooks.callHook(`kubb:${pass.kind}:start`);
|
|
2432
|
+
const error = await runToolPass({
|
|
2433
|
+
pass,
|
|
2434
|
+
setting,
|
|
2435
|
+
outputPath,
|
|
2436
|
+
hooks,
|
|
2437
|
+
cwd: config.root,
|
|
2438
|
+
signal
|
|
2439
|
+
});
|
|
2440
|
+
if (error) await report(pass.code, pass.label, error);
|
|
2441
|
+
await hooks.callHook(`kubb:${pass.kind}:end`);
|
|
2442
|
+
signal?.throwIfAborted();
|
|
2443
|
+
}
|
|
2444
|
+
const commands = config.output.postGenerate ?? [];
|
|
2445
|
+
if (commands.length === 0) return diagnostics;
|
|
2446
|
+
await hooks.callHook("kubb:hooks:start");
|
|
2447
|
+
for (const entry of commands) {
|
|
2448
|
+
const { command, name } = typeof entry === "string" ? {
|
|
2449
|
+
command: entry,
|
|
2450
|
+
name: void 0
|
|
2451
|
+
} : entry;
|
|
2452
|
+
const [executable, ...args] = tokenize(command);
|
|
2453
|
+
if (!executable) continue;
|
|
2454
|
+
const result = await runHook({
|
|
2455
|
+
hooks,
|
|
2456
|
+
command: executable,
|
|
2457
|
+
args,
|
|
2458
|
+
name,
|
|
2459
|
+
cwd: config.root,
|
|
2460
|
+
signal
|
|
2461
|
+
});
|
|
2462
|
+
if (result.success) await hooks.callHook("kubb:success", { message: `${styleText("dim", name ?? command)} successfully executed` });
|
|
2463
|
+
if (!result.success) await report(Diagnostics.code.postGenerateFailed, "Post-generate command", result.error);
|
|
2464
|
+
signal?.throwIfAborted();
|
|
2465
|
+
}
|
|
2466
|
+
await hooks.callHook("kubb:hooks:end");
|
|
2467
|
+
return diagnostics;
|
|
2468
|
+
}
|
|
2469
|
+
//#endregion
|
|
2133
2470
|
//#region src/createKubb.ts
|
|
2471
|
+
/** Upper bound of listeners one plugin adds to a hook, sizing the emitter ceiling for large plugin sets. */
|
|
2472
|
+
const HOOK_LISTENERS_PER_PLUGIN = 4;
|
|
2134
2473
|
function resolveConfig(userConfig) {
|
|
2135
2474
|
return {
|
|
2136
2475
|
...userConfig,
|
|
@@ -2148,11 +2487,18 @@ function resolveConfig(userConfig) {
|
|
|
2148
2487
|
};
|
|
2149
2488
|
}
|
|
2150
2489
|
/**
|
|
2490
|
+
* Only the fs storage writes `output.path`, so only then do the passes have fresh files to run
|
|
2491
|
+
* over; a memory or custom storage would leave them formatting whatever an earlier run left there.
|
|
2492
|
+
*/
|
|
2493
|
+
function writesOutputDirectory(storage) {
|
|
2494
|
+
return storage.name === "fs";
|
|
2495
|
+
}
|
|
2496
|
+
/**
|
|
2151
2497
|
* Whether anything runs over the output directory after the files are written. Only then can the
|
|
2152
2498
|
* bytes on disk stop matching what Kubb wrote, which is what the manifest exists to track.
|
|
2153
2499
|
*/
|
|
2154
|
-
function hasOutputPasses(output) {
|
|
2155
|
-
return Boolean(output.format || output.lint || output.postGenerate?.length);
|
|
2500
|
+
function hasOutputPasses({ output, storage }) {
|
|
2501
|
+
return writesOutputDirectory(storage) && Boolean(output.format || output.lint || output.postGenerate?.length);
|
|
2156
2502
|
}
|
|
2157
2503
|
/**
|
|
2158
2504
|
* Kubb code-generation instance bound to a single config entry. Resolves the user
|
|
@@ -2175,7 +2521,6 @@ var Kubb = class {
|
|
|
2175
2521
|
hooks;
|
|
2176
2522
|
config;
|
|
2177
2523
|
#driver = null;
|
|
2178
|
-
#storage = null;
|
|
2179
2524
|
#manifest = null;
|
|
2180
2525
|
#signal;
|
|
2181
2526
|
constructor(userConfig, options = {}) {
|
|
@@ -2184,8 +2529,7 @@ var Kubb = class {
|
|
|
2184
2529
|
this.#signal = options.signal;
|
|
2185
2530
|
}
|
|
2186
2531
|
get storage() {
|
|
2187
|
-
|
|
2188
|
-
return this.#storage;
|
|
2532
|
+
return this.config.storage;
|
|
2189
2533
|
}
|
|
2190
2534
|
get driver() {
|
|
2191
2535
|
if (!this.#driver) throw new Error("[kubb] setup() must be called before accessing driver");
|
|
@@ -2198,7 +2542,7 @@ var Kubb = class {
|
|
|
2198
2542
|
const signal = this.#signal;
|
|
2199
2543
|
signal?.throwIfAborted();
|
|
2200
2544
|
const config = this.config;
|
|
2201
|
-
const manifest = hasOutputPasses(config
|
|
2545
|
+
const manifest = hasOutputPasses(config) ? await createOutputManifest({
|
|
2202
2546
|
storage: config.storage,
|
|
2203
2547
|
cache: cacheStorage({ root: config.root })
|
|
2204
2548
|
}) : void 0;
|
|
@@ -2207,7 +2551,7 @@ var Kubb = class {
|
|
|
2207
2551
|
manifest,
|
|
2208
2552
|
signal
|
|
2209
2553
|
});
|
|
2210
|
-
this.hooks.setMaxListeners(Math.max(10, config.plugins.length *
|
|
2554
|
+
this.hooks.setMaxListeners(Math.max(10, config.plugins.length * HOOK_LISTENERS_PER_PLUGIN));
|
|
2211
2555
|
if (config.output.clean) {
|
|
2212
2556
|
const cleanPath = resolve(config.root, config.output.path);
|
|
2213
2557
|
if (isPathInside(config.root, cleanPath)) throw new Diagnostics.Error({
|
|
@@ -2221,7 +2565,6 @@ var Kubb = class {
|
|
|
2221
2565
|
}
|
|
2222
2566
|
await driver.setup();
|
|
2223
2567
|
this.#driver = driver;
|
|
2224
|
-
this.#storage = config.storage;
|
|
2225
2568
|
this.#manifest = manifest ?? null;
|
|
2226
2569
|
}
|
|
2227
2570
|
/**
|
|
@@ -2308,9 +2651,12 @@ var Kubb = class {
|
|
|
2308
2651
|
diagnostics
|
|
2309
2652
|
};
|
|
2310
2653
|
}
|
|
2311
|
-
const
|
|
2654
|
+
const processOutput = options.processOutput ?? runOutputPasses;
|
|
2655
|
+
const outputDiagnostics = writesOutputDirectory(config.storage) ? await processOutput({
|
|
2312
2656
|
config,
|
|
2313
|
-
outputPath: resolve(config.root, config.output.path)
|
|
2657
|
+
outputPath: resolve(config.root, config.output.path),
|
|
2658
|
+
hooks,
|
|
2659
|
+
signal
|
|
2314
2660
|
}) : [];
|
|
2315
2661
|
const finalDiagnostics = [...diagnostics, ...outputDiagnostics];
|
|
2316
2662
|
const failed = Diagnostics.hasError(outputDiagnostics);
|
|
@@ -2837,6 +3183,6 @@ const memoryStorage = createStorage(() => {
|
|
|
2837
3183
|
};
|
|
2838
3184
|
});
|
|
2839
3185
|
//#endregion
|
|
2840
|
-
export { Diagnostics, Hookable, KubbDriver, Resolver, applyConfigDefaults, cacheStorage, cliReporter, createAdapter, createCliReporter, createKubb, createRenderer, createReporter, createResolver, createStorage, defineGenerator, defineParser, definePlugin, fileReporter, fsStorage, getInputKind, htmlReporter, jsonReporter, logLevel, memoryStorage, resolveCacheDir };
|
|
3186
|
+
export { Diagnostics, Hookable, KubbDriver, Resolver, applyConfigDefaults, cacheStorage, cliReporter, createAdapter, createCliReporter, createKubb, createRenderer, createReporter, createResolver, createStorage, defineGenerator, defineParser, definePlugin, fileReporter, fsStorage, getInputKind, htmlReporter, jsonReporter, logLevel, memoryStorage, resolveCacheDir, runHook, runOutputPasses };
|
|
2841
3187
|
|
|
2842
3188
|
//# sourceMappingURL=index.js.map
|