@kubb/core 5.0.0-beta.2 → 5.0.0-beta.21
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/README.md +8 -38
- package/dist/KubbDriver-BBRa5CH2.cjs +2231 -0
- package/dist/KubbDriver-BBRa5CH2.cjs.map +1 -0
- package/dist/KubbDriver-Cq1isv2P.js +2110 -0
- package/dist/KubbDriver-Cq1isv2P.js.map +1 -0
- package/dist/{types-CC09VtBt.d.ts → createKubb-CYrw_xaR.d.ts} +1414 -1255
- package/dist/index.cjs +221 -1074
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -185
- package/dist/index.js +211 -1068
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +30 -21
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +5 -5
- package/dist/mocks.js +29 -20
- package/dist/mocks.js.map +1 -1
- package/package.json +6 -18
- package/src/FileManager.ts +75 -58
- package/src/FileProcessor.ts +48 -38
- package/src/KubbDriver.ts +915 -0
- package/src/constants.ts +11 -6
- package/src/createAdapter.ts +84 -1
- package/src/createKubb.ts +1022 -485
- package/src/createRenderer.ts +33 -22
- package/src/defineGenerator.ts +96 -7
- package/src/defineLogger.ts +42 -3
- package/src/defineMiddleware.ts +1 -1
- package/src/defineParser.ts +1 -1
- package/src/definePlugin.ts +304 -8
- package/src/defineResolver.ts +271 -150
- package/src/devtools.ts +8 -1
- package/src/index.ts +2 -2
- package/src/mocks.ts +11 -14
- package/src/storages/fsStorage.ts +13 -37
- package/src/types.ts +39 -1292
- package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
- package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
- package/dist/PluginDriver-DV3p2Hky.js +0 -945
- package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
- package/src/Kubb.ts +0 -300
- package/src/PluginDriver.ts +0 -424
- package/src/renderNode.ts +0 -35
- package/src/utils/diagnostics.ts +0 -18
- package/src/utils/isInputPath.ts +0 -10
- package/src/utils/packageJSON.ts +0 -99
package/dist/mocks.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import {
|
|
2
|
+
import { a as FileManager, n as applyHookResult, t as KubbDriver } from "./KubbDriver-Cq1isv2P.js";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { transform } from "@kubb/ast";
|
|
5
5
|
//#region src/mocks.ts
|
|
@@ -26,11 +26,9 @@ function createMockedPluginDriver(options = {}) {
|
|
|
26
26
|
* `getImports` returns `[]` by default.
|
|
27
27
|
*/
|
|
28
28
|
function createMockedAdapter(options = {}) {
|
|
29
|
-
const inputNode = options.inputNode ?? null;
|
|
30
29
|
return {
|
|
31
30
|
name: options.name ?? "oas",
|
|
32
31
|
options: options.resolvedOptions ?? {},
|
|
33
|
-
inputNode,
|
|
34
32
|
parse: options.parse ?? (async () => ({
|
|
35
33
|
kind: "Input",
|
|
36
34
|
schemas: [],
|
|
@@ -60,16 +58,15 @@ function createMockedPluginContext(opts) {
|
|
|
60
58
|
return {
|
|
61
59
|
config: opts.config,
|
|
62
60
|
root,
|
|
63
|
-
getMode: (output) =>
|
|
61
|
+
getMode: (output) => KubbDriver.getMode(resolve(root, output.path)),
|
|
64
62
|
adapter: opts.adapter,
|
|
65
63
|
resolver: opts.resolver,
|
|
66
64
|
plugin: opts.plugin,
|
|
67
65
|
driver: opts.driver,
|
|
68
66
|
getResolver: (name) => opts.driver.getResolver(name),
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
operations: []
|
|
67
|
+
meta: opts.meta ?? {
|
|
68
|
+
circularNames: [],
|
|
69
|
+
enumNames: []
|
|
73
70
|
},
|
|
74
71
|
addFile: async (...files) => opts.driver.fileManager.add(...files),
|
|
75
72
|
upsertFile: async (...files) => opts.driver.fileManager.upsert(...files),
|
|
@@ -93,10 +90,14 @@ async function renderGeneratorSchema(generator, node, opts) {
|
|
|
93
90
|
if (!generator.schema) return;
|
|
94
91
|
const context = createMockedPluginContext(opts);
|
|
95
92
|
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node;
|
|
96
|
-
await applyHookResult(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
await applyHookResult({
|
|
94
|
+
result: await generator.schema(transformedNode, {
|
|
95
|
+
...context,
|
|
96
|
+
options: opts.options
|
|
97
|
+
}),
|
|
98
|
+
driver: opts.driver,
|
|
99
|
+
rendererFactory: generator.renderer
|
|
100
|
+
});
|
|
100
101
|
}
|
|
101
102
|
/**
|
|
102
103
|
* Renders a generator's `operation` method in a test context.
|
|
@@ -111,10 +112,14 @@ async function renderGeneratorOperation(generator, node, opts) {
|
|
|
111
112
|
if (!generator.operation) return;
|
|
112
113
|
const context = createMockedPluginContext(opts);
|
|
113
114
|
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node;
|
|
114
|
-
await applyHookResult(
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
await applyHookResult({
|
|
116
|
+
result: await generator.operation(transformedNode, {
|
|
117
|
+
...context,
|
|
118
|
+
options: opts.options
|
|
119
|
+
}),
|
|
120
|
+
driver: opts.driver,
|
|
121
|
+
rendererFactory: generator.renderer
|
|
122
|
+
});
|
|
118
123
|
}
|
|
119
124
|
/**
|
|
120
125
|
* Renders a generator's `operations` method in a test context.
|
|
@@ -129,10 +134,14 @@ async function renderGeneratorOperations(generator, nodes, opts) {
|
|
|
129
134
|
if (!generator.operations) return;
|
|
130
135
|
const context = createMockedPluginContext(opts);
|
|
131
136
|
const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer)) : nodes;
|
|
132
|
-
await applyHookResult(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
137
|
+
await applyHookResult({
|
|
138
|
+
result: await generator.operations(transformedNodes, {
|
|
139
|
+
...context,
|
|
140
|
+
options: opts.options
|
|
141
|
+
}),
|
|
142
|
+
driver: opts.driver,
|
|
143
|
+
rendererFactory: generator.renderer
|
|
144
|
+
});
|
|
136
145
|
}
|
|
137
146
|
//#endregion
|
|
138
147
|
export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
|
package/dist/mocks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mocks.js","names":[],"sources":["../src/mocks.ts"],"sourcesContent":["import { resolve } from 'node:path'\nimport type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'\nimport { transform } from '@kubb/ast'\nimport { FileManager } from './FileManager.ts'\nimport {
|
|
1
|
+
{"version":3,"file":"mocks.js","names":[],"sources":["../src/mocks.ts"],"sourcesContent":["import { resolve } from 'node:path'\nimport type { FileNode, InputMeta, OperationNode, SchemaNode, Visitor } from '@kubb/ast'\nimport { transform } from '@kubb/ast'\nimport { FileManager } from './FileManager.ts'\nimport { applyHookResult, KubbDriver } from './KubbDriver.ts'\nimport type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'\n\n/**\n\n * Creates a minimal `PluginDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): KubbDriver {\n return {\n config: options?.config ?? {\n root: '.',\n output: {\n path: './path',\n },\n },\n getPlugin(_pluginName: string): NormalizedPlugin | undefined {\n return options?.plugin\n },\n getResolver: (_pluginName: string) => options?.plugin?.resolver,\n fileManager: new FileManager(),\n } as unknown as KubbDriver\n}\n\n/**\n * Creates a minimal `Adapter` mock for unit tests.\n * `parse` returns an empty `InputNode` by default; override via `options.parse`.\n * `getImports` returns `[]` by default.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),\n getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),\n } as Adapter<TOptions>\n}\n\n/**\n * Creates a minimal plugin mock for unit tests.\n *\n * @example\n * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`\n */\nexport function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n transformer?: Visitor\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n transformer: params.transformer,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {\n config: Config\n adapter: Adapter\n meta?: InputMeta\n driver: KubbDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {\n const root = resolve(opts.config.root, opts.config.output.path)\n\n return {\n config: opts.config,\n root,\n getMode: (output: { path: string }) => KubbDriver.getMode(resolve(root, output.path)),\n adapter: opts.adapter,\n resolver: opts.resolver,\n plugin: opts.plugin,\n driver: opts.driver,\n getResolver: (name: string) => opts.driver.getResolver(name),\n meta: opts.meta ?? { circularNames: [], enumNames: [] },\n addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),\n upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),\n hooks: opts.driver.hooks ?? ({} as never),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n openInStudio: async () => {},\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\n}\n\n/**\n * Renders a generator's `schema` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult({ result, driver: opts.driver, rendererFactory: generator.renderer })\n}\n\n/**\n * Renders a generator's `operation` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult({ result, driver: opts.driver, rendererFactory: generator.renderer })\n}\n\n/**\n * Renders a generator's `operations` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(\n generator: Generator<TOptions>,\n nodes: Array<OperationNode>,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operations) return\n const context = createMockedPluginContext(opts)\n const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await applyHookResult({ result, driver: opts.driver, rendererFactory: generator.renderer })\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
4
|
-
"description": "Core
|
|
3
|
+
"version": "5.0.0-beta.21",
|
|
4
|
+
"description": "Core engine for Kubb's plugin-based code generation system. Provides the plugin driver, file manager, defineConfig, and build orchestration used by every Kubb plugin.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"ast",
|
|
7
6
|
"code-generator",
|
|
8
7
|
"codegen",
|
|
9
|
-
"core-library",
|
|
10
|
-
"file-system",
|
|
11
8
|
"kubb",
|
|
12
|
-
"oas",
|
|
13
9
|
"openapi",
|
|
14
|
-
"plugin-framework",
|
|
15
10
|
"plugin-system",
|
|
16
|
-
"plugins",
|
|
17
|
-
"swagger",
|
|
18
11
|
"typescript"
|
|
19
12
|
],
|
|
20
13
|
"license": "MIT",
|
|
@@ -63,17 +56,16 @@
|
|
|
63
56
|
"registry": "https://registry.npmjs.org/"
|
|
64
57
|
},
|
|
65
58
|
"dependencies": {
|
|
66
|
-
"fflate": "^0.8.
|
|
59
|
+
"fflate": "^0.8.3",
|
|
67
60
|
"tinyexec": "^1.1.2",
|
|
68
|
-
"@kubb/ast": "5.0.0-beta.
|
|
61
|
+
"@kubb/ast": "5.0.0-beta.21"
|
|
69
62
|
},
|
|
70
63
|
"devDependencies": {
|
|
71
|
-
"p-limit": "^7.3.0",
|
|
72
64
|
"@internals/utils": "0.0.0",
|
|
73
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
65
|
+
"@kubb/renderer-jsx": "5.0.0-beta.21"
|
|
74
66
|
},
|
|
75
67
|
"peerDependencies": {
|
|
76
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
68
|
+
"@kubb/renderer-jsx": "5.0.0-beta.21"
|
|
77
69
|
},
|
|
78
70
|
"size-limit": [
|
|
79
71
|
{
|
|
@@ -85,10 +77,6 @@
|
|
|
85
77
|
"engines": {
|
|
86
78
|
"node": ">=22"
|
|
87
79
|
},
|
|
88
|
-
"inlinedDependencies": {
|
|
89
|
-
"p-limit": "7.3.0",
|
|
90
|
-
"yocto-queue": "1.2.2"
|
|
91
|
-
},
|
|
92
80
|
"scripts": {
|
|
93
81
|
"build": "tsdown && size-limit",
|
|
94
82
|
"clean": "npx rimraf ./dist",
|
package/src/FileManager.ts
CHANGED
|
@@ -9,72 +9,91 @@ function mergeFile<TMeta extends object = object>(a: FileNode<TMeta>, b: FileNod
|
|
|
9
9
|
// at the same path.
|
|
10
10
|
banner: b.banner,
|
|
11
11
|
footer: b.footer,
|
|
12
|
-
sources:
|
|
13
|
-
imports:
|
|
14
|
-
exports:
|
|
12
|
+
sources: a.sources.length ? (b.sources.length ? [...a.sources, ...b.sources] : a.sources) : b.sources,
|
|
13
|
+
imports: a.imports.length ? (b.imports.length ? [...a.imports, ...b.imports] : a.imports) : b.imports,
|
|
14
|
+
exports: a.exports.length ? (b.exports.length ? [...a.exports, ...b.exports] : a.exports) : b.exports,
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
18
|
+
function isIndexPath(path: string): boolean {
|
|
19
|
+
return path.endsWith('/index.ts') || path === 'index.ts'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Sort order: shortest path first; within a length bucket, index.ts barrels last.
|
|
23
|
+
function compareFiles(a: FileNode, b: FileNode): number {
|
|
24
|
+
const lenDiff = a.path.length - b.path.length
|
|
25
|
+
if (lenDiff !== 0) return lenDiff
|
|
26
|
+
const aIsIndex = isIndexPath(a.path)
|
|
27
|
+
const bIsIndex = isIndexPath(b.path)
|
|
28
|
+
if (aIsIndex && !bIsIndex) return 1
|
|
29
|
+
if (!aIsIndex && bIsIndex) return -1
|
|
30
|
+
return 0
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/**
|
|
32
|
-
* In-memory file store for generated files.
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* The `files` getter returns all stored files sorted by path length (shortest first).
|
|
34
|
+
* In-memory file store for generated files. Files sharing a `path` are merged
|
|
35
|
+
* (sources/imports/exports concatenated). The `files` getter is sorted by
|
|
36
|
+
* path length (barrel `index.ts` last within a bucket).
|
|
36
37
|
*
|
|
37
38
|
* @example
|
|
38
39
|
* ```ts
|
|
39
|
-
* import { FileManager } from '@kubb/core'
|
|
40
|
-
*
|
|
41
40
|
* const manager = new FileManager()
|
|
42
41
|
* manager.upsert(myFile)
|
|
43
|
-
*
|
|
42
|
+
* manager.files // sorted view
|
|
44
43
|
* ```
|
|
45
44
|
*/
|
|
46
45
|
export class FileManager {
|
|
47
46
|
readonly #cache = new Map<string, FileNode>()
|
|
48
|
-
|
|
47
|
+
// Cached sorted view; null means stale and rebuilt lazily on next `files` read.
|
|
48
|
+
// Nulled (not mutated) on every write so callers holding a prior reference
|
|
49
|
+
// keep their snapshot — `dispose()` must not silently empty an array the
|
|
50
|
+
// consumer already holds.
|
|
51
|
+
#sorted: Array<FileNode> | null = null
|
|
52
|
+
#onUpsert: ((file: FileNode) => void) | null = null
|
|
49
53
|
|
|
50
54
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
55
|
+
* Registers a callback invoked with the resolved {@link FileNode} on every
|
|
56
|
+
* `add` / `upsert`. Used by the build loop to track newly written files
|
|
57
|
+
* without keeping its own scan-based diff. Single subscriber by design —
|
|
58
|
+
* setting again replaces the previous callback. Pass `null` to detach.
|
|
54
59
|
*/
|
|
60
|
+
setOnUpsert(callback: ((file: FileNode) => void) | null): void {
|
|
61
|
+
this.#onUpsert = callback
|
|
62
|
+
}
|
|
63
|
+
|
|
55
64
|
add(...files: Array<FileNode>): Array<FileNode> {
|
|
56
65
|
return this.#store(files, false)
|
|
57
66
|
}
|
|
58
67
|
|
|
59
|
-
/**
|
|
60
|
-
* Adds or merges one or more files.
|
|
61
|
-
* If a file with the same path already exists in the cache, its
|
|
62
|
-
* sources/imports/exports are merged into the incoming file.
|
|
63
|
-
*/
|
|
64
68
|
upsert(...files: Array<FileNode>): Array<FileNode> {
|
|
65
69
|
return this.#store(files, true)
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
#store(files: ReadonlyArray<FileNode>, mergeExisting: boolean): Array<FileNode> {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.#cache.
|
|
74
|
-
|
|
73
|
+
const batch = files.length > 1 ? this.#dedupe(files) : files
|
|
74
|
+
const resolved: Array<FileNode> = []
|
|
75
|
+
|
|
76
|
+
for (const file of batch) {
|
|
77
|
+
const existing = this.#cache.get(file.path)
|
|
78
|
+
const merged = existing && mergeExisting ? createFile(mergeFile(existing, file)) : file
|
|
79
|
+
this.#cache.set(merged.path, merged)
|
|
80
|
+
resolved.push(merged)
|
|
81
|
+
this.#onUpsert?.(merged)
|
|
75
82
|
}
|
|
76
|
-
|
|
77
|
-
|
|
83
|
+
|
|
84
|
+
if (resolved.length > 0) this.#sorted = null
|
|
85
|
+
return resolved
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Merges same-path entries within a batch so the cache update loop stays
|
|
89
|
+
// uniform. Only called for multi-file batches.
|
|
90
|
+
#dedupe(files: ReadonlyArray<FileNode>): Array<FileNode> {
|
|
91
|
+
const seen = new Map<string, FileNode>()
|
|
92
|
+
for (const file of files) {
|
|
93
|
+
const prev = seen.get(file.path)
|
|
94
|
+
seen.set(file.path, prev ? mergeFile(prev, file) : file)
|
|
95
|
+
}
|
|
96
|
+
return [...seen.values()]
|
|
78
97
|
}
|
|
79
98
|
|
|
80
99
|
getByPath(path: string): FileNode | null {
|
|
@@ -82,34 +101,32 @@ export class FileManager {
|
|
|
82
101
|
}
|
|
83
102
|
|
|
84
103
|
deleteByPath(path: string): void {
|
|
85
|
-
this.#cache.delete(path)
|
|
86
|
-
this.#
|
|
104
|
+
if (!this.#cache.delete(path)) return
|
|
105
|
+
this.#sorted = null
|
|
87
106
|
}
|
|
88
107
|
|
|
89
108
|
clear(): void {
|
|
90
109
|
this.#cache.clear()
|
|
91
|
-
this.#
|
|
110
|
+
this.#sorted = null
|
|
92
111
|
}
|
|
93
112
|
|
|
94
113
|
/**
|
|
95
|
-
*
|
|
114
|
+
* Releases all stored files. Called by the core after `kubb:build:end`.
|
|
96
115
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
116
|
+
dispose(): void {
|
|
117
|
+
this.clear()
|
|
118
|
+
this.#onUpsert = null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
[Symbol.dispose](): void {
|
|
122
|
+
this.dispose()
|
|
123
|
+
}
|
|
101
124
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const bIsIndex = b.path.endsWith('/index.ts') || b.path === 'index.ts'
|
|
109
|
-
if (aIsIndex && !bIsIndex) return 1
|
|
110
|
-
if (!aIsIndex && bIsIndex) return -1
|
|
111
|
-
return 0
|
|
112
|
-
})
|
|
113
|
-
return this.#filesCache
|
|
125
|
+
/**
|
|
126
|
+
* All stored files in stable sort order (shortest path first, barrel files
|
|
127
|
+
* last within a length bucket). Returns a cached view — do not mutate.
|
|
128
|
+
*/
|
|
129
|
+
get files(): Array<FileNode> {
|
|
130
|
+
return (this.#sorted ??= [...this.#cache.values()].sort(compareFiles))
|
|
114
131
|
}
|
|
115
132
|
}
|
package/src/FileProcessor.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { CodeNode, FileNode } from '@kubb/ast'
|
|
2
2
|
import { extractStringsFromNodes } from '@kubb/ast'
|
|
3
|
-
import
|
|
4
|
-
import { PARALLEL_CONCURRENCY_LIMIT } from './constants.ts'
|
|
3
|
+
import { AsyncEventEmitter } from '@internals/utils'
|
|
5
4
|
import type { Parser } from './defineParser.ts'
|
|
6
5
|
|
|
7
6
|
type ParseOptions = {
|
|
@@ -9,21 +8,29 @@ type ParseOptions = {
|
|
|
9
8
|
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
|
|
10
9
|
}
|
|
11
10
|
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
export type FileProcessorEvents = {
|
|
12
|
+
start: [files: Array<FileNode>]
|
|
13
|
+
update: [params: { file: FileNode; source?: string; processed: number; total: number; percentage: number }]
|
|
14
|
+
end: [files: Array<FileNode>]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type ParsedFile = {
|
|
18
|
+
file: FileNode
|
|
19
|
+
source: string
|
|
20
|
+
processed: number
|
|
21
|
+
total: number
|
|
22
|
+
percentage: number
|
|
20
23
|
}
|
|
21
24
|
|
|
22
25
|
function joinSources(file: FileNode): string {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
const sources = file.sources
|
|
27
|
+
if (sources.length === 0) return ''
|
|
28
|
+
const parts: string[] = []
|
|
29
|
+
for (const source of sources) {
|
|
30
|
+
const s = extractStringsFromNodes(source.nodes as Array<CodeNode>)
|
|
31
|
+
if (s) parts.push(s)
|
|
32
|
+
}
|
|
33
|
+
return parts.join('\n\n')
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
/**
|
|
@@ -33,9 +40,9 @@ function joinSources(file: FileNode): string {
|
|
|
33
40
|
* @internal
|
|
34
41
|
*/
|
|
35
42
|
export class FileProcessor {
|
|
36
|
-
readonly
|
|
43
|
+
readonly events = new AsyncEventEmitter<FileProcessorEvents>()
|
|
37
44
|
|
|
38
|
-
|
|
45
|
+
parse(file: FileNode, { parsers, extension }: ParseOptions = {}): string {
|
|
39
46
|
const parseExtName = extension?.[file.extname] || undefined
|
|
40
47
|
|
|
41
48
|
if (!parsers || !file.extname) {
|
|
@@ -51,36 +58,39 @@ export class FileProcessor {
|
|
|
51
58
|
return parser.parse(file, { extname: parseExtName })
|
|
52
59
|
}
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
await onStart?.(files)
|
|
56
|
-
|
|
61
|
+
*stream(files: ReadonlyArray<FileNode>, options: ParseOptions = {}): Generator<ParsedFile> {
|
|
57
62
|
const total = files.length
|
|
63
|
+
if (total === 0) return
|
|
64
|
+
|
|
58
65
|
let processed = 0
|
|
66
|
+
for (const file of files) {
|
|
67
|
+
const source = this.parse(file, options)
|
|
68
|
+
processed++
|
|
59
69
|
|
|
60
|
-
|
|
61
|
-
const source = await this.parse(file, { extension, parsers })
|
|
62
|
-
const currentProcessed = ++processed
|
|
63
|
-
const percentage = (currentProcessed / total) * 100
|
|
64
|
-
|
|
65
|
-
await onUpdate?.({
|
|
66
|
-
file,
|
|
67
|
-
source,
|
|
68
|
-
processed: currentProcessed,
|
|
69
|
-
percentage,
|
|
70
|
-
total,
|
|
71
|
-
})
|
|
70
|
+
yield { file, source, processed, total, percentage: (processed / total) * 100 }
|
|
72
71
|
}
|
|
72
|
+
}
|
|
73
73
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await Promise.all(files.map((file) => this.#limit(() => processOne(file))))
|
|
74
|
+
async run(files: Array<FileNode>, options: ParseOptions = {}): Promise<Array<FileNode>> {
|
|
75
|
+
await this.events.emit('start', files)
|
|
76
|
+
|
|
77
|
+
for (const { file, source, processed, total, percentage } of this.stream(files, options)) {
|
|
78
|
+
await this.events.emit('update', { file, source, processed, percentage, total })
|
|
80
79
|
}
|
|
81
80
|
|
|
82
|
-
await
|
|
81
|
+
await this.events.emit('end', files)
|
|
83
82
|
|
|
84
83
|
return files
|
|
85
84
|
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Clears all registered event listeners.
|
|
88
|
+
*/
|
|
89
|
+
dispose(): void {
|
|
90
|
+
this.events.removeAll()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
[Symbol.dispose](): void {
|
|
94
|
+
this.dispose()
|
|
95
|
+
}
|
|
86
96
|
}
|