@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.
Files changed (71) hide show
  1. package/README.md +3 -2
  2. package/dist/PluginDriver-6E8zd8MW.cjs +1086 -0
  3. package/dist/PluginDriver-6E8zd8MW.cjs.map +1 -0
  4. package/dist/PluginDriver-D6wQFD4r.js +983 -0
  5. package/dist/PluginDriver-D6wQFD4r.js.map +1 -0
  6. package/dist/index.cjs +1013 -1829
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +279 -265
  9. package/dist/index.js +1003 -1799
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +138 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +74 -0
  14. package/dist/mocks.js +133 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-DfEv9d_c.d.ts +1721 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +133 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +154 -101
  21. package/src/PluginDriver.ts +418 -0
  22. package/src/constants.ts +43 -47
  23. package/src/createAdapter.ts +25 -0
  24. package/src/createKubb.ts +614 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +58 -0
  27. package/src/defineGenerator.ts +88 -100
  28. package/src/defineLogger.ts +13 -3
  29. package/src/defineParser.ts +45 -0
  30. package/src/definePlugin.ts +68 -7
  31. package/src/defineResolver.ts +521 -0
  32. package/src/devtools.ts +14 -14
  33. package/src/index.ts +12 -17
  34. package/src/mocks.ts +171 -0
  35. package/src/renderNode.ts +35 -0
  36. package/src/storages/fsStorage.ts +40 -11
  37. package/src/storages/memoryStorage.ts +4 -3
  38. package/src/types.ts +575 -205
  39. package/src/utils/TreeNode.ts +47 -9
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/getBarrelFiles.ts +94 -16
  42. package/src/utils/isInputPath.ts +10 -0
  43. package/src/utils/packageJSON.ts +99 -0
  44. package/dist/PluginManager-vZodFEMe.d.ts +0 -1056
  45. package/dist/chunk-ByKO4r7w.cjs +0 -38
  46. package/dist/hooks.cjs +0 -60
  47. package/dist/hooks.cjs.map +0 -1
  48. package/dist/hooks.d.ts +0 -56
  49. package/dist/hooks.js +0 -56
  50. package/dist/hooks.js.map +0 -1
  51. package/src/BarrelManager.ts +0 -74
  52. package/src/PackageManager.ts +0 -180
  53. package/src/PluginManager.ts +0 -667
  54. package/src/PromiseManager.ts +0 -40
  55. package/src/build.ts +0 -419
  56. package/src/config.ts +0 -56
  57. package/src/defineAdapter.ts +0 -22
  58. package/src/defineStorage.ts +0 -56
  59. package/src/errors.ts +0 -1
  60. package/src/hooks/index.ts +0 -4
  61. package/src/hooks/useKubb.ts +0 -46
  62. package/src/hooks/useMode.ts +0 -11
  63. package/src/hooks/usePlugin.ts +0 -11
  64. package/src/hooks/usePluginManager.ts +0 -11
  65. package/src/utils/FunctionParams.ts +0 -155
  66. package/src/utils/executeStrategies.ts +0 -81
  67. package/src/utils/formatters.ts +0 -56
  68. package/src/utils/getConfigs.ts +0 -30
  69. package/src/utils/getPlugins.ts +0 -23
  70. package/src/utils/linters.ts +0 -25
  71. package/src/utils/resolveOptions.ts +0 -93
@@ -0,0 +1,418 @@
1
+ import { extname, resolve } from 'node:path'
2
+ import type { AsyncEventEmitter } from '@internals/utils'
3
+ import type { FileNode, InputNode, OperationNode, SchemaNode } from '@kubb/ast'
4
+ import { createFile } from '@kubb/ast'
5
+ import { DEFAULT_STUDIO_URL } from './constants.ts'
6
+ import type { Generator } from './defineGenerator.ts'
7
+ import type { Plugin } from './definePlugin.ts'
8
+ import { defineResolver } from './defineResolver.ts'
9
+ import { openInStudio as openInStudioFn } from './devtools.ts'
10
+ import { FileManager } from './FileManager.ts'
11
+ import { applyHookResult } from './renderNode.ts'
12
+
13
+ import type {
14
+ Adapter,
15
+ Config,
16
+ DevtoolsOptions,
17
+ GeneratorContext,
18
+ KubbHooks,
19
+ KubbPluginSetupContext,
20
+ NormalizedPlugin,
21
+ PluginFactoryOptions,
22
+ Resolver,
23
+ } from './types.ts'
24
+
25
+ // inspired by: https://github.com/rollup/rollup/blob/master/src/utils/PluginDriver.ts#
26
+
27
+ type Options = {
28
+ hooks: AsyncEventEmitter<KubbHooks>
29
+ }
30
+
31
+ export class PluginDriver {
32
+ readonly config: Config
33
+ readonly options: Options
34
+
35
+ /**
36
+ * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * PluginDriver.getMode('src/gen/types.ts') // 'single'
41
+ * PluginDriver.getMode('src/gen/types') // 'split'
42
+ * ```
43
+ */
44
+ static getMode(fileOrFolder: string | undefined | null): 'single' | 'split' {
45
+ if (!fileOrFolder) {
46
+ return 'split'
47
+ }
48
+ return extname(fileOrFolder) ? 'single' : 'split'
49
+ }
50
+
51
+ /**
52
+ * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
53
+ * the build pipeline after the adapter's `parse()` resolves.
54
+ */
55
+ inputNode: InputNode | undefined = undefined
56
+ adapter: Adapter | undefined = undefined
57
+ #studioIsOpen = false
58
+
59
+ /**
60
+ * Central file store for all generated files.
61
+ * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
62
+ * add files; this property gives direct read/write access when needed.
63
+ */
64
+ readonly fileManager = new FileManager()
65
+
66
+ readonly plugins = new Map<string, NormalizedPlugin>()
67
+
68
+ /**
69
+ * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
70
+ * Used by the build loop to decide whether to emit generator events for a given plugin.
71
+ */
72
+ readonly #pluginsWithEventGenerators = new Set<string>()
73
+ readonly #resolvers = new Map<string, Resolver>()
74
+ readonly #defaultResolvers = new Map<string, Resolver>()
75
+ readonly #hookListeners = new Map<keyof KubbHooks, Set<(...args: never[]) => void | Promise<void>>>()
76
+
77
+ constructor(config: Config, options: Options) {
78
+ this.config = config
79
+ this.options = options
80
+ config.plugins
81
+ .map((rawPlugin) => this.#normalizePlugin(rawPlugin as Plugin))
82
+ .filter((plugin) => {
83
+ if (typeof plugin.apply === 'function') {
84
+ return plugin.apply(config)
85
+ }
86
+ return true
87
+ })
88
+ .sort((a, b) => {
89
+ if (b.dependencies?.includes(a.name)) return -1
90
+ if (a.dependencies?.includes(b.name)) return 1
91
+ return 0
92
+ })
93
+ .forEach((plugin) => {
94
+ this.plugins.set(plugin.name, plugin)
95
+ })
96
+ }
97
+
98
+ get hooks() {
99
+ return this.options.hooks
100
+ }
101
+
102
+ /**
103
+ * Creates an `NormalizedPlugin` from a hook-style plugin and registers
104
+ * its lifecycle handlers on the `AsyncEventEmitter`.
105
+ */
106
+ #normalizePlugin(hookPlugin: Plugin): NormalizedPlugin {
107
+ const normalizedPlugin = {
108
+ name: hookPlugin.name,
109
+ dependencies: hookPlugin.dependencies,
110
+ options: { output: { path: '.' }, exclude: [], override: [] },
111
+ } as unknown as NormalizedPlugin
112
+
113
+ this.registerPluginHooks(hookPlugin, normalizedPlugin)
114
+ return normalizedPlugin
115
+ }
116
+
117
+ /**
118
+ * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
119
+ *
120
+ * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
121
+ * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
122
+ * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
123
+ *
124
+ * All other hooks are iterated and registered directly as pass-through listeners.
125
+ * Any event key present in the global `KubbHooks` interface can be subscribed to.
126
+ *
127
+ * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
128
+ * the plugin lifecycle without modifying plugin behavior.
129
+ *
130
+ * @internal
131
+ */
132
+ registerPluginHooks(hookPlugin: Plugin, normalizedPlugin: NormalizedPlugin): void {
133
+ const { hooks } = hookPlugin
134
+
135
+ // kubb:plugin:setup gets special treatment: the globally emitted context is wrapped with
136
+ // plugin-specific implementations so that addGenerator / setResolver / etc. target
137
+ // this plugin's normalizedPlugin entry rather than being no-ops.
138
+ if (hooks['kubb:plugin:setup']) {
139
+ const setupHandler = (globalCtx: KubbPluginSetupContext) => {
140
+ const pluginCtx: KubbPluginSetupContext = {
141
+ ...globalCtx,
142
+ options: hookPlugin.options ?? {},
143
+ addGenerator: (gen) => {
144
+ this.registerGenerator(normalizedPlugin.name, gen)
145
+ },
146
+ setResolver: (resolver) => {
147
+ this.setPluginResolver(normalizedPlugin.name, resolver)
148
+ },
149
+ setTransformer: (visitor) => {
150
+ normalizedPlugin.transformer = visitor
151
+ },
152
+ setRenderer: (renderer) => {
153
+ normalizedPlugin.renderer = renderer
154
+ },
155
+ setOptions: (opts) => {
156
+ normalizedPlugin.options = { ...normalizedPlugin.options, ...opts }
157
+ },
158
+ injectFile: ({ sources = [], ...rest }) => {
159
+ this.fileManager.add(createFile({ imports: [], exports: [], sources, ...rest }))
160
+ },
161
+ }
162
+ return hooks['kubb:plugin:setup']!(pluginCtx)
163
+ }
164
+
165
+ this.hooks.on('kubb:plugin:setup', setupHandler)
166
+ this.#trackHookListener('kubb:plugin:setup', setupHandler as (...args: never[]) => void | Promise<void>)
167
+ }
168
+
169
+ // All other hooks are registered as direct pass-through listeners on the shared emitter.
170
+ for (const [event, handler] of Object.entries(hooks) as Array<[keyof KubbHooks, ((...args: never[]) => void | Promise<void>) | undefined]>) {
171
+ if (event === 'kubb:plugin:setup' || !handler) continue
172
+
173
+ this.hooks.on(event, handler as never)
174
+ this.#trackHookListener(event, handler as (...args: never[]) => void | Promise<void>)
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
180
+ * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
181
+ *
182
+ * Call this once from `safeBuild` before the plugin execution loop begins.
183
+ */
184
+ async emitSetupHooks(): Promise<void> {
185
+ const noop = () => {}
186
+ await this.hooks.emit('kubb:plugin:setup', {
187
+ config: this.config,
188
+ options: {},
189
+ addGenerator: noop,
190
+ setResolver: noop,
191
+ setTransformer: noop,
192
+ setRenderer: noop,
193
+ setOptions: noop,
194
+ injectFile: noop,
195
+ updateConfig: noop,
196
+ })
197
+ }
198
+
199
+ /**
200
+ * Registers a generator for the given plugin on the shared event emitter.
201
+ *
202
+ * The generator's `schema`, `operation`, and `operations` methods are registered as
203
+ * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
204
+ * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
205
+ * so that generators from different plugins do not cross-fire.
206
+ *
207
+ * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
208
+ * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
209
+ * declares a renderer.
210
+ *
211
+ * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
212
+ */
213
+ registerGenerator(pluginName: string, gen: Generator): void {
214
+ const resolveRenderer = () => {
215
+ const plugin = this.plugins.get(pluginName)
216
+ return gen.renderer === null ? undefined : (gen.renderer ?? plugin?.renderer ?? this.config.renderer)
217
+ }
218
+
219
+ if (gen.schema) {
220
+ const schemaHandler = async (node: SchemaNode, ctx: GeneratorContext) => {
221
+ if (ctx.plugin.name !== pluginName) return
222
+ const result = await gen.schema!(node, ctx)
223
+ await applyHookResult(result, this, resolveRenderer())
224
+ }
225
+
226
+ this.hooks.on('kubb:generate:schema', schemaHandler)
227
+ this.#trackHookListener('kubb:generate:schema', schemaHandler as (...args: never[]) => void | Promise<void>)
228
+ }
229
+
230
+ if (gen.operation) {
231
+ const operationHandler = async (node: OperationNode, ctx: GeneratorContext) => {
232
+ if (ctx.plugin.name !== pluginName) return
233
+ const result = await gen.operation!(node, ctx)
234
+ await applyHookResult(result, this, resolveRenderer())
235
+ }
236
+
237
+ this.hooks.on('kubb:generate:operation', operationHandler)
238
+ this.#trackHookListener('kubb:generate:operation', operationHandler as (...args: never[]) => void | Promise<void>)
239
+ }
240
+
241
+ if (gen.operations) {
242
+ const operationsHandler = async (nodes: Array<OperationNode>, ctx: GeneratorContext) => {
243
+ if (ctx.plugin.name !== pluginName) return
244
+ const result = await gen.operations!(nodes, ctx)
245
+ await applyHookResult(result, this, resolveRenderer())
246
+ }
247
+
248
+ this.hooks.on('kubb:generate:operations', operationsHandler)
249
+ this.#trackHookListener('kubb:generate:operations', operationsHandler as (...args: never[]) => void | Promise<void>)
250
+ }
251
+
252
+ this.#pluginsWithEventGenerators.add(pluginName)
253
+ }
254
+
255
+ /**
256
+ * Returns `true` when at least one generator was registered for the given plugin
257
+ * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
258
+ *
259
+ * Used by the build loop to decide whether to walk the AST and emit generator events
260
+ * for a plugin that has no static `plugin.generators`.
261
+ */
262
+ hasRegisteredGenerators(pluginName: string): boolean {
263
+ return this.#pluginsWithEventGenerators.has(pluginName)
264
+ }
265
+
266
+ /**
267
+ * Unregisters all plugin lifecycle listeners from the shared event emitter.
268
+ * Called at the end of a build to prevent listener leaks across repeated builds.
269
+ *
270
+ * @internal
271
+ */
272
+ dispose(): void {
273
+ for (const [event, handlers] of this.#hookListeners) {
274
+ for (const handler of handlers) {
275
+ this.hooks.off(event, handler as never)
276
+ }
277
+ }
278
+ this.#hookListeners.clear()
279
+ this.#pluginsWithEventGenerators.clear()
280
+ }
281
+
282
+ #trackHookListener(event: keyof KubbHooks, handler: (...args: never[]) => void | Promise<void>): void {
283
+ let handlers = this.#hookListeners.get(event)
284
+ if (!handlers) {
285
+ handlers = new Set()
286
+ this.#hookListeners.set(event, handlers)
287
+ }
288
+ handlers.add(handler)
289
+ }
290
+
291
+ #createDefaultResolver(pluginName: string): Resolver {
292
+ const existingResolver = this.#defaultResolvers.get(pluginName)
293
+ if (existingResolver) {
294
+ return existingResolver
295
+ }
296
+
297
+ const resolver = defineResolver<PluginFactoryOptions>((_ctx) => ({
298
+ name: 'default',
299
+ pluginName,
300
+ }))
301
+ this.#defaultResolvers.set(pluginName, resolver)
302
+ return resolver
303
+ }
304
+
305
+ /**
306
+ * Merges `partial` with the plugin's default resolver and stores the result.
307
+ * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
308
+ * get the up-to-date resolver without going through `getResolver()`.
309
+ */
310
+ setPluginResolver(pluginName: string, partial: Partial<Resolver>): void {
311
+ const defaultResolver = this.#createDefaultResolver(pluginName)
312
+ const merged = { ...defaultResolver, ...partial }
313
+ this.#resolvers.set(pluginName, merged)
314
+ const plugin = this.plugins.get(pluginName)
315
+ if (plugin) {
316
+ plugin.resolver = merged
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Returns the resolver for the given plugin.
322
+ *
323
+ * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
324
+ * plugin → lazily created default resolver (identity name, no path transforms).
325
+ */
326
+ getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver']
327
+ getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver
328
+ getResolver(pluginName: string): Resolver {
329
+ return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName)
330
+ }
331
+
332
+ getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown> {
333
+ const driver = this
334
+
335
+ const baseContext = {
336
+ config: driver.config,
337
+ get root(): string {
338
+ return resolve(driver.config.root, driver.config.output.path)
339
+ },
340
+ getMode(output: { path: string }): 'single' | 'split' {
341
+ return PluginDriver.getMode(resolve(driver.config.root, driver.config.output.path, output.path))
342
+ },
343
+ hooks: driver.hooks,
344
+ plugin,
345
+ getPlugin: driver.getPlugin.bind(driver),
346
+ requirePlugin: driver.requirePlugin.bind(driver),
347
+ getResolver: driver.getResolver.bind(driver),
348
+ driver,
349
+ addFile: async (...files: Array<FileNode>) => {
350
+ driver.fileManager.add(...files)
351
+ },
352
+ upsertFile: async (...files: Array<FileNode>) => {
353
+ driver.fileManager.upsert(...files)
354
+ },
355
+ get inputNode(): InputNode | undefined {
356
+ return driver.inputNode
357
+ },
358
+ get adapter(): Adapter | undefined {
359
+ return driver.adapter
360
+ },
361
+ get resolver() {
362
+ return driver.getResolver(plugin.name)
363
+ },
364
+ get transformer() {
365
+ return plugin.transformer
366
+ },
367
+ warn(message: string) {
368
+ driver.hooks.emit('kubb:warn', message)
369
+ },
370
+ error(error: string | Error) {
371
+ driver.hooks.emit('kubb:error', typeof error === 'string' ? new Error(error) : error)
372
+ },
373
+ info(message: string) {
374
+ driver.hooks.emit('kubb:info', message)
375
+ },
376
+ openInStudio(options?: DevtoolsOptions) {
377
+ if (!driver.config.devtools || driver.#studioIsOpen) {
378
+ return
379
+ }
380
+
381
+ if (typeof driver.config.devtools !== 'object') {
382
+ throw new Error('Devtools must be an object')
383
+ }
384
+
385
+ if (!driver.inputNode || !driver.adapter) {
386
+ throw new Error('adapter is not defined, make sure you have set the parser in kubb.config.ts')
387
+ }
388
+
389
+ driver.#studioIsOpen = true
390
+
391
+ const studioUrl = driver.config.devtools?.studioUrl ?? DEFAULT_STUDIO_URL
392
+
393
+ return openInStudioFn(driver.inputNode, studioUrl, options)
394
+ },
395
+ } as unknown as GeneratorContext<TOptions>
396
+
397
+ return baseContext
398
+ }
399
+
400
+ getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
401
+ getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined
402
+ getPlugin(pluginName: string): Plugin | undefined {
403
+ return this.plugins.get(pluginName)
404
+ }
405
+
406
+ /**
407
+ * Like `getPlugin` but throws a descriptive error when the plugin is not found.
408
+ */
409
+ requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>
410
+ requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>
411
+ requirePlugin(pluginName: string): Plugin {
412
+ const plugin = this.plugins.get(pluginName)
413
+ if (!plugin) {
414
+ throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`)
415
+ }
416
+ return plugin
417
+ }
418
+ }
package/src/constants.ts CHANGED
@@ -1,21 +1,53 @@
1
- import type { KubbFile } from '@kubb/fabric-core/types'
1
+ import type { FileNode } from '@kubb/ast'
2
2
 
3
+ /**
4
+ * Base URL for the Kubb Studio web app.
5
+ */
3
6
  export const DEFAULT_STUDIO_URL = 'https://studio.kubb.dev' as const
4
7
 
5
- export const CORE_PLUGIN_NAME = 'core' as const
6
-
7
- export const DEFAULT_MAX_LISTENERS = 100
8
-
8
+ /**
9
+ * Default number of plugins that may run concurrently during a build.
10
+ */
9
11
  export const DEFAULT_CONCURRENCY = 15
10
12
 
11
- export const BARREL_FILENAME = 'index.ts' as const
12
-
13
+ /**
14
+ * Maximum number of files processed in parallel by FileProcessor.
15
+ */
16
+ export const PARALLEL_CONCURRENCY_LIMIT = 100
17
+
18
+ /**
19
+ * Basename (without extension) of generated barrel files.
20
+ *
21
+ * Used to detect whether a path already points at a barrel so the generator
22
+ * avoids re-creating one on top of it.
23
+ */
24
+ export const BARREL_BASENAME = 'index' as const
25
+
26
+ /**
27
+ * File name used for generated barrel (index) files.
28
+ */
29
+ export const BARREL_FILENAME = `${BARREL_BASENAME}.ts` as const
30
+
31
+ /**
32
+ * Default banner style written at the top of every generated file.
33
+ */
13
34
  export const DEFAULT_BANNER = 'simple' as const
14
35
 
15
- export const DEFAULT_EXTENSION: Record<KubbFile.Extname, KubbFile.Extname | ''> = { '.ts': '.ts' }
16
-
17
- export const PATH_SEPARATORS = ['/', '\\'] as const
18
-
36
+ /**
37
+ * Default file-extension mapping used when no explicit mapping is configured.
38
+ */
39
+ export const DEFAULT_EXTENSION: Record<FileNode['extname'], FileNode['extname'] | ''> = { '.ts': '.ts' }
40
+
41
+ /**
42
+ * Characters recognized as path separators on both POSIX and Windows.
43
+ */
44
+ export const PATH_SEPARATORS = new Set(['/', '\\'] as const)
45
+
46
+ /**
47
+ * Numeric log-level thresholds used internally to compare verbosity.
48
+ *
49
+ * Higher numbers are more verbose.
50
+ */
19
51
  export const logLevel = {
20
52
  silent: Number.NEGATIVE_INFINITY,
21
53
  error: 0,
@@ -24,39 +56,3 @@ export const logLevel = {
24
56
  verbose: 4,
25
57
  debug: 5,
26
58
  } as const
27
-
28
- export const linters = {
29
- eslint: {
30
- command: 'eslint',
31
- args: (outputPath: string) => [outputPath, '--fix'],
32
- errorMessage: 'Eslint not found',
33
- },
34
- biome: {
35
- command: 'biome',
36
- args: (outputPath: string) => ['lint', '--fix', outputPath],
37
- errorMessage: 'Biome not found',
38
- },
39
- oxlint: {
40
- command: 'oxlint',
41
- args: (outputPath: string) => ['--fix', outputPath],
42
- errorMessage: 'Oxlint not found',
43
- },
44
- } as const
45
-
46
- export const formatters = {
47
- prettier: {
48
- command: 'prettier',
49
- args: (outputPath: string) => ['--ignore-unknown', '--write', outputPath],
50
- errorMessage: 'Prettier not found',
51
- },
52
- biome: {
53
- command: 'biome',
54
- args: (outputPath: string) => ['format', '--write', outputPath],
55
- errorMessage: 'Biome not found',
56
- },
57
- oxfmt: {
58
- command: 'oxfmt',
59
- args: (outputPath: string) => [outputPath],
60
- errorMessage: 'Oxfmt not found',
61
- },
62
- } as const
@@ -0,0 +1,25 @@
1
+ import type { Adapter, AdapterFactoryOptions } from './types.ts'
2
+
3
+ /**
4
+ * Builder type for an {@link Adapter} — takes options and returns the adapter instance.
5
+ */
6
+ type AdapterBuilder<T extends AdapterFactoryOptions> = (options: T['options']) => Adapter<T>
7
+
8
+ /**
9
+ * Creates an adapter factory. Call the returned function with optional options to get the adapter instance.
10
+ *
11
+ * @example
12
+ * export const myAdapter = createAdapter<MyAdapter>((options) => {
13
+ * return {
14
+ * name: 'my-adapter',
15
+ * options,
16
+ * async parse(source) { ... },
17
+ * }
18
+ * })
19
+ *
20
+ * // instantiate
21
+ * const adapter = myAdapter({ validate: true })
22
+ */
23
+ export function createAdapter<T extends AdapterFactoryOptions = AdapterFactoryOptions>(build: AdapterBuilder<T>): (options?: T['options']) => Adapter<T> {
24
+ return (options) => build(options ?? ({} as T['options']))
25
+ }