@kubb/core 5.18.7 → 5.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +157 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +67 -2
- package/dist/index.d.ts +67 -2
- package/dist/index.js +160 -88
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -37,6 +37,57 @@ export declare function pascalCase(text: string, { prefix, suffix }?: Options$1)
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
type PossiblePromise<T> = Promise<T> | T;
|
|
40
|
+
type Store<TKey, TValue> = {
|
|
41
|
+
has(key: TKey): boolean;
|
|
42
|
+
get(key: TKey): TValue | undefined;
|
|
43
|
+
set(key: TKey, value: TValue): unknown;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
47
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
48
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
52
|
+
*/
|
|
53
|
+
declare function memo$1<TKey, TValue>(store: Store<TKey, TValue>, key: TKey, compute: () => TValue): TValue;
|
|
54
|
+
declare namespace memo$1 {
|
|
55
|
+
export { createStore as create };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates a store for `memo`. With no options it is a `Map`. Each option changes one thing:
|
|
59
|
+
*
|
|
60
|
+
* - `weak` holds the keys weakly, in a `WeakMap`, so an entry goes away with its key.
|
|
61
|
+
* - `max` bounds the store: it empties itself before the entry that would pass `max`, which keeps a
|
|
62
|
+
* cache in a long-running process from growing without end. `max: 1` keeps only the last entry.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* `const patterns = memo.create<string, RegExp>()`
|
|
66
|
+
*
|
|
67
|
+
* @example Freed with its key
|
|
68
|
+
* `const refs = memo.create<SchemaNode, Array<string>>({ weak: true })`
|
|
69
|
+
*
|
|
70
|
+
* @example Bounded
|
|
71
|
+
* `const names = memo.create<string, string>({ max: 100_000 })`
|
|
72
|
+
*
|
|
73
|
+
* @example Many values per key
|
|
74
|
+
* ```ts
|
|
75
|
+
* const filesByPlugin = memo.create<string, Array<string>>()
|
|
76
|
+
* memo(filesByPlugin, 'plugin-ts', () => []).push('models.ts')
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare function createStore<TKey, TValue>(options?: {
|
|
80
|
+
weak?: false;
|
|
81
|
+
max?: undefined;
|
|
82
|
+
}): Map<TKey, TValue>;
|
|
83
|
+
declare function createStore<TKey extends WeakKey, TValue>(options: {
|
|
84
|
+
weak: true;
|
|
85
|
+
max?: undefined;
|
|
86
|
+
}): WeakMap<TKey, TValue>;
|
|
87
|
+
declare function createStore<TKey, TValue>(options: {
|
|
88
|
+
max: number;
|
|
89
|
+
weak?: false;
|
|
90
|
+
}): Store<TKey, TValue>;
|
|
40
91
|
/** A union of known literals that still accepts any other value of `Base`, without collapsing
|
|
41
92
|
* the literals away. Editors keep autocompleting the known members while arbitrary strings stay
|
|
42
93
|
* assignable.
|
|
@@ -909,7 +960,7 @@ declare class FileManager {
|
|
|
909
960
|
* memory rather than holding every source, while still overlapping each file's write with the
|
|
910
961
|
* next file's parse. Files finish in whatever order they parse, so each finished file flushes the
|
|
911
962
|
* run of finished files ahead of it as one `kubb:files:processing:update`, keeping rows in input order.
|
|
912
|
-
* Each parsed source goes out once on `kubb:file:processed`,
|
|
963
|
+
* Each parsed source goes out once on `kubb:file:processed`, next to what the storage held before the write.
|
|
913
964
|
*
|
|
914
965
|
* A file the storage already holds is skipped, so a rebuild that generates identical output
|
|
915
966
|
* writes nothing and leaves every mtime where it was. `kubb:files:processing:end` lists the
|
|
@@ -1620,6 +1671,8 @@ export type KubbFileProcessedContext = {
|
|
|
1620
1671
|
file: FileNode;
|
|
1621
1672
|
/** The parsed content handed to the storage, before it trims the content or an output pass such as a formatter rewrites it. */
|
|
1622
1673
|
source: string;
|
|
1674
|
+
/** What the storage held at `file.path` before the write, or `null` when the path was missing or `output.clean` emptied its folder. */
|
|
1675
|
+
stored?: string | null;
|
|
1623
1676
|
};
|
|
1624
1677
|
export type KubbFilesProcessingEndContext = {
|
|
1625
1678
|
/**
|
|
@@ -3342,8 +3395,20 @@ export declare function setMacros(macros: ReadonlyArray<Macro>): void;
|
|
|
3342
3395
|
export declare function injectFile(file: UserFileNode): void;
|
|
3343
3396
|
/**
|
|
3344
3397
|
* Returns a value computed once per build and shared by every plugin, like `ctx.memo`.
|
|
3398
|
+
*
|
|
3399
|
+
* Pass a store first to cache in it instead, with no build needed: a `Map` or `WeakMap`, or one
|
|
3400
|
+
* from `memo.create`.
|
|
3401
|
+
*
|
|
3402
|
+
* @example Once per build
|
|
3403
|
+
* `const names = memo('myPlugin:names', () => collectNames(schemas))`
|
|
3404
|
+
*
|
|
3405
|
+
* @example In your own store
|
|
3406
|
+
* ```ts
|
|
3407
|
+
* const cache = memo.create<ast.SchemaNode, Array<string>>({ weak: true })
|
|
3408
|
+
* const refs = memo(cache, node, () => collectRefs(node))
|
|
3409
|
+
* ```
|
|
3345
3410
|
*/
|
|
3346
|
-
export declare const memo: Memo;
|
|
3411
|
+
export declare const memo: Memo & typeof memo$1;
|
|
3347
3412
|
/**
|
|
3348
3413
|
* Returns the options the user passed to the plugin being set up, like `ctx.options`.
|
|
3349
3414
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -37,6 +37,57 @@ export declare function pascalCase(text: string, { prefix, suffix }?: Options$1)
|
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
39
|
type PossiblePromise<T> = Promise<T> | T;
|
|
40
|
+
type Store<TKey, TValue> = {
|
|
41
|
+
has(key: TKey): boolean;
|
|
42
|
+
get(key: TKey): TValue | undefined;
|
|
43
|
+
set(key: TKey, value: TValue): unknown;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
47
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
48
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
52
|
+
*/
|
|
53
|
+
declare function memo$1<TKey, TValue>(store: Store<TKey, TValue>, key: TKey, compute: () => TValue): TValue;
|
|
54
|
+
declare namespace memo$1 {
|
|
55
|
+
export { createStore as create };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates a store for `memo`. With no options it is a `Map`. Each option changes one thing:
|
|
59
|
+
*
|
|
60
|
+
* - `weak` holds the keys weakly, in a `WeakMap`, so an entry goes away with its key.
|
|
61
|
+
* - `max` bounds the store: it empties itself before the entry that would pass `max`, which keeps a
|
|
62
|
+
* cache in a long-running process from growing without end. `max: 1` keeps only the last entry.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* `const patterns = memo.create<string, RegExp>()`
|
|
66
|
+
*
|
|
67
|
+
* @example Freed with its key
|
|
68
|
+
* `const refs = memo.create<SchemaNode, Array<string>>({ weak: true })`
|
|
69
|
+
*
|
|
70
|
+
* @example Bounded
|
|
71
|
+
* `const names = memo.create<string, string>({ max: 100_000 })`
|
|
72
|
+
*
|
|
73
|
+
* @example Many values per key
|
|
74
|
+
* ```ts
|
|
75
|
+
* const filesByPlugin = memo.create<string, Array<string>>()
|
|
76
|
+
* memo(filesByPlugin, 'plugin-ts', () => []).push('models.ts')
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare function createStore<TKey, TValue>(options?: {
|
|
80
|
+
weak?: false;
|
|
81
|
+
max?: undefined;
|
|
82
|
+
}): Map<TKey, TValue>;
|
|
83
|
+
declare function createStore<TKey extends WeakKey, TValue>(options: {
|
|
84
|
+
weak: true;
|
|
85
|
+
max?: undefined;
|
|
86
|
+
}): WeakMap<TKey, TValue>;
|
|
87
|
+
declare function createStore<TKey, TValue>(options: {
|
|
88
|
+
max: number;
|
|
89
|
+
weak?: false;
|
|
90
|
+
}): Store<TKey, TValue>;
|
|
40
91
|
/** A union of known literals that still accepts any other value of `Base`, without collapsing
|
|
41
92
|
* the literals away. Editors keep autocompleting the known members while arbitrary strings stay
|
|
42
93
|
* assignable.
|
|
@@ -909,7 +960,7 @@ declare class FileManager {
|
|
|
909
960
|
* memory rather than holding every source, while still overlapping each file's write with the
|
|
910
961
|
* next file's parse. Files finish in whatever order they parse, so each finished file flushes the
|
|
911
962
|
* run of finished files ahead of it as one `kubb:files:processing:update`, keeping rows in input order.
|
|
912
|
-
* Each parsed source goes out once on `kubb:file:processed`,
|
|
963
|
+
* Each parsed source goes out once on `kubb:file:processed`, next to what the storage held before the write.
|
|
913
964
|
*
|
|
914
965
|
* A file the storage already holds is skipped, so a rebuild that generates identical output
|
|
915
966
|
* writes nothing and leaves every mtime where it was. `kubb:files:processing:end` lists the
|
|
@@ -1620,6 +1671,8 @@ export type KubbFileProcessedContext = {
|
|
|
1620
1671
|
file: FileNode;
|
|
1621
1672
|
/** The parsed content handed to the storage, before it trims the content or an output pass such as a formatter rewrites it. */
|
|
1622
1673
|
source: string;
|
|
1674
|
+
/** What the storage held at `file.path` before the write, or `null` when the path was missing or `output.clean` emptied its folder. */
|
|
1675
|
+
stored?: string | null;
|
|
1623
1676
|
};
|
|
1624
1677
|
export type KubbFilesProcessingEndContext = {
|
|
1625
1678
|
/**
|
|
@@ -3342,8 +3395,20 @@ export declare function setMacros(macros: ReadonlyArray<Macro>): void;
|
|
|
3342
3395
|
export declare function injectFile(file: UserFileNode): void;
|
|
3343
3396
|
/**
|
|
3344
3397
|
* Returns a value computed once per build and shared by every plugin, like `ctx.memo`.
|
|
3398
|
+
*
|
|
3399
|
+
* Pass a store first to cache in it instead, with no build needed: a `Map` or `WeakMap`, or one
|
|
3400
|
+
* from `memo.create`.
|
|
3401
|
+
*
|
|
3402
|
+
* @example Once per build
|
|
3403
|
+
* `const names = memo('myPlugin:names', () => collectNames(schemas))`
|
|
3404
|
+
*
|
|
3405
|
+
* @example In your own store
|
|
3406
|
+
* ```ts
|
|
3407
|
+
* const cache = memo.create<ast.SchemaNode, Array<string>>({ weak: true })
|
|
3408
|
+
* const refs = memo(cache, node, () => collectRefs(node))
|
|
3409
|
+
* ```
|
|
3345
3410
|
*/
|
|
3346
|
-
export declare const memo: Memo;
|
|
3411
|
+
export declare const memo: Memo & typeof memo$1;
|
|
3347
3412
|
/**
|
|
3348
3413
|
* Returns the options the user passed to the plugin being set up, like `ctx.options`.
|
|
3349
3414
|
*/
|
package/dist/index.js
CHANGED
|
@@ -3,9 +3,9 @@ import { isValid, satisfies } from "verkit";
|
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
4
|
import process$1 from "node:process";
|
|
5
5
|
import { x } from "tinyexec";
|
|
6
|
-
import {
|
|
6
|
+
import { chmodSync, existsSync, existsSync as existsSync$1, mkdirSync, readFile, readFileSync, writeFile } from "node:fs";
|
|
7
|
+
import { access, glob, mkdir as mkdir$1, mkdtemp as mkdtemp$1, readFile as readFile$1, readdir, rename, rm, rm as rm$1 } from "node:fs/promises";
|
|
7
8
|
import path, { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
8
|
-
import { chmodSync, existsSync, existsSync as existsSync$1, mkdirSync } from "node:fs";
|
|
9
9
|
import { stripVTControlCharacters, styleText } from "node:util";
|
|
10
10
|
import { ast } from "@kubb/ast";
|
|
11
11
|
import dns from "node:dns";
|
|
@@ -21,7 +21,65 @@ var __name = (target, value) => __defProp(target, "name", {
|
|
|
21
21
|
configurable: true
|
|
22
22
|
});
|
|
23
23
|
// package.json
|
|
24
|
-
var version = "5.
|
|
24
|
+
var version = "5.19.0";
|
|
25
|
+
// ../../internals/utils/src/promise.ts
|
|
26
|
+
/**
|
|
27
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
28
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
29
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
33
|
+
*/
|
|
34
|
+
function memo$1(store, key, compute) {
|
|
35
|
+
const cached = store.get(key);
|
|
36
|
+
if (cached !== void 0 || store.has(key)) return cached;
|
|
37
|
+
const value = compute();
|
|
38
|
+
store.set(key, value);
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
__name(memo$1, "memo");
|
|
42
|
+
function createStore({ weak, max } = {}) {
|
|
43
|
+
if (weak) return /* @__PURE__ */ new WeakMap();
|
|
44
|
+
const entry = /* @__PURE__ */ new Map();
|
|
45
|
+
if (max === void 0) return entry;
|
|
46
|
+
const set = (key, value) => {
|
|
47
|
+
if (entry.size >= max) entry.clear();
|
|
48
|
+
entry.set(key, value);
|
|
49
|
+
};
|
|
50
|
+
if (max !== 1) return {
|
|
51
|
+
has: (key) => entry.has(key),
|
|
52
|
+
get: (key) => entry.get(key),
|
|
53
|
+
set
|
|
54
|
+
};
|
|
55
|
+
const touch = (key) => {
|
|
56
|
+
if (!entry.has(key)) return false;
|
|
57
|
+
set(key, entry.get(key));
|
|
58
|
+
return true;
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
has: touch,
|
|
62
|
+
get: (key) => touch(key) ? entry.get(key) : void 0,
|
|
63
|
+
set
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
memo$1.create = createStore;
|
|
67
|
+
/**
|
|
68
|
+
* Runs `run` over every item with at most `limit` in flight. Workers share one iterator, so each
|
|
69
|
+
* takes the next item the moment it frees up instead of waiting for a batch to drain.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* await inParallel({ items: files, limit: 50, run: (file) => storage.writeItem(file.path, file.source) })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
async function inParallel({ items, limit, run }) {
|
|
77
|
+
const queue = items.entries();
|
|
78
|
+
const worker = async () => {
|
|
79
|
+
for (const [index, item] of queue) await run(item, index);
|
|
80
|
+
};
|
|
81
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => worker()));
|
|
82
|
+
}
|
|
25
83
|
// ../../internals/utils/src/casing.ts
|
|
26
84
|
/**
|
|
27
85
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -36,16 +94,10 @@ function convert(text, pascal) {
|
|
|
36
94
|
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
37
95
|
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
38
96
|
}
|
|
39
|
-
const cache =
|
|
40
|
-
const cacheLimit = 1e5;
|
|
97
|
+
const cache = memo$1.create({ max: 1e5 });
|
|
41
98
|
function toCamelOrPascal(text, pascal) {
|
|
42
99
|
const key = `${pascal ? "P" : "c"}${text}`;
|
|
43
|
-
|
|
44
|
-
if (cached !== void 0) return cached;
|
|
45
|
-
const result = convert(text, pascal);
|
|
46
|
-
if (cache.size >= cacheLimit) cache.clear();
|
|
47
|
-
cache.set(key, result);
|
|
48
|
-
return result;
|
|
100
|
+
return memo$1(cache, key, () => convert(text, pascal));
|
|
49
101
|
}
|
|
50
102
|
/**
|
|
51
103
|
* Converts `text` to camelCase.
|
|
@@ -196,7 +248,25 @@ async function exists(path) {
|
|
|
196
248
|
*/
|
|
197
249
|
async function read(path) {
|
|
198
250
|
if (runtime.isBun) return Bun.file(path).text();
|
|
199
|
-
return readFile(path, { encoding: "utf8" });
|
|
251
|
+
return new Promise((resolvePromise, reject) => readFile(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Reads the file at `path` as a UTF-8 string, or `null` when it does not exist or cannot be read.
|
|
255
|
+
* Uses `Bun.file().text()` when running under Bun, `fs.readFileSync` otherwise.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* const stored = await readIfExists('./src/gen/Pet.ts') // null on a first run
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
async function readIfExists(path) {
|
|
263
|
+
if (runtime.isBun) return Bun.file(path).text().catch(() => null);
|
|
264
|
+
if (!existsSync$1(path)) return null;
|
|
265
|
+
try {
|
|
266
|
+
return readFileSync(path, "utf8");
|
|
267
|
+
} catch (_error) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
200
270
|
}
|
|
201
271
|
/**
|
|
202
272
|
* Whether `stored` already holds `source`, comparing on the trimmed text rather than the exact
|
|
@@ -231,7 +301,7 @@ async function write(path, data, options = {}) {
|
|
|
231
301
|
const content = `${trimmed}\n`;
|
|
232
302
|
const resolved = resolve(path);
|
|
233
303
|
if (matchesStored({
|
|
234
|
-
stored: (options.stored === void 0 ? await
|
|
304
|
+
stored: (options.stored === void 0 ? await readIfExists(resolved) : options.stored) ?? "",
|
|
235
305
|
source: trimmed
|
|
236
306
|
})) return null;
|
|
237
307
|
await writeRaw(resolved, content);
|
|
@@ -256,11 +326,11 @@ async function writeRaw(path, content) {
|
|
|
256
326
|
return;
|
|
257
327
|
}
|
|
258
328
|
try {
|
|
259
|
-
await writeFile(resolved, content, { encoding: "utf-8" });
|
|
329
|
+
await new Promise((resolvePromise, reject) => writeFile(resolved, content, { encoding: "utf-8" }, (error) => error ? reject(error) : resolvePromise()));
|
|
260
330
|
} catch (error) {
|
|
261
331
|
if (error.code !== "ENOENT") throw error;
|
|
262
332
|
await mkdir$1(dirname(resolved), { recursive: true });
|
|
263
|
-
await writeFile(resolved, content, { encoding: "utf-8" });
|
|
333
|
+
await new Promise((resolvePromise, reject) => writeFile(resolved, content, { encoding: "utf-8" }, (error) => error ? reject(error) : resolvePromise()));
|
|
264
334
|
}
|
|
265
335
|
}
|
|
266
336
|
/**
|
|
@@ -324,38 +394,6 @@ function isPathInside(path, parent) {
|
|
|
324
394
|
const rel = relative(resolvedParent, resolvedPath);
|
|
325
395
|
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
|
|
326
396
|
}
|
|
327
|
-
// ../../internals/utils/src/promise.ts
|
|
328
|
-
/**
|
|
329
|
-
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
330
|
-
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
331
|
-
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
332
|
-
*
|
|
333
|
-
* @example
|
|
334
|
-
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
335
|
-
*/
|
|
336
|
-
function memo$1(store, key, compute) {
|
|
337
|
-
if (store.has(key)) return store.get(key);
|
|
338
|
-
const value = compute();
|
|
339
|
-
store.set(key, value);
|
|
340
|
-
return value;
|
|
341
|
-
}
|
|
342
|
-
__name(memo$1, "memo");
|
|
343
|
-
/**
|
|
344
|
-
* Runs `run` over every item with at most `limit` in flight. Workers share one iterator, so each
|
|
345
|
-
* takes the next item the moment it frees up instead of waiting for a batch to drain.
|
|
346
|
-
*
|
|
347
|
-
* @example
|
|
348
|
-
* ```ts
|
|
349
|
-
* await inParallel({ items: files, limit: 50, run: (file) => storage.writeItem(file.path, file.source) })
|
|
350
|
-
* ```
|
|
351
|
-
*/
|
|
352
|
-
async function inParallel({ items, limit, run }) {
|
|
353
|
-
const queue = items.entries();
|
|
354
|
-
const worker = async () => {
|
|
355
|
-
for (const [index, item] of queue) await run(item, index);
|
|
356
|
-
};
|
|
357
|
-
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, () => worker()));
|
|
358
|
-
}
|
|
359
397
|
// ../../internals/utils/src/tools.ts
|
|
360
398
|
/**
|
|
361
399
|
* CLI command descriptors for each supported code formatter.
|
|
@@ -1355,8 +1393,20 @@ function injectFile(file) {
|
|
|
1355
1393
|
}
|
|
1356
1394
|
/**
|
|
1357
1395
|
* Returns a value computed once per build and shared by every plugin, like `ctx.memo`.
|
|
1396
|
+
*
|
|
1397
|
+
* Pass a store first to cache in it instead, with no build needed: a `Map` or `WeakMap`, or one
|
|
1398
|
+
* from `memo.create`.
|
|
1399
|
+
*
|
|
1400
|
+
* @example Once per build
|
|
1401
|
+
* `const names = memo('myPlugin:names', () => collectNames(schemas))`
|
|
1402
|
+
*
|
|
1403
|
+
* @example In your own store
|
|
1404
|
+
* ```ts
|
|
1405
|
+
* const cache = memo.create<ast.SchemaNode, Array<string>>({ weak: true })
|
|
1406
|
+
* const refs = memo(cache, node, () => collectRefs(node))
|
|
1407
|
+
* ```
|
|
1358
1408
|
*/
|
|
1359
|
-
const memo = (
|
|
1409
|
+
const memo = Object.assign((...args) => args.length === 3 ? memo$1(...args) : requireContext("memo").memo(...args), { create: memo$1.create });
|
|
1360
1410
|
/**
|
|
1361
1411
|
* Returns the options the user passed to the plugin being set up, like `ctx.options`.
|
|
1362
1412
|
*/
|
|
@@ -1717,9 +1767,9 @@ function inputToAdapterSource(config) {
|
|
|
1717
1767
|
};
|
|
1718
1768
|
}
|
|
1719
1769
|
// src/resolveOptions.ts
|
|
1720
|
-
const patterns =
|
|
1721
|
-
const methodPatterns =
|
|
1722
|
-
const nodeCaches =
|
|
1770
|
+
const patterns = memo$1.create();
|
|
1771
|
+
const methodPatterns = memo$1.create();
|
|
1772
|
+
const nodeCaches = memo$1.create({ weak: true });
|
|
1723
1773
|
function testPattern(value, pattern) {
|
|
1724
1774
|
if (typeof pattern === "string") return memo$1(patterns, pattern, () => new RegExp(pattern)).test(value);
|
|
1725
1775
|
return value.match(pattern) !== null;
|
|
@@ -1786,6 +1836,7 @@ function isNamespace(value) {
|
|
|
1786
1836
|
* reachable and a `file` override is never dropped.
|
|
1787
1837
|
*/
|
|
1788
1838
|
const resolverOptions = Symbol.for("@kubb/core/resolver/options");
|
|
1839
|
+
const createNameNode = () => ({ next: /* @__PURE__ */ new Map() });
|
|
1789
1840
|
/**
|
|
1790
1841
|
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
1791
1842
|
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
@@ -1849,8 +1900,12 @@ var Resolver = class Resolver {
|
|
|
1849
1900
|
/** The built-in machinery, untouched by `name`/`file` overrides and built once since the driver reads it per node. */
|
|
1850
1901
|
default;
|
|
1851
1902
|
#options;
|
|
1852
|
-
#memo =
|
|
1903
|
+
#memo = memo$1.create();
|
|
1853
1904
|
#usedNames = /* @__PURE__ */ new Set();
|
|
1905
|
+
#stringNames = memo$1.create();
|
|
1906
|
+
#arrayNames = createNameNode();
|
|
1907
|
+
#outputDirs = memo$1.create({ weak: true });
|
|
1908
|
+
#importPaths = memo$1.create({ weak: true });
|
|
1854
1909
|
#tagParents = /* @__PURE__ */ new Map();
|
|
1855
1910
|
#baseName;
|
|
1856
1911
|
#filePath;
|
|
@@ -1917,16 +1972,30 @@ var Resolver = class Resolver {
|
|
|
1917
1972
|
* ```
|
|
1918
1973
|
*/
|
|
1919
1974
|
resolveName({ key, name, normalize = (name) => name }) {
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1975
|
+
if (typeof key === "string") return memo$1(this.#stringNames, key, () => this.#allocateName({
|
|
1976
|
+
name,
|
|
1977
|
+
normalize,
|
|
1978
|
+
suffix: 1
|
|
1979
|
+
}));
|
|
1980
|
+
const node = key.reduce((parent, segment) => memo$1(parent.next, segment, createNameNode), this.#arrayNames);
|
|
1981
|
+
node.name ??= this.#allocateName({
|
|
1982
|
+
name,
|
|
1983
|
+
normalize,
|
|
1984
|
+
suffix: 1
|
|
1985
|
+
});
|
|
1986
|
+
return node.name;
|
|
1987
|
+
}
|
|
1988
|
+
#allocateName({ name, normalize, suffix }) {
|
|
1989
|
+
const candidate = suffix === 1 ? name : `${name}${suffix}`;
|
|
1990
|
+
const normalized = normalize(candidate);
|
|
1991
|
+
const names = typeof normalized === "string" ? [normalized] : normalized;
|
|
1992
|
+
if (names.some((emittedName) => this.#usedNames.has(emittedName))) return this.#allocateName({
|
|
1993
|
+
name,
|
|
1994
|
+
normalize,
|
|
1995
|
+
suffix: suffix + 1
|
|
1996
|
+
});
|
|
1997
|
+
this.reserveNames(names);
|
|
1998
|
+
return candidate;
|
|
1930
1999
|
}
|
|
1931
2000
|
file(options) {
|
|
1932
2001
|
return this.#resolveFile(options);
|
|
@@ -1943,17 +2012,17 @@ var Resolver = class Resolver {
|
|
|
1943
2012
|
* reads the same ref set instead of re-scanning it per plugin.
|
|
1944
2013
|
*/
|
|
1945
2014
|
imports(options) {
|
|
1946
|
-
const { node, root, output,
|
|
2015
|
+
const { node, root, output, extname = ".ts", name, checks } = options;
|
|
1947
2016
|
const resolveName = name ?? ((schemaName) => this.name(schemaName));
|
|
2017
|
+
const paths = memo$1(memo$1(memo$1(this.#importPaths, output, () => /* @__PURE__ */ new Map()), root, () => /* @__PURE__ */ new Map()), extname, () => /* @__PURE__ */ new Map());
|
|
1948
2018
|
return ast.graph.getImportedNames(node, { checks }).map((schemaName) => ast.factory.createImport({
|
|
1949
2019
|
name: [resolveName(schemaName)],
|
|
1950
|
-
path: this.#resolveFilePath({
|
|
2020
|
+
path: memo$1(paths, schemaName, () => this.#resolveFilePath({
|
|
1951
2021
|
name: schemaName,
|
|
1952
2022
|
extname,
|
|
1953
2023
|
root,
|
|
1954
|
-
output
|
|
1955
|
-
|
|
1956
|
-
})
|
|
2024
|
+
output
|
|
2025
|
+
}))
|
|
1957
2026
|
}));
|
|
1958
2027
|
}
|
|
1959
2028
|
/**
|
|
@@ -2025,8 +2094,8 @@ var Resolver = class Resolver {
|
|
|
2025
2094
|
* are provided.
|
|
2026
2095
|
*/
|
|
2027
2096
|
#resolvePath({ baseName, tag, path: groupPath, root, output, group }) {
|
|
2028
|
-
|
|
2029
|
-
|
|
2097
|
+
const outputDir = memo$1(memo$1(this.#outputDirs, output, () => /* @__PURE__ */ new Map()), root, () => path.resolve(root, output.path));
|
|
2098
|
+
if (output.mode !== "directory") return outputDir;
|
|
2030
2099
|
const groupValue = group?.type === "path" ? groupPath : tag;
|
|
2031
2100
|
const result = group && groupValue ? path.resolve(outputDir, this.groupDir(group, groupValue), baseName) : path.resolve(outputDir, baseName);
|
|
2032
2101
|
assertInside({
|
|
@@ -2268,7 +2337,7 @@ function runParser(parser, print) {
|
|
|
2268
2337
|
* Reads a `copy` file and prints it through the parser's `copy` hook, or verbatim with `banner`/`footer` when there is none.
|
|
2269
2338
|
*/
|
|
2270
2339
|
async function parseCopy(file, parser) {
|
|
2271
|
-
const content = await readFile(file.copy, "utf8").catch((err) => {
|
|
2340
|
+
const content = await readFile$1(file.copy, "utf8").catch((err) => {
|
|
2272
2341
|
throw new Diagnostics.Error({
|
|
2273
2342
|
code: Diagnostics.code.pluginFailed,
|
|
2274
2343
|
severity: "error",
|
|
@@ -2304,7 +2373,8 @@ function compareFiles(a, b) {
|
|
|
2304
2373
|
/**
|
|
2305
2374
|
* Writes `source` to `file.path` unless the storage already holds it, either verbatim or, per the
|
|
2306
2375
|
* manifest, as what the output passes make of it. Checking here rather than in a driver gives a
|
|
2307
|
-
* storage writing to S3 or a database the same guarantee `fsStorage` has. `false` when
|
|
2376
|
+
* storage writing to S3 or a database the same guarantee `fsStorage` has. `written` is `false` when
|
|
2377
|
+
* skipped, and `stored` is what the storage held before the write.
|
|
2308
2378
|
*/
|
|
2309
2379
|
async function writeChanged({ storage, manifest, file, source, signal, cleaned }) {
|
|
2310
2380
|
signal?.throwIfAborted();
|
|
@@ -2316,14 +2386,20 @@ async function writeChanged({ storage, manifest, file, source, signal, cleaned }
|
|
|
2316
2386
|
key: file.path,
|
|
2317
2387
|
source,
|
|
2318
2388
|
disk: stored
|
|
2319
|
-
}))) return
|
|
2389
|
+
}))) return {
|
|
2390
|
+
written: false,
|
|
2391
|
+
stored
|
|
2392
|
+
};
|
|
2320
2393
|
signal?.throwIfAborted();
|
|
2321
2394
|
await storage.writeItem(file.path, source, { stored });
|
|
2322
2395
|
manifest?.track({
|
|
2323
2396
|
key: file.path,
|
|
2324
2397
|
source
|
|
2325
2398
|
});
|
|
2326
|
-
return
|
|
2399
|
+
return {
|
|
2400
|
+
written: true,
|
|
2401
|
+
stored
|
|
2402
|
+
};
|
|
2327
2403
|
}
|
|
2328
2404
|
/** Reports finished files in input order: each finished file flushes the run of finished files ahead of it as one update. */
|
|
2329
2405
|
function createProgress(files, config, hooks) {
|
|
@@ -2483,7 +2559,7 @@ var FileManager = class {
|
|
|
2483
2559
|
* memory rather than holding every source, while still overlapping each file's write with the
|
|
2484
2560
|
* next file's parse. Files finish in whatever order they parse, so each finished file flushes the
|
|
2485
2561
|
* run of finished files ahead of it as one `kubb:files:processing:update`, keeping rows in input order.
|
|
2486
|
-
* Each parsed source goes out once on `kubb:file:processed`,
|
|
2562
|
+
* Each parsed source goes out once on `kubb:file:processed`, next to what the storage held before the write.
|
|
2487
2563
|
*
|
|
2488
2564
|
* A file the storage already holds is skipped, so a rebuild that generates identical output
|
|
2489
2565
|
* writes nothing and leaves every mtime where it was. `kubb:files:processing:end` lists the
|
|
@@ -2501,17 +2577,19 @@ var FileManager = class {
|
|
|
2501
2577
|
signal?.throwIfAborted();
|
|
2502
2578
|
const source = await this.parse(file, { parsers });
|
|
2503
2579
|
if (source) {
|
|
2504
|
-
|
|
2580
|
+
const { written, stored } = await writeChanged({
|
|
2505
2581
|
storage,
|
|
2506
2582
|
manifest,
|
|
2507
2583
|
file,
|
|
2508
2584
|
source,
|
|
2509
2585
|
signal,
|
|
2510
2586
|
cleaned
|
|
2511
|
-
})
|
|
2587
|
+
});
|
|
2588
|
+
if (!written) unchanged.push(file.path);
|
|
2512
2589
|
await hooks?.callHook("kubb:file:processed", {
|
|
2513
2590
|
file,
|
|
2514
|
-
source
|
|
2591
|
+
source,
|
|
2592
|
+
stored
|
|
2515
2593
|
});
|
|
2516
2594
|
}
|
|
2517
2595
|
await progress?.(index);
|
|
@@ -2634,7 +2712,7 @@ var KubbDriver = class {
|
|
|
2634
2712
|
*/
|
|
2635
2713
|
fileManager = new FileManager();
|
|
2636
2714
|
plugins = /* @__PURE__ */ new Map();
|
|
2637
|
-
#memo =
|
|
2715
|
+
#memo = memo$1.create();
|
|
2638
2716
|
/**
|
|
2639
2717
|
* Removers for every listener the driver added (plugin, generator) so `dispose()` can detach
|
|
2640
2718
|
* them in one pass. External `hooks.hook(...)` listeners are not tracked.
|
|
@@ -3388,11 +3466,7 @@ const createStorage = (build) => (options) => build(options ?? {});
|
|
|
3388
3466
|
const fsStorage = createStorage(() => ({
|
|
3389
3467
|
name: "fs",
|
|
3390
3468
|
async readItem(key) {
|
|
3391
|
-
|
|
3392
|
-
return await read(resolve(key));
|
|
3393
|
-
} catch (_error) {
|
|
3394
|
-
return null;
|
|
3395
|
-
}
|
|
3469
|
+
return readIfExists(resolve(key));
|
|
3396
3470
|
},
|
|
3397
3471
|
async writeItem(key, value, options) {
|
|
3398
3472
|
await write(resolve(key), value, {
|
|
@@ -3480,7 +3554,7 @@ const cacheStorage = createStorage(({ root = process.cwd() }) => {
|
|
|
3480
3554
|
* ```
|
|
3481
3555
|
*/
|
|
3482
3556
|
var Hookable = class {
|
|
3483
|
-
#listeners =
|
|
3557
|
+
#listeners = memo$1.create();
|
|
3484
3558
|
/**
|
|
3485
3559
|
* Calls `hookName` and awaits all registered listeners sequentially.
|
|
3486
3560
|
* Throws if any listener rejects, wrapping the cause in an error that names the hook.
|
|
@@ -3512,9 +3586,7 @@ var Hookable = class {
|
|
|
3512
3586
|
* ```
|
|
3513
3587
|
*/
|
|
3514
3588
|
hook(hookName, handler) {
|
|
3515
|
-
|
|
3516
|
-
if (!listeners) this.#listeners.set(hookName, listeners = /* @__PURE__ */ new Set());
|
|
3517
|
-
listeners.add(handler);
|
|
3589
|
+
memo$1(this.#listeners, hookName, () => /* @__PURE__ */ new Set()).add(handler);
|
|
3518
3590
|
return () => this.removeHook(hookName, handler);
|
|
3519
3591
|
}
|
|
3520
3592
|
/**
|
|
@@ -4587,7 +4659,7 @@ function isRegistryEntry(value) {
|
|
|
4587
4659
|
}
|
|
4588
4660
|
// src/installedPackages.ts
|
|
4589
4661
|
/** Reads and parses a JSON file. Throws when it is missing or not JSON. */
|
|
4590
|
-
const readJSON = async (file) => JSON.parse(await readFile(file, "utf8"));
|
|
4662
|
+
const readJSON = async (file) => JSON.parse(await readFile$1(file, "utf8"));
|
|
4591
4663
|
/**
|
|
4592
4664
|
* The `dependencies` and `devDependencies` of the project at `root`, plus `packages`, with the
|
|
4593
4665
|
* installed version and the valid `kubb-extension.json` each one ships, when it does. A package
|