@kubb/core 3.0.0-alpha.1 → 3.0.0-alpha.11

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 (102) hide show
  1. package/dist/{FileManager-EVJig-LT.d.ts → FileManager-BbUCeEyv.d.cts} +108 -94
  2. package/dist/{FileManager-DSLZ6ps7.d.cts → FileManager-CpuFz6eH.d.ts} +108 -94
  3. package/dist/chunk-2EU7DMPM.js +96 -0
  4. package/dist/chunk-2EU7DMPM.js.map +1 -0
  5. package/dist/chunk-4X5FFJPJ.js +8 -13
  6. package/dist/chunk-4X5FFJPJ.js.map +1 -1
  7. package/dist/chunk-52WA2JF5.cjs +1536 -0
  8. package/dist/chunk-52WA2JF5.cjs.map +1 -0
  9. package/dist/chunk-ADFKVVPE.cjs +42 -0
  10. package/dist/chunk-ADFKVVPE.cjs.map +1 -0
  11. package/dist/{chunk-3OXCZ5DJ.js → chunk-E6CN2CZC.js} +63 -54
  12. package/dist/chunk-E6CN2CZC.js.map +1 -0
  13. package/dist/chunk-HBQM723K.js +1049 -0
  14. package/dist/chunk-HBQM723K.js.map +1 -0
  15. package/dist/{chunk-67C6RBGQ.cjs → chunk-HMCSWG5L.cjs} +25 -26
  16. package/dist/chunk-HMCSWG5L.cjs.map +1 -0
  17. package/dist/chunk-L6YLVCKM.js +894 -0
  18. package/dist/chunk-L6YLVCKM.js.map +1 -0
  19. package/dist/chunk-PJLUPV3P.cjs +1088 -0
  20. package/dist/chunk-PJLUPV3P.cjs.map +1 -0
  21. package/dist/chunk-TTDCUWK7.cjs +101 -0
  22. package/dist/chunk-TTDCUWK7.cjs.map +1 -0
  23. package/dist/{chunk-LM2YQC3T.cjs → chunk-YEVCYV36.cjs} +81 -51
  24. package/dist/chunk-YEVCYV36.cjs.map +1 -0
  25. package/dist/index.cjs +545 -610
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.cts +6 -19
  28. package/dist/index.d.ts +6 -19
  29. package/dist/index.js +438 -522
  30. package/dist/index.js.map +1 -1
  31. package/dist/{logger-DChjnJMn.d.cts → logger-DvbHXjIO.d.cts} +29 -19
  32. package/dist/{logger-DChjnJMn.d.ts → logger-DvbHXjIO.d.ts} +29 -19
  33. package/dist/logger.cjs +25 -15
  34. package/dist/logger.cjs.map +1 -1
  35. package/dist/logger.d.cts +1 -2
  36. package/dist/logger.d.ts +1 -2
  37. package/dist/logger.js +3 -15
  38. package/dist/logger.js.map +1 -1
  39. package/dist/mocks.cjs +27 -26
  40. package/dist/mocks.cjs.map +1 -1
  41. package/dist/mocks.d.cts +6 -9
  42. package/dist/mocks.d.ts +6 -9
  43. package/dist/mocks.js +22 -24
  44. package/dist/mocks.js.map +1 -1
  45. package/dist/prompt-L5KFKY73.cjs +760 -0
  46. package/dist/prompt-L5KFKY73.cjs.map +1 -0
  47. package/dist/prompt-WQQUN22Z.js +749 -0
  48. package/dist/prompt-WQQUN22Z.js.map +1 -0
  49. package/dist/transformers.cjs +215 -49
  50. package/dist/transformers.cjs.map +1 -1
  51. package/dist/transformers.d.cts +1 -3
  52. package/dist/transformers.d.ts +1 -3
  53. package/dist/transformers.js +148 -35
  54. package/dist/transformers.js.map +1 -1
  55. package/dist/utils.cjs +65 -26
  56. package/dist/utils.cjs.map +1 -1
  57. package/dist/utils.d.cts +31 -3
  58. package/dist/utils.d.ts +31 -3
  59. package/dist/utils.js +3 -26
  60. package/dist/utils.js.map +1 -1
  61. package/package.json +10 -10
  62. package/src/BarrelManager.ts +93 -107
  63. package/src/{Generator.ts → BaseGenerator.ts} +1 -1
  64. package/src/FileManager.ts +198 -297
  65. package/src/PackageManager.ts +1 -1
  66. package/src/PluginManager.ts +152 -101
  67. package/src/__snapshots__/barrel.json +91 -0
  68. package/src/__snapshots__/grouped.json +114 -0
  69. package/src/__snapshots__/ordered.json +62 -0
  70. package/src/build.ts +86 -171
  71. package/src/errors.ts +0 -11
  72. package/src/index.ts +1 -2
  73. package/src/logger.ts +76 -34
  74. package/src/plugin.ts +3 -3
  75. package/src/transformers/index.ts +2 -3
  76. package/src/transformers/trim.ts +0 -4
  77. package/src/types.ts +35 -35
  78. package/src/utils/TreeNode.ts +132 -50
  79. package/src/utils/executeStrategies.ts +1 -1
  80. package/src/utils/index.ts +2 -1
  81. package/src/utils/parser.ts +157 -0
  82. package/dist/chunk-3OXCZ5DJ.js.map +0 -1
  83. package/dist/chunk-5JZNFPUP.js +0 -309
  84. package/dist/chunk-5JZNFPUP.js.map +0 -1
  85. package/dist/chunk-67C6RBGQ.cjs.map +0 -1
  86. package/dist/chunk-ADC5UNZ5.cjs +0 -1227
  87. package/dist/chunk-ADC5UNZ5.cjs.map +0 -1
  88. package/dist/chunk-HMLY7DHA.js +0 -16
  89. package/dist/chunk-HMLY7DHA.js.map +0 -1
  90. package/dist/chunk-JKZG2IJR.js +0 -283
  91. package/dist/chunk-JKZG2IJR.js.map +0 -1
  92. package/dist/chunk-LM2YQC3T.cjs.map +0 -1
  93. package/dist/chunk-PZT4CTBV.cjs +0 -299
  94. package/dist/chunk-PZT4CTBV.cjs.map +0 -1
  95. package/dist/chunk-SA2GZKXS.js +0 -596
  96. package/dist/chunk-SA2GZKXS.js.map +0 -1
  97. package/dist/chunk-XCPFG6DO.cjs +0 -66
  98. package/dist/chunk-XCPFG6DO.cjs.map +0 -1
  99. package/dist/chunk-YTSNYMHW.cjs +0 -320
  100. package/dist/chunk-YTSNYMHW.cjs.map +0 -1
  101. package/schema.json +0 -86
  102. package/src/utils/getParser.ts +0 -17
@@ -1,20 +1,14 @@
1
- import { getExports } from '@kubb/parser-ts'
1
+ import { join } from 'node:path'
2
2
 
3
- import path from 'node:path'
4
-
5
- import { trimExtName } from './transformers/trim.ts'
6
3
  import { TreeNode } from './utils/TreeNode.ts'
7
4
 
5
+ import { getRelativePath } from '@kubb/fs'
8
6
  import type * as KubbFile from '@kubb/fs/types'
9
- import type { DirectoryTreeOptions } from 'directory-tree'
7
+ import type { Logger } from './logger.ts'
8
+ import type { FileMetaBase } from './FileManager.ts'
10
9
 
11
10
  export type BarrelManagerOptions = {
12
- treeNode?: DirectoryTreeOptions
13
- isTypeOnly?: boolean
14
- /**
15
- * Add .ts or .js
16
- */
17
- extName?: KubbFile.Extname
11
+ logger?: Logger
18
12
  }
19
13
 
20
14
  export class BarrelManager {
@@ -26,116 +20,108 @@ export class BarrelManager {
26
20
  return this
27
21
  }
28
22
 
29
- getNamedExport(root: string, item: KubbFile.Export): KubbFile.Export[] {
30
- const exportedNames = getExports(path.resolve(root, item.path))
31
-
32
- if (!exportedNames) {
33
- return [item]
34
- }
35
-
36
- return exportedNames.reduce(
37
- (prev, curr) => {
38
- if (!prev[0]?.name || !prev[1]?.name) {
39
- return prev
40
- }
41
-
42
- if (curr.isTypeOnly) {
43
- prev[1] = { ...prev[1], name: [...prev[1].name, curr.name] }
44
- } else {
45
- prev[0] = { ...prev[0], name: [...prev[0].name, curr.name] }
46
- }
47
-
48
- return prev
49
- },
50
- [
51
- {
52
- ...item,
53
- name: [],
54
- isTypeOnly: false,
55
- },
56
- {
57
- ...item,
58
- name: [],
59
- isTypeOnly: true,
60
- },
61
- ] as KubbFile.Export[],
62
- )
63
- }
23
+ getFiles({ files: generatedFiles, root, meta }: { files: KubbFile.File[]; root?: string; meta?: FileMetaBase | undefined }): Array<KubbFile.File> {
24
+ const { logger } = this.#options
64
25
 
65
- getNamedExports(root: string, exports: KubbFile.Export[]): KubbFile.Export[] {
66
- return exports?.flatMap((item) => {
67
- return this.getNamedExport(root, item)
68
- })
69
- }
26
+ const cachedFiles = new Map<KubbFile.Path, KubbFile.File>()
70
27
 
71
- getIndexes(root: string): Array<KubbFile.File> | null {
72
- const { treeNode = {}, isTypeOnly, extName } = this.#options
73
- const tree = TreeNode.build(root, treeNode)
28
+ logger?.emit('debug', { date: new Date(), logs: [`Start barrel generation for pluginKey ${meta?.pluginKey?.join('.')} and root '${root}'`] })
74
29
 
75
- if (!tree) {
76
- return null
77
- }
30
+ TreeNode.build(generatedFiles, root)?.forEach((treeNode) => {
31
+ if (!treeNode || !treeNode.children || !treeNode.parent?.data.path) {
32
+ return undefined
33
+ }
78
34
 
79
- const fileReducer = (files: Array<KubbFile.File>, treeNode: TreeNode) => {
80
- if (!treeNode.children) {
81
- return []
35
+ const barrelFile: KubbFile.File = {
36
+ path: join(treeNode.parent?.data.path, 'index.ts') as KubbFile.Path,
37
+ baseName: 'index.ts',
38
+ exports: [],
39
+ sources: [],
82
40
  }
41
+ const previousBarrelFile = cachedFiles.get(barrelFile.path)
42
+ const leaves = treeNode.leaves
83
43
 
84
- if (treeNode.children.length > 1) {
85
- const indexPath: KubbFile.Path = path.resolve(treeNode.data.path, 'index.ts')
44
+ leaves.forEach((item) => {
45
+ if (!item.data.name) {
46
+ return undefined
47
+ }
86
48
 
87
- const exports: Array<KubbFile.Export> = treeNode.children
88
- .filter(Boolean)
89
- .map((file) => {
90
- const importPath: string = file.data.type === 'split' ? `./${file.data.name}/index` : `./${trimExtName(file.data.name)}`
49
+ const sources = item.data.file?.sources || []
91
50
 
92
- if (importPath.endsWith('index') && file.data.type === 'single') {
93
- return undefined
94
- }
51
+ if (!sources.some((source) => source.isIndexable)) {
52
+ logger?.emit(
53
+ 'warning',
54
+ `No isIndexable source found(source should have a name and isIndexable):\nFile: ${JSON.stringify(item.data.file, undefined, 2)}`,
55
+ )
56
+ }
95
57
 
96
- return {
97
- path: extName ? `${importPath}${extName}` : importPath,
98
- isTypeOnly,
99
- } as KubbFile.Export
58
+ sources.forEach((source) => {
59
+ if (!item.data.file?.path || !source.isIndexable || !source.name) {
60
+ return undefined
61
+ }
62
+ const alreadyContainInPreviousBarrelFile = previousBarrelFile?.sources.some((item) => item.name === source.name)
63
+
64
+ if (alreadyContainInPreviousBarrelFile) {
65
+ return undefined
66
+ }
67
+
68
+ if (!barrelFile.exports) {
69
+ barrelFile.exports = []
70
+ }
71
+
72
+ // true when we have a subdirectory that also contains barrel files
73
+ const isSubExport = !!treeNode.parent?.data.path?.split?.('/')?.length
74
+
75
+ if (isSubExport) {
76
+ barrelFile.exports.push({
77
+ name: [source.name],
78
+ path: getRelativePath(treeNode.parent?.data.path, item.data.path),
79
+ isTypeOnly: source.isTypeOnly,
80
+ })
81
+ } else {
82
+ barrelFile.exports.push({
83
+ name: [source.name],
84
+ path: `./${item.data.file.baseName}`,
85
+ isTypeOnly: source.isTypeOnly,
86
+ })
87
+ }
88
+
89
+ barrelFile.sources.push({
90
+ name: source.name,
91
+ isTypeOnly: source.isTypeOnly,
92
+ //TODO use parser to generate import
93
+ value: '',
94
+ isExportable: false,
95
+ isIndexable: false,
100
96
  })
101
- .filter(Boolean)
102
-
103
- files.push({
104
- path: indexPath,
105
- baseName: 'index.ts',
106
- source: '',
107
- exports,
108
- exportable: true,
109
97
  })
110
- } else if (treeNode.children.length === 1) {
111
- const [treeNodeChild] = treeNode.children as [TreeNode]
112
-
113
- const indexPath = path.resolve(treeNode.data.path, 'index.ts')
114
- const importPath = treeNodeChild.data.type === 'split' ? `./${treeNodeChild.data.name}/index` : `./${trimExtName(treeNodeChild.data.name)}`
115
-
116
- const exports = [
117
- {
118
- path: extName ? `${importPath}${extName}` : importPath,
119
- isTypeOnly,
120
- },
121
- ]
122
-
123
- files.push({
124
- path: indexPath,
125
- baseName: 'index.ts',
126
- source: '',
127
- exports,
128
- exportable: true,
129
- })
130
- }
98
+ })
99
+
100
+ logger?.emit('debug', {
101
+ date: new Date(),
102
+ logs: [
103
+ `Generating barrelFile '${getRelativePath(root, barrelFile.path)}' for '${getRelativePath(root, treeNode.data?.path)}' with ${barrelFile.sources.length} indexable exports: '${barrelFile.sources?.map((source) => source.name).join(', ')}'`,
104
+ ],
105
+ })
131
106
 
132
- treeNode.children.forEach((childItem) => {
133
- fileReducer(files, childItem)
107
+ logger?.emit('debug', {
108
+ date: new Date(),
109
+ logs: [
110
+ `Generated barrelFile '${getRelativePath(root, barrelFile.path)}' for '${getRelativePath(root, treeNode.data?.path)}' with exports: '${cachedFiles
111
+ .get(barrelFile.path)
112
+ ?.sources?.map((source) => source.name)
113
+ .join(', ')}'`,
114
+ ],
134
115
  })
135
116
 
136
- return files
137
- }
117
+ if (previousBarrelFile) {
118
+ previousBarrelFile.sources.push(...barrelFile.sources)
119
+ previousBarrelFile.exports?.push(...(barrelFile.exports || []))
120
+ } else {
121
+ cachedFiles.set(barrelFile.path, barrelFile)
122
+ }
123
+ })
138
124
 
139
- return fileReducer([], tree).reverse()
125
+ return [...cachedFiles.values()]
140
126
  }
141
127
  }
@@ -2,7 +2,7 @@
2
2
  * Abstract class that contains the building blocks for plugins to create their own Generator
3
3
  * @link idea based on https://github.com/colinhacks/zod/blob/master/src/types.ts#L137
4
4
  */
5
- export abstract class Generator<TOptions = unknown, TContext = unknown> {
5
+ export abstract class BaseGenerator<TOptions = unknown, TContext = unknown> {
6
6
  #options: TOptions = {} as TOptions
7
7
  #context: TContext = {} as TContext
8
8