@kubb/core 5.0.0-alpha.35 → 5.0.0-alpha.38
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/PluginDriver-BQwm8hDd.cjs +1729 -0
- package/dist/PluginDriver-BQwm8hDd.cjs.map +1 -0
- package/dist/PluginDriver-CgXFtmNP.js +1617 -0
- package/dist/PluginDriver-CgXFtmNP.js.map +1 -0
- package/dist/index.cjs +149 -2142
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +45 -317
- package/dist/index.js +142 -2110
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +164 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +159 -0
- package/dist/mocks.js.map +1 -0
- package/dist/{PluginDriver-D8lWvtUg.d.ts → types-DUc5lEUp.d.ts} +597 -716
- package/package.json +9 -9
- package/src/PluginDriver.ts +18 -17
- package/src/constants.ts +0 -48
- package/src/createKubb.ts +1 -1
- package/src/defineResolver.ts +3 -3
- package/src/index.ts +5 -20
- package/src/mocks.ts +234 -0
- package/src/types.ts +4 -13
- package/src/utils/TreeNode.ts +3 -3
- package/src/utils/executeStrategies.ts +0 -16
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -32
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -23
- package/dist/hooks.js +0 -29
- package/dist/hooks.js.map +0 -1
- package/src/hooks/index.ts +0 -3
- package/src/hooks/useDriver.ts +0 -9
- package/src/hooks/useMode.ts +0 -8
- package/src/hooks/usePlugin.ts +0 -9
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/formatters.ts +0 -45
- package/src/utils/getFunctionParams.ts +0 -254
- package/src/utils/linters.ts +0 -45
package/dist/index.cjs
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_PluginDriver = require("./PluginDriver-BQwm8hDd.cjs");
|
|
3
3
|
let node_events = require("node:events");
|
|
4
|
-
let node_fs = require("node:fs");
|
|
5
4
|
let node_fs_promises = require("node:fs/promises");
|
|
6
5
|
let node_path = require("node:path");
|
|
7
|
-
node_path =
|
|
6
|
+
node_path = require_PluginDriver.__toESM(node_path, 1);
|
|
8
7
|
let _kubb_ast = require("@kubb/ast");
|
|
9
|
-
_kubb_ast =
|
|
10
|
-
let node_perf_hooks = require("node:perf_hooks");
|
|
11
|
-
let fflate = require("fflate");
|
|
12
|
-
let tinyexec = require("tinyexec");
|
|
8
|
+
_kubb_ast = require_PluginDriver.__toESM(_kubb_ast, 1);
|
|
13
9
|
let node_process = require("node:process");
|
|
14
|
-
let remeda = require("remeda");
|
|
15
|
-
let semver = require("semver");
|
|
16
10
|
//#region ../../internals/utils/src/errors.ts
|
|
17
11
|
/**
|
|
18
12
|
* Thrown when one or more errors occur during a Kubb build.
|
|
@@ -153,64 +147,6 @@ var AsyncEventEmitter = class {
|
|
|
153
147
|
}
|
|
154
148
|
};
|
|
155
149
|
//#endregion
|
|
156
|
-
//#region ../../internals/utils/src/casing.ts
|
|
157
|
-
/**
|
|
158
|
-
* Shared implementation for camelCase and PascalCase conversion.
|
|
159
|
-
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
160
|
-
* and capitalizes each word according to `pascal`.
|
|
161
|
-
*
|
|
162
|
-
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
163
|
-
*/
|
|
164
|
-
function toCamelOrPascal(text, pascal) {
|
|
165
|
-
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
166
|
-
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
167
|
-
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
|
|
168
|
-
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
169
|
-
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
173
|
-
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
174
|
-
* Segments are joined with `/` to form a file path.
|
|
175
|
-
*
|
|
176
|
-
* Only splits on dots followed by a letter so that version numbers
|
|
177
|
-
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
178
|
-
*/
|
|
179
|
-
function applyToFileParts(text, transformPart) {
|
|
180
|
-
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
181
|
-
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Converts `text` to camelCase.
|
|
185
|
-
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
186
|
-
*
|
|
187
|
-
* @example
|
|
188
|
-
* camelCase('hello-world') // 'helloWorld'
|
|
189
|
-
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
190
|
-
*/
|
|
191
|
-
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
192
|
-
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
193
|
-
prefix,
|
|
194
|
-
suffix
|
|
195
|
-
} : {}));
|
|
196
|
-
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Converts `text` to PascalCase.
|
|
200
|
-
* When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
|
|
201
|
-
*
|
|
202
|
-
* @example
|
|
203
|
-
* pascalCase('hello-world') // 'HelloWorld'
|
|
204
|
-
* pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
|
|
205
|
-
*/
|
|
206
|
-
function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
207
|
-
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
|
|
208
|
-
prefix,
|
|
209
|
-
suffix
|
|
210
|
-
}) : camelCase(part));
|
|
211
|
-
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
212
|
-
}
|
|
213
|
-
//#endregion
|
|
214
150
|
//#region ../../internals/utils/src/time.ts
|
|
215
151
|
/**
|
|
216
152
|
* Calculates elapsed time in milliseconds from a high-resolution `process.hrtime` start time.
|
|
@@ -246,26 +182,6 @@ function formatMs(ms) {
|
|
|
246
182
|
//#endregion
|
|
247
183
|
//#region ../../internals/utils/src/fs.ts
|
|
248
184
|
/**
|
|
249
|
-
* Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and
|
|
250
|
-
* returns the absolute path of the nearest `package.json`, or `null` when none
|
|
251
|
-
* is found before reaching the filesystem root.
|
|
252
|
-
*
|
|
253
|
-
* @example
|
|
254
|
-
* ```ts
|
|
255
|
-
* const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'
|
|
256
|
-
* ```
|
|
257
|
-
*/
|
|
258
|
-
function findPackageJSON(cwd) {
|
|
259
|
-
let dir = cwd ? (0, node_path.resolve)(cwd) : process.cwd();
|
|
260
|
-
while (true) {
|
|
261
|
-
const pkgPath = (0, node_path.join)(dir, "package.json");
|
|
262
|
-
if ((0, node_fs.existsSync)(pkgPath)) return pkgPath;
|
|
263
|
-
const parent = (0, node_path.dirname)(dir);
|
|
264
|
-
if (parent === dir) return null;
|
|
265
|
-
dir = parent;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
185
|
* Converts all backslashes to forward slashes.
|
|
270
186
|
* Extended-length Windows paths (`\\?\...`) are left unchanged.
|
|
271
187
|
*/
|
|
@@ -304,17 +220,6 @@ async function exists(path) {
|
|
|
304
220
|
return (0, node_fs_promises.access)(path).then(() => true, () => false);
|
|
305
221
|
}
|
|
306
222
|
/**
|
|
307
|
-
* Synchronous counterpart of `read`.
|
|
308
|
-
*
|
|
309
|
-
* @example
|
|
310
|
-
* ```ts
|
|
311
|
-
* const source = readSync('./src/Pet.ts')
|
|
312
|
-
* ```
|
|
313
|
-
*/
|
|
314
|
-
function readSync(path) {
|
|
315
|
-
return (0, node_fs.readFileSync)(path, { encoding: "utf8" });
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
223
|
* Writes `data` to `path`, trimming leading/trailing whitespace before saving.
|
|
319
224
|
* Skips the write when the trimmed content is empty or identical to what is already on disk.
|
|
320
225
|
* Creates any missing parent directories automatically.
|
|
@@ -364,159 +269,6 @@ async function clean(path) {
|
|
|
364
269
|
});
|
|
365
270
|
}
|
|
366
271
|
//#endregion
|
|
367
|
-
//#region ../../internals/utils/src/string.ts
|
|
368
|
-
/**
|
|
369
|
-
* Strips the file extension from a path or file name.
|
|
370
|
-
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* trimExtName('petStore.ts') // 'petStore'
|
|
374
|
-
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
375
|
-
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
376
|
-
* trimExtName('noExtension') // 'noExtension'
|
|
377
|
-
*/
|
|
378
|
-
function trimExtName$1(text) {
|
|
379
|
-
const dotIndex = text.lastIndexOf(".");
|
|
380
|
-
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
381
|
-
return text;
|
|
382
|
-
}
|
|
383
|
-
require_chunk.__name(trimExtName$1, "trimExtName");
|
|
384
|
-
//#endregion
|
|
385
|
-
//#region ../../internals/utils/src/promise.ts
|
|
386
|
-
/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.
|
|
387
|
-
*
|
|
388
|
-
* @example
|
|
389
|
-
* ```ts
|
|
390
|
-
* const results = await Promise.allSettled([p1, p2])
|
|
391
|
-
* results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)
|
|
392
|
-
* ```
|
|
393
|
-
*/
|
|
394
|
-
function isPromiseRejectedResult(result) {
|
|
395
|
-
return result.status === "rejected";
|
|
396
|
-
}
|
|
397
|
-
//#endregion
|
|
398
|
-
//#region ../../internals/utils/src/reserved.ts
|
|
399
|
-
/**
|
|
400
|
-
* JavaScript and Java reserved words.
|
|
401
|
-
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
402
|
-
*/
|
|
403
|
-
const reservedWords = new Set([
|
|
404
|
-
"abstract",
|
|
405
|
-
"arguments",
|
|
406
|
-
"boolean",
|
|
407
|
-
"break",
|
|
408
|
-
"byte",
|
|
409
|
-
"case",
|
|
410
|
-
"catch",
|
|
411
|
-
"char",
|
|
412
|
-
"class",
|
|
413
|
-
"const",
|
|
414
|
-
"continue",
|
|
415
|
-
"debugger",
|
|
416
|
-
"default",
|
|
417
|
-
"delete",
|
|
418
|
-
"do",
|
|
419
|
-
"double",
|
|
420
|
-
"else",
|
|
421
|
-
"enum",
|
|
422
|
-
"eval",
|
|
423
|
-
"export",
|
|
424
|
-
"extends",
|
|
425
|
-
"false",
|
|
426
|
-
"final",
|
|
427
|
-
"finally",
|
|
428
|
-
"float",
|
|
429
|
-
"for",
|
|
430
|
-
"function",
|
|
431
|
-
"goto",
|
|
432
|
-
"if",
|
|
433
|
-
"implements",
|
|
434
|
-
"import",
|
|
435
|
-
"in",
|
|
436
|
-
"instanceof",
|
|
437
|
-
"int",
|
|
438
|
-
"interface",
|
|
439
|
-
"let",
|
|
440
|
-
"long",
|
|
441
|
-
"native",
|
|
442
|
-
"new",
|
|
443
|
-
"null",
|
|
444
|
-
"package",
|
|
445
|
-
"private",
|
|
446
|
-
"protected",
|
|
447
|
-
"public",
|
|
448
|
-
"return",
|
|
449
|
-
"short",
|
|
450
|
-
"static",
|
|
451
|
-
"super",
|
|
452
|
-
"switch",
|
|
453
|
-
"synchronized",
|
|
454
|
-
"this",
|
|
455
|
-
"throw",
|
|
456
|
-
"throws",
|
|
457
|
-
"transient",
|
|
458
|
-
"true",
|
|
459
|
-
"try",
|
|
460
|
-
"typeof",
|
|
461
|
-
"var",
|
|
462
|
-
"void",
|
|
463
|
-
"volatile",
|
|
464
|
-
"while",
|
|
465
|
-
"with",
|
|
466
|
-
"yield",
|
|
467
|
-
"Array",
|
|
468
|
-
"Date",
|
|
469
|
-
"hasOwnProperty",
|
|
470
|
-
"Infinity",
|
|
471
|
-
"isFinite",
|
|
472
|
-
"isNaN",
|
|
473
|
-
"isPrototypeOf",
|
|
474
|
-
"length",
|
|
475
|
-
"Math",
|
|
476
|
-
"name",
|
|
477
|
-
"NaN",
|
|
478
|
-
"Number",
|
|
479
|
-
"Object",
|
|
480
|
-
"prototype",
|
|
481
|
-
"String",
|
|
482
|
-
"toString",
|
|
483
|
-
"undefined",
|
|
484
|
-
"valueOf"
|
|
485
|
-
]);
|
|
486
|
-
/**
|
|
487
|
-
* Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.
|
|
488
|
-
*
|
|
489
|
-
* @example
|
|
490
|
-
* ```ts
|
|
491
|
-
* transformReservedWord('class') // '_class'
|
|
492
|
-
* transformReservedWord('42foo') // '_42foo'
|
|
493
|
-
* transformReservedWord('status') // 'status'
|
|
494
|
-
* ```
|
|
495
|
-
*/
|
|
496
|
-
function transformReservedWord(word) {
|
|
497
|
-
const firstChar = word.charCodeAt(0);
|
|
498
|
-
if (word && (reservedWords.has(word) || firstChar >= 48 && firstChar <= 57)) return `_${word}`;
|
|
499
|
-
return word;
|
|
500
|
-
}
|
|
501
|
-
/**
|
|
502
|
-
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
503
|
-
*
|
|
504
|
-
* @example
|
|
505
|
-
* ```ts
|
|
506
|
-
* isValidVarName('status') // true
|
|
507
|
-
* isValidVarName('class') // false (reserved word)
|
|
508
|
-
* isValidVarName('42foo') // false (starts with digit)
|
|
509
|
-
* ```
|
|
510
|
-
*/
|
|
511
|
-
function isValidVarName(name) {
|
|
512
|
-
try {
|
|
513
|
-
new Function(`var ${name}`);
|
|
514
|
-
} catch {
|
|
515
|
-
return false;
|
|
516
|
-
}
|
|
517
|
-
return true;
|
|
518
|
-
}
|
|
519
|
-
//#endregion
|
|
520
272
|
//#region ../../internals/utils/src/urlPath.ts
|
|
521
273
|
/**
|
|
522
274
|
* Parses and transforms an OpenAPI/Swagger path string into various URL formats.
|
|
@@ -574,1610 +326,155 @@ var URLPath = class {
|
|
|
574
326
|
/** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
|
|
575
327
|
*
|
|
576
328
|
* @example
|
|
577
|
-
* ```ts
|
|
578
|
-
* new URLPath('/pet/{petId}').object
|
|
579
|
-
* // { url: '/pet/:petId', params: { petId: 'petId' } }
|
|
580
|
-
* ```
|
|
581
|
-
*/
|
|
582
|
-
get object() {
|
|
583
|
-
return this.toObject();
|
|
584
|
-
}
|
|
585
|
-
/** Returns a map of path parameter names, or `undefined` when the path has no parameters.
|
|
586
|
-
*
|
|
587
|
-
* @example
|
|
588
|
-
* ```ts
|
|
589
|
-
* new URLPath('/pet/{petId}').params // { petId: 'petId' }
|
|
590
|
-
* new URLPath('/pet').params // undefined
|
|
591
|
-
* ```
|
|
592
|
-
*/
|
|
593
|
-
get params() {
|
|
594
|
-
return this.getParams();
|
|
595
|
-
}
|
|
596
|
-
#transformParam(raw) {
|
|
597
|
-
const param = isValidVarName(raw) ? raw : camelCase(raw);
|
|
598
|
-
return this.#options.casing === "camelcase" ? camelCase(param) : param;
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
|
|
602
|
-
*/
|
|
603
|
-
#eachParam(fn) {
|
|
604
|
-
for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
|
|
605
|
-
const raw = match[1];
|
|
606
|
-
fn(raw, this.#transformParam(raw));
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
toObject({ type = "path", replacer, stringify } = {}) {
|
|
610
|
-
const object = {
|
|
611
|
-
url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
|
|
612
|
-
params: this.getParams()
|
|
613
|
-
};
|
|
614
|
-
if (stringify) {
|
|
615
|
-
if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
|
|
616
|
-
if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
|
|
617
|
-
return `{ url: '${object.url}' }`;
|
|
618
|
-
}
|
|
619
|
-
return object;
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Converts the OpenAPI path to a TypeScript template literal string.
|
|
623
|
-
* An optional `replacer` can transform each extracted parameter name before interpolation.
|
|
624
|
-
*
|
|
625
|
-
* @example
|
|
626
|
-
* new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
|
|
627
|
-
*/
|
|
628
|
-
toTemplateString({ prefix = "", replacer } = {}) {
|
|
629
|
-
return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
630
|
-
if (i % 2 === 0) return part;
|
|
631
|
-
const param = this.#transformParam(part);
|
|
632
|
-
return `\${${replacer ? replacer(param) : param}}`;
|
|
633
|
-
}).join("")}\``;
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Extracts all `{param}` segments from the path and returns them as a key-value map.
|
|
637
|
-
* An optional `replacer` transforms each parameter name in both key and value positions.
|
|
638
|
-
* Returns `undefined` when no path parameters are found.
|
|
639
|
-
*
|
|
640
|
-
* @example
|
|
641
|
-
* ```ts
|
|
642
|
-
* new URLPath('/pet/{petId}/tag/{tagId}').getParams()
|
|
643
|
-
* // { petId: 'petId', tagId: 'tagId' }
|
|
644
|
-
* ```
|
|
645
|
-
*/
|
|
646
|
-
getParams(replacer) {
|
|
647
|
-
const params = {};
|
|
648
|
-
this.#eachParam((_raw, param) => {
|
|
649
|
-
const key = replacer ? replacer(param) : param;
|
|
650
|
-
params[key] = key;
|
|
651
|
-
});
|
|
652
|
-
return Object.keys(params).length > 0 ? params : void 0;
|
|
653
|
-
}
|
|
654
|
-
/** Converts the OpenAPI path to Express-style colon syntax.
|
|
655
|
-
*
|
|
656
|
-
* @example
|
|
657
|
-
* ```ts
|
|
658
|
-
* new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
|
|
659
|
-
* ```
|
|
660
|
-
*/
|
|
661
|
-
toURLPath() {
|
|
662
|
-
return this.path.replace(/\{([^}]+)\}/g, ":$1");
|
|
663
|
-
}
|
|
664
|
-
};
|
|
665
|
-
//#endregion
|
|
666
|
-
//#region src/constants.ts
|
|
667
|
-
/**
|
|
668
|
-
* Base URL for the Kubb Studio web app.
|
|
669
|
-
*/
|
|
670
|
-
const DEFAULT_STUDIO_URL = "https://studio.kubb.dev";
|
|
671
|
-
/**
|
|
672
|
-
* File name used for generated barrel (index) files.
|
|
673
|
-
*/
|
|
674
|
-
const BARREL_FILENAME = "index.ts";
|
|
675
|
-
/**
|
|
676
|
-
* Default banner style written at the top of every generated file.
|
|
677
|
-
*/
|
|
678
|
-
const DEFAULT_BANNER = "simple";
|
|
679
|
-
/**
|
|
680
|
-
* Default file-extension mapping used when no explicit mapping is configured.
|
|
681
|
-
*/
|
|
682
|
-
const DEFAULT_EXTENSION = { ".ts": ".ts" };
|
|
683
|
-
/**
|
|
684
|
-
* Numeric log-level thresholds used internally to compare verbosity.
|
|
685
|
-
*
|
|
686
|
-
* Higher numbers are more verbose.
|
|
687
|
-
*/
|
|
688
|
-
const logLevel = {
|
|
689
|
-
silent: Number.NEGATIVE_INFINITY,
|
|
690
|
-
error: 0,
|
|
691
|
-
warn: 1,
|
|
692
|
-
info: 3,
|
|
693
|
-
verbose: 4,
|
|
694
|
-
debug: 5
|
|
695
|
-
};
|
|
696
|
-
/**
|
|
697
|
-
* CLI command descriptors for each supported linter.
|
|
698
|
-
*
|
|
699
|
-
* Each entry contains the executable `command`, an `args` factory that maps an
|
|
700
|
-
* output path to the correct argument list, and an `errorMessage` shown when
|
|
701
|
-
* the linter is not found.
|
|
702
|
-
*/
|
|
703
|
-
const linters = {
|
|
704
|
-
eslint: {
|
|
705
|
-
command: "eslint",
|
|
706
|
-
args: (outputPath) => [outputPath, "--fix"],
|
|
707
|
-
errorMessage: "Eslint not found"
|
|
708
|
-
},
|
|
709
|
-
biome: {
|
|
710
|
-
command: "biome",
|
|
711
|
-
args: (outputPath) => [
|
|
712
|
-
"lint",
|
|
713
|
-
"--fix",
|
|
714
|
-
outputPath
|
|
715
|
-
],
|
|
716
|
-
errorMessage: "Biome not found"
|
|
717
|
-
},
|
|
718
|
-
oxlint: {
|
|
719
|
-
command: "oxlint",
|
|
720
|
-
args: (outputPath) => ["--fix", outputPath],
|
|
721
|
-
errorMessage: "Oxlint not found"
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
/**
|
|
725
|
-
* CLI command descriptors for each supported code formatter.
|
|
726
|
-
*
|
|
727
|
-
* Each entry contains the executable `command`, an `args` factory that maps an
|
|
728
|
-
* output path to the correct argument list, and an `errorMessage` shown when
|
|
729
|
-
* the formatter is not found.
|
|
730
|
-
*/
|
|
731
|
-
const formatters = {
|
|
732
|
-
prettier: {
|
|
733
|
-
command: "prettier",
|
|
734
|
-
args: (outputPath) => [
|
|
735
|
-
"--ignore-unknown",
|
|
736
|
-
"--write",
|
|
737
|
-
outputPath
|
|
738
|
-
],
|
|
739
|
-
errorMessage: "Prettier not found"
|
|
740
|
-
},
|
|
741
|
-
biome: {
|
|
742
|
-
command: "biome",
|
|
743
|
-
args: (outputPath) => [
|
|
744
|
-
"format",
|
|
745
|
-
"--write",
|
|
746
|
-
outputPath
|
|
747
|
-
],
|
|
748
|
-
errorMessage: "Biome not found"
|
|
749
|
-
},
|
|
750
|
-
oxfmt: {
|
|
751
|
-
command: "oxfmt",
|
|
752
|
-
args: (outputPath) => [outputPath],
|
|
753
|
-
errorMessage: "Oxfmt not found"
|
|
754
|
-
}
|
|
755
|
-
};
|
|
756
|
-
//#endregion
|
|
757
|
-
//#region src/createAdapter.ts
|
|
758
|
-
/**
|
|
759
|
-
* Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
|
|
760
|
-
*
|
|
761
|
-
* @example
|
|
762
|
-
* export const myAdapter = createAdapter<MyAdapter>((options) => {
|
|
763
|
-
* return {
|
|
764
|
-
* name: 'my-adapter',
|
|
765
|
-
* options,
|
|
766
|
-
* async parse(source) { ... },
|
|
767
|
-
* }
|
|
768
|
-
* })
|
|
769
|
-
*
|
|
770
|
-
* // instantiate
|
|
771
|
-
* const adapter = myAdapter({ validate: true })
|
|
772
|
-
*/
|
|
773
|
-
function createAdapter(build) {
|
|
774
|
-
return (options) => build(options ?? {});
|
|
775
|
-
}
|
|
776
|
-
//#endregion
|
|
777
|
-
//#region ../../node_modules/.pnpm/yocto-queue@1.2.2/node_modules/yocto-queue/index.js
|
|
778
|
-
var Node = class {
|
|
779
|
-
value;
|
|
780
|
-
next;
|
|
781
|
-
constructor(value) {
|
|
782
|
-
this.value = value;
|
|
783
|
-
}
|
|
784
|
-
};
|
|
785
|
-
var Queue = class {
|
|
786
|
-
#head;
|
|
787
|
-
#tail;
|
|
788
|
-
#size;
|
|
789
|
-
constructor() {
|
|
790
|
-
this.clear();
|
|
791
|
-
}
|
|
792
|
-
enqueue(value) {
|
|
793
|
-
const node = new Node(value);
|
|
794
|
-
if (this.#head) {
|
|
795
|
-
this.#tail.next = node;
|
|
796
|
-
this.#tail = node;
|
|
797
|
-
} else {
|
|
798
|
-
this.#head = node;
|
|
799
|
-
this.#tail = node;
|
|
800
|
-
}
|
|
801
|
-
this.#size++;
|
|
802
|
-
}
|
|
803
|
-
dequeue() {
|
|
804
|
-
const current = this.#head;
|
|
805
|
-
if (!current) return;
|
|
806
|
-
this.#head = this.#head.next;
|
|
807
|
-
this.#size--;
|
|
808
|
-
if (!this.#head) this.#tail = void 0;
|
|
809
|
-
return current.value;
|
|
810
|
-
}
|
|
811
|
-
peek() {
|
|
812
|
-
if (!this.#head) return;
|
|
813
|
-
return this.#head.value;
|
|
814
|
-
}
|
|
815
|
-
clear() {
|
|
816
|
-
this.#head = void 0;
|
|
817
|
-
this.#tail = void 0;
|
|
818
|
-
this.#size = 0;
|
|
819
|
-
}
|
|
820
|
-
get size() {
|
|
821
|
-
return this.#size;
|
|
822
|
-
}
|
|
823
|
-
*[Symbol.iterator]() {
|
|
824
|
-
let current = this.#head;
|
|
825
|
-
while (current) {
|
|
826
|
-
yield current.value;
|
|
827
|
-
current = current.next;
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
|
-
*drain() {
|
|
831
|
-
while (this.#head) yield this.dequeue();
|
|
832
|
-
}
|
|
833
|
-
};
|
|
834
|
-
//#endregion
|
|
835
|
-
//#region ../../node_modules/.pnpm/p-limit@7.3.0/node_modules/p-limit/index.js
|
|
836
|
-
function pLimit(concurrency) {
|
|
837
|
-
let rejectOnClear = false;
|
|
838
|
-
if (typeof concurrency === "object") ({concurrency, rejectOnClear = false} = concurrency);
|
|
839
|
-
validateConcurrency(concurrency);
|
|
840
|
-
if (typeof rejectOnClear !== "boolean") throw new TypeError("Expected `rejectOnClear` to be a boolean");
|
|
841
|
-
const queue = new Queue();
|
|
842
|
-
let activeCount = 0;
|
|
843
|
-
const resumeNext = () => {
|
|
844
|
-
if (activeCount < concurrency && queue.size > 0) {
|
|
845
|
-
activeCount++;
|
|
846
|
-
queue.dequeue().run();
|
|
847
|
-
}
|
|
848
|
-
};
|
|
849
|
-
const next = () => {
|
|
850
|
-
activeCount--;
|
|
851
|
-
resumeNext();
|
|
852
|
-
};
|
|
853
|
-
const run = async (function_, resolve, arguments_) => {
|
|
854
|
-
const result = (async () => function_(...arguments_))();
|
|
855
|
-
resolve(result);
|
|
856
|
-
try {
|
|
857
|
-
await result;
|
|
858
|
-
} catch {}
|
|
859
|
-
next();
|
|
860
|
-
};
|
|
861
|
-
const enqueue = (function_, resolve, reject, arguments_) => {
|
|
862
|
-
const queueItem = { reject };
|
|
863
|
-
new Promise((internalResolve) => {
|
|
864
|
-
queueItem.run = internalResolve;
|
|
865
|
-
queue.enqueue(queueItem);
|
|
866
|
-
}).then(run.bind(void 0, function_, resolve, arguments_));
|
|
867
|
-
if (activeCount < concurrency) resumeNext();
|
|
868
|
-
};
|
|
869
|
-
const generator = (function_, ...arguments_) => new Promise((resolve, reject) => {
|
|
870
|
-
enqueue(function_, resolve, reject, arguments_);
|
|
871
|
-
});
|
|
872
|
-
Object.defineProperties(generator, {
|
|
873
|
-
activeCount: { get: () => activeCount },
|
|
874
|
-
pendingCount: { get: () => queue.size },
|
|
875
|
-
clearQueue: { value() {
|
|
876
|
-
if (!rejectOnClear) {
|
|
877
|
-
queue.clear();
|
|
878
|
-
return;
|
|
879
|
-
}
|
|
880
|
-
const abortError = AbortSignal.abort().reason;
|
|
881
|
-
while (queue.size > 0) queue.dequeue().reject(abortError);
|
|
882
|
-
} },
|
|
883
|
-
concurrency: {
|
|
884
|
-
get: () => concurrency,
|
|
885
|
-
set(newConcurrency) {
|
|
886
|
-
validateConcurrency(newConcurrency);
|
|
887
|
-
concurrency = newConcurrency;
|
|
888
|
-
queueMicrotask(() => {
|
|
889
|
-
while (activeCount < concurrency && queue.size > 0) resumeNext();
|
|
890
|
-
});
|
|
891
|
-
}
|
|
892
|
-
},
|
|
893
|
-
map: { async value(iterable, function_) {
|
|
894
|
-
const promises = Array.from(iterable, (value, index) => this(function_, value, index));
|
|
895
|
-
return Promise.all(promises);
|
|
896
|
-
} }
|
|
897
|
-
});
|
|
898
|
-
return generator;
|
|
899
|
-
}
|
|
900
|
-
function validateConcurrency(concurrency) {
|
|
901
|
-
if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) throw new TypeError("Expected `concurrency` to be a number from 1 and up");
|
|
902
|
-
}
|
|
903
|
-
//#endregion
|
|
904
|
-
//#region src/FileProcessor.ts
|
|
905
|
-
function joinSources(file) {
|
|
906
|
-
return file.sources.map((item) => (0, _kubb_ast.extractStringsFromNodes)(item.nodes)).filter(Boolean).join("\n\n");
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* Converts a single file to a string using the registered parsers.
|
|
910
|
-
* Falls back to joining source values when no matching parser is found.
|
|
911
|
-
*/
|
|
912
|
-
var FileProcessor = class {
|
|
913
|
-
#limit = pLimit(100);
|
|
914
|
-
async parse(file, { parsers, extension } = {}) {
|
|
915
|
-
const parseExtName = extension?.[file.extname] || void 0;
|
|
916
|
-
if (!parsers || !file.extname) return joinSources(file);
|
|
917
|
-
const parser = parsers.get(file.extname);
|
|
918
|
-
if (!parser) return joinSources(file);
|
|
919
|
-
return parser.parse(file, { extname: parseExtName });
|
|
920
|
-
}
|
|
921
|
-
async run(files, { parsers, mode = "sequential", extension, onStart, onEnd, onUpdate } = {}) {
|
|
922
|
-
await onStart?.(files);
|
|
923
|
-
const total = files.length;
|
|
924
|
-
let processed = 0;
|
|
925
|
-
const processOne = async (file) => {
|
|
926
|
-
const source = await this.parse(file, {
|
|
927
|
-
extension,
|
|
928
|
-
parsers
|
|
929
|
-
});
|
|
930
|
-
const currentProcessed = ++processed;
|
|
931
|
-
const percentage = currentProcessed / total * 100;
|
|
932
|
-
await onUpdate?.({
|
|
933
|
-
file,
|
|
934
|
-
source,
|
|
935
|
-
processed: currentProcessed,
|
|
936
|
-
percentage,
|
|
937
|
-
total
|
|
938
|
-
});
|
|
939
|
-
};
|
|
940
|
-
if (mode === "sequential") for (const file of files) await processOne(file);
|
|
941
|
-
else await Promise.all(files.map((file) => this.#limit(() => processOne(file))));
|
|
942
|
-
await onEnd?.(files);
|
|
943
|
-
return files;
|
|
944
|
-
}
|
|
945
|
-
};
|
|
946
|
-
//#endregion
|
|
947
|
-
//#region src/definePlugin.ts
|
|
948
|
-
/**
|
|
949
|
-
* Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
|
|
950
|
-
*
|
|
951
|
-
* Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
|
|
952
|
-
* so it can normalize them and register their handlers on the `AsyncEventEmitter`.
|
|
953
|
-
*/
|
|
954
|
-
function isHookStylePlugin(plugin) {
|
|
955
|
-
return typeof plugin === "object" && plugin !== null && "hooks" in plugin;
|
|
956
|
-
}
|
|
957
|
-
/**
|
|
958
|
-
* Creates a plugin factory using the new hook-style (`hooks:`) API.
|
|
959
|
-
*
|
|
960
|
-
* The returned factory is called with optional options and produces a `HookStylePlugin`
|
|
961
|
-
* that coexists with plugins created via the legacy `createPlugin` API in the same
|
|
962
|
-
* `kubb.config.ts`.
|
|
963
|
-
*
|
|
964
|
-
* Lifecycle handlers are registered on the `PluginDriver`'s `AsyncEventEmitter`, enabling
|
|
965
|
-
* both the plugin's own handlers and external tooling (CLI, devtools) to observe every event.
|
|
966
|
-
*
|
|
967
|
-
* @example
|
|
968
|
-
* ```ts
|
|
969
|
-
* // With PluginFactoryOptions (recommended for real plugins)
|
|
970
|
-
* export const pluginTs = definePlugin<PluginTs>((options) => ({
|
|
971
|
-
* name: 'plugin-ts',
|
|
972
|
-
* hooks: {
|
|
973
|
-
* 'kubb:plugin:setup'(ctx) {
|
|
974
|
-
* ctx.setResolver(resolverTs) // typed as Partial<ResolverTs>
|
|
975
|
-
* },
|
|
976
|
-
* },
|
|
977
|
-
* }))
|
|
978
|
-
* ```
|
|
979
|
-
*/
|
|
980
|
-
function definePlugin(factory) {
|
|
981
|
-
return (options) => factory(options ?? {});
|
|
982
|
-
}
|
|
983
|
-
//#endregion
|
|
984
|
-
//#region src/defineResolver.ts
|
|
985
|
-
/**
|
|
986
|
-
* Checks if an operation matches a pattern for a given filter type (`tag`, `operationId`, `path`, `method`).
|
|
987
|
-
*/
|
|
988
|
-
function matchesOperationPattern(node, type, pattern) {
|
|
989
|
-
switch (type) {
|
|
990
|
-
case "tag": return node.tags.some((tag) => !!tag.match(pattern));
|
|
991
|
-
case "operationId": return !!node.operationId.match(pattern);
|
|
992
|
-
case "path": return !!node.path.match(pattern);
|
|
993
|
-
case "method": return !!node.method.toLowerCase().match(pattern);
|
|
994
|
-
case "contentType": return !!node.requestBody?.contentType?.match(pattern);
|
|
995
|
-
default: return false;
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
/**
|
|
999
|
-
* Checks if a schema matches a pattern for a given filter type (`schemaName`).
|
|
1000
|
-
*
|
|
1001
|
-
* Returns `null` when the filter type doesn't apply to schemas.
|
|
1002
|
-
*/
|
|
1003
|
-
function matchesSchemaPattern(node, type, pattern) {
|
|
1004
|
-
switch (type) {
|
|
1005
|
-
case "schemaName": return node.name ? !!node.name.match(pattern) : false;
|
|
1006
|
-
default: return null;
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
/**
|
|
1010
|
-
* Default name resolver used by `defineResolver`.
|
|
1011
|
-
*
|
|
1012
|
-
* - `camelCase` for `function` and `file` types.
|
|
1013
|
-
* - `PascalCase` for `type`.
|
|
1014
|
-
* - `camelCase` for everything else.
|
|
1015
|
-
*/
|
|
1016
|
-
function defaultResolver(name, type) {
|
|
1017
|
-
let resolvedName = camelCase(name);
|
|
1018
|
-
if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
|
|
1019
|
-
if (type === "type") resolvedName = pascalCase(name);
|
|
1020
|
-
return resolvedName;
|
|
1021
|
-
}
|
|
1022
|
-
/**
|
|
1023
|
-
* Default option resolver — applies include/exclude filters and merges matching override options.
|
|
1024
|
-
*
|
|
1025
|
-
* Returns `null` when the node is filtered out by an `exclude` rule or not matched by any `include` rule.
|
|
1026
|
-
*
|
|
1027
|
-
* @example Include/exclude filtering
|
|
1028
|
-
* ```ts
|
|
1029
|
-
* const options = defaultResolveOptions(operationNode, {
|
|
1030
|
-
* options: { output: 'types' },
|
|
1031
|
-
* exclude: [{ type: 'tag', pattern: 'internal' }],
|
|
1032
|
-
* })
|
|
1033
|
-
* // → null when node has tag 'internal'
|
|
1034
|
-
* ```
|
|
1035
|
-
*
|
|
1036
|
-
* @example Override merging
|
|
1037
|
-
* ```ts
|
|
1038
|
-
* const options = defaultResolveOptions(operationNode, {
|
|
1039
|
-
* options: { enumType: 'asConst' },
|
|
1040
|
-
* override: [{ type: 'operationId', pattern: 'listPets', options: { enumType: 'enum' } }],
|
|
1041
|
-
* })
|
|
1042
|
-
* // → { enumType: 'enum' } when operationId matches
|
|
1043
|
-
* ```
|
|
1044
|
-
*/
|
|
1045
|
-
function defaultResolveOptions(node, { options, exclude = [], include, override = [] }) {
|
|
1046
|
-
if ((0, _kubb_ast.isOperationNode)(node)) {
|
|
1047
|
-
if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
|
|
1048
|
-
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
|
|
1049
|
-
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
|
|
1050
|
-
return {
|
|
1051
|
-
...options,
|
|
1052
|
-
...overrideOptions
|
|
1053
|
-
};
|
|
1054
|
-
}
|
|
1055
|
-
if ((0, _kubb_ast.isSchemaNode)(node)) {
|
|
1056
|
-
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
|
|
1057
|
-
if (include) {
|
|
1058
|
-
const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
|
|
1059
|
-
if (applicable.length > 0 && !applicable.includes(true)) return null;
|
|
1060
|
-
}
|
|
1061
|
-
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
|
|
1062
|
-
return {
|
|
1063
|
-
...options,
|
|
1064
|
-
...overrideOptions
|
|
1065
|
-
};
|
|
1066
|
-
}
|
|
1067
|
-
return options;
|
|
1068
|
-
}
|
|
1069
|
-
/**
|
|
1070
|
-
* Default path resolver used by `defineResolver`.
|
|
1071
|
-
*
|
|
1072
|
-
* - Returns the output directory in `single` mode.
|
|
1073
|
-
* - Resolves into a tag- or path-based subdirectory when `group` and a `tag`/`path` value are provided.
|
|
1074
|
-
* - Falls back to a flat `output/baseName` path otherwise.
|
|
1075
|
-
*
|
|
1076
|
-
* A custom `group.name` function overrides the default subdirectory naming.
|
|
1077
|
-
* For `tag` groups the default is `${camelCase(tag)}Controller`.
|
|
1078
|
-
* For `path` groups the default is the first path segment after `/`.
|
|
1079
|
-
*
|
|
1080
|
-
* @example Flat output
|
|
1081
|
-
* ```ts
|
|
1082
|
-
* defaultResolvePath({ baseName: 'petTypes.ts' }, { root: '/src', output: { path: 'types' } })
|
|
1083
|
-
* // → '/src/types/petTypes.ts'
|
|
1084
|
-
* ```
|
|
1085
|
-
*
|
|
1086
|
-
* @example Tag-based grouping
|
|
1087
|
-
* ```ts
|
|
1088
|
-
* defaultResolvePath(
|
|
1089
|
-
* { baseName: 'petTypes.ts', tag: 'pets' },
|
|
1090
|
-
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
1091
|
-
* )
|
|
1092
|
-
* // → '/src/types/petsController/petTypes.ts'
|
|
1093
|
-
* ```
|
|
1094
|
-
*
|
|
1095
|
-
* @example Path-based grouping
|
|
1096
|
-
* ```ts
|
|
1097
|
-
* defaultResolvePath(
|
|
1098
|
-
* { baseName: 'petTypes.ts', path: '/pets/list' },
|
|
1099
|
-
* { root: '/src', output: { path: 'types' }, group: { type: 'path' } },
|
|
1100
|
-
* )
|
|
1101
|
-
* // → '/src/types/pets/petTypes.ts'
|
|
1102
|
-
* ```
|
|
1103
|
-
*
|
|
1104
|
-
* @example Single-file mode
|
|
1105
|
-
* ```ts
|
|
1106
|
-
* defaultResolvePath(
|
|
1107
|
-
* { baseName: 'petTypes.ts', pathMode: 'single' },
|
|
1108
|
-
* { root: '/src', output: { path: 'types' } },
|
|
1109
|
-
* )
|
|
1110
|
-
* // → '/src/types'
|
|
1111
|
-
* ```
|
|
1112
|
-
*/
|
|
1113
|
-
function defaultResolvePath({ baseName, pathMode, tag, path: groupPath }, { root, output, group }) {
|
|
1114
|
-
if ((pathMode ?? getMode(node_path.default.resolve(root, output.path))) === "single") return node_path.default.resolve(root, output.path);
|
|
1115
|
-
if (group && (groupPath || tag)) return node_path.default.resolve(root, output.path, group.name({ group: group.type === "path" ? groupPath : tag }), baseName);
|
|
1116
|
-
return node_path.default.resolve(root, output.path, baseName);
|
|
1117
|
-
}
|
|
1118
|
-
/**
|
|
1119
|
-
* Default file resolver used by `defineResolver`.
|
|
1120
|
-
*
|
|
1121
|
-
* Resolves a `FileNode` by combining name resolution (`resolver.default`) with
|
|
1122
|
-
* path resolution (`resolver.resolvePath`). The resolved file always has empty
|
|
1123
|
-
* `sources`, `imports`, and `exports` arrays — consumers populate those separately.
|
|
1124
|
-
*
|
|
1125
|
-
* In `single` mode the name is omitted and the file sits directly in the output directory.
|
|
1126
|
-
*
|
|
1127
|
-
* @example Resolve a schema file
|
|
1128
|
-
* ```ts
|
|
1129
|
-
* const file = defaultResolveFile.call(resolver,
|
|
1130
|
-
* { name: 'pet', extname: '.ts' },
|
|
1131
|
-
* { root: '/src', output: { path: 'types' } },
|
|
1132
|
-
* )
|
|
1133
|
-
* // → { baseName: 'pet.ts', path: '/src/types/pet.ts', sources: [], ... }
|
|
1134
|
-
* ```
|
|
1135
|
-
*
|
|
1136
|
-
* @example Resolve an operation file with tag grouping
|
|
1137
|
-
* ```ts
|
|
1138
|
-
* const file = defaultResolveFile.call(resolver,
|
|
1139
|
-
* { name: 'listPets', extname: '.ts', tag: 'pets' },
|
|
1140
|
-
* { root: '/src', output: { path: 'types' }, group: { type: 'tag' } },
|
|
1141
|
-
* )
|
|
1142
|
-
* // → { baseName: 'listPets.ts', path: '/src/types/petsController/listPets.ts', ... }
|
|
1143
|
-
* ```
|
|
1144
|
-
*/
|
|
1145
|
-
function defaultResolveFile({ name, extname, tag, path: groupPath }, context) {
|
|
1146
|
-
const pathMode = getMode(node_path.default.resolve(context.root, context.output.path));
|
|
1147
|
-
const baseName = `${pathMode === "single" ? "" : this.default(name, "file")}${extname}`;
|
|
1148
|
-
const filePath = this.resolvePath({
|
|
1149
|
-
baseName,
|
|
1150
|
-
pathMode,
|
|
1151
|
-
tag,
|
|
1152
|
-
path: groupPath
|
|
1153
|
-
}, context);
|
|
1154
|
-
return (0, _kubb_ast.createFile)({
|
|
1155
|
-
path: filePath,
|
|
1156
|
-
baseName: node_path.default.basename(filePath),
|
|
1157
|
-
meta: { pluginName: this.pluginName },
|
|
1158
|
-
sources: [],
|
|
1159
|
-
imports: [],
|
|
1160
|
-
exports: []
|
|
1161
|
-
});
|
|
1162
|
-
}
|
|
1163
|
-
/**
|
|
1164
|
-
* Generates the default "Generated by Kubb" banner from config and optional node metadata.
|
|
1165
|
-
*/
|
|
1166
|
-
function buildDefaultBanner({ title, description, version, config }) {
|
|
1167
|
-
try {
|
|
1168
|
-
let source = "";
|
|
1169
|
-
if (Array.isArray(config.input)) {
|
|
1170
|
-
const first = config.input[0];
|
|
1171
|
-
if (first && "path" in first) source = node_path.default.basename(first.path);
|
|
1172
|
-
} else if ("path" in config.input) source = node_path.default.basename(config.input.path);
|
|
1173
|
-
else if ("data" in config.input) source = "text content";
|
|
1174
|
-
let banner = "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n";
|
|
1175
|
-
if (config.output.defaultBanner === "simple") {
|
|
1176
|
-
banner += "*/\n";
|
|
1177
|
-
return banner;
|
|
1178
|
-
}
|
|
1179
|
-
if (source) banner += `* Source: ${source}\n`;
|
|
1180
|
-
if (title) banner += `* Title: ${title}\n`;
|
|
1181
|
-
if (description) {
|
|
1182
|
-
const formattedDescription = description.replace(/\n/gm, "\n* ");
|
|
1183
|
-
banner += `* Description: ${formattedDescription}\n`;
|
|
1184
|
-
}
|
|
1185
|
-
if (version) banner += `* OpenAPI spec version: ${version}\n`;
|
|
1186
|
-
banner += "*/\n";
|
|
1187
|
-
return banner;
|
|
1188
|
-
} catch (_error) {
|
|
1189
|
-
return "/**\n* Generated by Kubb (https://kubb.dev/).\n* Do not edit manually.\n*/";
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
/**
|
|
1193
|
-
* Default banner resolver — returns the banner string for a generated file.
|
|
1194
|
-
*
|
|
1195
|
-
* A user-supplied `output.banner` overrides the default Kubb "Generated by Kubb" notice.
|
|
1196
|
-
* When no `output.banner` is set, the Kubb notice is used (including `title` and `version`
|
|
1197
|
-
* from the OAS spec when a `node` is provided).
|
|
1198
|
-
*
|
|
1199
|
-
* - When `output.banner` is a function and `node` is provided, returns `output.banner(node)`.
|
|
1200
|
-
* - When `output.banner` is a function and `node` is absent, falls back to the Kubb notice.
|
|
1201
|
-
* - When `output.banner` is a string, returns it directly.
|
|
1202
|
-
* - When `config.output.defaultBanner` is `false`, returns `undefined`.
|
|
1203
|
-
* - Otherwise returns the Kubb "Generated by Kubb" notice.
|
|
1204
|
-
*
|
|
1205
|
-
* @example String banner overrides default
|
|
1206
|
-
* ```ts
|
|
1207
|
-
* defaultResolveBanner(undefined, { output: { banner: '// my banner' }, config })
|
|
1208
|
-
* // → '// my banner'
|
|
1209
|
-
* ```
|
|
1210
|
-
*
|
|
1211
|
-
* @example Function banner with node
|
|
1212
|
-
* ```ts
|
|
1213
|
-
* defaultResolveBanner(inputNode, { output: { banner: (node) => `// v${node.version}` }, config })
|
|
1214
|
-
* // → '// v3.0.0'
|
|
1215
|
-
* ```
|
|
1216
|
-
*
|
|
1217
|
-
* @example No user banner — Kubb notice with OAS metadata
|
|
1218
|
-
* ```ts
|
|
1219
|
-
* defaultResolveBanner(inputNode, { config })
|
|
1220
|
-
* // → '/** Generated by Kubb ... Title: Pet Store ... *\/'
|
|
1221
|
-
* ```
|
|
1222
|
-
*
|
|
1223
|
-
* @example Disabled default banner
|
|
1224
|
-
* ```ts
|
|
1225
|
-
* defaultResolveBanner(undefined, { config: { output: { defaultBanner: false }, ...config } })
|
|
1226
|
-
* // → undefined
|
|
1227
|
-
* ```
|
|
1228
|
-
*/
|
|
1229
|
-
function defaultResolveBanner(node, { output, config }) {
|
|
1230
|
-
if (typeof output?.banner === "function") return output.banner(node);
|
|
1231
|
-
if (typeof output?.banner === "string") return output.banner;
|
|
1232
|
-
if (config.output.defaultBanner === false) return;
|
|
1233
|
-
return buildDefaultBanner({
|
|
1234
|
-
title: node?.meta?.title,
|
|
1235
|
-
version: node?.meta?.version,
|
|
1236
|
-
config
|
|
1237
|
-
});
|
|
1238
|
-
}
|
|
1239
|
-
/**
|
|
1240
|
-
* Default footer resolver — returns the footer string for a generated file.
|
|
1241
|
-
*
|
|
1242
|
-
* - When `output.footer` is a function and `node` is provided, calls it with the node.
|
|
1243
|
-
* - When `output.footer` is a function and `node` is absent, returns `undefined`.
|
|
1244
|
-
* - When `output.footer` is a string, returns it directly.
|
|
1245
|
-
* - Otherwise returns `undefined`.
|
|
1246
|
-
*
|
|
1247
|
-
* @example String footer
|
|
1248
|
-
* ```ts
|
|
1249
|
-
* defaultResolveFooter(undefined, { output: { footer: '// end of file' }, config })
|
|
1250
|
-
* // → '// end of file'
|
|
1251
|
-
* ```
|
|
1252
|
-
*
|
|
1253
|
-
* @example Function footer with node
|
|
1254
|
-
* ```ts
|
|
1255
|
-
* defaultResolveFooter(inputNode, { output: { footer: (node) => `// ${node.title}` }, config })
|
|
1256
|
-
* // → '// Pet Store'
|
|
1257
|
-
* ```
|
|
1258
|
-
*/
|
|
1259
|
-
function defaultResolveFooter(node, { output }) {
|
|
1260
|
-
if (typeof output?.footer === "function") return node ? output.footer(node) : void 0;
|
|
1261
|
-
if (typeof output?.footer === "string") return output.footer;
|
|
1262
|
-
}
|
|
1263
|
-
/**
|
|
1264
|
-
* Defines a resolver for a plugin, injecting built-in defaults for name casing,
|
|
1265
|
-
* include/exclude/override filtering, path resolution, and file construction.
|
|
1266
|
-
*
|
|
1267
|
-
* All four defaults can be overridden by providing them in the builder function:
|
|
1268
|
-
* - `default` — name casing strategy (camelCase / PascalCase)
|
|
1269
|
-
* - `resolveOptions` — include/exclude/override filtering
|
|
1270
|
-
* - `resolvePath` — output path computation
|
|
1271
|
-
* - `resolveFile` — full `FileNode` construction
|
|
1272
|
-
*
|
|
1273
|
-
* Methods in the builder have access to `this` (the full resolver object), so they
|
|
1274
|
-
* can call other resolver methods without circular imports.
|
|
1275
|
-
*
|
|
1276
|
-
* @example Basic resolver with naming helpers
|
|
1277
|
-
* ```ts
|
|
1278
|
-
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
1279
|
-
* name: 'default',
|
|
1280
|
-
* resolveName(node) {
|
|
1281
|
-
* return this.default(node.name, 'function')
|
|
1282
|
-
* },
|
|
1283
|
-
* resolveTypedName(node) {
|
|
1284
|
-
* return this.default(node.name, 'type')
|
|
1285
|
-
* },
|
|
1286
|
-
* }))
|
|
1287
|
-
* ```
|
|
1288
|
-
*
|
|
1289
|
-
* @example Override resolvePath for a custom output structure
|
|
1290
|
-
* ```ts
|
|
1291
|
-
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
1292
|
-
* name: 'custom',
|
|
1293
|
-
* resolvePath({ baseName }, { root, output }) {
|
|
1294
|
-
* return path.resolve(root, output.path, 'generated', baseName)
|
|
1295
|
-
* },
|
|
1296
|
-
* }))
|
|
1297
|
-
* ```
|
|
1298
|
-
*
|
|
1299
|
-
* @example Use this.default inside a helper
|
|
1300
|
-
* ```ts
|
|
1301
|
-
* export const resolver = defineResolver<PluginTs>(() => ({
|
|
1302
|
-
* name: 'default',
|
|
1303
|
-
* resolveParamName(node, param) {
|
|
1304
|
-
* return this.default(`${node.operationId} ${param.in} ${param.name}`, 'type')
|
|
1305
|
-
* },
|
|
1306
|
-
* }))
|
|
1307
|
-
* ```
|
|
1308
|
-
*/
|
|
1309
|
-
function defineResolver(build) {
|
|
1310
|
-
return {
|
|
1311
|
-
default: defaultResolver,
|
|
1312
|
-
resolveOptions: defaultResolveOptions,
|
|
1313
|
-
resolvePath: defaultResolvePath,
|
|
1314
|
-
resolveFile: defaultResolveFile,
|
|
1315
|
-
resolveBanner: defaultResolveBanner,
|
|
1316
|
-
resolveFooter: defaultResolveFooter,
|
|
1317
|
-
...build()
|
|
1318
|
-
};
|
|
1319
|
-
}
|
|
1320
|
-
//#endregion
|
|
1321
|
-
//#region src/devtools.ts
|
|
1322
|
-
/**
|
|
1323
|
-
* Encodes an `InputNode` as a compressed, URL-safe string.
|
|
1324
|
-
*
|
|
1325
|
-
* The JSON representation is deflate-compressed with {@link deflateSync} before
|
|
1326
|
-
* base64url encoding, which typically reduces payload size by 70–80 % and
|
|
1327
|
-
* keeps URLs well within browser and server path-length limits.
|
|
1328
|
-
*
|
|
1329
|
-
* Use {@link decodeAst} to reverse.
|
|
1330
|
-
*/
|
|
1331
|
-
function encodeAst(input) {
|
|
1332
|
-
const compressed = (0, fflate.deflateSync)(new TextEncoder().encode(JSON.stringify(input)));
|
|
1333
|
-
return Buffer.from(compressed).toString("base64url");
|
|
1334
|
-
}
|
|
1335
|
-
/**
|
|
1336
|
-
* Constructs the Kubb Studio URL for the given `InputNode`.
|
|
1337
|
-
* When `options.ast` is `true`, navigates to the AST inspector (`/ast`).
|
|
1338
|
-
* The `input` is encoded and attached as the `?root=` query parameter so Studio
|
|
1339
|
-
* can decode and render it without a round-trip to any server.
|
|
1340
|
-
*/
|
|
1341
|
-
function getStudioUrl(input, studioUrl, options = {}) {
|
|
1342
|
-
return `${studioUrl.replace(/\/$/, "")}${options.ast ? "/ast" : ""}?root=${encodeAst(input)}`;
|
|
1343
|
-
}
|
|
1344
|
-
/**
|
|
1345
|
-
* Opens the Kubb Studio URL for the given `InputNode` in the default browser —
|
|
1346
|
-
*
|
|
1347
|
-
* Falls back to printing the URL if the browser cannot be launched.
|
|
1348
|
-
*/
|
|
1349
|
-
async function openInStudio(input, studioUrl, options = {}) {
|
|
1350
|
-
const url = getStudioUrl(input, studioUrl, options);
|
|
1351
|
-
const cmd = process.platform === "win32" ? "cmd" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
1352
|
-
const args = process.platform === "win32" ? [
|
|
1353
|
-
"/c",
|
|
1354
|
-
"start",
|
|
1355
|
-
"",
|
|
1356
|
-
url
|
|
1357
|
-
] : [url];
|
|
1358
|
-
try {
|
|
1359
|
-
await (0, tinyexec.x)(cmd, args);
|
|
1360
|
-
} catch {
|
|
1361
|
-
console.log(`\n ${url}\n`);
|
|
1362
|
-
}
|
|
1363
|
-
}
|
|
1364
|
-
//#endregion
|
|
1365
|
-
//#region src/FileManager.ts
|
|
1366
|
-
function mergeFile(a, b) {
|
|
1367
|
-
return {
|
|
1368
|
-
...a,
|
|
1369
|
-
sources: [...a.sources || [], ...b.sources || []],
|
|
1370
|
-
imports: [...a.imports || [], ...b.imports || []],
|
|
1371
|
-
exports: [...a.exports || [], ...b.exports || []]
|
|
1372
|
-
};
|
|
1373
|
-
}
|
|
1374
|
-
/**
|
|
1375
|
-
* In-memory file store for generated files.
|
|
1376
|
-
*
|
|
1377
|
-
* Files with the same `path` are merged — sources, imports, and exports are concatenated.
|
|
1378
|
-
* The `files` getter returns all stored files sorted by path length (shortest first).
|
|
1379
|
-
*
|
|
1380
|
-
* @example
|
|
1381
|
-
* ```ts
|
|
1382
|
-
* import { FileManager } from '@kubb/core'
|
|
1383
|
-
*
|
|
1384
|
-
* const manager = new FileManager()
|
|
1385
|
-
* manager.upsert(myFile)
|
|
1386
|
-
* console.log(manager.files) // all stored files
|
|
1387
|
-
* ```
|
|
1388
|
-
*/
|
|
1389
|
-
var FileManager = class {
|
|
1390
|
-
#cache = /* @__PURE__ */ new Map();
|
|
1391
|
-
#filesCache = null;
|
|
1392
|
-
/**
|
|
1393
|
-
* Adds one or more files. Files with the same path are merged — sources, imports,
|
|
1394
|
-
* and exports from all calls with the same path are concatenated together.
|
|
1395
|
-
*/
|
|
1396
|
-
add(...files) {
|
|
1397
|
-
const resolvedFiles = [];
|
|
1398
|
-
const mergedFiles = /* @__PURE__ */ new Map();
|
|
1399
|
-
files.forEach((file) => {
|
|
1400
|
-
const existing = mergedFiles.get(file.path);
|
|
1401
|
-
if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
|
|
1402
|
-
else mergedFiles.set(file.path, file);
|
|
1403
|
-
});
|
|
1404
|
-
for (const file of mergedFiles.values()) {
|
|
1405
|
-
const resolvedFile = (0, _kubb_ast.createFile)(file);
|
|
1406
|
-
this.#cache.set(resolvedFile.path, resolvedFile);
|
|
1407
|
-
this.#filesCache = null;
|
|
1408
|
-
resolvedFiles.push(resolvedFile);
|
|
1409
|
-
}
|
|
1410
|
-
return resolvedFiles;
|
|
1411
|
-
}
|
|
1412
|
-
/**
|
|
1413
|
-
* Adds or merges one or more files.
|
|
1414
|
-
* If a file with the same path already exists, its sources/imports/exports are merged together.
|
|
1415
|
-
*/
|
|
1416
|
-
upsert(...files) {
|
|
1417
|
-
const resolvedFiles = [];
|
|
1418
|
-
const mergedFiles = /* @__PURE__ */ new Map();
|
|
1419
|
-
files.forEach((file) => {
|
|
1420
|
-
const existing = mergedFiles.get(file.path);
|
|
1421
|
-
if (existing) mergedFiles.set(file.path, mergeFile(existing, file));
|
|
1422
|
-
else mergedFiles.set(file.path, file);
|
|
1423
|
-
});
|
|
1424
|
-
for (const file of mergedFiles.values()) {
|
|
1425
|
-
const existing = this.#cache.get(file.path);
|
|
1426
|
-
const resolvedFile = (0, _kubb_ast.createFile)(existing ? mergeFile(existing, file) : file);
|
|
1427
|
-
this.#cache.set(resolvedFile.path, resolvedFile);
|
|
1428
|
-
this.#filesCache = null;
|
|
1429
|
-
resolvedFiles.push(resolvedFile);
|
|
1430
|
-
}
|
|
1431
|
-
return resolvedFiles;
|
|
1432
|
-
}
|
|
1433
|
-
getByPath(path) {
|
|
1434
|
-
return this.#cache.get(path) ?? null;
|
|
1435
|
-
}
|
|
1436
|
-
deleteByPath(path) {
|
|
1437
|
-
this.#cache.delete(path);
|
|
1438
|
-
this.#filesCache = null;
|
|
1439
|
-
}
|
|
1440
|
-
clear() {
|
|
1441
|
-
this.#cache.clear();
|
|
1442
|
-
this.#filesCache = null;
|
|
1443
|
-
}
|
|
1444
|
-
/**
|
|
1445
|
-
* All stored files, sorted by path length (shorter paths first).
|
|
1446
|
-
* Barrel/index files (e.g. index.ts) are sorted last within each length bucket.
|
|
1447
|
-
*/
|
|
1448
|
-
get files() {
|
|
1449
|
-
if (this.#filesCache) return this.#filesCache;
|
|
1450
|
-
const keys = [...this.#cache.keys()].sort((a, b) => {
|
|
1451
|
-
if (a.length !== b.length) return a.length - b.length;
|
|
1452
|
-
const aIsIndex = trimExtName$1(a).endsWith("index");
|
|
1453
|
-
if (aIsIndex !== trimExtName$1(b).endsWith("index")) return aIsIndex ? 1 : -1;
|
|
1454
|
-
return 0;
|
|
1455
|
-
});
|
|
1456
|
-
const files = [];
|
|
1457
|
-
for (const key of keys) {
|
|
1458
|
-
const file = this.#cache.get(key);
|
|
1459
|
-
if (file) files.push(file);
|
|
1460
|
-
}
|
|
1461
|
-
this.#filesCache = files;
|
|
1462
|
-
return files;
|
|
1463
|
-
}
|
|
1464
|
-
};
|
|
1465
|
-
//#endregion
|
|
1466
|
-
//#region src/renderNode.ts
|
|
1467
|
-
/**
|
|
1468
|
-
* Handles the return value of a plugin AST hook or generator method.
|
|
1469
|
-
*
|
|
1470
|
-
* - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
|
|
1471
|
-
* - `Array<FileNode>` → added directly into `driver.fileManager`
|
|
1472
|
-
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
1473
|
-
*
|
|
1474
|
-
* Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
|
|
1475
|
-
* may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
|
|
1476
|
-
*/
|
|
1477
|
-
async function applyHookResult(result, driver, rendererFactory) {
|
|
1478
|
-
if (!result) return;
|
|
1479
|
-
if (Array.isArray(result)) {
|
|
1480
|
-
driver.fileManager.upsert(...result);
|
|
1481
|
-
return;
|
|
1482
|
-
}
|
|
1483
|
-
if (!rendererFactory) return;
|
|
1484
|
-
const renderer = rendererFactory();
|
|
1485
|
-
await renderer.render(result);
|
|
1486
|
-
driver.fileManager.upsert(...renderer.files);
|
|
1487
|
-
renderer.unmount();
|
|
1488
|
-
}
|
|
1489
|
-
//#endregion
|
|
1490
|
-
//#region src/utils/executeStrategies.ts
|
|
1491
|
-
/**
|
|
1492
|
-
* Runs promise functions in sequence, threading each result into the next call.
|
|
1493
|
-
*
|
|
1494
|
-
* - Each function receives the accumulated state from the previous call.
|
|
1495
|
-
* - Skips functions that return a falsy value (acts as a no-op for that step).
|
|
1496
|
-
* - Returns an array of all individual results.
|
|
1497
|
-
* @deprecated
|
|
1498
|
-
*/
|
|
1499
|
-
function hookSeq(promises) {
|
|
1500
|
-
return promises.filter(Boolean).reduce((promise, func) => {
|
|
1501
|
-
if (typeof func !== "function") throw new Error("HookSeq needs a function that returns a promise `() => Promise<unknown>`");
|
|
1502
|
-
return promise.then((state) => {
|
|
1503
|
-
const calledFunc = func(state);
|
|
1504
|
-
if (calledFunc) return calledFunc.then(Array.prototype.concat.bind(state));
|
|
1505
|
-
return state;
|
|
1506
|
-
});
|
|
1507
|
-
}, Promise.resolve([]));
|
|
1508
|
-
}
|
|
1509
|
-
/**
|
|
1510
|
-
* Runs promise functions in sequence and returns the first non-null result.
|
|
1511
|
-
*
|
|
1512
|
-
* - Stops as soon as `nullCheck` passes for a result (default: `!== null`).
|
|
1513
|
-
* - Subsequent functions are skipped once a match is found.
|
|
1514
|
-
* @deprecated
|
|
1515
|
-
*/
|
|
1516
|
-
function hookFirst(promises, nullCheck = (state) => state !== null) {
|
|
1517
|
-
let promise = Promise.resolve(null);
|
|
1518
|
-
for (const func of promises.filter(Boolean)) promise = promise.then((state) => {
|
|
1519
|
-
if (nullCheck(state)) return state;
|
|
1520
|
-
return func(state);
|
|
1521
|
-
});
|
|
1522
|
-
return promise;
|
|
1523
|
-
}
|
|
1524
|
-
/**
|
|
1525
|
-
* Runs promise functions concurrently and returns all settled results.
|
|
1526
|
-
*
|
|
1527
|
-
* - Limits simultaneous executions to `concurrency` (default: unlimited).
|
|
1528
|
-
* - Uses `Promise.allSettled` so individual failures do not cancel other tasks.
|
|
1529
|
-
* @deprecated
|
|
1530
|
-
*/
|
|
1531
|
-
function hookParallel(promises, concurrency = Number.POSITIVE_INFINITY) {
|
|
1532
|
-
const limit = pLimit(concurrency);
|
|
1533
|
-
const tasks = promises.filter(Boolean).map((promise) => limit(() => promise()));
|
|
1534
|
-
return Promise.allSettled(tasks);
|
|
1535
|
-
}
|
|
1536
|
-
//#endregion
|
|
1537
|
-
//#region src/PluginDriver.ts
|
|
1538
|
-
/**
|
|
1539
|
-
* Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
|
|
1540
|
-
*
|
|
1541
|
-
* @example
|
|
1542
|
-
* ```ts
|
|
1543
|
-
* getMode('src/gen/types.ts') // 'single'
|
|
1544
|
-
* getMode('src/gen/types') // 'split'
|
|
1545
|
-
* ```
|
|
1546
|
-
*/
|
|
1547
|
-
function getMode(fileOrFolder) {
|
|
1548
|
-
if (!fileOrFolder) return "split";
|
|
1549
|
-
return (0, node_path.extname)(fileOrFolder) ? "single" : "split";
|
|
1550
|
-
}
|
|
1551
|
-
const hookFirstNullCheck = (state) => !!state?.result;
|
|
1552
|
-
var PluginDriver = class {
|
|
1553
|
-
config;
|
|
1554
|
-
options;
|
|
1555
|
-
/**
|
|
1556
|
-
* The universal `@kubb/ast` `InputNode` produced by the adapter, set by
|
|
1557
|
-
* the build pipeline after the adapter's `parse()` resolves.
|
|
1558
|
-
*/
|
|
1559
|
-
inputNode = void 0;
|
|
1560
|
-
adapter = void 0;
|
|
1561
|
-
#studioIsOpen = false;
|
|
1562
|
-
/**
|
|
1563
|
-
* Central file store for all generated files.
|
|
1564
|
-
* Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
|
|
1565
|
-
* add files; this property gives direct read/write access when needed.
|
|
1566
|
-
*/
|
|
1567
|
-
fileManager = new FileManager();
|
|
1568
|
-
plugins = /* @__PURE__ */ new Map();
|
|
1569
|
-
/**
|
|
1570
|
-
* Tracks which plugins have generators registered via `addGenerator()` (event-based path).
|
|
1571
|
-
* Used by the build loop to decide whether to emit generator events for a given plugin.
|
|
1572
|
-
*/
|
|
1573
|
-
#pluginsWithEventGenerators = /* @__PURE__ */ new Set();
|
|
1574
|
-
#resolvers = /* @__PURE__ */ new Map();
|
|
1575
|
-
#defaultResolvers = /* @__PURE__ */ new Map();
|
|
1576
|
-
#hookListeners = /* @__PURE__ */ new Map();
|
|
1577
|
-
constructor(config, options) {
|
|
1578
|
-
this.config = config;
|
|
1579
|
-
this.options = {
|
|
1580
|
-
...options,
|
|
1581
|
-
hooks: options.hooks
|
|
1582
|
-
};
|
|
1583
|
-
config.plugins.map((rawPlugin) => {
|
|
1584
|
-
if (isHookStylePlugin(rawPlugin)) return this.#normalizeHookStylePlugin(rawPlugin);
|
|
1585
|
-
return {
|
|
1586
|
-
...rawPlugin,
|
|
1587
|
-
buildStart: rawPlugin.buildStart ?? (() => {}),
|
|
1588
|
-
buildEnd: rawPlugin.buildEnd ?? (() => {})
|
|
1589
|
-
};
|
|
1590
|
-
}).filter((plugin) => {
|
|
1591
|
-
if (typeof plugin.apply === "function") return plugin.apply(config);
|
|
1592
|
-
return true;
|
|
1593
|
-
}).sort((a, b) => {
|
|
1594
|
-
if (b.dependencies?.includes(a.name)) return -1;
|
|
1595
|
-
if (a.dependencies?.includes(b.name)) return 1;
|
|
1596
|
-
return 0;
|
|
1597
|
-
}).forEach((plugin) => {
|
|
1598
|
-
this.plugins.set(plugin.name, plugin);
|
|
1599
|
-
});
|
|
1600
|
-
}
|
|
1601
|
-
get hooks() {
|
|
1602
|
-
if (!this.options.hooks) throw new Error("hooks are not defined");
|
|
1603
|
-
return this.options.hooks;
|
|
1604
|
-
}
|
|
1605
|
-
/**
|
|
1606
|
-
* Creates a `Plugin`-compatible object from a hook-style plugin and registers
|
|
1607
|
-
* its lifecycle handlers on the `AsyncEventEmitter`.
|
|
1608
|
-
*
|
|
1609
|
-
* The normalized plugin has an empty `buildStart` — generators registered via
|
|
1610
|
-
* `addGenerator()` in `kubb:plugin:setup` are stored on `normalizedPlugin.generators`
|
|
1611
|
-
* and used by `runPluginAstHooks` during the build.
|
|
1612
|
-
*/
|
|
1613
|
-
#normalizeHookStylePlugin(hookPlugin) {
|
|
1614
|
-
const generators = [];
|
|
1615
|
-
const driver = this;
|
|
1616
|
-
const normalizedPlugin = {
|
|
1617
|
-
name: hookPlugin.name,
|
|
1618
|
-
dependencies: hookPlugin.dependencies,
|
|
1619
|
-
options: {
|
|
1620
|
-
output: { path: "." },
|
|
1621
|
-
exclude: [],
|
|
1622
|
-
override: []
|
|
1623
|
-
},
|
|
1624
|
-
generators,
|
|
1625
|
-
inject: () => void 0,
|
|
1626
|
-
resolveName(name, type) {
|
|
1627
|
-
return driver.getResolver(hookPlugin.name).default(name, type);
|
|
1628
|
-
},
|
|
1629
|
-
resolvePath(baseName, pathMode, resolveOptions) {
|
|
1630
|
-
const resolver = driver.getResolver(hookPlugin.name);
|
|
1631
|
-
const opts = normalizedPlugin.options;
|
|
1632
|
-
const group = resolveOptions?.group;
|
|
1633
|
-
return resolver.resolvePath({
|
|
1634
|
-
baseName,
|
|
1635
|
-
pathMode,
|
|
1636
|
-
tag: group?.tag,
|
|
1637
|
-
path: group?.path
|
|
1638
|
-
}, {
|
|
1639
|
-
root: (0, node_path.resolve)(driver.config.root, driver.config.output.path),
|
|
1640
|
-
output: opts.output,
|
|
1641
|
-
group: opts.group
|
|
1642
|
-
});
|
|
1643
|
-
},
|
|
1644
|
-
buildStart() {},
|
|
1645
|
-
buildEnd() {}
|
|
1646
|
-
};
|
|
1647
|
-
this.registerPluginHooks(hookPlugin, normalizedPlugin);
|
|
1648
|
-
return normalizedPlugin;
|
|
1649
|
-
}
|
|
1650
|
-
/**
|
|
1651
|
-
* Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
|
|
1652
|
-
*
|
|
1653
|
-
* For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
|
|
1654
|
-
* plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
|
|
1655
|
-
* `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
|
|
1656
|
-
*
|
|
1657
|
-
* All other hooks are iterated and registered directly as pass-through listeners.
|
|
1658
|
-
* Any event key present in the global `KubbHooks` interface can be subscribed to.
|
|
1659
|
-
*
|
|
1660
|
-
* External tooling can subscribe to any of these events via `hooks.on(...)` to observe
|
|
1661
|
-
* the plugin lifecycle without modifying plugin behavior.
|
|
1662
|
-
*/
|
|
1663
|
-
registerPluginHooks(hookPlugin, normalizedPlugin) {
|
|
1664
|
-
const { hooks } = hookPlugin;
|
|
1665
|
-
if (hooks["kubb:plugin:setup"]) {
|
|
1666
|
-
const setupHandler = (globalCtx) => {
|
|
1667
|
-
const pluginCtx = {
|
|
1668
|
-
...globalCtx,
|
|
1669
|
-
options: hookPlugin.options ?? {},
|
|
1670
|
-
addGenerator: (gen) => {
|
|
1671
|
-
this.registerGenerator(normalizedPlugin.name, gen);
|
|
1672
|
-
},
|
|
1673
|
-
setResolver: (resolver) => {
|
|
1674
|
-
this.setPluginResolver(normalizedPlugin.name, resolver);
|
|
1675
|
-
},
|
|
1676
|
-
setTransformer: (visitor) => {
|
|
1677
|
-
normalizedPlugin.transformer = visitor;
|
|
1678
|
-
},
|
|
1679
|
-
setRenderer: (renderer) => {
|
|
1680
|
-
normalizedPlugin.renderer = renderer;
|
|
1681
|
-
},
|
|
1682
|
-
setOptions: (opts) => {
|
|
1683
|
-
normalizedPlugin.options = {
|
|
1684
|
-
...normalizedPlugin.options,
|
|
1685
|
-
...opts
|
|
1686
|
-
};
|
|
1687
|
-
},
|
|
1688
|
-
injectFile: (file) => {
|
|
1689
|
-
const fileNode = (0, _kubb_ast.createFile)({
|
|
1690
|
-
baseName: file.baseName,
|
|
1691
|
-
path: file.path,
|
|
1692
|
-
sources: file.sources ?? [],
|
|
1693
|
-
imports: [],
|
|
1694
|
-
exports: []
|
|
1695
|
-
});
|
|
1696
|
-
this.fileManager.add(fileNode);
|
|
1697
|
-
}
|
|
1698
|
-
};
|
|
1699
|
-
return hooks["kubb:plugin:setup"](pluginCtx);
|
|
1700
|
-
};
|
|
1701
|
-
this.hooks.on("kubb:plugin:setup", setupHandler);
|
|
1702
|
-
this.#trackHookListener("kubb:plugin:setup", setupHandler);
|
|
1703
|
-
}
|
|
1704
|
-
for (const [event, handler] of Object.entries(hooks)) {
|
|
1705
|
-
if (event === "kubb:plugin:setup" || !handler) continue;
|
|
1706
|
-
this.hooks.on(event, handler);
|
|
1707
|
-
this.#trackHookListener(event, handler);
|
|
1708
|
-
}
|
|
1709
|
-
}
|
|
1710
|
-
/**
|
|
1711
|
-
* Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
|
|
1712
|
-
* can configure generators, resolvers, transformers and renderers before `buildStart` runs.
|
|
1713
|
-
*
|
|
1714
|
-
* Call this once from `safeBuild` before the plugin execution loop begins.
|
|
1715
|
-
*/
|
|
1716
|
-
async emitSetupHooks() {
|
|
1717
|
-
await this.hooks.emit("kubb:plugin:setup", {
|
|
1718
|
-
config: this.config,
|
|
1719
|
-
addGenerator: () => {},
|
|
1720
|
-
setResolver: () => {},
|
|
1721
|
-
setTransformer: () => {},
|
|
1722
|
-
setRenderer: () => {},
|
|
1723
|
-
setOptions: () => {},
|
|
1724
|
-
injectFile: () => {},
|
|
1725
|
-
updateConfig: () => {},
|
|
1726
|
-
options: {}
|
|
1727
|
-
});
|
|
1728
|
-
}
|
|
1729
|
-
/**
|
|
1730
|
-
* Registers a generator for the given plugin on the shared event emitter.
|
|
1731
|
-
*
|
|
1732
|
-
* The generator's `schema`, `operation`, and `operations` methods are registered as
|
|
1733
|
-
* listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
|
|
1734
|
-
* respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
|
|
1735
|
-
* so that generators from different plugins do not cross-fire.
|
|
1736
|
-
*
|
|
1737
|
-
* The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
|
|
1738
|
-
* Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
|
|
1739
|
-
* declares a renderer.
|
|
1740
|
-
*
|
|
1741
|
-
* Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
|
|
1742
|
-
*/
|
|
1743
|
-
registerGenerator(pluginName, gen) {
|
|
1744
|
-
const resolveRenderer = () => {
|
|
1745
|
-
const plugin = this.plugins.get(pluginName);
|
|
1746
|
-
return gen.renderer === null ? void 0 : gen.renderer ?? plugin?.renderer ?? this.config.renderer;
|
|
1747
|
-
};
|
|
1748
|
-
if (gen.schema) {
|
|
1749
|
-
const schemaHandler = async (node, ctx) => {
|
|
1750
|
-
if (ctx.plugin.name !== pluginName) return;
|
|
1751
|
-
await applyHookResult(await gen.schema(node, ctx), this, resolveRenderer());
|
|
1752
|
-
};
|
|
1753
|
-
this.hooks.on("kubb:generate:schema", schemaHandler);
|
|
1754
|
-
this.#trackHookListener("kubb:generate:schema", schemaHandler);
|
|
1755
|
-
}
|
|
1756
|
-
if (gen.operation) {
|
|
1757
|
-
const operationHandler = async (node, ctx) => {
|
|
1758
|
-
if (ctx.plugin.name !== pluginName) return;
|
|
1759
|
-
await applyHookResult(await gen.operation(node, ctx), this, resolveRenderer());
|
|
1760
|
-
};
|
|
1761
|
-
this.hooks.on("kubb:generate:operation", operationHandler);
|
|
1762
|
-
this.#trackHookListener("kubb:generate:operation", operationHandler);
|
|
1763
|
-
}
|
|
1764
|
-
if (gen.operations) {
|
|
1765
|
-
const operationsHandler = async (nodes, ctx) => {
|
|
1766
|
-
if (ctx.plugin.name !== pluginName) return;
|
|
1767
|
-
await applyHookResult(await gen.operations(nodes, ctx), this, resolveRenderer());
|
|
1768
|
-
};
|
|
1769
|
-
this.hooks.on("kubb:generate:operations", operationsHandler);
|
|
1770
|
-
this.#trackHookListener("kubb:generate:operations", operationsHandler);
|
|
1771
|
-
}
|
|
1772
|
-
this.#pluginsWithEventGenerators.add(pluginName);
|
|
1773
|
-
}
|
|
1774
|
-
/**
|
|
1775
|
-
* Returns `true` when at least one generator was registered for the given plugin
|
|
1776
|
-
* via `addGenerator()` in `kubb:plugin:setup` (event-based path).
|
|
1777
|
-
*
|
|
1778
|
-
* Used by the build loop to decide whether to walk the AST and emit generator events
|
|
1779
|
-
* for a plugin that has no static `plugin.generators`.
|
|
1780
|
-
*/
|
|
1781
|
-
hasRegisteredGenerators(pluginName) {
|
|
1782
|
-
return this.#pluginsWithEventGenerators.has(pluginName);
|
|
1783
|
-
}
|
|
1784
|
-
dispose() {
|
|
1785
|
-
for (const [event, handlers] of this.#hookListeners) for (const handler of handlers) this.hooks.off(event, handler);
|
|
1786
|
-
this.#hookListeners.clear();
|
|
1787
|
-
this.#pluginsWithEventGenerators.clear();
|
|
1788
|
-
}
|
|
1789
|
-
#trackHookListener(event, handler) {
|
|
1790
|
-
let handlers = this.#hookListeners.get(event);
|
|
1791
|
-
if (!handlers) {
|
|
1792
|
-
handlers = /* @__PURE__ */ new Set();
|
|
1793
|
-
this.#hookListeners.set(event, handlers);
|
|
1794
|
-
}
|
|
1795
|
-
handlers.add(handler);
|
|
1796
|
-
}
|
|
1797
|
-
#createDefaultResolver(pluginName) {
|
|
1798
|
-
const existingResolver = this.#defaultResolvers.get(pluginName);
|
|
1799
|
-
if (existingResolver) return existingResolver;
|
|
1800
|
-
const resolver = defineResolver(() => ({
|
|
1801
|
-
name: "default",
|
|
1802
|
-
pluginName
|
|
1803
|
-
}));
|
|
1804
|
-
this.#defaultResolvers.set(pluginName, resolver);
|
|
1805
|
-
return resolver;
|
|
1806
|
-
}
|
|
1807
|
-
setPluginResolver(pluginName, partial) {
|
|
1808
|
-
const merged = {
|
|
1809
|
-
...this.#createDefaultResolver(pluginName),
|
|
1810
|
-
...partial
|
|
1811
|
-
};
|
|
1812
|
-
this.#resolvers.set(pluginName, merged);
|
|
1813
|
-
const plugin = this.plugins.get(pluginName);
|
|
1814
|
-
if (plugin) plugin.resolver = merged;
|
|
1815
|
-
}
|
|
1816
|
-
getResolver(pluginName) {
|
|
1817
|
-
const dynamicResolver = this.#resolvers.get(pluginName);
|
|
1818
|
-
if (dynamicResolver) return dynamicResolver;
|
|
1819
|
-
const pluginResolver = this.plugins.get(pluginName)?.resolver;
|
|
1820
|
-
if (pluginResolver) return pluginResolver;
|
|
1821
|
-
return this.#createDefaultResolver(pluginName);
|
|
1822
|
-
}
|
|
1823
|
-
getContext(plugin) {
|
|
1824
|
-
const driver = this;
|
|
1825
|
-
const baseContext = {
|
|
1826
|
-
config: driver.config,
|
|
1827
|
-
get root() {
|
|
1828
|
-
return (0, node_path.resolve)(driver.config.root, driver.config.output.path);
|
|
1829
|
-
},
|
|
1830
|
-
getMode(output) {
|
|
1831
|
-
return getMode((0, node_path.resolve)(driver.config.root, driver.config.output.path, output.path));
|
|
1832
|
-
},
|
|
1833
|
-
hooks: driver.hooks,
|
|
1834
|
-
plugin,
|
|
1835
|
-
getPlugin: driver.getPlugin.bind(driver),
|
|
1836
|
-
requirePlugin: driver.requirePlugin.bind(driver),
|
|
1837
|
-
driver,
|
|
1838
|
-
addFile: async (...files) => {
|
|
1839
|
-
driver.fileManager.add(...files);
|
|
1840
|
-
},
|
|
1841
|
-
upsertFile: async (...files) => {
|
|
1842
|
-
driver.fileManager.upsert(...files);
|
|
1843
|
-
},
|
|
1844
|
-
get inputNode() {
|
|
1845
|
-
return driver.inputNode;
|
|
1846
|
-
},
|
|
1847
|
-
get adapter() {
|
|
1848
|
-
return driver.adapter;
|
|
1849
|
-
},
|
|
1850
|
-
get resolver() {
|
|
1851
|
-
return driver.getResolver(plugin.name);
|
|
1852
|
-
},
|
|
1853
|
-
get transformer() {
|
|
1854
|
-
return plugin.transformer;
|
|
1855
|
-
},
|
|
1856
|
-
warn(message) {
|
|
1857
|
-
driver.hooks.emit("kubb:warn", message);
|
|
1858
|
-
},
|
|
1859
|
-
error(error) {
|
|
1860
|
-
driver.hooks.emit("kubb:error", typeof error === "string" ? new Error(error) : error);
|
|
1861
|
-
},
|
|
1862
|
-
info(message) {
|
|
1863
|
-
driver.hooks.emit("kubb:info", message);
|
|
1864
|
-
},
|
|
1865
|
-
openInStudio(options) {
|
|
1866
|
-
if (!driver.config.devtools || driver.#studioIsOpen) return;
|
|
1867
|
-
if (typeof driver.config.devtools !== "object") throw new Error("Devtools must be an object");
|
|
1868
|
-
if (!driver.inputNode || !driver.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
|
|
1869
|
-
driver.#studioIsOpen = true;
|
|
1870
|
-
const studioUrl = driver.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
|
|
1871
|
-
return openInStudio(driver.inputNode, studioUrl, options);
|
|
1872
|
-
}
|
|
1873
|
-
};
|
|
1874
|
-
let mergedExtras = {};
|
|
1875
|
-
for (const p of this.plugins.values()) if (typeof p.inject === "function") {
|
|
1876
|
-
const result = p.inject.call(baseContext);
|
|
1877
|
-
if (result !== null && typeof result === "object") mergedExtras = {
|
|
1878
|
-
...mergedExtras,
|
|
1879
|
-
...result
|
|
1880
|
-
};
|
|
1881
|
-
}
|
|
1882
|
-
return {
|
|
1883
|
-
...baseContext,
|
|
1884
|
-
...mergedExtras
|
|
1885
|
-
};
|
|
1886
|
-
}
|
|
1887
|
-
/**
|
|
1888
|
-
* @deprecated use resolvers context instead
|
|
1889
|
-
*/
|
|
1890
|
-
getFile({ name, mode, extname, pluginName, options }) {
|
|
1891
|
-
const resolvedName = mode ? mode === "single" ? "" : this.resolveName({
|
|
1892
|
-
name,
|
|
1893
|
-
pluginName,
|
|
1894
|
-
type: "file"
|
|
1895
|
-
}) : name;
|
|
1896
|
-
const path = this.resolvePath({
|
|
1897
|
-
baseName: `${resolvedName}${extname}`,
|
|
1898
|
-
mode,
|
|
1899
|
-
pluginName,
|
|
1900
|
-
options
|
|
1901
|
-
});
|
|
1902
|
-
if (!path) throw new Error(`Filepath should be defined for resolvedName "${resolvedName}" and pluginName "${pluginName}"`);
|
|
1903
|
-
return (0, _kubb_ast.createFile)({
|
|
1904
|
-
path,
|
|
1905
|
-
baseName: (0, node_path.basename)(path),
|
|
1906
|
-
meta: { pluginName },
|
|
1907
|
-
sources: [],
|
|
1908
|
-
imports: [],
|
|
1909
|
-
exports: []
|
|
1910
|
-
});
|
|
1911
|
-
}
|
|
1912
|
-
/**
|
|
1913
|
-
* @deprecated use resolvers context instead
|
|
1914
|
-
*/
|
|
1915
|
-
resolvePath = (params) => {
|
|
1916
|
-
const defaultPath = (0, node_path.resolve)((0, node_path.resolve)(this.config.root, this.config.output.path), params.baseName);
|
|
1917
|
-
if (params.pluginName) return this.hookForPluginSync({
|
|
1918
|
-
pluginName: params.pluginName,
|
|
1919
|
-
hookName: "resolvePath",
|
|
1920
|
-
parameters: [
|
|
1921
|
-
params.baseName,
|
|
1922
|
-
params.mode,
|
|
1923
|
-
params.options
|
|
1924
|
-
]
|
|
1925
|
-
})?.at(0) || defaultPath;
|
|
1926
|
-
return this.hookFirstSync({
|
|
1927
|
-
hookName: "resolvePath",
|
|
1928
|
-
parameters: [
|
|
1929
|
-
params.baseName,
|
|
1930
|
-
params.mode,
|
|
1931
|
-
params.options
|
|
1932
|
-
]
|
|
1933
|
-
})?.result || defaultPath;
|
|
1934
|
-
};
|
|
1935
|
-
/**
|
|
1936
|
-
* @deprecated use resolvers context instead
|
|
1937
|
-
*/
|
|
1938
|
-
resolveName = (params) => {
|
|
1939
|
-
if (params.pluginName) return transformReservedWord(this.hookForPluginSync({
|
|
1940
|
-
pluginName: params.pluginName,
|
|
1941
|
-
hookName: "resolveName",
|
|
1942
|
-
parameters: [params.name.trim(), params.type]
|
|
1943
|
-
})?.at(0) ?? params.name);
|
|
1944
|
-
const name = this.hookFirstSync({
|
|
1945
|
-
hookName: "resolveName",
|
|
1946
|
-
parameters: [params.name.trim(), params.type]
|
|
1947
|
-
})?.result;
|
|
1948
|
-
return transformReservedWord(name ?? params.name);
|
|
1949
|
-
};
|
|
1950
|
-
/**
|
|
1951
|
-
* Run a specific hookName for plugin x.
|
|
329
|
+
* ```ts
|
|
330
|
+
* new URLPath('/pet/{petId}').object
|
|
331
|
+
* // { url: '/pet/:petId', params: { petId: 'petId' } }
|
|
332
|
+
* ```
|
|
1952
333
|
*/
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
if (!plugin) return [null];
|
|
1956
|
-
this.hooks.emit("kubb:plugins:hook:progress:start", {
|
|
1957
|
-
hookName,
|
|
1958
|
-
plugins: [plugin]
|
|
1959
|
-
});
|
|
1960
|
-
const result = await this.#execute({
|
|
1961
|
-
strategy: "hookFirst",
|
|
1962
|
-
hookName,
|
|
1963
|
-
parameters,
|
|
1964
|
-
plugin
|
|
1965
|
-
});
|
|
1966
|
-
this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
|
|
1967
|
-
return [result];
|
|
334
|
+
get object() {
|
|
335
|
+
return this.toObject();
|
|
1968
336
|
}
|
|
1969
|
-
/**
|
|
1970
|
-
*
|
|
337
|
+
/** Returns a map of path parameter names, or `undefined` when the path has no parameters.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* new URLPath('/pet/{petId}').params // { petId: 'petId' }
|
|
342
|
+
* new URLPath('/pet').params // undefined
|
|
343
|
+
* ```
|
|
1971
344
|
*/
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
if (!plugin) return null;
|
|
1975
|
-
const result = this.#executeSync({
|
|
1976
|
-
strategy: "hookFirst",
|
|
1977
|
-
hookName,
|
|
1978
|
-
parameters,
|
|
1979
|
-
plugin
|
|
1980
|
-
});
|
|
1981
|
-
return result !== null ? [result] : [];
|
|
345
|
+
get params() {
|
|
346
|
+
return this.getParams();
|
|
1982
347
|
}
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
async hookFirst({ hookName, parameters, skipped }) {
|
|
1987
|
-
const plugins = [];
|
|
1988
|
-
for (const plugin of this.plugins.values()) if (hookName in plugin && (skipped ? !skipped.has(plugin) : true)) plugins.push(plugin);
|
|
1989
|
-
this.hooks.emit("kubb:plugins:hook:progress:start", {
|
|
1990
|
-
hookName,
|
|
1991
|
-
plugins
|
|
1992
|
-
});
|
|
1993
|
-
const result = await hookFirst(plugins.map((plugin) => {
|
|
1994
|
-
return async () => {
|
|
1995
|
-
const value = await this.#execute({
|
|
1996
|
-
strategy: "hookFirst",
|
|
1997
|
-
hookName,
|
|
1998
|
-
parameters,
|
|
1999
|
-
plugin
|
|
2000
|
-
});
|
|
2001
|
-
return Promise.resolve({
|
|
2002
|
-
plugin,
|
|
2003
|
-
result: value
|
|
2004
|
-
});
|
|
2005
|
-
};
|
|
2006
|
-
}), hookFirstNullCheck);
|
|
2007
|
-
this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
|
|
2008
|
-
return result;
|
|
348
|
+
#transformParam(raw) {
|
|
349
|
+
const param = require_PluginDriver.isValidVarName(raw) ? raw : require_PluginDriver.camelCase(raw);
|
|
350
|
+
return this.#options.casing === "camelcase" ? require_PluginDriver.camelCase(param) : param;
|
|
2009
351
|
}
|
|
2010
352
|
/**
|
|
2011
|
-
*
|
|
353
|
+
* Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
|
|
2012
354
|
*/
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
}
|
|
2027
|
-
|
|
355
|
+
#eachParam(fn) {
|
|
356
|
+
for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
|
|
357
|
+
const raw = match[1];
|
|
358
|
+
fn(raw, this.#transformParam(raw));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
toObject({ type = "path", replacer, stringify } = {}) {
|
|
362
|
+
const object = {
|
|
363
|
+
url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
|
|
364
|
+
params: this.getParams()
|
|
365
|
+
};
|
|
366
|
+
if (stringify) {
|
|
367
|
+
if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
|
|
368
|
+
if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
|
|
369
|
+
return `{ url: '${object.url}' }`;
|
|
2028
370
|
}
|
|
2029
|
-
return
|
|
371
|
+
return object;
|
|
2030
372
|
}
|
|
2031
373
|
/**
|
|
2032
|
-
*
|
|
374
|
+
* Converts the OpenAPI path to a TypeScript template literal string.
|
|
375
|
+
* An optional `replacer` can transform each extracted parameter name before interpolation.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
|
|
2033
379
|
*/
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
});
|
|
2041
|
-
const pluginStartTimes = /* @__PURE__ */ new Map();
|
|
2042
|
-
const results = await hookParallel(plugins.map((plugin) => {
|
|
2043
|
-
return () => {
|
|
2044
|
-
pluginStartTimes.set(plugin, node_perf_hooks.performance.now());
|
|
2045
|
-
return this.#execute({
|
|
2046
|
-
strategy: "hookParallel",
|
|
2047
|
-
hookName,
|
|
2048
|
-
parameters,
|
|
2049
|
-
plugin
|
|
2050
|
-
});
|
|
2051
|
-
};
|
|
2052
|
-
}), this.options.concurrency);
|
|
2053
|
-
results.forEach((result, index) => {
|
|
2054
|
-
if (isPromiseRejectedResult(result)) {
|
|
2055
|
-
const plugin = plugins[index];
|
|
2056
|
-
if (plugin) {
|
|
2057
|
-
const startTime = pluginStartTimes.get(plugin) ?? node_perf_hooks.performance.now();
|
|
2058
|
-
this.hooks.emit("kubb:error", result.reason, {
|
|
2059
|
-
plugin,
|
|
2060
|
-
hookName,
|
|
2061
|
-
strategy: "hookParallel",
|
|
2062
|
-
duration: Math.round(node_perf_hooks.performance.now() - startTime),
|
|
2063
|
-
parameters
|
|
2064
|
-
});
|
|
2065
|
-
}
|
|
2066
|
-
}
|
|
2067
|
-
});
|
|
2068
|
-
this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
|
|
2069
|
-
return results.reduce((acc, result) => {
|
|
2070
|
-
if (result.status === "fulfilled") acc.push(result.value);
|
|
2071
|
-
return acc;
|
|
2072
|
-
}, []);
|
|
380
|
+
toTemplateString({ prefix = "", replacer } = {}) {
|
|
381
|
+
return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
382
|
+
if (i % 2 === 0) return part;
|
|
383
|
+
const param = this.#transformParam(part);
|
|
384
|
+
return `\${${replacer ? replacer(param) : param}}`;
|
|
385
|
+
}).join("")}\``;
|
|
2073
386
|
}
|
|
2074
387
|
/**
|
|
2075
|
-
*
|
|
388
|
+
* Extracts all `{param}` segments from the path and returns them as a key-value map.
|
|
389
|
+
* An optional `replacer` transforms each parameter name in both key and value positions.
|
|
390
|
+
* Returns `undefined` when no path parameters are found.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* new URLPath('/pet/{petId}/tag/{tagId}').getParams()
|
|
395
|
+
* // { petId: 'petId', tagId: 'tagId' }
|
|
396
|
+
* ```
|
|
2076
397
|
*/
|
|
2077
|
-
|
|
2078
|
-
const
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
plugins
|
|
398
|
+
getParams(replacer) {
|
|
399
|
+
const params = {};
|
|
400
|
+
this.#eachParam((_raw, param) => {
|
|
401
|
+
const key = replacer ? replacer(param) : param;
|
|
402
|
+
params[key] = key;
|
|
2083
403
|
});
|
|
2084
|
-
|
|
2085
|
-
return () => this.#execute({
|
|
2086
|
-
strategy: "hookSeq",
|
|
2087
|
-
hookName,
|
|
2088
|
-
parameters,
|
|
2089
|
-
plugin
|
|
2090
|
-
});
|
|
2091
|
-
}));
|
|
2092
|
-
this.hooks.emit("kubb:plugins:hook:progress:end", { hookName });
|
|
2093
|
-
}
|
|
2094
|
-
getPlugin(pluginName) {
|
|
2095
|
-
return this.plugins.get(pluginName);
|
|
2096
|
-
}
|
|
2097
|
-
requirePlugin(pluginName) {
|
|
2098
|
-
const plugin = this.plugins.get(pluginName);
|
|
2099
|
-
if (!plugin) throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`);
|
|
2100
|
-
return plugin;
|
|
404
|
+
return Object.keys(params).length > 0 ? params : void 0;
|
|
2101
405
|
}
|
|
2102
|
-
/**
|
|
2103
|
-
*
|
|
406
|
+
/** Converts the OpenAPI path to Express-style colon syntax.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```ts
|
|
410
|
+
* new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
|
|
411
|
+
* ```
|
|
2104
412
|
*/
|
|
2105
|
-
|
|
2106
|
-
this.
|
|
2107
|
-
duration: Math.round(node_perf_hooks.performance.now() - startTime),
|
|
2108
|
-
parameters,
|
|
2109
|
-
output,
|
|
2110
|
-
strategy,
|
|
2111
|
-
hookName,
|
|
2112
|
-
plugin
|
|
2113
|
-
});
|
|
413
|
+
toURLPath() {
|
|
414
|
+
return this.path.replace(/\{([^}]+)\}/g, ":$1");
|
|
2114
415
|
}
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
416
|
+
};
|
|
417
|
+
//#endregion
|
|
418
|
+
//#region src/createAdapter.ts
|
|
419
|
+
/**
|
|
420
|
+
* Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* export const myAdapter = createAdapter<MyAdapter>((options) => {
|
|
424
|
+
* return {
|
|
425
|
+
* name: 'my-adapter',
|
|
426
|
+
* options,
|
|
427
|
+
* async parse(source) { ... },
|
|
428
|
+
* }
|
|
429
|
+
* })
|
|
430
|
+
*
|
|
431
|
+
* // instantiate
|
|
432
|
+
* const adapter = myAdapter({ validate: true })
|
|
433
|
+
*/
|
|
434
|
+
function createAdapter(build) {
|
|
435
|
+
return (options) => build(options ?? {});
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
//#region src/FileProcessor.ts
|
|
439
|
+
function joinSources(file) {
|
|
440
|
+
return file.sources.map((item) => (0, _kubb_ast.extractStringsFromNodes)(item.nodes)).filter(Boolean).join("\n\n");
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Converts a single file to a string using the registered parsers.
|
|
444
|
+
* Falls back to joining source values when no matching parser is found.
|
|
445
|
+
*/
|
|
446
|
+
var FileProcessor = class {
|
|
447
|
+
#limit = require_PluginDriver.pLimit(100);
|
|
448
|
+
async parse(file, { parsers, extension } = {}) {
|
|
449
|
+
const parseExtName = extension?.[file.extname] || void 0;
|
|
450
|
+
if (!parsers || !file.extname) return joinSources(file);
|
|
451
|
+
const parser = parsers.get(file.extname);
|
|
452
|
+
if (!parser) return joinSources(file);
|
|
453
|
+
return parser.parse(file, { extname: parseExtName });
|
|
2147
454
|
}
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
const
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
hookName,
|
|
2157
|
-
parameters,
|
|
2158
|
-
plugin
|
|
2159
|
-
});
|
|
2160
|
-
const startTime = node_perf_hooks.performance.now();
|
|
2161
|
-
try {
|
|
2162
|
-
const output = typeof hook === "function" ? hook.apply(this.getContext(plugin), parameters) : hook;
|
|
2163
|
-
this.#emitProcessingEnd({
|
|
2164
|
-
startTime,
|
|
2165
|
-
output,
|
|
2166
|
-
strategy,
|
|
2167
|
-
hookName,
|
|
2168
|
-
plugin,
|
|
2169
|
-
parameters
|
|
455
|
+
async run(files, { parsers, mode = "sequential", extension, onStart, onEnd, onUpdate } = {}) {
|
|
456
|
+
await onStart?.(files);
|
|
457
|
+
const total = files.length;
|
|
458
|
+
let processed = 0;
|
|
459
|
+
const processOne = async (file) => {
|
|
460
|
+
const source = await this.parse(file, {
|
|
461
|
+
extension,
|
|
462
|
+
parsers
|
|
2170
463
|
});
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
464
|
+
const currentProcessed = ++processed;
|
|
465
|
+
const percentage = currentProcessed / total * 100;
|
|
466
|
+
await onUpdate?.({
|
|
467
|
+
file,
|
|
468
|
+
source,
|
|
469
|
+
processed: currentProcessed,
|
|
470
|
+
percentage,
|
|
471
|
+
total
|
|
2178
472
|
});
|
|
2179
|
-
|
|
2180
|
-
|
|
473
|
+
};
|
|
474
|
+
if (mode === "sequential") for (const file of files) await processOne(file);
|
|
475
|
+
else await Promise.all(files.map((file) => this.#limit(() => processOne(file))));
|
|
476
|
+
await onEnd?.(files);
|
|
477
|
+
return files;
|
|
2181
478
|
}
|
|
2182
479
|
};
|
|
2183
480
|
//#endregion
|
|
@@ -2278,7 +575,7 @@ const fsStorage = createStorage(() => ({
|
|
|
2278
575
|
}));
|
|
2279
576
|
//#endregion
|
|
2280
577
|
//#region package.json
|
|
2281
|
-
var version = "5.0.0-alpha.
|
|
578
|
+
var version = "5.0.0-alpha.38";
|
|
2282
579
|
//#endregion
|
|
2283
580
|
//#region src/utils/diagnostics.ts
|
|
2284
581
|
/**
|
|
@@ -2393,14 +690,14 @@ var TreeNode = class TreeNode {
|
|
|
2393
690
|
name: filteredTree.name,
|
|
2394
691
|
path: filteredTree.path,
|
|
2395
692
|
file: filteredTree.file,
|
|
2396
|
-
type: getMode(filteredTree.path)
|
|
693
|
+
type: require_PluginDriver.PluginDriver.getMode(filteredTree.path)
|
|
2397
694
|
});
|
|
2398
695
|
const recurse = (node, item) => {
|
|
2399
696
|
const subNode = node.addChild({
|
|
2400
697
|
name: item.name,
|
|
2401
698
|
path: item.path,
|
|
2402
699
|
file: item.file,
|
|
2403
|
-
type: getMode(item.path)
|
|
700
|
+
type: require_PluginDriver.PluginDriver.getMode(item.path)
|
|
2404
701
|
});
|
|
2405
702
|
if (item.children?.length) item.children?.forEach((child) => {
|
|
2406
703
|
recurse(subNode, child);
|
|
@@ -2585,12 +882,12 @@ async function setup(options) {
|
|
|
2585
882
|
output: {
|
|
2586
883
|
write: true,
|
|
2587
884
|
barrelType: "named",
|
|
2588
|
-
extension: DEFAULT_EXTENSION,
|
|
2589
|
-
defaultBanner: DEFAULT_BANNER,
|
|
885
|
+
extension: require_PluginDriver.DEFAULT_EXTENSION,
|
|
886
|
+
defaultBanner: require_PluginDriver.DEFAULT_BANNER,
|
|
2590
887
|
...userConfig.output
|
|
2591
888
|
},
|
|
2592
889
|
devtools: userConfig.devtools ? {
|
|
2593
|
-
studioUrl: DEFAULT_STUDIO_URL,
|
|
890
|
+
studioUrl: require_PluginDriver.DEFAULT_STUDIO_URL,
|
|
2594
891
|
...typeof userConfig.devtools === "boolean" ? {} : userConfig.devtools
|
|
2595
892
|
} : void 0,
|
|
2596
893
|
plugins: userConfig.plugins
|
|
@@ -2603,7 +900,7 @@ async function setup(options) {
|
|
|
2603
900
|
});
|
|
2604
901
|
await storage?.clear((0, node_path.resolve)(config.root, config.output.path));
|
|
2605
902
|
}
|
|
2606
|
-
const driver = new PluginDriver(config, {
|
|
903
|
+
const driver = new require_PluginDriver.PluginDriver(config, {
|
|
2607
904
|
hooks,
|
|
2608
905
|
concurrency: 15
|
|
2609
906
|
});
|
|
@@ -2666,7 +963,7 @@ async function runPluginAstHooks(plugin, context) {
|
|
|
2666
963
|
};
|
|
2667
964
|
for (const gen of generators) {
|
|
2668
965
|
if (!gen.schema) continue;
|
|
2669
|
-
await applyHookResult(await gen.schema(transformedNode, ctx), driver, resolveRenderer(gen));
|
|
966
|
+
await require_PluginDriver.applyHookResult(await gen.schema(transformedNode, ctx), driver, resolveRenderer(gen));
|
|
2670
967
|
}
|
|
2671
968
|
await driver.hooks.emit("kubb:generate:schema", transformedNode, ctx);
|
|
2672
969
|
},
|
|
@@ -2686,7 +983,7 @@ async function runPluginAstHooks(plugin, context) {
|
|
|
2686
983
|
};
|
|
2687
984
|
for (const gen of generators) {
|
|
2688
985
|
if (!gen.operation) continue;
|
|
2689
|
-
await applyHookResult(await gen.operation(transformedNode, ctx), driver, resolveRenderer(gen));
|
|
986
|
+
await require_PluginDriver.applyHookResult(await gen.operation(transformedNode, ctx), driver, resolveRenderer(gen));
|
|
2690
987
|
}
|
|
2691
988
|
await driver.hooks.emit("kubb:generate:operation", transformedNode, ctx);
|
|
2692
989
|
}
|
|
@@ -2699,7 +996,7 @@ async function runPluginAstHooks(plugin, context) {
|
|
|
2699
996
|
};
|
|
2700
997
|
for (const gen of generators) {
|
|
2701
998
|
if (!gen.operations) continue;
|
|
2702
|
-
await applyHookResult(await gen.operations(collectedOperations, ctx), driver, resolveRenderer(gen));
|
|
999
|
+
await require_PluginDriver.applyHookResult(await gen.operations(collectedOperations, ctx), driver, resolveRenderer(gen));
|
|
2703
1000
|
}
|
|
2704
1001
|
await driver.hooks.emit("kubb:generate:operations", collectedOperations, ctx);
|
|
2705
1002
|
}
|
|
@@ -2776,7 +1073,7 @@ async function safeBuild(setupResult) {
|
|
|
2776
1073
|
}
|
|
2777
1074
|
}
|
|
2778
1075
|
if (config.output.barrelType) {
|
|
2779
|
-
const rootPath = (0, node_path.resolve)((0, node_path.resolve)(config.root), config.output.path, BARREL_FILENAME);
|
|
1076
|
+
const rootPath = (0, node_path.resolve)((0, node_path.resolve)(config.root), config.output.path, require_PluginDriver.BARREL_FILENAME);
|
|
2780
1077
|
const rootDir = (0, node_path.dirname)(rootPath);
|
|
2781
1078
|
await hooks.emit("kubb:debug", {
|
|
2782
1079
|
date: /* @__PURE__ */ new Date(),
|
|
@@ -2796,7 +1093,7 @@ async function safeBuild(setupResult) {
|
|
|
2796
1093
|
const existingBarrel = driver.fileManager.files.find((f) => f.path === rootPath);
|
|
2797
1094
|
const rootFile = (0, _kubb_ast.createFile)({
|
|
2798
1095
|
path: rootPath,
|
|
2799
|
-
baseName: BARREL_FILENAME,
|
|
1096
|
+
baseName: require_PluginDriver.BARREL_FILENAME,
|
|
2800
1097
|
exports: buildBarrelExports({
|
|
2801
1098
|
barrelFiles,
|
|
2802
1099
|
rootDir,
|
|
@@ -2985,30 +1282,6 @@ function createKubb(options) {
|
|
|
2985
1282
|
return instance;
|
|
2986
1283
|
}
|
|
2987
1284
|
//#endregion
|
|
2988
|
-
//#region src/createPlugin.ts
|
|
2989
|
-
/**
|
|
2990
|
-
* Creates a plugin factory. Call the returned function with optional options to get the plugin instance.
|
|
2991
|
-
*
|
|
2992
|
-
* @example
|
|
2993
|
-
* ```ts
|
|
2994
|
-
* export const myPlugin = createPlugin<MyPlugin>((options) => {
|
|
2995
|
-
* return {
|
|
2996
|
-
* name: 'my-plugin',
|
|
2997
|
-
* get options() { return options },
|
|
2998
|
-
* resolvePath(baseName) { ... },
|
|
2999
|
-
* resolveName(name, type) { ... },
|
|
3000
|
-
* }
|
|
3001
|
-
* })
|
|
3002
|
-
*
|
|
3003
|
-
* // instantiate
|
|
3004
|
-
* const plugin = myPlugin({ output: { path: 'src/gen' } })
|
|
3005
|
-
* ```
|
|
3006
|
-
* @deprecated use definePlugin instead
|
|
3007
|
-
*/
|
|
3008
|
-
function createPlugin(build) {
|
|
3009
|
-
return (options) => build(options ?? {});
|
|
3010
|
-
}
|
|
3011
|
-
//#endregion
|
|
3012
1285
|
//#region src/createRenderer.ts
|
|
3013
1286
|
/**
|
|
3014
1287
|
* Creates a renderer factory for use in generator definitions.
|
|
@@ -3142,248 +1415,10 @@ const memoryStorage = createStorage(() => {
|
|
|
3142
1415
|
};
|
|
3143
1416
|
});
|
|
3144
1417
|
//#endregion
|
|
3145
|
-
//#region src/utils/formatters.ts
|
|
3146
|
-
/**
|
|
3147
|
-
* Returns `true` when the given formatter is installed and callable.
|
|
3148
|
-
*
|
|
3149
|
-
* Availability is detected by running `<formatter> --version` and checking
|
|
3150
|
-
* that the process exits without error.
|
|
3151
|
-
*/
|
|
3152
|
-
async function isFormatterAvailable(formatter) {
|
|
3153
|
-
try {
|
|
3154
|
-
await (0, tinyexec.x)(formatter, ["--version"], { nodeOptions: { stdio: "ignore" } });
|
|
3155
|
-
return true;
|
|
3156
|
-
} catch {
|
|
3157
|
-
return false;
|
|
3158
|
-
}
|
|
3159
|
-
}
|
|
3160
|
-
/**
|
|
3161
|
-
* Detects the first available code formatter on the current system.
|
|
3162
|
-
*
|
|
3163
|
-
* - Checks in preference order: `biome`, `oxfmt`, `prettier`.
|
|
3164
|
-
* - Returns `null` when none are found.
|
|
3165
|
-
*
|
|
3166
|
-
* @example
|
|
3167
|
-
* ```ts
|
|
3168
|
-
* const formatter = await detectFormatter()
|
|
3169
|
-
* if (formatter) {
|
|
3170
|
-
* console.log(`Using ${formatter} for formatting`)
|
|
3171
|
-
* }
|
|
3172
|
-
* ```
|
|
3173
|
-
*/
|
|
3174
|
-
async function detectFormatter() {
|
|
3175
|
-
const formatterNames = new Set([
|
|
3176
|
-
"biome",
|
|
3177
|
-
"oxfmt",
|
|
3178
|
-
"prettier"
|
|
3179
|
-
]);
|
|
3180
|
-
for (const formatter of formatterNames) if (await isFormatterAvailable(formatter)) return formatter;
|
|
3181
|
-
return null;
|
|
3182
|
-
}
|
|
3183
|
-
//#endregion
|
|
3184
|
-
//#region src/utils/getFunctionParams.ts
|
|
3185
|
-
function order(items) {
|
|
3186
|
-
return (0, remeda.sortBy)(items.filter(Boolean), ([_key, item]) => {
|
|
3187
|
-
if (item?.children) return 0;
|
|
3188
|
-
if (item?.optional) return 1;
|
|
3189
|
-
if (item?.default) return 2;
|
|
3190
|
-
return 0;
|
|
3191
|
-
});
|
|
3192
|
-
}
|
|
3193
|
-
function parseChild(key, item, options) {
|
|
3194
|
-
const entries = order(Object.entries(item.children));
|
|
3195
|
-
const types = [];
|
|
3196
|
-
const names = [];
|
|
3197
|
-
const optional = entries.every(([_key, item]) => item?.optional || !!item?.default);
|
|
3198
|
-
entries.forEach(([key, entryItem]) => {
|
|
3199
|
-
if (entryItem) {
|
|
3200
|
-
const name = parseItem(key, {
|
|
3201
|
-
...entryItem,
|
|
3202
|
-
type: void 0
|
|
3203
|
-
}, options);
|
|
3204
|
-
if (entryItem.children) {
|
|
3205
|
-
const subTypes = Object.entries(entryItem.children).map(([key]) => {
|
|
3206
|
-
return key;
|
|
3207
|
-
}).join(", ");
|
|
3208
|
-
if (subTypes) names.push(`${name}: { ${subTypes} }`);
|
|
3209
|
-
else names.push(name);
|
|
3210
|
-
} else if (options.type === "call" && options.transformName) names.push(`${key}: ${name}`);
|
|
3211
|
-
else names.push(name);
|
|
3212
|
-
if (entries.some(([_key, item]) => item?.type)) types.push(parseItem(key, {
|
|
3213
|
-
...entryItem,
|
|
3214
|
-
default: void 0
|
|
3215
|
-
}, options));
|
|
3216
|
-
}
|
|
3217
|
-
});
|
|
3218
|
-
const name = item.mode === "inline" ? key : names.length ? `{ ${names.join(", ")} }` : void 0;
|
|
3219
|
-
const type = item.type ? item.type : types.length ? `{ ${types.join("; ")} }` : void 0;
|
|
3220
|
-
if (!name) return null;
|
|
3221
|
-
return parseItem(name, {
|
|
3222
|
-
type,
|
|
3223
|
-
default: item.default,
|
|
3224
|
-
optional: !item.default ? optional : void 0
|
|
3225
|
-
}, options);
|
|
3226
|
-
}
|
|
3227
|
-
function parseItem(name, item, options) {
|
|
3228
|
-
const acc = [];
|
|
3229
|
-
const transformedName = options.transformName ? options.transformName(name) : name;
|
|
3230
|
-
const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type;
|
|
3231
|
-
if (options.type === "object") return transformedName;
|
|
3232
|
-
if (options.type === "objectValue") return item.value ? `${transformedName}: ${item.value}` : transformedName;
|
|
3233
|
-
if (item.type && options.type === "constructor") if (item.optional) if (transformedName.startsWith("{")) acc.push(`${transformedName}: ${transformedType} = {}`);
|
|
3234
|
-
else acc.push(`${transformedName}?: ${transformedType}`);
|
|
3235
|
-
else acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ""}`);
|
|
3236
|
-
else if (item.default && options.type === "constructor") acc.push(`${transformedName} = ${item.default}`);
|
|
3237
|
-
else if (item.value) acc.push(`${transformedName} : ${item.value}`);
|
|
3238
|
-
else if (item.mode === "inlineSpread") acc.push(`... ${transformedName}`);
|
|
3239
|
-
else acc.push(transformedName);
|
|
3240
|
-
return acc[0];
|
|
3241
|
-
}
|
|
3242
|
-
function getFunctionParams(params, options) {
|
|
3243
|
-
return order(Object.entries(params)).reduce((acc, [key, item]) => {
|
|
3244
|
-
if (!item) return acc;
|
|
3245
|
-
if (item.children) {
|
|
3246
|
-
if (Object.keys(item.children).length === 0) return acc;
|
|
3247
|
-
if (item.mode === "inlineSpread") return [...acc, getFunctionParams(item.children, options)];
|
|
3248
|
-
const parsedItem = parseChild(key, item, options);
|
|
3249
|
-
if (!parsedItem) return acc;
|
|
3250
|
-
return [...acc, parsedItem];
|
|
3251
|
-
}
|
|
3252
|
-
const parsedItem = parseItem(key, item, options);
|
|
3253
|
-
return [...acc, parsedItem];
|
|
3254
|
-
}, []).join(", ");
|
|
3255
|
-
}
|
|
3256
|
-
/**
|
|
3257
|
-
* @deprecated use @kubb/ast
|
|
3258
|
-
*/
|
|
3259
|
-
function createFunctionParams(params) {
|
|
3260
|
-
return params;
|
|
3261
|
-
}
|
|
3262
|
-
/**
|
|
3263
|
-
* @deprecated use @kubb/ast
|
|
3264
|
-
*/
|
|
3265
|
-
var FunctionParams = class FunctionParams {
|
|
3266
|
-
#params;
|
|
3267
|
-
static factory(params) {
|
|
3268
|
-
return new FunctionParams(params);
|
|
3269
|
-
}
|
|
3270
|
-
constructor(params) {
|
|
3271
|
-
this.#params = params;
|
|
3272
|
-
}
|
|
3273
|
-
get params() {
|
|
3274
|
-
return this.#params;
|
|
3275
|
-
}
|
|
3276
|
-
get flatParams() {
|
|
3277
|
-
const flatter = (acc, [key, item]) => {
|
|
3278
|
-
if (item?.children) return Object.entries(item.children).reduce(flatter, acc);
|
|
3279
|
-
if (item) acc[key] = item;
|
|
3280
|
-
return acc;
|
|
3281
|
-
};
|
|
3282
|
-
return Object.entries(this.#params).reduce(flatter, {});
|
|
3283
|
-
}
|
|
3284
|
-
toCall({ transformName, transformType } = {}) {
|
|
3285
|
-
return getFunctionParams(this.#params, {
|
|
3286
|
-
type: "call",
|
|
3287
|
-
transformName,
|
|
3288
|
-
transformType
|
|
3289
|
-
});
|
|
3290
|
-
}
|
|
3291
|
-
toObject() {
|
|
3292
|
-
return getFunctionParams(this.#params, { type: "object" });
|
|
3293
|
-
}
|
|
3294
|
-
toObjectValue() {
|
|
3295
|
-
return getFunctionParams(this.#params, { type: "objectValue" });
|
|
3296
|
-
}
|
|
3297
|
-
toConstructor() {
|
|
3298
|
-
return getFunctionParams(this.#params, { type: "constructor" });
|
|
3299
|
-
}
|
|
3300
|
-
};
|
|
3301
|
-
//#endregion
|
|
3302
|
-
//#region src/utils/linters.ts
|
|
3303
|
-
/**
|
|
3304
|
-
* Returns `true` when the given linter is installed and callable.
|
|
3305
|
-
*
|
|
3306
|
-
* Availability is detected by running `<linter> --version` and checking
|
|
3307
|
-
* that the process exits without error.
|
|
3308
|
-
*/
|
|
3309
|
-
async function isLinterAvailable(linter) {
|
|
3310
|
-
try {
|
|
3311
|
-
await (0, tinyexec.x)(linter, ["--version"], { nodeOptions: { stdio: "ignore" } });
|
|
3312
|
-
return true;
|
|
3313
|
-
} catch {
|
|
3314
|
-
return false;
|
|
3315
|
-
}
|
|
3316
|
-
}
|
|
3317
|
-
/**
|
|
3318
|
-
* Detects the first available linter on the current system.
|
|
3319
|
-
*
|
|
3320
|
-
* - Checks in preference order: `biome`, `oxlint`, `eslint`.
|
|
3321
|
-
* - Returns `null` when none are found.
|
|
3322
|
-
*
|
|
3323
|
-
* @example
|
|
3324
|
-
* ```ts
|
|
3325
|
-
* const linter = await detectLinter()
|
|
3326
|
-
* if (linter) {
|
|
3327
|
-
* console.log(`Using ${linter} for linting`)
|
|
3328
|
-
* }
|
|
3329
|
-
* ```
|
|
3330
|
-
*/
|
|
3331
|
-
async function detectLinter() {
|
|
3332
|
-
const linterNames = new Set([
|
|
3333
|
-
"biome",
|
|
3334
|
-
"oxlint",
|
|
3335
|
-
"eslint"
|
|
3336
|
-
]);
|
|
3337
|
-
for (const linter of linterNames) if (await isLinterAvailable(linter)) return linter;
|
|
3338
|
-
return null;
|
|
3339
|
-
}
|
|
3340
|
-
//#endregion
|
|
3341
|
-
//#region src/utils/packageJSON.ts
|
|
3342
|
-
function getPackageJSONSync(cwd) {
|
|
3343
|
-
const pkgPath = findPackageJSON(cwd);
|
|
3344
|
-
if (!pkgPath) return null;
|
|
3345
|
-
return JSON.parse(readSync(pkgPath));
|
|
3346
|
-
}
|
|
3347
|
-
function match(packageJSON, dependency) {
|
|
3348
|
-
const dependencies = {
|
|
3349
|
-
...packageJSON.dependencies || {},
|
|
3350
|
-
...packageJSON.devDependencies || {}
|
|
3351
|
-
};
|
|
3352
|
-
if (typeof dependency === "string" && dependencies[dependency]) return dependencies[dependency];
|
|
3353
|
-
const matched = Object.keys(dependencies).find((dep) => dep.match(dependency));
|
|
3354
|
-
return matched ? dependencies[matched] ?? null : null;
|
|
3355
|
-
}
|
|
3356
|
-
function getVersionSync(dependency, cwd) {
|
|
3357
|
-
const packageJSON = getPackageJSONSync(cwd);
|
|
3358
|
-
return packageJSON ? match(packageJSON, dependency) : null;
|
|
3359
|
-
}
|
|
3360
|
-
/**
|
|
3361
|
-
* Returns `true` when the nearest `package.json` declares a dependency that
|
|
3362
|
-
* satisfies the given semver range.
|
|
3363
|
-
*
|
|
3364
|
-
* - Searches both `dependencies` and `devDependencies`.
|
|
3365
|
-
* - Accepts a string package name or a `RegExp` to match scoped/pattern packages.
|
|
3366
|
-
* - Uses `semver.satisfies` for range comparison; returns `false` when the
|
|
3367
|
-
* version string cannot be coerced into a valid semver.
|
|
3368
|
-
*
|
|
3369
|
-
* @example
|
|
3370
|
-
* ```ts
|
|
3371
|
-
* satisfiesDependency('react', '>=18') // true when react@18.x is installed
|
|
3372
|
-
* satisfiesDependency(/^@tanstack\//, '>=5') // true when any @tanstack/* >=5 is found
|
|
3373
|
-
* ```
|
|
3374
|
-
*/
|
|
3375
|
-
function satisfiesDependency(dependency, version, cwd) {
|
|
3376
|
-
const packageVersion = getVersionSync(dependency, cwd);
|
|
3377
|
-
if (!packageVersion) return false;
|
|
3378
|
-
if (packageVersion === version) return true;
|
|
3379
|
-
const semVer = (0, semver.coerce)(packageVersion);
|
|
3380
|
-
if (!semVer) return false;
|
|
3381
|
-
return (0, semver.satisfies)(semVer, version);
|
|
3382
|
-
}
|
|
3383
|
-
//#endregion
|
|
3384
1418
|
exports.AsyncEventEmitter = AsyncEventEmitter;
|
|
3385
|
-
exports.
|
|
3386
|
-
exports.
|
|
1419
|
+
exports.FileManager = require_PluginDriver.FileManager;
|
|
1420
|
+
exports.FileProcessor = FileProcessor;
|
|
1421
|
+
exports.PluginDriver = require_PluginDriver.PluginDriver;
|
|
3387
1422
|
exports.URLPath = URLPath;
|
|
3388
1423
|
Object.defineProperty(exports, "ast", {
|
|
3389
1424
|
enumerable: true,
|
|
@@ -3391,46 +1426,18 @@ Object.defineProperty(exports, "ast", {
|
|
|
3391
1426
|
return _kubb_ast;
|
|
3392
1427
|
}
|
|
3393
1428
|
});
|
|
3394
|
-
exports.buildDefaultBanner = buildDefaultBanner;
|
|
3395
|
-
Object.defineProperty(exports, "composeTransformers", {
|
|
3396
|
-
enumerable: true,
|
|
3397
|
-
get: function() {
|
|
3398
|
-
return _kubb_ast.composeTransformers;
|
|
3399
|
-
}
|
|
3400
|
-
});
|
|
3401
1429
|
exports.createAdapter = createAdapter;
|
|
3402
|
-
exports.createFunctionParams = createFunctionParams;
|
|
3403
1430
|
exports.createKubb = createKubb;
|
|
3404
|
-
exports.createPlugin = createPlugin;
|
|
3405
1431
|
exports.createRenderer = createRenderer;
|
|
3406
1432
|
exports.createStorage = createStorage;
|
|
3407
|
-
exports.defaultResolveBanner = defaultResolveBanner;
|
|
3408
|
-
exports.defaultResolveFile = defaultResolveFile;
|
|
3409
|
-
exports.defaultResolveFooter = defaultResolveFooter;
|
|
3410
|
-
exports.defaultResolveOptions = defaultResolveOptions;
|
|
3411
|
-
exports.defaultResolvePath = defaultResolvePath;
|
|
3412
1433
|
exports.defineGenerator = defineGenerator;
|
|
3413
1434
|
exports.defineLogger = defineLogger;
|
|
3414
1435
|
exports.defineParser = defineParser;
|
|
3415
|
-
exports.definePlugin = definePlugin;
|
|
3416
|
-
|
|
3417
|
-
enumerable: true,
|
|
3418
|
-
get: function() {
|
|
3419
|
-
return _kubb_ast.definePrinter;
|
|
3420
|
-
}
|
|
3421
|
-
});
|
|
3422
|
-
exports.defineResolver = defineResolver;
|
|
3423
|
-
exports.detectFormatter = detectFormatter;
|
|
3424
|
-
exports.detectLinter = detectLinter;
|
|
3425
|
-
exports.formatters = formatters;
|
|
1436
|
+
exports.definePlugin = require_PluginDriver.definePlugin;
|
|
1437
|
+
exports.defineResolver = require_PluginDriver.defineResolver;
|
|
3426
1438
|
exports.fsStorage = fsStorage;
|
|
3427
|
-
exports.getBarrelFiles = getBarrelFiles;
|
|
3428
|
-
exports.getFunctionParams = getFunctionParams;
|
|
3429
|
-
exports.getMode = getMode;
|
|
3430
1439
|
exports.isInputPath = isInputPath;
|
|
3431
|
-
exports.
|
|
3432
|
-
exports.logLevel = logLevel;
|
|
1440
|
+
exports.logLevel = require_PluginDriver.logLevel;
|
|
3433
1441
|
exports.memoryStorage = memoryStorage;
|
|
3434
|
-
exports.satisfiesDependency = satisfiesDependency;
|
|
3435
1442
|
|
|
3436
1443
|
//# sourceMappingURL=index.cjs.map
|