@kubb/core 5.0.0-beta.9 → 5.0.0-beta.93
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/LICENSE +17 -10
- package/README.md +20 -123
- package/dist/index.cjs +2217 -1132
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +135 -178
- package/dist/index.js +2209 -1125
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +77 -24
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +37 -11
- package/dist/mocks.js +79 -28
- package/dist/mocks.js.map +1 -1
- package/dist/types-BLFyAJLZ.d.ts +2785 -0
- package/dist/usingCtx-BriKju-v.js +577 -0
- package/dist/usingCtx-BriKju-v.js.map +1 -0
- package/dist/usingCtx-eyNeehd2.cjs +679 -0
- package/dist/usingCtx-eyNeehd2.cjs.map +1 -0
- package/package.json +7 -28
- package/dist/PluginDriver-Cu1Kj9S-.cjs +0 -1075
- package/dist/PluginDriver-Cu1Kj9S-.cjs.map +0 -1
- package/dist/PluginDriver-D8Z0Htid.js +0 -978
- package/dist/PluginDriver-D8Z0Htid.js.map +0 -1
- package/dist/createKubb-ALdb8lmq.d.ts +0 -2082
- package/src/FileManager.ts +0 -115
- package/src/FileProcessor.ts +0 -86
- package/src/PluginDriver.ts +0 -457
- package/src/constants.ts +0 -35
- package/src/createAdapter.ts +0 -108
- package/src/createKubb.ts +0 -1268
- package/src/createRenderer.ts +0 -57
- package/src/createStorage.ts +0 -70
- package/src/defineGenerator.ts +0 -175
- package/src/defineLogger.ts +0 -58
- package/src/defineMiddleware.ts +0 -62
- package/src/defineParser.ts +0 -44
- package/src/definePlugin.ts +0 -379
- package/src/defineResolver.ts +0 -654
- package/src/devtools.ts +0 -66
- package/src/index.ts +0 -20
- package/src/mocks.ts +0 -177
- package/src/storages/fsStorage.ts +0 -90
- package/src/storages/memoryStorage.ts +0 -55
- package/src/types.ts +0 -41
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", {
|
|
5
|
+
value,
|
|
6
|
+
configurable: true
|
|
7
|
+
});
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
//#endregion
|
|
27
|
+
let node_fs_promises = require("node:fs/promises");
|
|
28
|
+
let node_path = require("node:path");
|
|
29
|
+
let _kubb_ast = require("@kubb/ast");
|
|
30
|
+
let node_events = require("node:events");
|
|
31
|
+
//#region ../../internals/utils/src/casing.ts
|
|
32
|
+
/**
|
|
33
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
34
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
35
|
+
* and capitalizes each word according to `pascal`.
|
|
36
|
+
*
|
|
37
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
38
|
+
*/
|
|
39
|
+
function toCamelOrPascal(text, pascal) {
|
|
40
|
+
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) => {
|
|
41
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
42
|
+
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
43
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Converts `text` to camelCase.
|
|
47
|
+
*
|
|
48
|
+
* @example Word boundaries
|
|
49
|
+
* `camelCase('hello-world') // 'helloWorld'`
|
|
50
|
+
*
|
|
51
|
+
* @example With a prefix
|
|
52
|
+
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
|
|
53
|
+
*/
|
|
54
|
+
function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
55
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region ../../internals/utils/src/errors.ts
|
|
59
|
+
/**
|
|
60
|
+
* Thrown when one or more errors occur during a Kubb build.
|
|
61
|
+
* Carries the full list of underlying errors on `errors`.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* throw new BuildError('Build failed', { errors: [err1, err2] })
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
var BuildError = class extends Error {
|
|
69
|
+
errors;
|
|
70
|
+
constructor(message, options) {
|
|
71
|
+
super(message, { cause: options.cause });
|
|
72
|
+
this.name = "BuildError";
|
|
73
|
+
this.errors = options.errors;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Coerces an unknown thrown value to an `Error` instance.
|
|
78
|
+
* Returns the value as-is when it is already an `Error`; otherwise wraps it with `String(value)`.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* try { ... } catch(err) {
|
|
83
|
+
* throw new BuildError('Build failed', { cause: toError(err), errors: [] })
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function toError(value) {
|
|
88
|
+
return value instanceof Error ? value : new Error(String(value));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Extracts a human-readable message from any thrown value.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* getErrorMessage(new Error('oops')) // 'oops'
|
|
96
|
+
* getErrorMessage('plain string') // 'plain string'
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
function getErrorMessage(value) {
|
|
100
|
+
return value instanceof Error ? value.message : String(value);
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region ../../internals/utils/src/runtime.ts
|
|
104
|
+
/**
|
|
105
|
+
* Detects the JavaScript runtime executing the current process and exposes its name and version.
|
|
106
|
+
*
|
|
107
|
+
* Prefer the shared {@link runtime} instance over constructing your own.
|
|
108
|
+
*/
|
|
109
|
+
var Runtime = class {
|
|
110
|
+
/**
|
|
111
|
+
* `true` when the current process is running under Bun.
|
|
112
|
+
*
|
|
113
|
+
* Detection keys off the global `Bun` object rather than `process.versions`,
|
|
114
|
+
* because Bun polyfills `process.versions.node` for Node compatibility and would
|
|
115
|
+
* otherwise look like Node.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* if (runtime.isBun) {
|
|
120
|
+
* await Bun.write(path, data)
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
get isBun() {
|
|
125
|
+
return typeof Bun !== "undefined";
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* `true` when the current process is running under Deno.
|
|
129
|
+
*/
|
|
130
|
+
get isDeno() {
|
|
131
|
+
return typeof globalThis.Deno !== "undefined";
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* `true` when the current process is running under Node.
|
|
135
|
+
*
|
|
136
|
+
* Bun and Deno are excluded first so a polyfilled `process` does not register as Node.
|
|
137
|
+
*/
|
|
138
|
+
get isNode() {
|
|
139
|
+
return !this.isBun && !this.isDeno && typeof process !== "undefined" && process.versions?.node != null;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Name of the runtime executing the current process.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* runtime.name // 'bun' when run with `bun kubb`, 'node' otherwise
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
get name() {
|
|
150
|
+
if (this.isBun) return "bun";
|
|
151
|
+
if (this.isDeno) return "deno";
|
|
152
|
+
return "node";
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Version of the active runtime, or an empty string when it cannot be read.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* runtime.version // '1.3.11' under Bun, '22.22.2' under Node
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
get version() {
|
|
163
|
+
if (this.isBun) return process.versions.bun ?? "";
|
|
164
|
+
if (this.isDeno) return globalThis.Deno?.version?.deno ?? "";
|
|
165
|
+
return process.versions?.node ?? "";
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Shared {@link Runtime} instance describing the JavaScript runtime executing the current process.
|
|
170
|
+
*/
|
|
171
|
+
const runtime = new Runtime();
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region ../../internals/utils/src/fs.ts
|
|
174
|
+
/**
|
|
175
|
+
* Reads the file at `path` as a UTF-8 string.
|
|
176
|
+
* Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const source = await read('./src/Pet.ts')
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
async function read(path) {
|
|
184
|
+
if (runtime.isBun) return Bun.file(path).text();
|
|
185
|
+
return (0, node_fs_promises.readFile)(path, { encoding: "utf8" });
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Writes `data` to `path`, trimming leading/trailing whitespace before saving.
|
|
189
|
+
* Skips the write when the trimmed content is empty or identical to what is already on disk.
|
|
190
|
+
* Creates any missing parent directories automatically.
|
|
191
|
+
* When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* await write('./src/Pet.ts', source) // writes and returns trimmed content
|
|
196
|
+
* await write('./src/Pet.ts', source) // null — file unchanged
|
|
197
|
+
* await write('./src/Pet.ts', ' ') // null — empty content skipped
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
async function write(path, data, options = {}) {
|
|
201
|
+
const trimmed = data.trim();
|
|
202
|
+
if (trimmed === "") return null;
|
|
203
|
+
const resolved = (0, node_path.resolve)(path);
|
|
204
|
+
if (runtime.isBun) {
|
|
205
|
+
const file = Bun.file(resolved);
|
|
206
|
+
if ((await file.exists() ? await file.text() : null) === trimmed) return null;
|
|
207
|
+
await Bun.write(resolved, trimmed);
|
|
208
|
+
return trimmed;
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
if (await (0, node_fs_promises.readFile)(resolved, { encoding: "utf-8" }) === trimmed) return null;
|
|
212
|
+
} catch {}
|
|
213
|
+
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(resolved), { recursive: true });
|
|
214
|
+
await (0, node_fs_promises.writeFile)(resolved, trimmed, { encoding: "utf-8" });
|
|
215
|
+
if (options.sanity) {
|
|
216
|
+
const savedData = await (0, node_fs_promises.readFile)(resolved, { encoding: "utf-8" });
|
|
217
|
+
if (savedData !== trimmed) throw new Error(`Sanity check failed for ${path}\n\nData[${data.length}]:\n${data}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
218
|
+
return savedData;
|
|
219
|
+
}
|
|
220
|
+
return trimmed;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Recursively removes `path`. Silently succeeds when `path` does not exist.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* await clean('./dist')
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
230
|
+
async function clean(path) {
|
|
231
|
+
return (0, node_fs_promises.rm)(path, {
|
|
232
|
+
recursive: true,
|
|
233
|
+
force: true
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Converts a filesystem path to use POSIX (`/`) separators.
|
|
238
|
+
*
|
|
239
|
+
* Most of the codebase compares and composes paths as strings (prefix matching, joining for
|
|
240
|
+
* import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated
|
|
241
|
+
* paths, but on Windows it returns `\`-separated paths, which breaks every such comparison.
|
|
242
|
+
*
|
|
243
|
+
* Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the
|
|
244
|
+
* code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is
|
|
245
|
+
* exercisable from POSIX CI.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* toPosixPath('C:\\repo\\src\\pet.ts') // 'C:/repo/src/pet.ts'
|
|
249
|
+
*/
|
|
250
|
+
function toPosixPath(filePath) {
|
|
251
|
+
return filePath.replaceAll("\\", "/");
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
255
|
+
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
256
|
+
* every earlier segment, applies `caseLast` to the final segment, and joins with `/`.
|
|
257
|
+
*
|
|
258
|
+
* Empty segments are dropped before joining. They arise when the name starts with a dot
|
|
259
|
+
* followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to
|
|
260
|
+
* an empty string). Without this a leading `/` would form, which `path.resolve` reads as an
|
|
261
|
+
* absolute path, letting generated files escape the configured output directory.
|
|
262
|
+
*
|
|
263
|
+
* @example Nested path from a dotted name
|
|
264
|
+
* `toFilePath('pet.petId') // 'pet/petId'`
|
|
265
|
+
*
|
|
266
|
+
* @example PascalCase the final segment
|
|
267
|
+
* `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`
|
|
268
|
+
*
|
|
269
|
+
* @example Suffix applied to the final segment only
|
|
270
|
+
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
271
|
+
*/
|
|
272
|
+
function toFilePath(name, caseLast = camelCase) {
|
|
273
|
+
const parts = name.split(/\.(?=[a-zA-Z])/);
|
|
274
|
+
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
275
|
+
}
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region src/Hookable.ts
|
|
278
|
+
/**
|
|
279
|
+
* Typed hook emitter that awaits all async listeners before resolving.
|
|
280
|
+
* Wraps Node's `EventEmitter` with full TypeScript hook-map inference.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* const hooks = new Hookable<{ build: [name: string] }>()
|
|
285
|
+
* hooks.hook('build', async (name) => { console.log(name) })
|
|
286
|
+
* await hooks.callHook('build', 'petstore') // all listeners awaited
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
var Hookable = class {
|
|
290
|
+
/**
|
|
291
|
+
* Maximum number of listeners per hook before Node emits a memory-leak warning.
|
|
292
|
+
* @default 10
|
|
293
|
+
*/
|
|
294
|
+
constructor(maxListener = 10) {
|
|
295
|
+
this.#emitter.setMaxListeners(maxListener);
|
|
296
|
+
}
|
|
297
|
+
#emitter = new node_events.EventEmitter();
|
|
298
|
+
/**
|
|
299
|
+
* Calls `hookName` and awaits all registered listeners sequentially.
|
|
300
|
+
* Throws if any listener rejects, wrapping the cause with the hook name and serialized arguments.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* await hooks.callHook('build', 'petstore')
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
callHook(hookName, ...hookArgs) {
|
|
308
|
+
const listeners = this.#emitter.listeners(hookName);
|
|
309
|
+
if (listeners.length === 0) return;
|
|
310
|
+
return this.#emitAll(hookName, listeners, hookArgs);
|
|
311
|
+
}
|
|
312
|
+
async #emitAll(hookName, listeners, hookArgs) {
|
|
313
|
+
for (const listener of listeners) try {
|
|
314
|
+
await listener(...hookArgs);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
let serializedArgs;
|
|
317
|
+
try {
|
|
318
|
+
serializedArgs = JSON.stringify(hookArgs);
|
|
319
|
+
} catch {
|
|
320
|
+
serializedArgs = String(hookArgs);
|
|
321
|
+
}
|
|
322
|
+
throw new Error(`Error in async listener for "${hookName}" with hookArgs ${serializedArgs}`, { cause: toError(err) });
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Registers a persistent listener for `hookName` and returns a function that removes it.
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* const unhook = hooks.hook('build', async (name) => { console.log(name) })
|
|
331
|
+
* unhook() // removes it
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
hook(hookName, handler) {
|
|
335
|
+
this.#emitter.on(hookName, handler);
|
|
336
|
+
return () => this.removeHook(hookName, handler);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Registers every handler in `configHooks` at once and returns a function that removes them
|
|
340
|
+
* all. Undefined entries are skipped, so a partial hook object registers only its present keys.
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* ```ts
|
|
344
|
+
* const unhook = hooks.addHooks({ build: onBuild, done: onDone })
|
|
345
|
+
* unhook() // removes both
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
348
|
+
addHooks(configHooks) {
|
|
349
|
+
const unhooks = Object.keys(configHooks).filter((name) => configHooks[name]).map((name) => this.hook(name, configHooks[name]));
|
|
350
|
+
return () => {
|
|
351
|
+
for (const unhook of unhooks) unhook();
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Removes a previously registered listener.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* hooks.removeHook('build', handler)
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
removeHook(hookName, handler) {
|
|
363
|
+
this.#emitter.off(hookName, handler);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Returns the number of listeners registered for `hookName`.
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```ts
|
|
370
|
+
* hooks.hook('build', handler)
|
|
371
|
+
* hooks.listenerCount('build') // 1
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
listenerCount(hookName) {
|
|
375
|
+
return this.#emitter.listenerCount(hookName);
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Raises or lowers the per-hook listener ceiling before Node warns about a memory leak.
|
|
379
|
+
* Set this above the expected listener count when many listeners attach by design.
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```ts
|
|
383
|
+
* hooks.setMaxListeners(40)
|
|
384
|
+
* ```
|
|
385
|
+
*/
|
|
386
|
+
setMaxListeners(max) {
|
|
387
|
+
this.#emitter.setMaxListeners(max);
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Removes all listeners from every hook channel.
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* hooks.removeAllHooks()
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
removeAllHooks() {
|
|
398
|
+
this.#emitter.removeAllListeners();
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
//#endregion
|
|
402
|
+
//#region src/FileManager.ts
|
|
403
|
+
function joinSources(file) {
|
|
404
|
+
return file.sources.map((source) => (0, _kubb_ast.extractStringsFromNodes)(source.nodes)).filter(Boolean).join("\n\n");
|
|
405
|
+
}
|
|
406
|
+
async function parseCopy(file) {
|
|
407
|
+
let content;
|
|
408
|
+
try {
|
|
409
|
+
content = await read(file.copy);
|
|
410
|
+
} catch (err) {
|
|
411
|
+
throw new Error(`[kubb] Could not copy file into output: ${file.copy}`, { cause: err });
|
|
412
|
+
}
|
|
413
|
+
return [
|
|
414
|
+
file.banner,
|
|
415
|
+
content,
|
|
416
|
+
file.footer
|
|
417
|
+
].filter((segment) => Boolean(segment)).map((segment) => segment.trimEnd()).join("\n");
|
|
418
|
+
}
|
|
419
|
+
function mergeFile(a, b) {
|
|
420
|
+
return {
|
|
421
|
+
...a,
|
|
422
|
+
banner: b.banner,
|
|
423
|
+
footer: b.footer,
|
|
424
|
+
copy: b.copy ?? a.copy,
|
|
425
|
+
sources: a.sources.length ? b.sources.length ? [...a.sources, ...b.sources] : a.sources : b.sources,
|
|
426
|
+
imports: a.imports.length ? b.imports.length ? [...a.imports, ...b.imports] : a.imports : b.imports,
|
|
427
|
+
exports: a.exports.length ? b.exports.length ? [...a.exports, ...b.exports] : a.exports : b.exports
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
function isIndexPath(path) {
|
|
431
|
+
return path.endsWith("/index.ts") || path === "index.ts";
|
|
432
|
+
}
|
|
433
|
+
function compareFiles(a, b) {
|
|
434
|
+
const lenDiff = a.path.length - b.path.length;
|
|
435
|
+
if (lenDiff !== 0) return lenDiff;
|
|
436
|
+
const aIsIndex = isIndexPath(a.path);
|
|
437
|
+
const bIsIndex = isIndexPath(b.path);
|
|
438
|
+
if (aIsIndex && !bIsIndex) return 1;
|
|
439
|
+
if (!aIsIndex && bIsIndex) return -1;
|
|
440
|
+
return 0;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* In-memory file store for generated files, and the writer that turns them into source
|
|
444
|
+
* strings on `storage`. Files sharing a `path` are merged (sources/imports/exports
|
|
445
|
+
* concatenated). The `files` getter is sorted by path length (barrel `index.ts` last
|
|
446
|
+
* within a bucket).
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* const manager = new FileManager()
|
|
451
|
+
* manager.upsert(myFile)
|
|
452
|
+
* manager.files // sorted view
|
|
453
|
+
* await manager.write(manager.files, { storage: fsStorage() })
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
456
|
+
var FileManager = class {
|
|
457
|
+
hooks = new Hookable();
|
|
458
|
+
#cache = /* @__PURE__ */ new Map();
|
|
459
|
+
#sorted = null;
|
|
460
|
+
add(...files) {
|
|
461
|
+
return this.#store(files, false);
|
|
462
|
+
}
|
|
463
|
+
upsert(...files) {
|
|
464
|
+
return this.#store(files, true);
|
|
465
|
+
}
|
|
466
|
+
#store(files, mergeExisting) {
|
|
467
|
+
const batch = files.length > 1 ? this.#dedupe(files) : files;
|
|
468
|
+
const resolved = [];
|
|
469
|
+
for (const file of batch) {
|
|
470
|
+
const existing = this.#cache.get(file.path);
|
|
471
|
+
const merged = existing && mergeExisting ? _kubb_ast.ast.factory.createFile(mergeFile(existing, file)) : _kubb_ast.ast.factory.createFile(file);
|
|
472
|
+
this.#cache.set(merged.path, merged);
|
|
473
|
+
resolved.push(merged);
|
|
474
|
+
}
|
|
475
|
+
if (resolved.length > 0) this.#sorted = null;
|
|
476
|
+
return resolved;
|
|
477
|
+
}
|
|
478
|
+
#dedupe(files) {
|
|
479
|
+
const seen = /* @__PURE__ */ new Map();
|
|
480
|
+
for (const file of files) {
|
|
481
|
+
const prev = seen.get(file.path);
|
|
482
|
+
seen.set(file.path, prev ? mergeFile(prev, file) : file);
|
|
483
|
+
}
|
|
484
|
+
return [...seen.values()];
|
|
485
|
+
}
|
|
486
|
+
clear() {
|
|
487
|
+
this.#cache.clear();
|
|
488
|
+
this.#sorted = null;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Releases all stored files and clears every `hooks` listener. Called by the core after
|
|
492
|
+
* `kubb:build:end`.
|
|
493
|
+
*/
|
|
494
|
+
dispose() {
|
|
495
|
+
this.clear();
|
|
496
|
+
this.hooks.removeAllHooks();
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* All stored files in stable sort order (shortest path first, barrel files
|
|
500
|
+
* last within a length bucket). Returns a cached view, do not mutate.
|
|
501
|
+
*/
|
|
502
|
+
get files() {
|
|
503
|
+
return this.#sorted ??= [...this.#cache.values()].sort(compareFiles);
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Converts a file's AST sources (or its `copy` source) into the final on-disk string.
|
|
507
|
+
*/
|
|
508
|
+
async parse(file, { parsers } = {}) {
|
|
509
|
+
if (file.copy) return parseCopy(file);
|
|
510
|
+
if (!parsers || !file.extname) return joinSources(file);
|
|
511
|
+
const parser = parsers.get(file.extname);
|
|
512
|
+
if (!parser) return joinSources(file);
|
|
513
|
+
return parser.parse(file);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Converts and writes every file at once, letting `storage.setItem` decide how much of
|
|
517
|
+
* that runs concurrently.
|
|
518
|
+
*/
|
|
519
|
+
async write(files, { storage, parsers }) {
|
|
520
|
+
if (files.length === 0) return;
|
|
521
|
+
await this.hooks.callHook("start", files);
|
|
522
|
+
const total = files.length;
|
|
523
|
+
let processed = 0;
|
|
524
|
+
await Promise.all(files.map(async (file) => {
|
|
525
|
+
const source = await this.parse(file, { parsers });
|
|
526
|
+
processed++;
|
|
527
|
+
await this.hooks.callHook("update", {
|
|
528
|
+
file,
|
|
529
|
+
source,
|
|
530
|
+
processed,
|
|
531
|
+
total,
|
|
532
|
+
percentage: processed / total * 100
|
|
533
|
+
});
|
|
534
|
+
if (source) await storage.setItem(file.path, source);
|
|
535
|
+
}));
|
|
536
|
+
await this.hooks.callHook("end", files);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
//#endregion
|
|
540
|
+
//#region \0@oxc-project+runtime@0.138.0/helpers/esm/usingCtx.js
|
|
541
|
+
function _usingCtx() {
|
|
542
|
+
var r = "function" == typeof SuppressedError ? SuppressedError : function(r, e) {
|
|
543
|
+
var n = Error();
|
|
544
|
+
return n.name = "SuppressedError", n.error = r, n.suppressed = e, n;
|
|
545
|
+
};
|
|
546
|
+
var e = {};
|
|
547
|
+
var n = [];
|
|
548
|
+
function using(r, e) {
|
|
549
|
+
if (null != e) {
|
|
550
|
+
if (Object(e) !== e) throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
|
|
551
|
+
if (r) var o = e[Symbol.asyncDispose || Symbol["for"]("Symbol.asyncDispose")];
|
|
552
|
+
if (void 0 === o && (o = e[Symbol.dispose || Symbol["for"]("Symbol.dispose")], r)) var t = o;
|
|
553
|
+
if ("function" != typeof o) throw new TypeError("Object is not disposable.");
|
|
554
|
+
t && (o = function o() {
|
|
555
|
+
try {
|
|
556
|
+
t.call(e);
|
|
557
|
+
} catch (r) {
|
|
558
|
+
return Promise.reject(r);
|
|
559
|
+
}
|
|
560
|
+
}), n.push({
|
|
561
|
+
v: e,
|
|
562
|
+
d: o,
|
|
563
|
+
a: r
|
|
564
|
+
});
|
|
565
|
+
} else r && n.push({
|
|
566
|
+
d: e,
|
|
567
|
+
a: r
|
|
568
|
+
});
|
|
569
|
+
return e;
|
|
570
|
+
}
|
|
571
|
+
return {
|
|
572
|
+
e,
|
|
573
|
+
u: using.bind(null, !1),
|
|
574
|
+
a: using.bind(null, !0),
|
|
575
|
+
d: function d() {
|
|
576
|
+
var o;
|
|
577
|
+
var t = this.e;
|
|
578
|
+
var s = 0;
|
|
579
|
+
function next() {
|
|
580
|
+
for (; o = n.pop();) try {
|
|
581
|
+
if (!o.a && 1 === s) return s = 0, n.push(o), Promise.resolve().then(next);
|
|
582
|
+
if (o.d) {
|
|
583
|
+
var r = o.d.call(o.v);
|
|
584
|
+
if (o.a) return s |= 2, Promise.resolve(r).then(next, err);
|
|
585
|
+
} else s |= 1;
|
|
586
|
+
} catch (r) {
|
|
587
|
+
return err(r);
|
|
588
|
+
}
|
|
589
|
+
if (1 === s) return t !== e ? Promise.reject(t) : Promise.resolve();
|
|
590
|
+
if (t !== e) throw t;
|
|
591
|
+
}
|
|
592
|
+
function err(n) {
|
|
593
|
+
return t = t !== e ? new r(n, t) : n, next();
|
|
594
|
+
}
|
|
595
|
+
return next();
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
//#endregion
|
|
600
|
+
Object.defineProperty(exports, "BuildError", {
|
|
601
|
+
enumerable: true,
|
|
602
|
+
get: function() {
|
|
603
|
+
return BuildError;
|
|
604
|
+
}
|
|
605
|
+
});
|
|
606
|
+
Object.defineProperty(exports, "FileManager", {
|
|
607
|
+
enumerable: true,
|
|
608
|
+
get: function() {
|
|
609
|
+
return FileManager;
|
|
610
|
+
}
|
|
611
|
+
});
|
|
612
|
+
Object.defineProperty(exports, "Hookable", {
|
|
613
|
+
enumerable: true,
|
|
614
|
+
get: function() {
|
|
615
|
+
return Hookable;
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
Object.defineProperty(exports, "__name", {
|
|
619
|
+
enumerable: true,
|
|
620
|
+
get: function() {
|
|
621
|
+
return __name;
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
Object.defineProperty(exports, "__toESM", {
|
|
625
|
+
enumerable: true,
|
|
626
|
+
get: function() {
|
|
627
|
+
return __toESM;
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
Object.defineProperty(exports, "_usingCtx", {
|
|
631
|
+
enumerable: true,
|
|
632
|
+
get: function() {
|
|
633
|
+
return _usingCtx;
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
Object.defineProperty(exports, "camelCase", {
|
|
637
|
+
enumerable: true,
|
|
638
|
+
get: function() {
|
|
639
|
+
return camelCase;
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
Object.defineProperty(exports, "clean", {
|
|
643
|
+
enumerable: true,
|
|
644
|
+
get: function() {
|
|
645
|
+
return clean;
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
Object.defineProperty(exports, "getErrorMessage", {
|
|
649
|
+
enumerable: true,
|
|
650
|
+
get: function() {
|
|
651
|
+
return getErrorMessage;
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
Object.defineProperty(exports, "toError", {
|
|
655
|
+
enumerable: true,
|
|
656
|
+
get: function() {
|
|
657
|
+
return toError;
|
|
658
|
+
}
|
|
659
|
+
});
|
|
660
|
+
Object.defineProperty(exports, "toFilePath", {
|
|
661
|
+
enumerable: true,
|
|
662
|
+
get: function() {
|
|
663
|
+
return toFilePath;
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
Object.defineProperty(exports, "toPosixPath", {
|
|
667
|
+
enumerable: true,
|
|
668
|
+
get: function() {
|
|
669
|
+
return toPosixPath;
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
Object.defineProperty(exports, "write", {
|
|
673
|
+
enumerable: true,
|
|
674
|
+
get: function() {
|
|
675
|
+
return write;
|
|
676
|
+
}
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
//# sourceMappingURL=usingCtx-eyNeehd2.cjs.map
|