@kubb/core 5.0.0-beta.36 → 5.0.0-beta.37
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/{KubbDriver-Dil5m3NF.cjs → KubbDriver-CXoKVRxI.cjs} +1090 -412
- package/dist/KubbDriver-CXoKVRxI.cjs.map +1 -0
- package/dist/{KubbDriver-DrG5-FFe.js → KubbDriver-CckeYpMG.js} +1049 -401
- package/dist/KubbDriver-CckeYpMG.js.map +1 -0
- package/dist/{createKubb-BKpcUB6g.d.ts → diagnostics-B0ONXReg.d.ts} +790 -287
- package/dist/index.cjs +48 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +40 -90
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +23 -23
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +2 -2
- package/dist/mocks.js +23 -23
- package/dist/mocks.js.map +1 -1
- package/package.json +4 -4
- package/src/FileManager.ts +23 -18
- package/src/FileProcessor.ts +142 -24
- package/src/HookRegistry.ts +45 -0
- package/src/KubbDriver.ts +333 -319
- package/src/Transform.ts +58 -0
- package/src/constants.ts +105 -11
- package/src/createKubb.ts +95 -201
- package/src/createRenderer.ts +2 -2
- package/src/createReporter.ts +84 -0
- package/src/createStorage.ts +1 -1
- package/src/defineGenerator.ts +11 -7
- package/src/defineParser.ts +1 -1
- package/src/definePlugin.ts +10 -22
- package/src/defineResolver.ts +29 -24
- package/src/devtools.ts +2 -3
- package/src/diagnostics.ts +591 -0
- package/src/index.ts +11 -1
- package/src/mocks.ts +5 -5
- package/src/types.ts +16 -4
- package/dist/KubbDriver-Dil5m3NF.cjs.map +0 -1
- package/dist/KubbDriver-DrG5-FFe.js.map +0 -1
package/dist/mocks.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_KubbDriver = require("./KubbDriver-
|
|
2
|
+
const require_KubbDriver = require("./KubbDriver-CXoKVRxI.cjs");
|
|
3
3
|
let node_path = require("node:path");
|
|
4
4
|
let _kubb_ast = require("@kubb/ast");
|
|
5
5
|
//#region src/mocks.ts
|
|
@@ -22,7 +22,7 @@ function createMockedPluginDriver(options = {}) {
|
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Creates a minimal `Adapter` mock for unit tests.
|
|
25
|
-
* `parse` returns an empty `InputNode` by default
|
|
25
|
+
* `parse` returns an empty `InputNode` by default. Override via `options.parse`.
|
|
26
26
|
* `getImports` returns `[]` by default.
|
|
27
27
|
*/
|
|
28
28
|
function createMockedAdapter(options = {}) {
|
|
@@ -90,13 +90,13 @@ async function renderGeneratorSchema(generator, node, opts) {
|
|
|
90
90
|
if (!generator.schema) return;
|
|
91
91
|
const context = createMockedPluginContext(opts);
|
|
92
92
|
const transformedNode = opts.plugin.transformer ? (0, _kubb_ast.transform)(node, opts.plugin.transformer) : node;
|
|
93
|
-
await
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
const result = await generator.schema(transformedNode, {
|
|
94
|
+
...context,
|
|
95
|
+
options: opts.options
|
|
96
|
+
});
|
|
97
|
+
await opts.driver.dispatch({
|
|
98
|
+
result,
|
|
99
|
+
renderer: generator.renderer
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
@@ -112,13 +112,13 @@ async function renderGeneratorOperation(generator, node, opts) {
|
|
|
112
112
|
if (!generator.operation) return;
|
|
113
113
|
const context = createMockedPluginContext(opts);
|
|
114
114
|
const transformedNode = opts.plugin.transformer ? (0, _kubb_ast.transform)(node, opts.plugin.transformer) : node;
|
|
115
|
-
await
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
115
|
+
const result = await generator.operation(transformedNode, {
|
|
116
|
+
...context,
|
|
117
|
+
options: opts.options
|
|
118
|
+
});
|
|
119
|
+
await opts.driver.dispatch({
|
|
120
|
+
result,
|
|
121
|
+
renderer: generator.renderer
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
@@ -134,13 +134,13 @@ async function renderGeneratorOperations(generator, nodes, opts) {
|
|
|
134
134
|
if (!generator.operations) return;
|
|
135
135
|
const context = createMockedPluginContext(opts);
|
|
136
136
|
const transformedNodes = opts.plugin.transformer ? nodes.map((n) => (0, _kubb_ast.transform)(n, opts.plugin.transformer)) : nodes;
|
|
137
|
-
await
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
137
|
+
const result = await generator.operations(transformedNodes, {
|
|
138
|
+
...context,
|
|
139
|
+
options: opts.options
|
|
140
|
+
});
|
|
141
|
+
await opts.driver.dispatch({
|
|
142
|
+
result,
|
|
143
|
+
renderer: generator.renderer
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
//#endregion
|
package/dist/mocks.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mocks.cjs","names":["FileManager","KubbDriver"
|
|
1
|
+
{"version":3,"file":"mocks.cjs","names":["FileManager","KubbDriver"],"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 { 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 opts.driver.dispatch({ result, renderer: 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 opts.driver.dispatch({ result, renderer: 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 opts.driver.dispatch({ result, renderer: generator.renderer })\n}\n"],"mappings":"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"}
|
package/dist/mocks.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./chunk-C0LytTxp.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Gt as AdapterFactoryOptions, J as Generator, Wt as Adapter, Z as KubbDriver, at as NormalizedPlugin, lt as PluginFactoryOptions, x as Config } from "./diagnostics-B0ONXReg.js";
|
|
3
3
|
import { InputMeta, OperationNode, SchemaNode, Visitor } from "@kubb/ast";
|
|
4
4
|
|
|
5
5
|
//#region src/mocks.d.ts
|
|
@@ -14,7 +14,7 @@ declare function createMockedPluginDriver(options?: {
|
|
|
14
14
|
}): KubbDriver;
|
|
15
15
|
/**
|
|
16
16
|
* Creates a minimal `Adapter` mock for unit tests.
|
|
17
|
-
* `parse` returns an empty `InputNode` by default
|
|
17
|
+
* `parse` returns an empty `InputNode` by default. Override via `options.parse`.
|
|
18
18
|
* `getImports` returns `[]` by default.
|
|
19
19
|
*/
|
|
20
20
|
declare function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(options?: {
|
package/dist/mocks.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./chunk-C0LytTxp.js";
|
|
2
|
-
import {
|
|
2
|
+
import { i as FileManager, t as KubbDriver } from "./KubbDriver-CckeYpMG.js";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { transform } from "@kubb/ast";
|
|
5
5
|
//#region src/mocks.ts
|
|
@@ -22,7 +22,7 @@ function createMockedPluginDriver(options = {}) {
|
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Creates a minimal `Adapter` mock for unit tests.
|
|
25
|
-
* `parse` returns an empty `InputNode` by default
|
|
25
|
+
* `parse` returns an empty `InputNode` by default. Override via `options.parse`.
|
|
26
26
|
* `getImports` returns `[]` by default.
|
|
27
27
|
*/
|
|
28
28
|
function createMockedAdapter(options = {}) {
|
|
@@ -90,13 +90,13 @@ async function renderGeneratorSchema(generator, node, opts) {
|
|
|
90
90
|
if (!generator.schema) return;
|
|
91
91
|
const context = createMockedPluginContext(opts);
|
|
92
92
|
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node;
|
|
93
|
-
await
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
const result = await generator.schema(transformedNode, {
|
|
94
|
+
...context,
|
|
95
|
+
options: opts.options
|
|
96
|
+
});
|
|
97
|
+
await opts.driver.dispatch({
|
|
98
|
+
result,
|
|
99
|
+
renderer: generator.renderer
|
|
100
100
|
});
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
@@ -112,13 +112,13 @@ async function renderGeneratorOperation(generator, node, opts) {
|
|
|
112
112
|
if (!generator.operation) return;
|
|
113
113
|
const context = createMockedPluginContext(opts);
|
|
114
114
|
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node;
|
|
115
|
-
await
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
115
|
+
const result = await generator.operation(transformedNode, {
|
|
116
|
+
...context,
|
|
117
|
+
options: opts.options
|
|
118
|
+
});
|
|
119
|
+
await opts.driver.dispatch({
|
|
120
|
+
result,
|
|
121
|
+
renderer: generator.renderer
|
|
122
122
|
});
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
@@ -134,13 +134,13 @@ async function renderGeneratorOperations(generator, nodes, opts) {
|
|
|
134
134
|
if (!generator.operations) return;
|
|
135
135
|
const context = createMockedPluginContext(opts);
|
|
136
136
|
const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer)) : nodes;
|
|
137
|
-
await
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
137
|
+
const result = await generator.operations(transformedNodes, {
|
|
138
|
+
...context,
|
|
139
|
+
options: opts.options
|
|
140
|
+
});
|
|
141
|
+
await opts.driver.dispatch({
|
|
142
|
+
result,
|
|
143
|
+
renderer: generator.renderer
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
//#endregion
|
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, InputMeta, 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 { 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 opts.driver.dispatch({ result, renderer: 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 opts.driver.dispatch({ result, renderer: 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 opts.driver.dispatch({ result, renderer: generator.renderer })\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.37",
|
|
4
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
6
|
"code-generator",
|
|
@@ -58,14 +58,14 @@
|
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"fflate": "^0.8.3",
|
|
60
60
|
"tinyexec": "~1.1.2",
|
|
61
|
-
"@kubb/ast": "5.0.0-beta.
|
|
61
|
+
"@kubb/ast": "5.0.0-beta.37"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@internals/utils": "0.0.0",
|
|
65
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
65
|
+
"@kubb/renderer-jsx": "5.0.0-beta.37"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
|
-
"@kubb/renderer-jsx": "5.0.0-beta.
|
|
68
|
+
"@kubb/renderer-jsx": "5.0.0-beta.37"
|
|
69
69
|
},
|
|
70
70
|
"size-limit": [
|
|
71
71
|
{
|
package/src/FileManager.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
import { AsyncEventEmitter } from '@internals/utils'
|
|
1
2
|
import type { FileNode } from '@kubb/ast'
|
|
2
3
|
import { createFile } from '@kubb/ast'
|
|
3
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Hooks fired by a `FileManager`.
|
|
7
|
+
*
|
|
8
|
+
* - `upsert` fires once per resolved file added through `add` or `upsert`.
|
|
9
|
+
*/
|
|
10
|
+
export type FileManagerHooks = {
|
|
11
|
+
upsert: [file: FileNode]
|
|
12
|
+
}
|
|
13
|
+
|
|
4
14
|
function mergeFile<TMeta extends object = object>(a: FileNode<TMeta>, b: FileNode<TMeta>): FileNode<TMeta> {
|
|
5
15
|
return {
|
|
6
16
|
...a,
|
|
@@ -19,7 +29,7 @@ function isIndexPath(path: string): boolean {
|
|
|
19
29
|
return path.endsWith('/index.ts') || path === 'index.ts'
|
|
20
30
|
}
|
|
21
31
|
|
|
22
|
-
// Sort order: shortest path first
|
|
32
|
+
// Sort order: shortest path first. Within a length bucket, index.ts barrels last.
|
|
23
33
|
function compareFiles(a: FileNode, b: FileNode): number {
|
|
24
34
|
const lenDiff = a.path.length - b.path.length
|
|
25
35
|
if (lenDiff !== 0) return lenDiff
|
|
@@ -43,23 +53,17 @@ function compareFiles(a: FileNode, b: FileNode): number {
|
|
|
43
53
|
* ```
|
|
44
54
|
*/
|
|
45
55
|
export class FileManager {
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to file-store changes. Listeners on `upsert` see each resolved file as it lands
|
|
58
|
+
* through `add` or `upsert`.
|
|
59
|
+
*/
|
|
60
|
+
readonly hooks = new AsyncEventEmitter<FileManagerHooks>()
|
|
46
61
|
readonly #cache = new Map<string, FileNode>()
|
|
47
|
-
// Cached sorted view
|
|
62
|
+
// Cached sorted view. Null means stale and rebuilt lazily on next `files` read.
|
|
48
63
|
// Nulled (not mutated) on every write so callers holding a prior reference
|
|
49
|
-
// keep their snapshot
|
|
64
|
+
// keep their snapshot, `dispose()` must not silently empty an array the
|
|
50
65
|
// consumer already holds.
|
|
51
66
|
#sorted: Array<FileNode> | null = null
|
|
52
|
-
#onUpsert: ((file: FileNode) => void) | null = null
|
|
53
|
-
|
|
54
|
-
/**
|
|
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.
|
|
59
|
-
*/
|
|
60
|
-
setOnUpsert(callback: ((file: FileNode) => void) | null): void {
|
|
61
|
-
this.#onUpsert = callback
|
|
62
|
-
}
|
|
63
67
|
|
|
64
68
|
add(...files: Array<FileNode>): Array<FileNode> {
|
|
65
69
|
return this.#store(files, false)
|
|
@@ -78,7 +82,7 @@ export class FileManager {
|
|
|
78
82
|
const merged = existing && mergeExisting ? createFile(mergeFile(existing, file)) : createFile(file)
|
|
79
83
|
this.#cache.set(merged.path, merged)
|
|
80
84
|
resolved.push(merged)
|
|
81
|
-
this
|
|
85
|
+
this.hooks.emit('upsert', merged)
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
if (resolved.length > 0) this.#sorted = null
|
|
@@ -111,11 +115,12 @@ export class FileManager {
|
|
|
111
115
|
}
|
|
112
116
|
|
|
113
117
|
/**
|
|
114
|
-
* Releases all stored files. Called by the core after
|
|
118
|
+
* Releases all stored files and clears every `hooks` listener. Called by the core after
|
|
119
|
+
* `kubb:build:end`.
|
|
115
120
|
*/
|
|
116
121
|
dispose(): void {
|
|
117
122
|
this.clear()
|
|
118
|
-
this
|
|
123
|
+
this.hooks.removeAll()
|
|
119
124
|
}
|
|
120
125
|
|
|
121
126
|
[Symbol.dispose](): void {
|
|
@@ -124,7 +129,7 @@ export class FileManager {
|
|
|
124
129
|
|
|
125
130
|
/**
|
|
126
131
|
* All stored files in stable sort order (shortest path first, barrel files
|
|
127
|
-
* last within a length bucket). Returns a cached view
|
|
132
|
+
* last within a length bucket). Returns a cached view, do not mutate.
|
|
128
133
|
*/
|
|
129
134
|
get files(): Array<FileNode> {
|
|
130
135
|
return (this.#sorted ??= [...this.#cache.values()].sort(compareFiles))
|
package/src/FileProcessor.ts
CHANGED
|
@@ -1,19 +1,30 @@
|
|
|
1
|
+
import { AsyncEventEmitter } from '@internals/utils'
|
|
1
2
|
import type { CodeNode, FileNode } from '@kubb/ast'
|
|
2
3
|
import { extractStringsFromNodes } from '@kubb/ast'
|
|
3
|
-
import {
|
|
4
|
+
import { STREAM_FLUSH_EVERY } from './constants.ts'
|
|
5
|
+
import type { Storage } from './createStorage.ts'
|
|
4
6
|
import type { Parser } from './defineParser.ts'
|
|
5
7
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Hooks fired by a `FileProcessor`.
|
|
10
|
+
*
|
|
11
|
+
* - `start` opens a batch, from `run` or a queue flush.
|
|
12
|
+
* - `update` fires once per file as it is converted.
|
|
13
|
+
* - `end` closes a batch.
|
|
14
|
+
* - `enqueue` fires for every `enqueue` call.
|
|
15
|
+
* - `drain` fires when `drain()` empties the queue with no in-flight batch left.
|
|
16
|
+
*/
|
|
17
|
+
export type FileProcessorHooks = {
|
|
12
18
|
start: [files: Array<FileNode>]
|
|
13
19
|
update: [params: { file: FileNode; source?: string; processed: number; total: number; percentage: number }]
|
|
14
20
|
end: [files: Array<FileNode>]
|
|
21
|
+
enqueue: [file: FileNode]
|
|
22
|
+
drain: []
|
|
15
23
|
}
|
|
16
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Per-file progress record yielded by `stream` and surfaced through the `update` event.
|
|
27
|
+
*/
|
|
17
28
|
export type ParsedFile = {
|
|
18
29
|
file: FileNode
|
|
19
30
|
source: string
|
|
@@ -22,28 +33,70 @@ export type ParsedFile = {
|
|
|
22
33
|
percentage: number
|
|
23
34
|
}
|
|
24
35
|
|
|
36
|
+
type FileProcessorOptions = {
|
|
37
|
+
/**
|
|
38
|
+
* Storage destination for queued writes.
|
|
39
|
+
*/
|
|
40
|
+
storage: Storage
|
|
41
|
+
/**
|
|
42
|
+
* Parsers indexed by file extension.
|
|
43
|
+
*/
|
|
44
|
+
parsers?: Map<FileNode['extname'], Parser>
|
|
45
|
+
/**
|
|
46
|
+
* Output extname per source extname, applied during conversion.
|
|
47
|
+
*/
|
|
48
|
+
extension?: Record<FileNode['extname'], FileNode['extname'] | ''>
|
|
49
|
+
}
|
|
50
|
+
|
|
25
51
|
function joinSources(file: FileNode): string {
|
|
26
52
|
const sources = file.sources
|
|
27
53
|
if (sources.length === 0) return ''
|
|
28
54
|
const parts: Array<string> = []
|
|
29
55
|
for (const source of sources) {
|
|
30
|
-
const
|
|
31
|
-
if (
|
|
56
|
+
const text = extractStringsFromNodes(source.nodes as Array<CodeNode>)
|
|
57
|
+
if (text) parts.push(text)
|
|
32
58
|
}
|
|
33
59
|
return parts.join('\n\n')
|
|
34
60
|
}
|
|
35
61
|
|
|
36
62
|
/**
|
|
37
|
-
*
|
|
38
|
-
* Falls back to joining source values when no matching parser is found.
|
|
63
|
+
* Turns `FileNode`s into source strings and writes them to storage.
|
|
39
64
|
*
|
|
40
|
-
*
|
|
65
|
+
* Two modes share the same instance. Stateless mode (`parse`, `stream`, `run`) just runs the
|
|
66
|
+
* conversion. Queue mode (`enqueue`, `flush`, `drain`) buffers files deduped by path and
|
|
67
|
+
* writes each batch through storage with up to `STREAM_FLUSH_EVERY` requests in flight.
|
|
68
|
+
*
|
|
69
|
+
* `flush` does not wait for its batch to finish, so dispatch can overlap with IO. The next
|
|
70
|
+
* `flush` or `drain` picks the in-flight batch up. `drain` blocks until everything has been
|
|
71
|
+
* written and is meant for the end of a build.
|
|
72
|
+
*
|
|
73
|
+
* To surface build-level hook signals (`kubb:files:processing:*` and friends) subscribe to
|
|
74
|
+
* `hooks` and re-emit on the kubb bus.
|
|
41
75
|
*/
|
|
42
76
|
export class FileProcessor {
|
|
43
|
-
readonly
|
|
77
|
+
readonly hooks = new AsyncEventEmitter<FileProcessorHooks>()
|
|
78
|
+
readonly #parsers: Map<FileNode['extname'], Parser> | null
|
|
79
|
+
readonly #storage: Storage
|
|
80
|
+
readonly #extension: Record<FileNode['extname'], FileNode['extname'] | ''> | null
|
|
81
|
+
readonly #pending = new Map<string, FileNode>()
|
|
82
|
+
#runningFlush: Promise<void> | null = null
|
|
83
|
+
|
|
84
|
+
constructor(options: FileProcessorOptions) {
|
|
85
|
+
this.#parsers = options.parsers ?? null
|
|
86
|
+
this.#storage = options.storage
|
|
87
|
+
this.#extension = options.extension ?? null
|
|
88
|
+
}
|
|
44
89
|
|
|
45
|
-
|
|
46
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Files waiting in the queue.
|
|
92
|
+
*/
|
|
93
|
+
get size(): number {
|
|
94
|
+
return this.#pending.size
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
parse(file: FileNode): string {
|
|
98
|
+
const parsers = this.#parsers
|
|
99
|
+
const parseExtName = this.#extension?.[file.extname] || undefined
|
|
47
100
|
|
|
48
101
|
if (!parsers || !file.extname) {
|
|
49
102
|
return joinSources(file)
|
|
@@ -58,36 +111,101 @@ export class FileProcessor {
|
|
|
58
111
|
return parser.parse(file, { extname: parseExtName })
|
|
59
112
|
}
|
|
60
113
|
|
|
61
|
-
*stream(files: ReadonlyArray<FileNode
|
|
114
|
+
*stream(files: ReadonlyArray<FileNode>): Generator<ParsedFile> {
|
|
62
115
|
const total = files.length
|
|
63
116
|
if (total === 0) return
|
|
64
117
|
|
|
65
118
|
let processed = 0
|
|
66
119
|
for (const file of files) {
|
|
67
|
-
const source = this.parse(file
|
|
120
|
+
const source = this.parse(file)
|
|
68
121
|
processed++
|
|
69
122
|
|
|
70
123
|
yield { file, source, processed, total, percentage: (processed / total) * 100 }
|
|
71
124
|
}
|
|
72
125
|
}
|
|
73
126
|
|
|
74
|
-
async run(files: Array<FileNode
|
|
75
|
-
await this.
|
|
127
|
+
async run(files: Array<FileNode>): Promise<Array<FileNode>> {
|
|
128
|
+
await this.hooks.emit('start', files)
|
|
76
129
|
|
|
77
|
-
for (const { file, source, processed, total, percentage } of this.stream(files
|
|
78
|
-
await this.
|
|
130
|
+
for (const { file, source, processed, total, percentage } of this.stream(files)) {
|
|
131
|
+
await this.hooks.emit('update', { file, source, processed, percentage, total })
|
|
79
132
|
}
|
|
80
133
|
|
|
81
|
-
await this.
|
|
134
|
+
await this.hooks.emit('end', files)
|
|
82
135
|
|
|
83
136
|
return files
|
|
84
137
|
}
|
|
85
138
|
|
|
86
139
|
/**
|
|
87
|
-
*
|
|
140
|
+
* Adds a file to the next flush. A later `enqueue` for the same path replaces the previous
|
|
141
|
+
* entry, matching `FileManager.upsert`. Fires the `enqueue` event.
|
|
142
|
+
*/
|
|
143
|
+
enqueue(file: FileNode): void {
|
|
144
|
+
this.#pending.set(file.path, file)
|
|
145
|
+
this.hooks.emit('enqueue', file)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Starts processing the queued files. Waits for any previous flush to finish (so two
|
|
150
|
+
* batches never run together) and then returns without waiting for the new one. The next
|
|
151
|
+
* `flush` or `drain` picks up the in-flight task.
|
|
152
|
+
*/
|
|
153
|
+
async flush(): Promise<void> {
|
|
154
|
+
if (this.#runningFlush) await this.#runningFlush
|
|
155
|
+
if (this.#pending.size === 0) return
|
|
156
|
+
|
|
157
|
+
const batch = [...this.#pending.values()]
|
|
158
|
+
this.#pending.clear()
|
|
159
|
+
|
|
160
|
+
this.#runningFlush = this.#processAndWrite(batch).finally(() => {
|
|
161
|
+
this.#runningFlush = null
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Waits for the in-flight flush and writes any files still queued. Fires the `drain` event
|
|
167
|
+
* when both are done.
|
|
168
|
+
*/
|
|
169
|
+
async drain(): Promise<void> {
|
|
170
|
+
if (this.#runningFlush) await this.#runningFlush
|
|
171
|
+
|
|
172
|
+
if (this.#pending.size > 0) {
|
|
173
|
+
const batch = [...this.#pending.values()]
|
|
174
|
+
this.#pending.clear()
|
|
175
|
+
await this.#processAndWrite(batch)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
await this.hooks.emit('drain')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async #processAndWrite(files: Array<FileNode>): Promise<void> {
|
|
182
|
+
const storage = this.#storage
|
|
183
|
+
|
|
184
|
+
await this.hooks.emit('start', files)
|
|
185
|
+
|
|
186
|
+
const items = [...this.stream(files)]
|
|
187
|
+
for (const item of items) {
|
|
188
|
+
await this.hooks.emit('update', item)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const queue: Array<Promise<void>> = []
|
|
192
|
+
for (const { file, source } of items) {
|
|
193
|
+
if (source) {
|
|
194
|
+
queue.push(storage.setItem(file.path, source))
|
|
195
|
+
if (queue.length >= STREAM_FLUSH_EVERY) await Promise.all(queue.splice(0))
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
await Promise.all(queue)
|
|
199
|
+
|
|
200
|
+
await this.hooks.emit('end', files)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Clears every listener and the pending queue.
|
|
88
205
|
*/
|
|
89
206
|
dispose(): void {
|
|
90
|
-
this.
|
|
207
|
+
this.hooks.removeAll()
|
|
208
|
+
this.#pending.clear()
|
|
91
209
|
}
|
|
92
210
|
|
|
93
211
|
[Symbol.dispose](): void {
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { AsyncEventEmitter } from '@internals/utils'
|
|
2
|
+
|
|
3
|
+
export type HookSource = 'plugin' | 'middleware' | 'driver'
|
|
4
|
+
|
|
5
|
+
export type HookListener<TArgs extends Array<unknown>, TResult = void> = (...args: TArgs) => TResult | Promise<TResult>
|
|
6
|
+
|
|
7
|
+
type AnyEntry = {
|
|
8
|
+
event: string
|
|
9
|
+
handler: HookListener<Array<unknown>, unknown>
|
|
10
|
+
source: HookSource
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Listener bookkeeping around an `AsyncEventEmitter`. Listeners attached through `register`
|
|
15
|
+
* stay on the emitter but are tracked so `dispose()` removes only them, listeners attached
|
|
16
|
+
* directly via `emitter.on(...)` survive.
|
|
17
|
+
*/
|
|
18
|
+
export class HookRegistry<TEvents extends { [K in keyof TEvents]: Array<unknown> }> {
|
|
19
|
+
readonly #emitter: AsyncEventEmitter<TEvents>
|
|
20
|
+
readonly #entries = new Set<AnyEntry>()
|
|
21
|
+
|
|
22
|
+
constructor(options: { emitter: AsyncEventEmitter<TEvents> }) {
|
|
23
|
+
this.#emitter = options.emitter
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get emitter(): AsyncEventEmitter<TEvents> {
|
|
27
|
+
return this.#emitter
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get size(): number {
|
|
31
|
+
return this.#entries.size
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
register<K extends keyof TEvents & string>(options: { event: K; handler: HookListener<TEvents[K], unknown>; source: HookSource }): void {
|
|
35
|
+
this.#emitter.on(options.event, options.handler as HookListener<TEvents[K]>)
|
|
36
|
+
this.#entries.add(options as unknown as AnyEntry)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
dispose(): void {
|
|
40
|
+
for (const entry of this.#entries) {
|
|
41
|
+
this.#emitter.off(entry.event as keyof TEvents & string, entry.handler as HookListener<Array<unknown>>)
|
|
42
|
+
}
|
|
43
|
+
this.#entries.clear()
|
|
44
|
+
}
|
|
45
|
+
}
|