@kubb/core 5.0.0-beta.2 → 5.0.0-beta.21

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 (45) hide show
  1. package/README.md +8 -38
  2. package/dist/KubbDriver-BBRa5CH2.cjs +2231 -0
  3. package/dist/KubbDriver-BBRa5CH2.cjs.map +1 -0
  4. package/dist/KubbDriver-Cq1isv2P.js +2110 -0
  5. package/dist/KubbDriver-Cq1isv2P.js.map +1 -0
  6. package/dist/{types-CC09VtBt.d.ts → createKubb-CYrw_xaR.d.ts} +1414 -1255
  7. package/dist/index.cjs +221 -1074
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +2 -185
  10. package/dist/index.js +211 -1068
  11. package/dist/index.js.map +1 -1
  12. package/dist/mocks.cjs +30 -21
  13. package/dist/mocks.cjs.map +1 -1
  14. package/dist/mocks.d.ts +5 -5
  15. package/dist/mocks.js +29 -20
  16. package/dist/mocks.js.map +1 -1
  17. package/package.json +6 -18
  18. package/src/FileManager.ts +75 -58
  19. package/src/FileProcessor.ts +48 -38
  20. package/src/KubbDriver.ts +915 -0
  21. package/src/constants.ts +11 -6
  22. package/src/createAdapter.ts +84 -1
  23. package/src/createKubb.ts +1022 -485
  24. package/src/createRenderer.ts +33 -22
  25. package/src/defineGenerator.ts +96 -7
  26. package/src/defineLogger.ts +42 -3
  27. package/src/defineMiddleware.ts +1 -1
  28. package/src/defineParser.ts +1 -1
  29. package/src/definePlugin.ts +304 -8
  30. package/src/defineResolver.ts +271 -150
  31. package/src/devtools.ts +8 -1
  32. package/src/index.ts +2 -2
  33. package/src/mocks.ts +11 -14
  34. package/src/storages/fsStorage.ts +13 -37
  35. package/src/types.ts +39 -1292
  36. package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
  37. package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
  38. package/dist/PluginDriver-DV3p2Hky.js +0 -945
  39. package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
  40. package/src/Kubb.ts +0 -300
  41. package/src/PluginDriver.ts +0 -424
  42. package/src/renderNode.ts +0 -35
  43. package/src/utils/diagnostics.ts +0 -18
  44. package/src/utils/isInputPath.ts +0 -10
  45. package/src/utils/packageJSON.ts +0 -99
@@ -1,424 +0,0 @@
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
- function enforceOrder(enforce: 'pre' | 'post' | undefined): number {
32
- return enforce === 'pre' ? -1 : enforce === 'post' ? 1 : 0
33
- }
34
-
35
- export class PluginDriver {
36
- readonly config: Config
37
- readonly options: Options
38
-
39
- /**
40
- * Returns `'single'` when `fileOrFolder` has a file extension, `'split'` otherwise.
41
- *
42
- * @example
43
- * ```ts
44
- * PluginDriver.getMode('src/gen/types.ts') // 'single'
45
- * PluginDriver.getMode('src/gen/types') // 'split'
46
- * ```
47
- */
48
- static getMode(fileOrFolder: string | undefined | null): 'single' | 'split' {
49
- if (!fileOrFolder) {
50
- return 'split'
51
- }
52
- return extname(fileOrFolder) ? 'single' : 'split'
53
- }
54
-
55
- /**
56
- * The universal `@kubb/ast` `InputNode` produced by the adapter, set by
57
- * the build pipeline after the adapter's `parse()` resolves.
58
- */
59
- inputNode: InputNode | undefined = undefined
60
- adapter: Adapter | undefined = undefined
61
- #studioIsOpen = false
62
-
63
- /**
64
- * Central file store for all generated files.
65
- * Plugins should use `this.addFile()` / `this.upsertFile()` (via their context) to
66
- * add files; this property gives direct read/write access when needed.
67
- */
68
- readonly fileManager = new FileManager()
69
-
70
- readonly plugins = new Map<string, NormalizedPlugin>()
71
-
72
- /**
73
- * Tracks which plugins have generators registered via `addGenerator()` (event-based path).
74
- * Used by the build loop to decide whether to emit generator events for a given plugin.
75
- */
76
- readonly #pluginsWithEventGenerators = new Set<string>()
77
- readonly #resolvers = new Map<string, Resolver>()
78
- readonly #defaultResolvers = new Map<string, Resolver>()
79
- readonly #hookListeners = new Map<keyof KubbHooks, Set<(...args: never[]) => void | Promise<void>>>()
80
-
81
- constructor(config: Config, options: Options) {
82
- this.config = config
83
- this.options = options
84
- config.plugins
85
- .map((rawPlugin) => this.#normalizePlugin(rawPlugin as Plugin))
86
- .filter((plugin) => {
87
- if (typeof plugin.apply === 'function') {
88
- return plugin.apply(config)
89
- }
90
- return true
91
- })
92
- .sort((a, b) => {
93
- if (b.dependencies?.includes(a.name)) return -1
94
- if (a.dependencies?.includes(b.name)) return 1
95
- // enforce: 'pre' plugins run first, 'post' plugins run last
96
- return enforceOrder(a.enforce) - enforceOrder(b.enforce)
97
- })
98
- .forEach((plugin) => {
99
- this.plugins.set(plugin.name, plugin)
100
- })
101
- }
102
-
103
- get hooks() {
104
- return this.options.hooks
105
- }
106
-
107
- /**
108
- * Creates an `NormalizedPlugin` from a hook-style plugin and registers
109
- * its lifecycle handlers on the `AsyncEventEmitter`.
110
- */
111
- #normalizePlugin(hookPlugin: Plugin): NormalizedPlugin {
112
- const normalizedPlugin = {
113
- name: hookPlugin.name,
114
- dependencies: hookPlugin.dependencies,
115
- enforce: hookPlugin.enforce,
116
- options: { output: { path: '.' }, exclude: [], override: [] },
117
- } as unknown as NormalizedPlugin
118
-
119
- this.registerPluginHooks(hookPlugin, normalizedPlugin)
120
- return normalizedPlugin
121
- }
122
-
123
- /**
124
- * Registers a hook-style plugin's lifecycle handlers on the shared `AsyncEventEmitter`.
125
- *
126
- * For `kubb:plugin:setup`, the registered listener wraps the globally emitted context with a
127
- * plugin-specific one so that `addGenerator`, `setResolver`, `setTransformer`, and
128
- * `setRenderer` all target the correct `normalizedPlugin` entry in the plugins map.
129
- *
130
- * All other hooks are iterated and registered directly as pass-through listeners.
131
- * Any event key present in the global `KubbHooks` interface can be subscribed to.
132
- *
133
- * External tooling can subscribe to any of these events via `hooks.on(...)` to observe
134
- * the plugin lifecycle without modifying plugin behavior.
135
- *
136
- * @internal
137
- */
138
- registerPluginHooks(hookPlugin: Plugin, normalizedPlugin: NormalizedPlugin): void {
139
- const { hooks } = hookPlugin
140
-
141
- // kubb:plugin:setup gets special treatment: the globally emitted context is wrapped with
142
- // plugin-specific implementations so that addGenerator / setResolver / etc. target
143
- // this plugin's normalizedPlugin entry rather than being no-ops.
144
- if (hooks['kubb:plugin:setup']) {
145
- const setupHandler = (globalCtx: KubbPluginSetupContext) => {
146
- const pluginCtx: KubbPluginSetupContext = {
147
- ...globalCtx,
148
- options: hookPlugin.options ?? {},
149
- addGenerator: (gen) => {
150
- this.registerGenerator(normalizedPlugin.name, gen)
151
- },
152
- setResolver: (resolver) => {
153
- this.setPluginResolver(normalizedPlugin.name, resolver)
154
- },
155
- setTransformer: (visitor) => {
156
- normalizedPlugin.transformer = visitor
157
- },
158
- setRenderer: (renderer) => {
159
- normalizedPlugin.renderer = renderer
160
- },
161
- setOptions: (opts) => {
162
- normalizedPlugin.options = { ...normalizedPlugin.options, ...opts }
163
- },
164
- injectFile: (userFileNode) => {
165
- this.fileManager.add(createFile(userFileNode))
166
- },
167
- }
168
- return hooks['kubb:plugin:setup']!(pluginCtx)
169
- }
170
-
171
- this.hooks.on('kubb:plugin:setup', setupHandler)
172
- this.#trackHookListener('kubb:plugin:setup', setupHandler as (...args: never[]) => void | Promise<void>)
173
- }
174
-
175
- // All other hooks are registered as direct pass-through listeners on the shared emitter.
176
- for (const [event, handler] of Object.entries(hooks) as Array<[keyof KubbHooks, ((...args: never[]) => void | Promise<void>) | undefined]>) {
177
- if (event === 'kubb:plugin:setup' || !handler) continue
178
-
179
- this.hooks.on(event, handler as never)
180
- this.#trackHookListener(event, handler as (...args: never[]) => void | Promise<void>)
181
- }
182
- }
183
-
184
- /**
185
- * Emits the `kubb:plugin:setup` event so that all registered hook-style plugin listeners
186
- * can configure generators, resolvers, transformers and renderers before `buildStart` runs.
187
- *
188
- * Call this once from `safeBuild` before the plugin execution loop begins.
189
- */
190
- async emitSetupHooks(): Promise<void> {
191
- const noop = () => {}
192
- await this.hooks.emit('kubb:plugin:setup', {
193
- config: this.config,
194
- options: {},
195
- addGenerator: noop,
196
- setResolver: noop,
197
- setTransformer: noop,
198
- setRenderer: noop,
199
- setOptions: noop,
200
- injectFile: noop,
201
- updateConfig: noop,
202
- })
203
- }
204
-
205
- /**
206
- * Registers a generator for the given plugin on the shared event emitter.
207
- *
208
- * The generator's `schema`, `operation`, and `operations` methods are registered as
209
- * listeners on `kubb:generate:schema`, `kubb:generate:operation`, and `kubb:generate:operations`
210
- * respectively. Each listener is scoped to the owning plugin via a `ctx.plugin.name` check
211
- * so that generators from different plugins do not cross-fire.
212
- *
213
- * The renderer resolution chain is: `generator.renderer → plugin.renderer → config.renderer`.
214
- * Set `generator.renderer = null` to explicitly opt out of rendering even when the plugin
215
- * declares a renderer.
216
- *
217
- * Call this method inside `addGenerator()` (in `kubb:plugin:setup`) to wire up a generator.
218
- */
219
- registerGenerator(pluginName: string, gen: Generator): void {
220
- const resolveRenderer = () => {
221
- const plugin = this.plugins.get(pluginName)
222
- return gen.renderer === null ? undefined : (gen.renderer ?? plugin?.renderer ?? this.config.renderer)
223
- }
224
-
225
- if (gen.schema) {
226
- const schemaHandler = async (node: SchemaNode, ctx: GeneratorContext) => {
227
- if (ctx.plugin.name !== pluginName) return
228
- const result = await gen.schema!(node, ctx)
229
- await applyHookResult(result, this, resolveRenderer())
230
- }
231
-
232
- this.hooks.on('kubb:generate:schema', schemaHandler)
233
- this.#trackHookListener('kubb:generate:schema', schemaHandler as (...args: never[]) => void | Promise<void>)
234
- }
235
-
236
- if (gen.operation) {
237
- const operationHandler = async (node: OperationNode, ctx: GeneratorContext) => {
238
- if (ctx.plugin.name !== pluginName) return
239
- const result = await gen.operation!(node, ctx)
240
- await applyHookResult(result, this, resolveRenderer())
241
- }
242
-
243
- this.hooks.on('kubb:generate:operation', operationHandler)
244
- this.#trackHookListener('kubb:generate:operation', operationHandler as (...args: never[]) => void | Promise<void>)
245
- }
246
-
247
- if (gen.operations) {
248
- const operationsHandler = async (nodes: Array<OperationNode>, ctx: GeneratorContext) => {
249
- if (ctx.plugin.name !== pluginName) return
250
- const result = await gen.operations!(nodes, ctx)
251
- await applyHookResult(result, this, resolveRenderer())
252
- }
253
-
254
- this.hooks.on('kubb:generate:operations', operationsHandler)
255
- this.#trackHookListener('kubb:generate:operations', operationsHandler as (...args: never[]) => void | Promise<void>)
256
- }
257
-
258
- this.#pluginsWithEventGenerators.add(pluginName)
259
- }
260
-
261
- /**
262
- * Returns `true` when at least one generator was registered for the given plugin
263
- * via `addGenerator()` in `kubb:plugin:setup` (event-based path).
264
- *
265
- * Used by the build loop to decide whether to walk the AST and emit generator events
266
- * for a plugin that has no static `plugin.generators`.
267
- */
268
- hasRegisteredGenerators(pluginName: string): boolean {
269
- return this.#pluginsWithEventGenerators.has(pluginName)
270
- }
271
-
272
- /**
273
- * Unregisters all plugin lifecycle listeners from the shared event emitter.
274
- * Called at the end of a build to prevent listener leaks across repeated builds.
275
- *
276
- * @internal
277
- */
278
- dispose(): void {
279
- for (const [event, handlers] of this.#hookListeners) {
280
- for (const handler of handlers) {
281
- this.hooks.off(event, handler as never)
282
- }
283
- }
284
- this.#hookListeners.clear()
285
- this.#pluginsWithEventGenerators.clear()
286
- }
287
-
288
- #trackHookListener(event: keyof KubbHooks, handler: (...args: never[]) => void | Promise<void>): void {
289
- let handlers = this.#hookListeners.get(event)
290
- if (!handlers) {
291
- handlers = new Set()
292
- this.#hookListeners.set(event, handlers)
293
- }
294
- handlers.add(handler)
295
- }
296
-
297
- #createDefaultResolver(pluginName: string): Resolver {
298
- const existingResolver = this.#defaultResolvers.get(pluginName)
299
- if (existingResolver) {
300
- return existingResolver
301
- }
302
-
303
- const resolver = defineResolver<PluginFactoryOptions>((_ctx) => ({
304
- name: 'default',
305
- pluginName,
306
- }))
307
- this.#defaultResolvers.set(pluginName, resolver)
308
- return resolver
309
- }
310
-
311
- /**
312
- * Merges `partial` with the plugin's default resolver and stores the result.
313
- * Also mirrors it onto `plugin.resolver` so callers using `getPlugin(name).resolver`
314
- * get the up-to-date resolver without going through `getResolver()`.
315
- */
316
- setPluginResolver(pluginName: string, partial: Partial<Resolver>): void {
317
- const defaultResolver = this.#createDefaultResolver(pluginName)
318
- const merged = { ...defaultResolver, ...partial }
319
- this.#resolvers.set(pluginName, merged)
320
- const plugin = this.plugins.get(pluginName)
321
- if (plugin) {
322
- plugin.resolver = merged
323
- }
324
- }
325
-
326
- /**
327
- * Returns the resolver for the given plugin.
328
- *
329
- * Resolution order: dynamic resolver set via `setPluginResolver` → static resolver on the
330
- * plugin → lazily created default resolver (identity name, no path transforms).
331
- */
332
- getResolver<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Kubb.PluginRegistry[TName]['resolver']
333
- getResolver<TResolver extends Resolver = Resolver>(pluginName: string): TResolver
334
- getResolver(pluginName: string): Resolver {
335
- return this.#resolvers.get(pluginName) ?? this.plugins.get(pluginName)?.resolver ?? this.#createDefaultResolver(pluginName)
336
- }
337
-
338
- getContext<TOptions extends PluginFactoryOptions>(plugin: NormalizedPlugin<TOptions>): GeneratorContext<TOptions> & Record<string, unknown> {
339
- const driver = this
340
-
341
- const baseContext = {
342
- config: driver.config,
343
- get root(): string {
344
- return resolve(driver.config.root, driver.config.output.path)
345
- },
346
- getMode(output: { path: string }): 'single' | 'split' {
347
- return PluginDriver.getMode(resolve(driver.config.root, driver.config.output.path, output.path))
348
- },
349
- hooks: driver.hooks,
350
- plugin,
351
- getPlugin: driver.getPlugin.bind(driver),
352
- requirePlugin: driver.requirePlugin.bind(driver),
353
- getResolver: driver.getResolver.bind(driver),
354
- driver,
355
- addFile: async (...files: Array<FileNode>) => {
356
- driver.fileManager.add(...files)
357
- },
358
- upsertFile: async (...files: Array<FileNode>) => {
359
- driver.fileManager.upsert(...files)
360
- },
361
- get inputNode(): InputNode | undefined {
362
- return driver.inputNode
363
- },
364
- get adapter(): Adapter | undefined {
365
- return driver.adapter
366
- },
367
- get resolver() {
368
- return driver.getResolver(plugin.name)
369
- },
370
- get transformer() {
371
- return plugin.transformer
372
- },
373
- warn(message: string) {
374
- driver.hooks.emit('kubb:warn', { message })
375
- },
376
- error(error: string | Error) {
377
- driver.hooks.emit('kubb:error', { error: typeof error === 'string' ? new Error(error) : error })
378
- },
379
- info(message: string) {
380
- driver.hooks.emit('kubb:info', { message })
381
- },
382
- openInStudio(options?: DevtoolsOptions) {
383
- if (!driver.config.devtools || driver.#studioIsOpen) {
384
- return
385
- }
386
-
387
- if (typeof driver.config.devtools !== 'object') {
388
- throw new Error('Devtools must be an object')
389
- }
390
-
391
- if (!driver.inputNode || !driver.adapter) {
392
- throw new Error('adapter is not defined, make sure you have set the parser in kubb.config.ts')
393
- }
394
-
395
- driver.#studioIsOpen = true
396
-
397
- const studioUrl = driver.config.devtools?.studioUrl ?? DEFAULT_STUDIO_URL
398
-
399
- return openInStudioFn(driver.inputNode, studioUrl, options)
400
- },
401
- } as unknown as GeneratorContext<TOptions>
402
-
403
- return baseContext
404
- }
405
-
406
- getPlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
407
- getPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions> | undefined
408
- getPlugin(pluginName: string): Plugin | undefined {
409
- return this.plugins.get(pluginName)
410
- }
411
-
412
- /**
413
- * Like `getPlugin` but throws a descriptive error when the plugin is not found.
414
- */
415
- requirePlugin<TName extends keyof Kubb.PluginRegistry>(pluginName: TName): Plugin<Kubb.PluginRegistry[TName]>
416
- requirePlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions>(pluginName: string): Plugin<TOptions>
417
- requirePlugin(pluginName: string): Plugin {
418
- const plugin = this.plugins.get(pluginName)
419
- if (!plugin) {
420
- throw new Error(`[kubb] Plugin "${pluginName}" is required but not found. Make sure it is included in your Kubb config.`)
421
- }
422
- return plugin
423
- }
424
- }
package/src/renderNode.ts DELETED
@@ -1,35 +0,0 @@
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
- }
@@ -1,18 +0,0 @@
1
- import { version as nodeVersion } from 'node:process'
2
- import { version as KubbVersion } from '../../package.json'
3
-
4
- /**
5
- * Returns a snapshot of the current runtime environment.
6
- *
7
- * Useful for attaching context to debug logs and error reports so that
8
- * issues can be reproduced without manual information gathering.
9
- */
10
- export function getDiagnosticInfo() {
11
- return {
12
- nodeVersion,
13
- KubbVersion,
14
- platform: process.platform,
15
- arch: process.arch,
16
- cwd: process.cwd(),
17
- } as const
18
- }
@@ -1,10 +0,0 @@
1
- import type { Config, InputPath, UserConfig } from '../types'
2
-
3
- /**
4
- * Type guard to check if a given config has an `input.path`.
5
- */
6
- export function isInputPath(config: UserConfig | undefined): config is UserConfig<InputPath>
7
- export function isInputPath(config: Config | undefined): config is Config<InputPath>
8
- export function isInputPath(config: Config | UserConfig | undefined): config is Config<InputPath> | UserConfig<InputPath> {
9
- return typeof config?.input === 'object' && config.input !== null && 'path' in config.input
10
- }
@@ -1,99 +0,0 @@
1
- import { findPackageJSON, readSync } from '@internals/utils'
2
-
3
- type PackageJSON = {
4
- dependencies?: Record<string, string>
5
- devDependencies?: Record<string, string>
6
- }
7
-
8
- type DependencyName = string
9
- type DependencyVersion = string
10
-
11
- function getPackageJSONSync(cwd?: string): PackageJSON | null {
12
- const pkgPath = findPackageJSON(cwd)
13
- if (!pkgPath) {
14
- return null
15
- }
16
-
17
- return JSON.parse(readSync(pkgPath)) as PackageJSON
18
- }
19
-
20
- function match(packageJSON: PackageJSON, dependency: DependencyName | RegExp): string | null {
21
- const dependencies = {
22
- ...(packageJSON.dependencies || {}),
23
- ...(packageJSON.devDependencies || {}),
24
- }
25
-
26
- if (typeof dependency === 'string' && dependencies[dependency]) {
27
- return dependencies[dependency]
28
- }
29
-
30
- const matched = Object.keys(dependencies).find((dep) => dep.match(dependency))
31
-
32
- return matched ? (dependencies[matched] ?? null) : null
33
- }
34
-
35
- function getVersionSync(dependency: DependencyName | RegExp, cwd?: string): DependencyVersion | null {
36
- const packageJSON = getPackageJSONSync(cwd)
37
-
38
- return packageJSON ? match(packageJSON, dependency) : null
39
- }
40
-
41
- /**
42
- * Returns `true` when the nearest `package.json` declares a dependency that
43
- * satisfies the given semver range.
44
- *
45
- * - Searches both `dependencies` and `devDependencies`.
46
- * - Accepts a string package name or a `RegExp` to match scoped/pattern packages.
47
- * - Uses `semver.satisfies` for range comparison; returns `false` when the
48
- * version string cannot be coerced into a valid semver.
49
- *
50
- * @example
51
- * ```ts
52
- * satisfiesDependency('react', '>=18') // true when react@18.x is installed
53
- * satisfiesDependency(/^@tanstack\//, '>=5') // true when any @tanstack/* >=5 is found
54
- * ```
55
- */
56
- function coerceSemver(version: string): [number, number, number] | null {
57
- const m = version.match(/(\d+)(?:\.(\d+))?(?:\.(\d+))?/)
58
- return m ? [Number(m[1]), Number(m[2] ?? 0), Number(m[3] ?? 0)] : null
59
- }
60
-
61
- function satisfiesSemver(v: [number, number, number], range: string): boolean {
62
- return range
63
- .trim()
64
- .split(/\s+/)
65
- .every((cond) => {
66
- const m = cond.match(/^(>=|<=|>|<|=|\^|~)?(\d+)(?:\.(\d+))?(?:\.(\d+))?$/)
67
- if (!m) return false
68
- const op = m[1] ?? '='
69
- const r: [number, number, number] = [Number(m[2]), Number(m[3] ?? 0), Number(m[4] ?? 0)]
70
- const cmp = v[0] !== r[0] ? v[0] - r[0] : v[1] !== r[1] ? v[1] - r[1] : v[2] - r[2]
71
- if (op === '>=') return cmp >= 0
72
- if (op === '<=') return cmp <= 0
73
- if (op === '>') return cmp > 0
74
- if (op === '<') return cmp < 0
75
- if (op === '^') return v[0] === r[0] && cmp >= 0
76
- if (op === '~') return v[0] === r[0] && v[1] === r[1] && cmp >= 0
77
- return cmp === 0
78
- })
79
- }
80
-
81
- export function satisfiesDependency(dependency: DependencyName | RegExp, version: DependencyVersion, cwd?: string): boolean {
82
- const packageVersion = getVersionSync(dependency, cwd)
83
-
84
- if (!packageVersion) {
85
- return false
86
- }
87
-
88
- if (packageVersion === version) {
89
- return true
90
- }
91
-
92
- const semVer = coerceSemver(packageVersion)
93
-
94
- if (!semVer) {
95
- return false
96
- }
97
-
98
- return satisfiesSemver(semVer, version)
99
- }