@kubb/core 5.0.0-alpha.5 → 5.0.0-alpha.51
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 +3 -2
- package/dist/PluginDriver-6E8zd8MW.cjs +1086 -0
- package/dist/PluginDriver-6E8zd8MW.cjs.map +1 -0
- package/dist/PluginDriver-D6wQFD4r.js +983 -0
- package/dist/PluginDriver-D6wQFD4r.js.map +1 -0
- package/dist/index.cjs +1013 -1829
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +279 -265
- package/dist/index.js +1003 -1799
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +138 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +74 -0
- package/dist/mocks.js +133 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-DfEv9d_c.d.ts +1721 -0
- package/package.json +51 -57
- package/src/FileManager.ts +133 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +154 -101
- package/src/PluginDriver.ts +418 -0
- package/src/constants.ts +43 -47
- package/src/createAdapter.ts +25 -0
- package/src/createKubb.ts +614 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +58 -0
- package/src/defineGenerator.ts +88 -100
- package/src/defineLogger.ts +13 -3
- package/src/defineParser.ts +45 -0
- package/src/definePlugin.ts +68 -7
- package/src/defineResolver.ts +521 -0
- package/src/devtools.ts +14 -14
- package/src/index.ts +12 -17
- package/src/mocks.ts +171 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +40 -11
- package/src/storages/memoryStorage.ts +4 -3
- package/src/types.ts +575 -205
- package/src/utils/TreeNode.ts +47 -9
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/getBarrelFiles.ts +94 -16
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +99 -0
- package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -60
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -56
- package/dist/hooks.js +0 -56
- package/dist/hooks.js.map +0 -1
- package/src/BarrelManager.ts +0 -74
- package/src/PackageManager.ts +0 -180
- package/src/PluginManager.ts +0 -667
- package/src/PromiseManager.ts +0 -40
- package/src/build.ts +0 -419
- package/src/config.ts +0 -56
- package/src/defineAdapter.ts +0 -22
- package/src/defineStorage.ts +0 -56
- package/src/errors.ts +0 -1
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -46
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginManager.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/executeStrategies.ts +0 -81
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getConfigs.ts +0 -30
- package/src/utils/getPlugins.ts +0 -23
- package/src/utils/linters.ts +0 -25
- package/src/utils/resolveOptions.ts +0 -93
package/src/mocks.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
import type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'
|
|
3
|
+
import { transform } from '@kubb/ast'
|
|
4
|
+
import { FileManager } from './FileManager.ts'
|
|
5
|
+
import { PluginDriver } from './PluginDriver.ts'
|
|
6
|
+
import { applyHookResult } from './renderNode.ts'
|
|
7
|
+
import type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Creates a minimal `PluginDriver` mock suitable for unit tests.
|
|
11
|
+
*/
|
|
12
|
+
export function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {
|
|
13
|
+
return {
|
|
14
|
+
config: options?.config ?? {
|
|
15
|
+
root: '.',
|
|
16
|
+
output: {
|
|
17
|
+
path: './path',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
getPlugin(_pluginName: string): NormalizedPlugin | undefined {
|
|
21
|
+
return options?.plugin
|
|
22
|
+
},
|
|
23
|
+
getResolver: (_pluginName: string) => options?.plugin?.resolver,
|
|
24
|
+
fileManager: new FileManager(),
|
|
25
|
+
} as unknown as PluginDriver
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Creates a minimal `Adapter` mock suitable for unit tests.
|
|
30
|
+
*
|
|
31
|
+
* - `parse` returns an empty `InputNode` by default; override via `options.parse`.
|
|
32
|
+
* - `getImports` returns `[]` by default (single-file mode, no cross-file imports).
|
|
33
|
+
*/
|
|
34
|
+
export function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(
|
|
35
|
+
options: {
|
|
36
|
+
name?: TOptions['name']
|
|
37
|
+
resolvedOptions?: TOptions['resolvedOptions']
|
|
38
|
+
inputNode?: Adapter<TOptions>['inputNode']
|
|
39
|
+
parse?: Adapter<TOptions>['parse']
|
|
40
|
+
getImports?: Adapter<TOptions>['getImports']
|
|
41
|
+
} = {},
|
|
42
|
+
): Adapter<TOptions> {
|
|
43
|
+
return {
|
|
44
|
+
name: (options.name ?? 'oas') as TOptions['name'],
|
|
45
|
+
options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],
|
|
46
|
+
inputNode: options.inputNode ?? null,
|
|
47
|
+
parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),
|
|
48
|
+
getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),
|
|
49
|
+
} as Adapter<TOptions>
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a minimal plugin mock suitable for unit tests.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })
|
|
57
|
+
*/
|
|
58
|
+
export function createMockedPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(params: {
|
|
59
|
+
name: TOptions['name']
|
|
60
|
+
options: TOptions['resolvedOptions']
|
|
61
|
+
resolver?: TOptions['resolver']
|
|
62
|
+
transformer?: Visitor
|
|
63
|
+
dependencies?: Array<string>
|
|
64
|
+
}): NormalizedPlugin<TOptions> {
|
|
65
|
+
return {
|
|
66
|
+
name: params.name,
|
|
67
|
+
options: params.options,
|
|
68
|
+
resolver: params.resolver,
|
|
69
|
+
transformer: params.transformer,
|
|
70
|
+
dependencies: params.dependencies,
|
|
71
|
+
hooks: {},
|
|
72
|
+
} as unknown as NormalizedPlugin<TOptions>
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
|
|
76
|
+
config: Config
|
|
77
|
+
adapter: Adapter
|
|
78
|
+
driver: PluginDriver
|
|
79
|
+
plugin: NormalizedPlugin<TOptions>
|
|
80
|
+
options: TOptions['resolvedOptions']
|
|
81
|
+
resolver: TOptions['resolver']
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function createMockedPluginContext<TOptions extends PluginFactoryOptions>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {
|
|
85
|
+
const root = resolve(opts.config.root, opts.config.output.path)
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
config: opts.config,
|
|
89
|
+
root,
|
|
90
|
+
getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),
|
|
91
|
+
adapter: opts.adapter,
|
|
92
|
+
resolver: opts.resolver,
|
|
93
|
+
plugin: opts.plugin,
|
|
94
|
+
driver: opts.driver,
|
|
95
|
+
getResolver: (name: string) => opts.driver.getResolver(name),
|
|
96
|
+
inputNode: { kind: 'Input', schemas: [], operations: [] },
|
|
97
|
+
addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),
|
|
98
|
+
upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),
|
|
99
|
+
hooks: opts.driver.hooks ?? ({} as never),
|
|
100
|
+
warn: (msg: string) => console.warn(msg),
|
|
101
|
+
error: (msg: string) => console.error(msg),
|
|
102
|
+
info: (msg: string) => console.info(msg),
|
|
103
|
+
openInStudio: async () => {},
|
|
104
|
+
} as unknown as Omit<GeneratorContext<TOptions>, 'options'>
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Renders a generator's `schema` method in a test context.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
112
|
+
* await matchFiles(driver.fileManager.files)
|
|
113
|
+
*/
|
|
114
|
+
export async function renderGeneratorSchema<TOptions extends PluginFactoryOptions>(
|
|
115
|
+
generator: Generator<TOptions>,
|
|
116
|
+
node: SchemaNode,
|
|
117
|
+
opts: RenderGeneratorOptions<TOptions>,
|
|
118
|
+
): Promise<void> {
|
|
119
|
+
if (!generator.schema) return
|
|
120
|
+
const context = createMockedPluginContext(opts)
|
|
121
|
+
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
|
|
122
|
+
const result = await generator.schema(transformedNode, {
|
|
123
|
+
...context,
|
|
124
|
+
options: opts.options,
|
|
125
|
+
})
|
|
126
|
+
await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Renders a generator's `operation` method in a test context.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
134
|
+
* await matchFiles(driver.fileManager.files)
|
|
135
|
+
*/
|
|
136
|
+
export async function renderGeneratorOperation<TOptions extends PluginFactoryOptions>(
|
|
137
|
+
generator: Generator<TOptions>,
|
|
138
|
+
node: OperationNode,
|
|
139
|
+
opts: RenderGeneratorOptions<TOptions>,
|
|
140
|
+
): Promise<void> {
|
|
141
|
+
if (!generator.operation) return
|
|
142
|
+
const context = createMockedPluginContext(opts)
|
|
143
|
+
const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node
|
|
144
|
+
const result = await generator.operation(transformedNode, {
|
|
145
|
+
...context,
|
|
146
|
+
options: opts.options,
|
|
147
|
+
})
|
|
148
|
+
await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Renders a generator's `operations` method in a test context.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })
|
|
156
|
+
* await matchFiles(driver.fileManager.files)
|
|
157
|
+
*/
|
|
158
|
+
export async function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(
|
|
159
|
+
generator: Generator<TOptions>,
|
|
160
|
+
nodes: Array<OperationNode>,
|
|
161
|
+
opts: RenderGeneratorOptions<TOptions>,
|
|
162
|
+
): Promise<void> {
|
|
163
|
+
if (!generator.operations) return
|
|
164
|
+
const context = createMockedPluginContext(opts)
|
|
165
|
+
const transformedNodes = opts.plugin.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes
|
|
166
|
+
const result = await generator.operations(transformedNodes, {
|
|
167
|
+
...context,
|
|
168
|
+
options: opts.options,
|
|
169
|
+
})
|
|
170
|
+
await applyHookResult(result, opts.driver, generator.renderer ?? undefined)
|
|
171
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { FileNode } from '@kubb/ast'
|
|
2
|
+
import type { RendererFactory } from './createRenderer.ts'
|
|
3
|
+
import type { PluginDriver } from './PluginDriver.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handles the return value of a plugin AST hook or generator method.
|
|
7
|
+
*
|
|
8
|
+
* - Renderer output → rendered via the provided `rendererFactory` (e.g. JSX), files stored in `driver.fileManager`
|
|
9
|
+
* - `Array<FileNode>` → added directly into `driver.fileManager`
|
|
10
|
+
* - `void` / `null` / `undefined` → no-op (plugin handled it via `this.upsertFile`)
|
|
11
|
+
*
|
|
12
|
+
* Pass a `rendererFactory` (e.g. `jsxRenderer` from `@kubb/renderer-jsx`) when the result
|
|
13
|
+
* may be a renderer element. Generators that only return `Array<FileNode>` do not need one.
|
|
14
|
+
*/
|
|
15
|
+
export async function applyHookResult<TElement = unknown>(
|
|
16
|
+
result: TElement | Array<FileNode> | void,
|
|
17
|
+
driver: PluginDriver,
|
|
18
|
+
rendererFactory?: RendererFactory<TElement>,
|
|
19
|
+
): Promise<void> {
|
|
20
|
+
if (!result) return
|
|
21
|
+
|
|
22
|
+
if (Array.isArray(result)) {
|
|
23
|
+
driver.fileManager.upsert(...(result as Array<FileNode>))
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!rendererFactory) {
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const renderer = rendererFactory()
|
|
32
|
+
await renderer.render(result)
|
|
33
|
+
driver.fileManager.upsert(...renderer.files)
|
|
34
|
+
renderer.unmount()
|
|
35
|
+
}
|
|
@@ -2,7 +2,14 @@ import type { Dirent } from 'node:fs'
|
|
|
2
2
|
import { access, readdir, readFile, rm } from 'node:fs/promises'
|
|
3
3
|
import { join, resolve } from 'node:path'
|
|
4
4
|
import { clean, write } from '@internals/utils'
|
|
5
|
-
import {
|
|
5
|
+
import { createStorage } from '../createStorage.ts'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Detects the filesystem error used to indicate that a path does not exist.
|
|
9
|
+
*/
|
|
10
|
+
function isMissingPathError(error: unknown): error is NodeJS.ErrnoException {
|
|
11
|
+
return typeof error === 'object' && error !== null && 'code' in error && (error as NodeJS.ErrnoException).code === 'ENOENT'
|
|
12
|
+
}
|
|
6
13
|
|
|
7
14
|
/**
|
|
8
15
|
* Built-in filesystem storage driver.
|
|
@@ -19,7 +26,8 @@ import { defineStorage } from '../defineStorage.ts'
|
|
|
19
26
|
*
|
|
20
27
|
* @example
|
|
21
28
|
* ```ts
|
|
22
|
-
* import {
|
|
29
|
+
* import { fsStorage } from '@kubb/core'
|
|
30
|
+
* import { defineConfig } from 'kubb'
|
|
23
31
|
*
|
|
24
32
|
* export default defineConfig({
|
|
25
33
|
* input: { path: './petStore.yaml' },
|
|
@@ -27,21 +35,33 @@ import { defineStorage } from '../defineStorage.ts'
|
|
|
27
35
|
* })
|
|
28
36
|
* ```
|
|
29
37
|
*/
|
|
30
|
-
export const fsStorage =
|
|
38
|
+
export const fsStorage = createStorage(() => ({
|
|
31
39
|
name: 'fs',
|
|
32
40
|
async hasItem(key: string) {
|
|
33
41
|
try {
|
|
34
42
|
await access(resolve(key))
|
|
35
43
|
return true
|
|
36
|
-
} catch {
|
|
37
|
-
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (isMissingPathError(error)) {
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
throw new Error(`Failed to access storage item "${key}"`, {
|
|
50
|
+
cause: error as Error,
|
|
51
|
+
})
|
|
38
52
|
}
|
|
39
53
|
},
|
|
40
54
|
async getItem(key: string) {
|
|
41
55
|
try {
|
|
42
56
|
return await readFile(resolve(key), 'utf8')
|
|
43
|
-
} catch {
|
|
44
|
-
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (isMissingPathError(error)) {
|
|
59
|
+
return null
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
throw new Error(`Failed to read storage item "${key}"`, {
|
|
63
|
+
cause: error as Error,
|
|
64
|
+
})
|
|
45
65
|
}
|
|
46
66
|
},
|
|
47
67
|
async setItem(key: string, value: string) {
|
|
@@ -52,13 +72,22 @@ export const fsStorage = defineStorage(() => ({
|
|
|
52
72
|
},
|
|
53
73
|
async getKeys(base?: string) {
|
|
54
74
|
const keys: Array<string> = []
|
|
75
|
+
const resolvedBase = resolve(base ?? process.cwd())
|
|
55
76
|
|
|
56
77
|
async function walk(dir: string, prefix: string): Promise<void> {
|
|
57
78
|
let entries: Array<Dirent>
|
|
58
79
|
try {
|
|
59
|
-
entries = (await readdir(dir, {
|
|
60
|
-
|
|
61
|
-
|
|
80
|
+
entries = (await readdir(dir, {
|
|
81
|
+
withFileTypes: true,
|
|
82
|
+
})) as Array<Dirent>
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (isMissingPathError(error)) {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
throw new Error(`Failed to list storage keys under "${resolvedBase}"`, {
|
|
89
|
+
cause: error as Error,
|
|
90
|
+
})
|
|
62
91
|
}
|
|
63
92
|
for (const entry of entries) {
|
|
64
93
|
const rel = prefix ? `${prefix}/${entry.name}` : entry.name
|
|
@@ -70,7 +99,7 @@ export const fsStorage = defineStorage(() => ({
|
|
|
70
99
|
}
|
|
71
100
|
}
|
|
72
101
|
|
|
73
|
-
await walk(
|
|
102
|
+
await walk(resolvedBase, '')
|
|
74
103
|
|
|
75
104
|
return keys
|
|
76
105
|
},
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createStorage } from '../createStorage.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* In-memory storage driver. Useful for testing and dry-run scenarios where
|
|
@@ -9,7 +9,8 @@ import { defineStorage } from '../defineStorage.ts'
|
|
|
9
9
|
*
|
|
10
10
|
* @example
|
|
11
11
|
* ```ts
|
|
12
|
-
* import {
|
|
12
|
+
* import { memoryStorage } from '@kubb/core'
|
|
13
|
+
* import { defineConfig } from 'kubb'
|
|
13
14
|
*
|
|
14
15
|
* export default defineConfig({
|
|
15
16
|
* input: { path: './petStore.yaml' },
|
|
@@ -17,7 +18,7 @@ import { defineStorage } from '../defineStorage.ts'
|
|
|
17
18
|
* })
|
|
18
19
|
* ```
|
|
19
20
|
*/
|
|
20
|
-
export const memoryStorage =
|
|
21
|
+
export const memoryStorage = createStorage(() => {
|
|
21
22
|
const store = new Map<string, string>()
|
|
22
23
|
|
|
23
24
|
return {
|