@kubb/core 5.0.0-alpha.9 → 5.0.0-beta.2
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 +24 -21
- package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
- package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
- package/dist/PluginDriver-DV3p2Hky.js +945 -0
- package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
- package/dist/index.cjs +752 -1641
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +271 -225
- package/dist/index.js +736 -1609
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +145 -0
- package/dist/mocks.cjs.map +1 -0
- package/dist/mocks.d.ts +80 -0
- package/dist/mocks.js +140 -0
- package/dist/mocks.js.map +1 -0
- package/dist/types-CC09VtBt.d.ts +2148 -0
- package/package.json +51 -57
- package/src/FileManager.ts +115 -0
- package/src/FileProcessor.ts +86 -0
- package/src/Kubb.ts +207 -131
- package/src/PluginDriver.ts +325 -564
- package/src/constants.ts +20 -47
- package/src/createAdapter.ts +13 -6
- package/src/createKubb.ts +574 -0
- package/src/createRenderer.ts +57 -0
- package/src/createStorage.ts +13 -1
- package/src/defineGenerator.ts +77 -124
- package/src/defineLogger.ts +4 -2
- package/src/defineMiddleware.ts +62 -0
- package/src/defineParser.ts +44 -0
- package/src/definePlugin.ts +83 -0
- package/src/defineResolver.ts +418 -28
- package/src/devtools.ts +14 -14
- package/src/index.ts +13 -15
- package/src/mocks.ts +178 -0
- package/src/renderNode.ts +35 -0
- package/src/storages/fsStorage.ts +41 -11
- package/src/storages/memoryStorage.ts +4 -2
- package/src/types.ts +1031 -283
- package/src/utils/diagnostics.ts +4 -1
- package/src/utils/isInputPath.ts +10 -0
- package/src/utils/packageJSON.ts +50 -12
- package/dist/PluginDriver-BkFepPdm.d.ts +0 -1054
- package/dist/chunk-ByKO4r7w.cjs +0 -38
- package/dist/hooks.cjs +0 -103
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.ts +0 -77
- package/dist/hooks.js +0 -98
- package/dist/hooks.js.map +0 -1
- package/src/build.ts +0 -418
- package/src/config.ts +0 -56
- package/src/createPlugin.ts +0 -28
- package/src/hooks/index.ts +0 -4
- package/src/hooks/useKubb.ts +0 -143
- package/src/hooks/useMode.ts +0 -11
- package/src/hooks/usePlugin.ts +0 -11
- package/src/hooks/usePluginDriver.ts +0 -11
- package/src/utils/FunctionParams.ts +0 -155
- package/src/utils/TreeNode.ts +0 -215
- package/src/utils/executeStrategies.ts +0 -81
- package/src/utils/formatters.ts +0 -56
- package/src/utils/getBarrelFiles.ts +0 -141
- package/src/utils/getConfigs.ts +0 -12
- package/src/utils/linters.ts +0 -25
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { FileNode } from '@kubb/ast'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Minimal interface any Kubb renderer must satisfy.
|
|
5
|
+
*
|
|
6
|
+
* The generic `TElement` is the type of the element the renderer accepts —
|
|
7
|
+
* e.g. `KubbReactElement` for `@kubb/renderer-jsx`, or a custom type for
|
|
8
|
+
* your own renderer. Defaults to `unknown` so that generators which do not
|
|
9
|
+
* care about the element type continue to work without specifying it.
|
|
10
|
+
*
|
|
11
|
+
* This allows core to drive rendering without a hard dependency on
|
|
12
|
+
* `@kubb/renderer-jsx` or any specific renderer implementation.
|
|
13
|
+
*/
|
|
14
|
+
export type Renderer<TElement = unknown> = {
|
|
15
|
+
render(element: TElement): Promise<void>
|
|
16
|
+
unmount(error?: Error | number | null): void
|
|
17
|
+
readonly files: Array<FileNode>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A factory function that produces a fresh {@link Renderer} per render.
|
|
22
|
+
*
|
|
23
|
+
* Generators use this to declare which renderer handles their output.
|
|
24
|
+
*/
|
|
25
|
+
export type RendererFactory<TElement = unknown> = () => Renderer<TElement>
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a renderer factory for use in generator definitions.
|
|
29
|
+
*
|
|
30
|
+
* Wrap your renderer factory function with this helper to register it as the
|
|
31
|
+
* renderer for a generator. Core will call this factory once per render cycle
|
|
32
|
+
* to obtain a fresh renderer instance.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* // packages/renderer-jsx/src/index.ts
|
|
37
|
+
* export const jsxRenderer = createRenderer(() => {
|
|
38
|
+
* const runtime = new Runtime()
|
|
39
|
+
* return {
|
|
40
|
+
* async render(element) { await runtime.render(element) },
|
|
41
|
+
* get files() { return runtime.nodes },
|
|
42
|
+
* unmount(error) { runtime.unmount(error) },
|
|
43
|
+
* }
|
|
44
|
+
* })
|
|
45
|
+
*
|
|
46
|
+
* // packages/plugin-zod/src/generators/zodGenerator.tsx
|
|
47
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
48
|
+
* export const zodGenerator = defineGenerator<PluginZod>({
|
|
49
|
+
* name: 'zod',
|
|
50
|
+
* renderer: jsxRenderer,
|
|
51
|
+
* schema(node, options) { return <File ...>...</File> },
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement> {
|
|
56
|
+
return factory
|
|
57
|
+
}
|
package/src/createStorage.ts
CHANGED
|
@@ -34,9 +34,17 @@ export type Storage = {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* Factory for implementing custom storage backends that control where generated files are written.
|
|
38
|
+
*
|
|
39
|
+
* Takes a builder function `(options: TOptions) => Storage` and returns a factory `(options?: TOptions) => Storage`.
|
|
40
|
+
* Kubb provides filesystem and in-memory implementations out of the box.
|
|
41
|
+
*
|
|
42
|
+
* @note Call the returned factory with optional options to instantiate the storage adapter.
|
|
38
43
|
*
|
|
39
44
|
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { createStorage } from '@kubb/core'
|
|
47
|
+
*
|
|
40
48
|
* export const memoryStorage = createStorage(() => {
|
|
41
49
|
* const store = new Map<string, string>()
|
|
42
50
|
* return {
|
|
@@ -52,6 +60,10 @@ export type Storage = {
|
|
|
52
60
|
* async clear(base) { if (!base) store.clear() },
|
|
53
61
|
* }
|
|
54
62
|
* })
|
|
63
|
+
*
|
|
64
|
+
* // Instantiate:
|
|
65
|
+
* const storage = memoryStorage()
|
|
66
|
+
* ```
|
|
55
67
|
*/
|
|
56
68
|
export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
|
|
57
69
|
return (options) => build(options ?? ({} as TOptions))
|
package/src/defineGenerator.ts
CHANGED
|
@@ -1,134 +1,87 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
1
|
+
import type { PossiblePromise } from '@internals/utils'
|
|
2
|
+
import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast'
|
|
3
|
+
import type { RendererFactory } from './createRenderer.ts'
|
|
4
|
+
import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type { GeneratorContext } from './types.ts'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
export type OperationsV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
12
|
-
config: Config
|
|
13
|
-
adapter: Adapter
|
|
14
|
-
options: Plugin<TPlugin>['options']
|
|
15
|
-
nodes: Array<OperationNode>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Props for the `operation` lifecycle — receives a single operation node.
|
|
20
|
-
*/
|
|
21
|
-
export type OperationV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
22
|
-
config: Config
|
|
23
|
-
adapter: Adapter
|
|
24
|
-
options: Plugin<TPlugin>['options']
|
|
25
|
-
node: OperationNode
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Props for the `schema` lifecycle — receives a single schema node.
|
|
30
|
-
*/
|
|
31
|
-
export type SchemaV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
32
|
-
config: Config
|
|
33
|
-
adapter: Adapter
|
|
34
|
-
options: Plugin<TPlugin>['options']
|
|
35
|
-
node: SchemaNode
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type UserCoreGeneratorV2<TPlugin extends PluginFactoryOptions> = {
|
|
39
|
-
name: string
|
|
40
|
-
type: 'core'
|
|
41
|
-
version?: '2'
|
|
42
|
-
operations?(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
43
|
-
operation?(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
44
|
-
schema?(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
type UserReactGeneratorV2<TPlugin extends PluginFactoryOptions> = {
|
|
48
|
-
name: string
|
|
49
|
-
type: 'react'
|
|
50
|
-
version?: '2'
|
|
51
|
-
Operations?(props: OperationsV2Props<TPlugin>): FabricReactNode
|
|
52
|
-
Operation?(props: OperationV2Props<TPlugin>): FabricReactNode
|
|
53
|
-
Schema?(props: SchemaV2Props<TPlugin>): FabricReactNode
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type CoreGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
57
|
-
name: string
|
|
58
|
-
type: 'core'
|
|
59
|
-
version: '2'
|
|
60
|
-
operations(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
61
|
-
operation(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
62
|
-
schema(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export type ReactGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
66
|
-
name: string
|
|
67
|
-
type: 'react'
|
|
68
|
-
version: '2'
|
|
69
|
-
Operations(props: OperationsV2Props<TPlugin>): FabricReactNode
|
|
70
|
-
Operation(props: OperationV2Props<TPlugin>): FabricReactNode
|
|
71
|
-
Schema(props: SchemaV2Props<TPlugin>): FabricReactNode
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export type Generator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Defines a generator with no-op defaults for any omitted lifecycle methods.
|
|
78
|
-
* Works for both `core` (async file output) and `react` (JSX component) generators.
|
|
9
|
+
* Declares a named generator unit that walks the AST and emits files.
|
|
79
10
|
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* Operation({ node, options }) { return <File>...</File> },
|
|
86
|
-
* Schema({ node, options }) { return <File>...</File> },
|
|
87
|
-
* })
|
|
11
|
+
* Each method (`schema`, `operation`, `operations`) is called for the matching node type.
|
|
12
|
+
* Each method returns `TElement | Array<FileNode> | void`. JSX-based generators require a `renderer` factory.
|
|
13
|
+
* Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `void` to bypass rendering.
|
|
14
|
+
*
|
|
15
|
+
* @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
|
|
88
16
|
*
|
|
89
17
|
* @example
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { defineGenerator } from '@kubb/core'
|
|
20
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
21
|
+
*
|
|
22
|
+
* export const typeGenerator = defineGenerator({
|
|
23
|
+
* name: 'typescript',
|
|
24
|
+
* renderer: jsxRenderer,
|
|
25
|
+
* schema(node, ctx) {
|
|
26
|
+
* const { adapter, resolver, root, options } = ctx
|
|
27
|
+
* return <File ...><Type node={node} resolver={resolver} /></File>
|
|
28
|
+
* },
|
|
95
29
|
* })
|
|
30
|
+
* ```
|
|
96
31
|
*/
|
|
97
|
-
export
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
32
|
+
export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown> = {
|
|
33
|
+
/**
|
|
34
|
+
* Used in diagnostic messages and debug output.
|
|
35
|
+
*/
|
|
36
|
+
name: string
|
|
37
|
+
/**
|
|
38
|
+
* Optional renderer factory that produces a {@link Renderer} for each render cycle.
|
|
39
|
+
*
|
|
40
|
+
* Generators that return renderer elements (e.g. JSX via `@kubb/renderer-jsx`) must set this
|
|
41
|
+
* to the matching renderer factory (e.g. `jsxRenderer` from `@kubb/renderer-jsx`).
|
|
42
|
+
*
|
|
43
|
+
* Generators that only return `Array<FileNode>` or `void` do not need to set this.
|
|
44
|
+
*
|
|
45
|
+
* Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
|
|
46
|
+
* declares a `renderer` (overrides the plugin-level fallback).
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { jsxRenderer } from '@kubb/renderer-jsx'
|
|
51
|
+
* export const myGenerator = defineGenerator<PluginTs>({
|
|
52
|
+
* renderer: jsxRenderer,
|
|
53
|
+
* schema(node, ctx) { return <File ...>...</File> },
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
renderer?: RendererFactory<TElement> | null
|
|
58
|
+
/**
|
|
59
|
+
* Called for each schema node in the AST walk.
|
|
60
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
61
|
+
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
62
|
+
*/
|
|
63
|
+
schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
|
|
64
|
+
/**
|
|
65
|
+
* Called for each operation node in the AST walk.
|
|
66
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
67
|
+
* plus `ctx.options` with the per-node resolved options (after exclude/include/override).
|
|
68
|
+
*/
|
|
69
|
+
operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
|
|
70
|
+
/**
|
|
71
|
+
* Called once after all operations have been walked.
|
|
72
|
+
* `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
|
|
73
|
+
* plus `ctx.options` with the plugin-level options for the batch call.
|
|
74
|
+
*/
|
|
75
|
+
operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
|
|
76
|
+
}
|
|
120
77
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return []
|
|
131
|
-
},
|
|
132
|
-
...generator,
|
|
133
|
-
}
|
|
78
|
+
/**
|
|
79
|
+
* Defines a generator. Returns the object as-is with correct `this` typings.
|
|
80
|
+
* `applyHookResult` handles renderer elements and `File[]` uniformly using
|
|
81
|
+
* the generator's declared `renderer` factory.
|
|
82
|
+
*/
|
|
83
|
+
export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
|
|
84
|
+
generator: Generator<TOptions, TElement>,
|
|
85
|
+
): Generator<TOptions, TElement> {
|
|
86
|
+
return generator
|
|
134
87
|
}
|
package/src/defineLogger.ts
CHANGED
|
@@ -4,13 +4,15 @@ import type { Logger, LoggerOptions, UserLogger } from './types.ts'
|
|
|
4
4
|
* Wraps a logger definition into a typed {@link Logger}.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
|
+
* ```ts
|
|
7
8
|
* export const myLogger = defineLogger({
|
|
8
9
|
* name: 'my-logger',
|
|
9
10
|
* install(context, options) {
|
|
10
|
-
* context.on('info', (message) => console.log('ℹ', message))
|
|
11
|
-
* context.on('error', (error) => console.error('✗', error.message))
|
|
11
|
+
* context.on('kubb:info', (message) => console.log('ℹ', message))
|
|
12
|
+
* context.on('kubb:error', (error) => console.error('✗', error.message))
|
|
12
13
|
* },
|
|
13
14
|
* })
|
|
15
|
+
* ```
|
|
14
16
|
*/
|
|
15
17
|
export function defineLogger<Options extends LoggerOptions = LoggerOptions>(logger: UserLogger<Options>): Logger<Options> {
|
|
16
18
|
return logger
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { KubbHooks } from './Kubb.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A middleware instance produced by calling a factory created with `defineMiddleware`.
|
|
5
|
+
* It declares event handlers under a `hooks` object which are registered on the
|
|
6
|
+
* shared emitter after all plugin hooks, so middleware handlers for any event
|
|
7
|
+
* always fire last.
|
|
8
|
+
*/
|
|
9
|
+
export type Middleware = {
|
|
10
|
+
/**
|
|
11
|
+
* Unique identifier for this middleware.
|
|
12
|
+
*/
|
|
13
|
+
name: string
|
|
14
|
+
/**
|
|
15
|
+
* Lifecycle event handlers for this middleware.
|
|
16
|
+
* Any event from the global `KubbHooks` map can be subscribed to here.
|
|
17
|
+
* Handlers are registered after all plugin handlers, so they always fire last.
|
|
18
|
+
*/
|
|
19
|
+
hooks: {
|
|
20
|
+
[K in keyof KubbHooks]?: (...args: KubbHooks[K]) => void | Promise<void>
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a middleware factory using the hook-style `hooks` API.
|
|
26
|
+
*
|
|
27
|
+
* Middleware handlers fire after all plugin handlers for any given event, making them ideal for post-processing, logging, and auditing.
|
|
28
|
+
* Per-build state (such as accumulators) belongs inside the factory closure so each `createKubb` invocation gets its own isolated instance.
|
|
29
|
+
*
|
|
30
|
+
* @note The factory can accept typed options. See examples for using options and per-build state patterns.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { defineMiddleware } from '@kubb/core'
|
|
35
|
+
*
|
|
36
|
+
* // Stateless middleware
|
|
37
|
+
* export const logMiddleware = defineMiddleware(() => ({
|
|
38
|
+
* name: 'log-middleware',
|
|
39
|
+
* hooks: {
|
|
40
|
+
* 'kubb:build:end'({ files }) {
|
|
41
|
+
* console.log(`Build complete with ${files.length} files`)
|
|
42
|
+
* },
|
|
43
|
+
* },
|
|
44
|
+
* }))
|
|
45
|
+
*
|
|
46
|
+
* // Middleware with options and per-build state
|
|
47
|
+
* export const prefixMiddleware = defineMiddleware((options: { prefix: string } = { prefix: '' }) => {
|
|
48
|
+
* const seen = new Set<string>()
|
|
49
|
+
* return {
|
|
50
|
+
* name: 'prefix-middleware',
|
|
51
|
+
* hooks: {
|
|
52
|
+
* 'kubb:plugin:end'({ plugin }) {
|
|
53
|
+
* seen.add(`${options.prefix}${plugin.name}`)
|
|
54
|
+
* },
|
|
55
|
+
* },
|
|
56
|
+
* }
|
|
57
|
+
* })
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function defineMiddleware<TOptions extends object = object>(factory: (options: TOptions) => Middleware): (options?: TOptions) => Middleware {
|
|
61
|
+
return (options) => factory(options ?? ({} as TOptions))
|
|
62
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { FileNode } from '@kubb/ast'
|
|
2
|
+
|
|
3
|
+
type PrintOptions = {
|
|
4
|
+
extname?: FileNode['extname']
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type Parser<TMeta extends object = any> = {
|
|
8
|
+
name: string
|
|
9
|
+
/**
|
|
10
|
+
* File extensions this parser handles.
|
|
11
|
+
* Use `undefined` to create a catch-all fallback parser.
|
|
12
|
+
*
|
|
13
|
+
* @example Handled extensions
|
|
14
|
+
* `['.ts', '.js']`
|
|
15
|
+
*/
|
|
16
|
+
extNames: Array<FileNode['extname']> | undefined
|
|
17
|
+
/**
|
|
18
|
+
* Convert a resolved file to a string.
|
|
19
|
+
*/
|
|
20
|
+
parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
|
|
25
|
+
*
|
|
26
|
+
* @note Call the returned factory with optional options to instantiate the parser.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { defineParser } from '@kubb/core'
|
|
31
|
+
*
|
|
32
|
+
* export const jsonParser = defineParser({
|
|
33
|
+
* name: 'json',
|
|
34
|
+
* extNames: ['.json'],
|
|
35
|
+
* parse(file) {
|
|
36
|
+
* const { extractStringsFromNodes } = await import('@kubb/ast')
|
|
37
|
+
* return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
|
|
38
|
+
* },
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
|
|
43
|
+
return parser
|
|
44
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { KubbHooks } from './Kubb.ts'
|
|
2
|
+
import type { KubbPluginSetupContext, PluginFactoryOptions } from './types.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A plugin object produced by `definePlugin`.
|
|
6
|
+
* Instead of flat lifecycle methods, it groups all handlers under a `hooks:` property
|
|
7
|
+
* (matching Astro's integration naming convention).
|
|
8
|
+
*
|
|
9
|
+
* @template TFactory - The plugin's `PluginFactoryOptions` type.
|
|
10
|
+
*/
|
|
11
|
+
export type Plugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions> = {
|
|
12
|
+
/**
|
|
13
|
+
* Unique name for the plugin, following the same naming convention as `createPlugin`.
|
|
14
|
+
*/
|
|
15
|
+
name: string
|
|
16
|
+
/**
|
|
17
|
+
* Plugins that must be registered before this plugin executes.
|
|
18
|
+
* An error is thrown at startup when any listed dependency is missing.
|
|
19
|
+
*/
|
|
20
|
+
dependencies?: Array<string>
|
|
21
|
+
/**
|
|
22
|
+
* Controls the execution order of this plugin relative to others.
|
|
23
|
+
*
|
|
24
|
+
* - `'pre'` — runs before all normal plugins.
|
|
25
|
+
* - `'post'` — runs after all normal plugins.
|
|
26
|
+
* - `undefined` (default) — runs in declaration order among normal plugins.
|
|
27
|
+
*
|
|
28
|
+
* Dependency constraints always take precedence over `enforce`.
|
|
29
|
+
*/
|
|
30
|
+
enforce?: 'pre' | 'post'
|
|
31
|
+
/**
|
|
32
|
+
* The options passed by the user when calling the plugin factory.
|
|
33
|
+
*/
|
|
34
|
+
options?: TFactory['options']
|
|
35
|
+
/**
|
|
36
|
+
* Lifecycle event handlers for this plugin.
|
|
37
|
+
* Any event from the global `KubbHooks` map can be subscribed to here.
|
|
38
|
+
*/
|
|
39
|
+
hooks: {
|
|
40
|
+
[K in Exclude<keyof KubbHooks, 'kubb:plugin:setup'>]?: (...args: KubbHooks[K]) => void | Promise<void>
|
|
41
|
+
} & {
|
|
42
|
+
'kubb:plugin:setup'?(ctx: KubbPluginSetupContext<TFactory>): void | Promise<void>
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` when `plugin` is a hook-style plugin created with `definePlugin`.
|
|
48
|
+
*
|
|
49
|
+
* Used by `PluginDriver` to distinguish hook-style plugins from legacy `createPlugin` plugins
|
|
50
|
+
* so it can normalize them and register their handlers on the `AsyncEventEmitter`.
|
|
51
|
+
*/
|
|
52
|
+
export function isPlugin(plugin: unknown): plugin is Plugin {
|
|
53
|
+
return typeof plugin === 'object' && plugin !== null && 'hooks' in plugin
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Wraps a factory function and returns a typed `Plugin` with lifecycle handlers grouped under `hooks`.
|
|
58
|
+
*
|
|
59
|
+
* Handlers live in a single `hooks` object (inspired by Astro integrations).
|
|
60
|
+
* All lifecycle events from `KubbHooks` are available for subscription.
|
|
61
|
+
*
|
|
62
|
+
* @note For real plugins, use a `PluginFactoryOptions` type parameter to get type-safe context in `kubb:plugin:setup`.
|
|
63
|
+
* Plugin names should follow the convention `plugin-<feature>` (e.g., `plugin-react-query`, `plugin-zod`).
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { definePlugin } from '@kubb/core'
|
|
68
|
+
*
|
|
69
|
+
* export const pluginTs = definePlugin((options: { prefix?: string } = {}) => ({
|
|
70
|
+
* name: 'plugin-ts',
|
|
71
|
+
* hooks: {
|
|
72
|
+
* 'kubb:plugin:setup'(ctx) {
|
|
73
|
+
* ctx.setResolver(resolverTs)
|
|
74
|
+
* },
|
|
75
|
+
* },
|
|
76
|
+
* }))
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export function definePlugin<TFactory extends PluginFactoryOptions = PluginFactoryOptions>(
|
|
80
|
+
factory: (options: TFactory['options']) => Plugin<TFactory>,
|
|
81
|
+
): (options?: TFactory['options']) => Plugin<TFactory> {
|
|
82
|
+
return (options) => factory(options ?? ({} as TFactory['options']))
|
|
83
|
+
}
|