@kubb/core 5.0.0-alpha.8 → 5.0.0-beta.1

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 (70) hide show
  1. package/README.md +23 -20
  2. package/dist/PluginDriver-BXibeQk-.cjs +1036 -0
  3. package/dist/PluginDriver-BXibeQk-.cjs.map +1 -0
  4. package/dist/PluginDriver-DV3p2Hky.js +945 -0
  5. package/dist/PluginDriver-DV3p2Hky.js.map +1 -0
  6. package/dist/index.cjs +756 -1693
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.ts +297 -239
  9. package/dist/index.js +743 -1661
  10. package/dist/index.js.map +1 -1
  11. package/dist/mocks.cjs +145 -0
  12. package/dist/mocks.cjs.map +1 -0
  13. package/dist/mocks.d.ts +80 -0
  14. package/dist/mocks.js +140 -0
  15. package/dist/mocks.js.map +1 -0
  16. package/dist/types-CuNocrbJ.d.ts +2148 -0
  17. package/package.json +51 -57
  18. package/src/FileManager.ts +115 -0
  19. package/src/FileProcessor.ts +86 -0
  20. package/src/Kubb.ts +208 -160
  21. package/src/PluginDriver.ts +326 -565
  22. package/src/constants.ts +20 -47
  23. package/src/createAdapter.ts +16 -6
  24. package/src/createKubb.ts +548 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +40 -26
  27. package/src/defineGenerator.ts +87 -0
  28. package/src/defineLogger.ts +19 -0
  29. package/src/defineMiddleware.ts +62 -0
  30. package/src/defineParser.ts +44 -0
  31. package/src/definePlugin.ts +83 -0
  32. package/src/defineResolver.ts +521 -0
  33. package/src/devtools.ts +14 -14
  34. package/src/index.ts +14 -17
  35. package/src/mocks.ts +178 -0
  36. package/src/renderNode.ts +35 -0
  37. package/src/storages/fsStorage.ts +41 -11
  38. package/src/storages/memoryStorage.ts +4 -2
  39. package/src/types.ts +1054 -270
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/isInputPath.ts +10 -0
  42. package/src/utils/packageJSON.ts +99 -0
  43. package/dist/PluginDriver-DRfJIbG1.d.ts +0 -1056
  44. package/dist/chunk-ByKO4r7w.cjs +0 -38
  45. package/dist/hooks.cjs +0 -102
  46. package/dist/hooks.cjs.map +0 -1
  47. package/dist/hooks.d.ts +0 -75
  48. package/dist/hooks.js +0 -97
  49. package/dist/hooks.js.map +0 -1
  50. package/src/PackageManager.ts +0 -180
  51. package/src/build.ts +0 -419
  52. package/src/config.ts +0 -56
  53. package/src/createGenerator.ts +0 -106
  54. package/src/createLogger.ts +0 -7
  55. package/src/createPlugin.ts +0 -12
  56. package/src/errors.ts +0 -1
  57. package/src/hooks/index.ts +0 -4
  58. package/src/hooks/useKubb.ts +0 -138
  59. package/src/hooks/useMode.ts +0 -11
  60. package/src/hooks/usePlugin.ts +0 -11
  61. package/src/hooks/usePluginDriver.ts +0 -11
  62. package/src/utils/FunctionParams.ts +0 -155
  63. package/src/utils/TreeNode.ts +0 -215
  64. package/src/utils/executeStrategies.ts +0 -81
  65. package/src/utils/formatters.ts +0 -56
  66. package/src/utils/getBarrelFiles.ts +0 -141
  67. package/src/utils/getConfigs.ts +0 -30
  68. package/src/utils/getPlugins.ts +0 -23
  69. package/src/utils/linters.ts +0 -25
  70. package/src/utils/resolveOptions.ts +0 -93
@@ -1,141 +0,0 @@
1
- /** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */
2
- import { join } from 'node:path'
3
- import { getRelativePath } from '@internals/utils'
4
- import type { KubbFile } from '@kubb/fabric-core/types'
5
- import type { BarrelType } from '../types.ts'
6
- import { TreeNode } from './TreeNode.ts'
7
-
8
- export type FileMetaBase = {
9
- pluginName?: string
10
- }
11
-
12
- type AddIndexesProps = {
13
- type: BarrelType | false | undefined
14
- /**
15
- * Root based on root and output.path specified in the config
16
- */
17
- root: string
18
- /**
19
- * Output for plugin
20
- */
21
- output: {
22
- path: string
23
- }
24
- group?: {
25
- output: string
26
- exportAs: string
27
- }
28
-
29
- meta?: FileMetaBase
30
- }
31
-
32
- function getBarrelFilesByRoot(root: string | undefined, files: Array<KubbFile.ResolvedFile>): Array<KubbFile.File> {
33
- const cachedFiles = new Map<KubbFile.Path, KubbFile.File>()
34
-
35
- TreeNode.build(files, root)?.forEach((treeNode) => {
36
- if (!treeNode || !treeNode.children || !treeNode.parent?.data.path) {
37
- return
38
- }
39
-
40
- const barrelFilePath = join(treeNode.parent?.data.path, 'index.ts') as KubbFile.Path
41
- const barrelFile: KubbFile.File = {
42
- path: barrelFilePath,
43
- baseName: 'index.ts',
44
- exports: [],
45
- imports: [],
46
- sources: [],
47
- }
48
- const previousBarrelFile = cachedFiles.get(barrelFile.path)
49
- const leaves = treeNode.leaves
50
-
51
- leaves.forEach((item) => {
52
- if (!item.data.name) {
53
- return
54
- }
55
-
56
- const sources = item.data.file?.sources || []
57
-
58
- sources.forEach((source) => {
59
- if (!item.data.file?.path || !source.isIndexable || !source.name) {
60
- return
61
- }
62
- const alreadyContainInPreviousBarrelFile = previousBarrelFile?.sources.some(
63
- (item) => item.name === source.name && item.isTypeOnly === source.isTypeOnly,
64
- )
65
-
66
- if (alreadyContainInPreviousBarrelFile) {
67
- return
68
- }
69
-
70
- barrelFile.exports!.push({
71
- name: [source.name],
72
- path: getRelativePath(treeNode.parent?.data.path, item.data.path),
73
- isTypeOnly: source.isTypeOnly,
74
- })
75
-
76
- barrelFile.sources.push({
77
- name: source.name,
78
- isTypeOnly: source.isTypeOnly,
79
- //TODO use parser to generate import
80
- value: '',
81
- isExportable: false,
82
- isIndexable: false,
83
- })
84
- })
85
- })
86
-
87
- if (previousBarrelFile) {
88
- previousBarrelFile.sources.push(...barrelFile.sources)
89
- previousBarrelFile.exports?.push(...(barrelFile.exports || []))
90
- } else {
91
- cachedFiles.set(barrelFile.path, barrelFile)
92
- }
93
- })
94
-
95
- return [...cachedFiles.values()]
96
- }
97
-
98
- function trimExtName(text: string): string {
99
- const dotIndex = text.lastIndexOf('.')
100
- // Only strip when the dot is found and no path separator follows it
101
- // (guards against stripping dots that are part of a directory name like /project.v2/gen)
102
- if (dotIndex > 0 && !text.includes('/', dotIndex)) {
103
- return text.slice(0, dotIndex)
104
- }
105
- return text
106
- }
107
-
108
- export async function getBarrelFiles(files: Array<KubbFile.ResolvedFile>, { type, meta = {}, root, output }: AddIndexesProps): Promise<Array<KubbFile.File>> {
109
- if (!type || type === 'propagate') {
110
- return []
111
- }
112
-
113
- const pathToBuildFrom = join(root, output.path)
114
-
115
- if (trimExtName(pathToBuildFrom).endsWith('index')) {
116
- return []
117
- }
118
-
119
- const barrelFiles = getBarrelFilesByRoot(pathToBuildFrom, files)
120
-
121
- if (type === 'all') {
122
- return barrelFiles.map((file) => {
123
- return {
124
- ...file,
125
- exports: file.exports?.map((exportItem) => {
126
- return {
127
- ...exportItem,
128
- name: undefined,
129
- }
130
- }),
131
- }
132
- })
133
- }
134
-
135
- return barrelFiles.map((indexFile) => {
136
- return {
137
- ...indexFile,
138
- meta,
139
- }
140
- })
141
- }
@@ -1,30 +0,0 @@
1
- import type { CLIOptions, ConfigInput } from '../config.ts'
2
- import type { Config, UserConfig } from '../types.ts'
3
- import { getPlugins } from './getPlugins.ts'
4
-
5
- /**
6
- * Converting UserConfig to Config Array without a change in the object beside the JSON convert.
7
- */
8
- export async function getConfigs(config: ConfigInput | UserConfig, args: CLIOptions): Promise<Array<Config>> {
9
- const resolvedConfig: Promise<UserConfig | Array<UserConfig>> =
10
- typeof config === 'function' ? Promise.resolve(config(args as CLIOptions)) : Promise.resolve(config)
11
-
12
- let userConfigs = await resolvedConfig
13
-
14
- if (!Array.isArray(userConfigs)) {
15
- userConfigs = [userConfigs]
16
- }
17
-
18
- const results: Array<Config> = []
19
-
20
- for (const item of userConfigs) {
21
- const plugins = item.plugins ? await getPlugins(item.plugins) : undefined
22
-
23
- results.push({
24
- ...item,
25
- plugins,
26
- } as Config)
27
- }
28
-
29
- return results
30
- }
@@ -1,23 +0,0 @@
1
- import type { UnknownUserPlugin, UserConfig } from '../types.ts'
2
-
3
- type PluginsArray = Array<Omit<UnknownUserPlugin, 'inject'>>
4
-
5
- function isJSONPlugins(plugins: UserConfig['plugins']): boolean {
6
- return Array.isArray(plugins) && plugins.some((plugin) => Array.isArray(plugin) && typeof (plugin as unknown[])[0] === 'string')
7
- }
8
-
9
- function isObjectPlugins(plugins: UserConfig['plugins']): boolean {
10
- return plugins instanceof Object && !Array.isArray(plugins)
11
- }
12
-
13
- export function getPlugins(plugins: UserConfig['plugins']): Promise<PluginsArray | undefined> {
14
- if (isObjectPlugins(plugins)) {
15
- throw new Error('Object plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json')
16
- }
17
-
18
- if (isJSONPlugins(plugins)) {
19
- throw new Error('JSON plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json')
20
- }
21
-
22
- return Promise.resolve(plugins)
23
- }
@@ -1,25 +0,0 @@
1
- import { x } from 'tinyexec'
2
- import type { linters } from '../constants.ts'
3
-
4
- type Linter = keyof typeof linters
5
-
6
- async function isLinterAvailable(linter: Linter): Promise<boolean> {
7
- try {
8
- await x(linter, ['--version'], { nodeOptions: { stdio: 'ignore' } })
9
- return true
10
- } catch {
11
- return false
12
- }
13
- }
14
-
15
- export async function detectLinter(): Promise<Linter | undefined> {
16
- const linterNames: Linter[] = ['biome', 'oxlint', 'eslint']
17
-
18
- for (const linter of linterNames) {
19
- if (await isLinterAvailable(linter)) {
20
- return linter
21
- }
22
- }
23
-
24
- return undefined
25
- }
@@ -1,93 +0,0 @@
1
- import { isOperationNode, isSchemaNode } from '@kubb/ast'
2
- import type { Node, OperationNode, SchemaNode } from '@kubb/ast/types'
3
-
4
- type FilterItem = {
5
- type: string
6
- pattern: string | RegExp
7
- }
8
-
9
- type OverrideItem<TOptions> = FilterItem & {
10
- options: Omit<Partial<TOptions>, 'override'>
11
- }
12
-
13
- type ResolveOptionsContext<TOptions> = {
14
- options: TOptions
15
- exclude?: Array<FilterItem>
16
- include?: Array<FilterItem>
17
- override?: Array<OverrideItem<TOptions>>
18
- }
19
-
20
- function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
21
- switch (type) {
22
- case 'tag':
23
- return node.tags.some((tag) => !!tag.match(pattern))
24
- case 'operationId':
25
- return !!node.operationId.match(pattern)
26
- case 'path':
27
- return !!node.path.match(pattern)
28
- case 'method':
29
- return !!(node.method.toLowerCase() as string).match(pattern)
30
- default:
31
- return false
32
- }
33
- }
34
-
35
- function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
36
- switch (type) {
37
- case 'schemaName':
38
- return node.name ? !!node.name.match(pattern) : false
39
- default:
40
- return null
41
- }
42
- }
43
-
44
- /**
45
- * Resolves the effective plugin options for a given AST node by applying
46
- * `exclude`, `include`, and `override` rules from the plugin configuration.
47
- *
48
- * Returns `null` when the node is excluded or not matched by `include`.
49
- * Returns the merged options (base options merged with any matching `override`) otherwise.
50
- *
51
- * Supported filter types for `OperationNode`: `tag`, `operationId`, `path`, `method`.
52
- * Supported filter types for `SchemaNode`: `schemaName`.
53
- *
54
- * @example
55
- * const resolved = resolveOptions(operationNode, { options, exclude, include, override })
56
- * if (!resolved) return // excluded
57
- */
58
- export function resolveOptions<TOptions>(node: Node, { options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>): TOptions | null {
59
- if (isOperationNode(node)) {
60
- const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
61
- if (isExcluded) {
62
- return null
63
- }
64
-
65
- if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
66
- return null
67
- }
68
-
69
- const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
70
-
71
- return { ...options, ...overrideOptions }
72
- }
73
-
74
- if (isSchemaNode(node)) {
75
- if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
76
- return null
77
- }
78
-
79
- if (include) {
80
- const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
81
- const applicable = results.filter((r) => r !== null)
82
- if (applicable.length > 0 && !applicable.includes(true)) {
83
- return null
84
- }
85
- }
86
-
87
- const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
88
-
89
- return { ...options, ...overrideOptions }
90
- }
91
-
92
- return options
93
- }