@kubb/core 5.0.0-alpha.9 → 5.0.0-beta.10

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.
Files changed (62) hide show
  1. package/README.md +13 -40
  2. package/dist/PluginDriver-Cu1Kj9S-.cjs +1075 -0
  3. package/dist/PluginDriver-Cu1Kj9S-.cjs.map +1 -0
  4. package/dist/PluginDriver-D8Z0Htid.js +978 -0
  5. package/dist/PluginDriver-D8Z0Htid.js.map +1 -0
  6. package/dist/createKubb-ALdb8lmq.d.ts +2082 -0
  7. package/dist/index.cjs +747 -1667
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +175 -269
  10. package/dist/index.js +734 -1638
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +145 -0
  13. package/dist/mocks.cjs.map +1 -0
  14. package/dist/mocks.d.ts +80 -0
  15. package/dist/mocks.js +140 -0
  16. package/dist/mocks.js.map +1 -0
  17. package/package.json +47 -60
  18. package/src/FileManager.ts +115 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/PluginDriver.ts +355 -561
  21. package/src/constants.ts +21 -48
  22. package/src/createAdapter.ts +88 -5
  23. package/src/createKubb.ts +1266 -0
  24. package/src/createRenderer.ts +57 -0
  25. package/src/createStorage.ts +13 -1
  26. package/src/defineGenerator.ts +160 -119
  27. package/src/defineLogger.ts +46 -5
  28. package/src/defineMiddleware.ts +62 -0
  29. package/src/defineParser.ts +44 -0
  30. package/src/definePlugin.ts +379 -0
  31. package/src/defineResolver.ts +548 -25
  32. package/src/devtools.ts +22 -15
  33. package/src/index.ts +13 -15
  34. package/src/mocks.ts +177 -0
  35. package/src/storages/fsStorage.ts +13 -8
  36. package/src/storages/memoryStorage.ts +4 -2
  37. package/src/types.ts +40 -547
  38. package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
  39. package/dist/chunk-ByKO4r7w.cjs +0 -38
  40. package/dist/hooks.cjs +0 -103
  41. package/dist/hooks.cjs.map +0 -1
  42. package/dist/hooks.d.ts +0 -77
  43. package/dist/hooks.js +0 -98
  44. package/dist/hooks.js.map +0 -1
  45. package/src/Kubb.ts +0 -224
  46. package/src/build.ts +0 -418
  47. package/src/config.ts +0 -56
  48. package/src/createPlugin.ts +0 -28
  49. package/src/hooks/index.ts +0 -4
  50. package/src/hooks/useKubb.ts +0 -143
  51. package/src/hooks/useMode.ts +0 -11
  52. package/src/hooks/usePlugin.ts +0 -11
  53. package/src/hooks/usePluginDriver.ts +0 -11
  54. package/src/utils/FunctionParams.ts +0 -155
  55. package/src/utils/TreeNode.ts +0 -215
  56. package/src/utils/diagnostics.ts +0 -15
  57. package/src/utils/executeStrategies.ts +0 -81
  58. package/src/utils/formatters.ts +0 -56
  59. package/src/utils/getBarrelFiles.ts +0 -141
  60. package/src/utils/getConfigs.ts +0 -12
  61. package/src/utils/linters.ts +0 -25
  62. package/src/utils/packageJSON.ts +0 -61
package/dist/mocks.cjs ADDED
@@ -0,0 +1,145 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_PluginDriver = require("./PluginDriver-Cu1Kj9S-.cjs");
3
+ let node_path = require("node:path");
4
+ let _kubb_ast = require("@kubb/ast");
5
+ //#region src/mocks.ts
6
+ /**
7
+
8
+ * Creates a minimal `PluginDriver` mock for unit tests.
9
+ */
10
+ function createMockedPluginDriver(options = {}) {
11
+ return {
12
+ config: options?.config ?? {
13
+ root: ".",
14
+ output: { path: "./path" }
15
+ },
16
+ getPlugin(_pluginName) {
17
+ return options?.plugin;
18
+ },
19
+ getResolver: (_pluginName) => options?.plugin?.resolver,
20
+ fileManager: new require_PluginDriver.FileManager()
21
+ };
22
+ }
23
+ /**
24
+ * Creates a minimal `Adapter` mock for unit tests.
25
+ * `parse` returns an empty `InputNode` by default; override via `options.parse`.
26
+ * `getImports` returns `[]` by default.
27
+ */
28
+ function createMockedAdapter(options = {}) {
29
+ const inputNode = options.inputNode ?? null;
30
+ return {
31
+ name: options.name ?? "oas",
32
+ options: options.resolvedOptions ?? {},
33
+ inputNode,
34
+ parse: options.parse ?? (async () => ({
35
+ kind: "Input",
36
+ schemas: [],
37
+ operations: []
38
+ })),
39
+ getImports: options.getImports ?? ((_node, _resolve) => [])
40
+ };
41
+ }
42
+ /**
43
+ * Creates a minimal plugin mock for unit tests.
44
+ *
45
+ * @example
46
+ * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`
47
+ */
48
+ function createMockedPlugin(params) {
49
+ return {
50
+ name: params.name,
51
+ options: params.options,
52
+ resolver: params.resolver,
53
+ transformer: params.transformer,
54
+ dependencies: params.dependencies,
55
+ hooks: {}
56
+ };
57
+ }
58
+ function createMockedPluginContext(opts) {
59
+ const root = (0, node_path.resolve)(opts.config.root, opts.config.output.path);
60
+ return {
61
+ config: opts.config,
62
+ root,
63
+ getMode: (output) => require_PluginDriver.PluginDriver.getMode((0, node_path.resolve)(root, output.path)),
64
+ adapter: opts.adapter,
65
+ resolver: opts.resolver,
66
+ plugin: opts.plugin,
67
+ driver: opts.driver,
68
+ getResolver: (name) => opts.driver.getResolver(name),
69
+ inputNode: {
70
+ kind: "Input",
71
+ schemas: [],
72
+ operations: []
73
+ },
74
+ addFile: async (...files) => opts.driver.fileManager.add(...files),
75
+ upsertFile: async (...files) => opts.driver.fileManager.upsert(...files),
76
+ hooks: opts.driver.hooks ?? {},
77
+ warn: (msg) => console.warn(msg),
78
+ error: (msg) => console.error(msg),
79
+ info: (msg) => console.info(msg),
80
+ openInStudio: async () => {}
81
+ };
82
+ }
83
+ /**
84
+ * Renders a generator's `schema` method in a test context.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
89
+ * await matchFiles(driver.fileManager.files)
90
+ * ```
91
+ */
92
+ async function renderGeneratorSchema(generator, node, opts) {
93
+ if (!generator.schema) return;
94
+ const context = createMockedPluginContext(opts);
95
+ const transformedNode = opts.plugin.transformer ? (0, _kubb_ast.transform)(node, opts.plugin.transformer) : node;
96
+ await require_PluginDriver.applyHookResult(await generator.schema(transformedNode, {
97
+ ...context,
98
+ options: opts.options
99
+ }), opts.driver, generator.renderer ?? void 0);
100
+ }
101
+ /**
102
+ * Renders a generator's `operation` method in a test context.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
107
+ * await matchFiles(driver.fileManager.files)
108
+ * ```
109
+ */
110
+ async function renderGeneratorOperation(generator, node, opts) {
111
+ if (!generator.operation) return;
112
+ const context = createMockedPluginContext(opts);
113
+ const transformedNode = opts.plugin.transformer ? (0, _kubb_ast.transform)(node, opts.plugin.transformer) : node;
114
+ await require_PluginDriver.applyHookResult(await generator.operation(transformedNode, {
115
+ ...context,
116
+ options: opts.options
117
+ }), opts.driver, generator.renderer ?? void 0);
118
+ }
119
+ /**
120
+ * Renders a generator's `operations` method in a test context.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
125
+ * await matchFiles(driver.fileManager.files)
126
+ * ```
127
+ */
128
+ async function renderGeneratorOperations(generator, nodes, opts) {
129
+ if (!generator.operations) return;
130
+ const context = createMockedPluginContext(opts);
131
+ const transformedNodes = opts.plugin.transformer ? nodes.map((n) => (0, _kubb_ast.transform)(n, opts.plugin.transformer)) : nodes;
132
+ await require_PluginDriver.applyHookResult(await generator.operations(transformedNodes, {
133
+ ...context,
134
+ options: opts.options
135
+ }), opts.driver, generator.renderer ?? void 0);
136
+ }
137
+ //#endregion
138
+ exports.createMockedAdapter = createMockedAdapter;
139
+ exports.createMockedPlugin = createMockedPlugin;
140
+ exports.createMockedPluginDriver = createMockedPluginDriver;
141
+ exports.renderGeneratorOperation = renderGeneratorOperation;
142
+ exports.renderGeneratorOperations = renderGeneratorOperations;
143
+ exports.renderGeneratorSchema = renderGeneratorSchema;
144
+
145
+ //# sourceMappingURL=mocks.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mocks.cjs","names":["FileManager","PluginDriver","applyHookResult"],"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 { applyHookResult, PluginDriver } from './PluginDriver.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 } = {}): PluginDriver {\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 PluginDriver\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 inputNode?: Adapter<TOptions>['inputNode']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n const inputNode = options.inputNode ?? null\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n inputNode,\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 driver: PluginDriver\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 }) => PluginDriver.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 inputNode: { kind: 'Input', schemas: [], operations: [] },\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, opts.driver, generator.renderer ?? undefined)\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, opts.driver, generator.renderer ?? undefined)\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, opts.driver, generator.renderer ?? undefined)\n}\n"],"mappings":"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"}
@@ -0,0 +1,80 @@
1
+ import { t as __name } from "./chunk--u3MIqq1.js";
2
+ import { H as NormalizedPlugin, K as PluginFactoryOptions, P as PluginDriver, _t as AdapterFactoryOptions, gt as Adapter, j as Generator, r as Config } from "./createKubb-ALdb8lmq.js";
3
+ import { OperationNode, SchemaNode, Visitor } from "@kubb/ast";
4
+
5
+ //#region src/mocks.d.ts
6
+ /**
7
+
8
+ * Creates a minimal `PluginDriver` mock for unit tests.
9
+ */
10
+ declare function createMockedPluginDriver(options?: {
11
+ name?: string;
12
+ plugin?: NormalizedPlugin;
13
+ config?: Config;
14
+ }): PluginDriver;
15
+ /**
16
+ * Creates a minimal `Adapter` mock for unit tests.
17
+ * `parse` returns an empty `InputNode` by default; override via `options.parse`.
18
+ * `getImports` returns `[]` by default.
19
+ */
20
+ declare function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(options?: {
21
+ name?: TOptions['name'];
22
+ resolvedOptions?: TOptions['resolvedOptions'];
23
+ inputNode?: Adapter<TOptions>['inputNode'];
24
+ parse?: Adapter<TOptions>['parse'];
25
+ getImports?: Adapter<TOptions>['getImports'];
26
+ }): Adapter<TOptions>;
27
+ /**
28
+ * Creates a minimal plugin mock for unit tests.
29
+ *
30
+ * @example
31
+ * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`
32
+ */
33
+ declare function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {
34
+ name: TOptions['name'];
35
+ options: TOptions['resolvedOptions'];
36
+ resolver?: TOptions['resolver'];
37
+ transformer?: Visitor;
38
+ dependencies?: Array<string>;
39
+ }): NormalizedPlugin<TOptions>;
40
+ type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
41
+ config: Config;
42
+ adapter: Adapter;
43
+ driver: PluginDriver;
44
+ plugin: NormalizedPlugin<TOptions>;
45
+ options: TOptions['resolvedOptions'];
46
+ resolver: TOptions['resolver'];
47
+ };
48
+ /**
49
+ * Renders a generator's `schema` method in a test context.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
54
+ * await matchFiles(driver.fileManager.files)
55
+ * ```
56
+ */
57
+ declare function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(generator: Generator<TOptions>, node: SchemaNode, opts: RenderGeneratorOptions<TOptions>): Promise<void>;
58
+ /**
59
+ * Renders a generator's `operation` method in a test context.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
64
+ * await matchFiles(driver.fileManager.files)
65
+ * ```
66
+ */
67
+ declare function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(generator: Generator<TOptions>, node: OperationNode, opts: RenderGeneratorOptions<TOptions>): Promise<void>;
68
+ /**
69
+ * Renders a generator's `operations` method in a test context.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
74
+ * await matchFiles(driver.fileManager.files)
75
+ * ```
76
+ */
77
+ declare function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(generator: Generator<TOptions>, nodes: Array<OperationNode>, opts: RenderGeneratorOptions<TOptions>): Promise<void>;
78
+ //#endregion
79
+ export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
80
+ //# sourceMappingURL=mocks.d.ts.map
package/dist/mocks.js ADDED
@@ -0,0 +1,140 @@
1
+ import "./chunk--u3MIqq1.js";
2
+ import { n as applyHookResult, r as FileManager, t as PluginDriver } from "./PluginDriver-D8Z0Htid.js";
3
+ import { resolve } from "node:path";
4
+ import { transform } from "@kubb/ast";
5
+ //#region src/mocks.ts
6
+ /**
7
+
8
+ * Creates a minimal `PluginDriver` mock for unit tests.
9
+ */
10
+ function createMockedPluginDriver(options = {}) {
11
+ return {
12
+ config: options?.config ?? {
13
+ root: ".",
14
+ output: { path: "./path" }
15
+ },
16
+ getPlugin(_pluginName) {
17
+ return options?.plugin;
18
+ },
19
+ getResolver: (_pluginName) => options?.plugin?.resolver,
20
+ fileManager: new FileManager()
21
+ };
22
+ }
23
+ /**
24
+ * Creates a minimal `Adapter` mock for unit tests.
25
+ * `parse` returns an empty `InputNode` by default; override via `options.parse`.
26
+ * `getImports` returns `[]` by default.
27
+ */
28
+ function createMockedAdapter(options = {}) {
29
+ const inputNode = options.inputNode ?? null;
30
+ return {
31
+ name: options.name ?? "oas",
32
+ options: options.resolvedOptions ?? {},
33
+ inputNode,
34
+ parse: options.parse ?? (async () => ({
35
+ kind: "Input",
36
+ schemas: [],
37
+ operations: []
38
+ })),
39
+ getImports: options.getImports ?? ((_node, _resolve) => [])
40
+ };
41
+ }
42
+ /**
43
+ * Creates a minimal plugin mock for unit tests.
44
+ *
45
+ * @example
46
+ * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`
47
+ */
48
+ function createMockedPlugin(params) {
49
+ return {
50
+ name: params.name,
51
+ options: params.options,
52
+ resolver: params.resolver,
53
+ transformer: params.transformer,
54
+ dependencies: params.dependencies,
55
+ hooks: {}
56
+ };
57
+ }
58
+ function createMockedPluginContext(opts) {
59
+ const root = resolve(opts.config.root, opts.config.output.path);
60
+ return {
61
+ config: opts.config,
62
+ root,
63
+ getMode: (output) => PluginDriver.getMode(resolve(root, output.path)),
64
+ adapter: opts.adapter,
65
+ resolver: opts.resolver,
66
+ plugin: opts.plugin,
67
+ driver: opts.driver,
68
+ getResolver: (name) => opts.driver.getResolver(name),
69
+ inputNode: {
70
+ kind: "Input",
71
+ schemas: [],
72
+ operations: []
73
+ },
74
+ addFile: async (...files) => opts.driver.fileManager.add(...files),
75
+ upsertFile: async (...files) => opts.driver.fileManager.upsert(...files),
76
+ hooks: opts.driver.hooks ?? {},
77
+ warn: (msg) => console.warn(msg),
78
+ error: (msg) => console.error(msg),
79
+ info: (msg) => console.info(msg),
80
+ openInStudio: async () => {}
81
+ };
82
+ }
83
+ /**
84
+ * Renders a generator's `schema` method in a test context.
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
89
+ * await matchFiles(driver.fileManager.files)
90
+ * ```
91
+ */
92
+ async function renderGeneratorSchema(generator, node, opts) {
93
+ if (!generator.schema) return;
94
+ const context = createMockedPluginContext(opts);
95
+ const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node;
96
+ await applyHookResult(await generator.schema(transformedNode, {
97
+ ...context,
98
+ options: opts.options
99
+ }), opts.driver, generator.renderer ?? void 0);
100
+ }
101
+ /**
102
+ * Renders a generator's `operation` method in a test context.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
107
+ * await matchFiles(driver.fileManager.files)
108
+ * ```
109
+ */
110
+ async function renderGeneratorOperation(generator, node, opts) {
111
+ if (!generator.operation) return;
112
+ const context = createMockedPluginContext(opts);
113
+ const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node;
114
+ await applyHookResult(await generator.operation(transformedNode, {
115
+ ...context,
116
+ options: opts.options
117
+ }), opts.driver, generator.renderer ?? void 0);
118
+ }
119
+ /**
120
+ * Renders a generator's `operations` method in a test context.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
125
+ * await matchFiles(driver.fileManager.files)
126
+ * ```
127
+ */
128
+ async function renderGeneratorOperations(generator, nodes, opts) {
129
+ if (!generator.operations) return;
130
+ const context = createMockedPluginContext(opts);
131
+ const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer)) : nodes;
132
+ await applyHookResult(await generator.operations(transformedNodes, {
133
+ ...context,
134
+ options: opts.options
135
+ }), opts.driver, generator.renderer ?? void 0);
136
+ }
137
+ //#endregion
138
+ export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
139
+
140
+ //# sourceMappingURL=mocks.js.map
@@ -0,0 +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 { applyHookResult, PluginDriver } from './PluginDriver.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 } = {}): PluginDriver {\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 PluginDriver\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 inputNode?: Adapter<TOptions>['inputNode']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n const inputNode = options.inputNode ?? null\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n inputNode,\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 driver: PluginDriver\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 }) => PluginDriver.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 inputNode: { kind: 'Input', schemas: [], operations: [] },\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, opts.driver, generator.renderer ?? undefined)\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, opts.driver, generator.renderer ?? undefined)\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, opts.driver, generator.renderer ?? undefined)\n}\n"],"mappings":"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"}
package/package.json CHANGED
@@ -1,50 +1,22 @@
1
1
  {
2
2
  "name": "@kubb/core",
3
- "version": "5.0.0-alpha.9",
4
- "description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
3
+ "version": "5.0.0-beta.10",
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
- "typescript",
7
- "plugins",
8
- "plugin-system",
9
- "plugin-framework",
10
6
  "code-generator",
11
7
  "codegen",
12
- "ast",
13
- "file-system",
8
+ "kubb",
14
9
  "openapi",
15
- "swagger",
16
- "oas",
17
- "core-library",
18
- "kubb"
10
+ "plugin-system",
11
+ "typescript"
19
12
  ],
13
+ "license": "MIT",
14
+ "author": "stijnvanhulle",
20
15
  "repository": {
21
16
  "type": "git",
22
17
  "url": "git+https://github.com/kubb-labs/kubb.git",
23
18
  "directory": "packages/core"
24
19
  },
25
- "license": "MIT",
26
- "author": "stijnvanhulle",
27
- "sideEffects": false,
28
- "type": "module",
29
- "exports": {
30
- ".": {
31
- "import": "./dist/index.js",
32
- "require": "./dist/index.cjs"
33
- },
34
- "./hooks": {
35
- "import": "./dist/hooks.js",
36
- "require": "./dist/hooks.cjs"
37
- },
38
- "./package.json": "./package.json"
39
- },
40
- "types": "./dist/index.d.ts",
41
- "typesVersions": {
42
- "*": {
43
- "hooks": [
44
- "./dist/hooks.d.ts"
45
- ]
46
- }
47
- },
48
20
  "files": [
49
21
  "src",
50
22
  "schemas",
@@ -56,41 +28,56 @@
56
28
  "!/**/__tests__/**",
57
29
  "!/**/__snapshots__/**"
58
30
  ],
59
- "size-limit": [
60
- {
61
- "path": "./dist/*.js",
62
- "limit": "510 KiB",
63
- "gzip": true
31
+ "type": "module",
32
+ "sideEffects": false,
33
+ "main": "./dist/index.cjs",
34
+ "module": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "typesVersions": {
37
+ "*": {
38
+ "mocks": [
39
+ "./dist/mocks.d.ts"
40
+ ]
64
41
  }
65
- ],
42
+ },
43
+ "exports": {
44
+ ".": {
45
+ "import": "./dist/index.js",
46
+ "require": "./dist/index.cjs"
47
+ },
48
+ "./mocks": {
49
+ "import": "./dist/mocks.js",
50
+ "require": "./dist/mocks.cjs"
51
+ },
52
+ "./package.json": "./package.json"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public",
56
+ "registry": "https://registry.npmjs.org/"
57
+ },
66
58
  "dependencies": {
67
- "@kubb/fabric-core": "0.14.0",
68
- "@kubb/react-fabric": "0.14.0",
69
- "empathic": "^2.0.0",
70
59
  "fflate": "^0.8.2",
71
- "remeda": "^2.33.6",
72
- "semver": "^7.7.4",
73
- "tinyexec": "^1.0.4",
74
- "@kubb/ast": "5.0.0-alpha.9"
60
+ "tinyexec": "^1.1.2",
61
+ "@kubb/ast": "5.0.0-beta.10"
75
62
  },
76
63
  "devDependencies": {
77
- "@types/semver": "^7.7.1",
78
64
  "p-limit": "^7.3.0",
79
- "@internals/utils": "0.0.0"
65
+ "@internals/utils": "0.0.0",
66
+ "@kubb/renderer-jsx": "5.0.0-beta.10"
80
67
  },
81
68
  "peerDependencies": {
82
- "@kubb/fabric-core": "0.14.0",
83
- "@kubb/react-fabric": "0.14.0"
69
+ "@kubb/renderer-jsx": "5.0.0-beta.10"
84
70
  },
71
+ "size-limit": [
72
+ {
73
+ "path": "./dist/*.js",
74
+ "limit": "510 KiB",
75
+ "gzip": true
76
+ }
77
+ ],
85
78
  "engines": {
86
79
  "node": ">=22"
87
80
  },
88
- "publishConfig": {
89
- "access": "public",
90
- "registry": "https://registry.npmjs.org/"
91
- },
92
- "main": "./dist/index.cjs",
93
- "module": "./dist/index.js",
94
81
  "inlinedDependencies": {
95
82
  "p-limit": "7.3.0",
96
83
  "yocto-queue": "1.2.2"
@@ -98,8 +85,8 @@
98
85
  "scripts": {
99
86
  "build": "tsdown && size-limit",
100
87
  "clean": "npx rimraf ./dist",
101
- "lint": "bun biome lint .",
102
- "lint:fix": "bun biome lint --fix --unsafe .",
88
+ "lint": "oxlint .",
89
+ "lint:fix": "oxlint --fix .",
103
90
  "release": "pnpm publish --no-git-check",
104
91
  "release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
105
92
  "start": "tsdown --watch",
@@ -0,0 +1,115 @@
1
+ import type { FileNode } from '@kubb/ast'
2
+ import { createFile } from '@kubb/ast'
3
+
4
+ function mergeFile<TMeta extends object = object>(a: FileNode<TMeta>, b: FileNode<TMeta>): FileNode<TMeta> {
5
+ return {
6
+ ...a,
7
+ // Incoming file (b) takes precedence for banner/footer so that barrel files,
8
+ // which never carry a banner, can clear banners set by plugin-generated files
9
+ // at the same path.
10
+ banner: b.banner,
11
+ footer: b.footer,
12
+ sources: [...(a.sources || []), ...(b.sources || [])],
13
+ imports: [...(a.imports || []), ...(b.imports || [])],
14
+ exports: [...(a.exports || []), ...(b.exports || [])],
15
+ }
16
+ }
17
+
18
+ /**
19
+ * Collapses a list of files so that duplicates sharing the same `path` are merged
20
+ * in arrival order. Keeps the original order of first occurrence.
21
+ */
22
+ function mergeFilesByPath(files: ReadonlyArray<FileNode>): Map<string, FileNode> {
23
+ const merged = new Map<string, FileNode>()
24
+ for (const file of files) {
25
+ const existing = merged.get(file.path)
26
+ merged.set(file.path, existing ? mergeFile(existing, file) : file)
27
+ }
28
+ return merged
29
+ }
30
+
31
+ /**
32
+ * In-memory file store for generated files.
33
+ *
34
+ * Files with the same `path` are merged — sources, imports, and exports are concatenated.
35
+ * The `files` getter returns all stored files sorted by path length (shortest first).
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * import { FileManager } from '@kubb/core'
40
+ *
41
+ * const manager = new FileManager()
42
+ * manager.upsert(myFile)
43
+ * console.log(manager.files) // all stored files
44
+ * ```
45
+ */
46
+ export class FileManager {
47
+ readonly #cache = new Map<string, FileNode>()
48
+ #filesCache: Array<FileNode> | null = null
49
+
50
+ /**
51
+ * Adds one or more files. Incoming files with the same path are merged
52
+ * (sources/imports/exports concatenated), but existing cache entries are
53
+ * replaced — use {@link upsert} when you want to merge into the cache too.
54
+ */
55
+ add(...files: Array<FileNode>): Array<FileNode> {
56
+ return this.#store(files, false)
57
+ }
58
+
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
+ upsert(...files: Array<FileNode>): Array<FileNode> {
65
+ return this.#store(files, true)
66
+ }
67
+
68
+ #store(files: ReadonlyArray<FileNode>, mergeExisting: boolean): Array<FileNode> {
69
+ const resolvedFiles: Array<FileNode> = []
70
+ for (const file of mergeFilesByPath(files).values()) {
71
+ const existing = mergeExisting ? this.#cache.get(file.path) : undefined
72
+ const resolvedFile = createFile(existing ? mergeFile(existing, file) : file)
73
+ this.#cache.set(resolvedFile.path, resolvedFile)
74
+ resolvedFiles.push(resolvedFile)
75
+ }
76
+ this.#filesCache = null
77
+ return resolvedFiles
78
+ }
79
+
80
+ getByPath(path: string): FileNode | null {
81
+ return this.#cache.get(path) ?? null
82
+ }
83
+
84
+ deleteByPath(path: string): void {
85
+ this.#cache.delete(path)
86
+ this.#filesCache = null
87
+ }
88
+
89
+ clear(): void {
90
+ this.#cache.clear()
91
+ this.#filesCache = null
92
+ }
93
+
94
+ /**
95
+ * All stored files, sorted by path length (shorter paths first).
96
+ */
97
+ get files(): Array<FileNode> {
98
+ if (this.#filesCache) {
99
+ return this.#filesCache
100
+ }
101
+
102
+ this.#filesCache = [...this.#cache.values()].sort((a, b) => {
103
+ const lenDiff = a.path.length - b.path.length
104
+ if (lenDiff !== 0) return lenDiff
105
+ // Within the same length bucket, index.ts barrel files go last so other
106
+ // files are always processed before their barrel file.
107
+ const aIsIndex = a.path.endsWith('/index.ts') || a.path === 'index.ts'
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
114
+ }
115
+ }