@kubb/core 2.0.0-alpha.9 → 2.0.0-beta.10
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 +1 -1
- package/dist/index.cjs +302 -248
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +72 -69
- package/dist/index.d.ts +72 -69
- package/dist/index.js +299 -244
- package/dist/index.js.map +1 -1
- package/dist/transformers.cjs +222 -0
- package/dist/transformers.cjs.map +1 -0
- package/dist/transformers.d.cts +55 -0
- package/dist/transformers.d.ts +55 -0
- package/dist/transformers.js +207 -0
- package/dist/transformers.js.map +1 -0
- package/dist/utils.cjs +302 -274
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +515 -67
- package/dist/utils.d.ts +515 -67
- package/dist/utils.js +303 -274
- package/dist/utils.js.map +1 -1
- package/package.json +19 -15
- package/src/BarrelManager.ts +55 -65
- package/src/FileManager.ts +100 -31
- package/src/PluginManager.ts +41 -39
- package/src/PromiseManager.ts +5 -1
- package/src/build.ts +1 -11
- package/src/index.ts +0 -1
- package/src/plugin.ts +4 -4
- package/src/transformers/casing.ts +9 -0
- package/src/transformers/createJSDocBlockText.ts +9 -0
- package/src/transformers/index.ts +36 -0
- package/src/transformers/trim.ts +7 -0
- package/src/types.ts +22 -39
- package/src/utils/FunctionParams.ts +3 -2
- package/src/utils/TreeNode.ts +6 -3
- package/src/utils/URLPath.ts +5 -5
- package/src/utils/executeStrategies.ts +14 -2
- package/src/utils/index.ts +0 -1
- package/src/SchemaGenerator.ts +0 -8
- package/src/utils/transformers/createJSDocBlockText.ts +0 -15
- package/src/utils/transformers/index.ts +0 -22
- package/src/utils/transformers/trim.ts +0 -3
- /package/src/{utils/transformers → transformers}/combineCodes.ts +0 -0
- /package/src/{utils/transformers → transformers}/escape.ts +0 -0
- /package/src/{utils/transformers → transformers}/indent.ts +0 -0
- /package/src/{utils/transformers → transformers}/nameSorter.ts +0 -0
- /package/src/{utils/transformers → transformers}/searchAndReplace.ts +0 -0
- /package/src/{utils/transformers → transformers}/transformReservedWord.ts +0 -0
package/src/BarrelManager.ts
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
|
|
3
|
+
import transformers from './transformers/index.ts'
|
|
3
4
|
import { TreeNode } from './utils/TreeNode.ts'
|
|
4
5
|
|
|
5
6
|
import type { DirectoryTreeOptions } from 'directory-tree'
|
|
6
7
|
import type { KubbFile } from './FileManager.ts'
|
|
8
|
+
import type { KubbPlugin } from './index.ts'
|
|
7
9
|
|
|
8
|
-
type
|
|
10
|
+
type FileMeta = {
|
|
11
|
+
pluginKey?: KubbPlugin['key']
|
|
12
|
+
treeNode: TreeNode
|
|
13
|
+
}
|
|
9
14
|
|
|
10
15
|
export type BarrelManagerOptions = {
|
|
11
16
|
treeNode?: DirectoryTreeOptions
|
|
12
17
|
isTypeOnly?: boolean
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Add .ts or .js
|
|
20
|
+
*/
|
|
21
|
+
extName?: KubbFile.Extname
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
export class BarrelManager {
|
|
20
|
-
#options: BarrelManagerOptions
|
|
25
|
+
#options: BarrelManagerOptions
|
|
21
26
|
|
|
22
27
|
constructor(options: BarrelManagerOptions = {}) {
|
|
23
28
|
this.#options = options
|
|
@@ -26,45 +31,34 @@ export class BarrelManager {
|
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
getIndexes(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
const extMapper: Record<KubbFile.Extname, DirectoryTreeOptions> = {
|
|
35
|
-
'.ts': {
|
|
36
|
-
extensions: /\.ts/,
|
|
37
|
-
exclude: [/schemas/, /json/],
|
|
38
|
-
},
|
|
39
|
-
'.json': {
|
|
40
|
-
extensions: /\.json/,
|
|
41
|
-
exclude: [],
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
const tree = TreeNode.build<BarrelData>(root, { ...(extMapper[extName as keyof typeof extMapper] || {}), ...treeNode })
|
|
34
|
+
pathToBuild: string,
|
|
35
|
+
): Array<KubbFile.File<FileMeta>> | null {
|
|
36
|
+
const { treeNode = {}, isTypeOnly, extName } = this.#options
|
|
37
|
+
const tree = TreeNode.build(pathToBuild, treeNode)
|
|
45
38
|
|
|
46
39
|
if (!tree) {
|
|
47
40
|
return null
|
|
48
41
|
}
|
|
49
42
|
|
|
50
|
-
const fileReducer = (files: Array<KubbFile.File
|
|
51
|
-
if (!
|
|
43
|
+
const fileReducer = (files: Array<KubbFile.File<FileMeta>>, treeNode: TreeNode) => {
|
|
44
|
+
if (!treeNode.children) {
|
|
52
45
|
return []
|
|
53
46
|
}
|
|
54
47
|
|
|
55
|
-
if (
|
|
56
|
-
const indexPath: KubbFile.Path = path.resolve(
|
|
57
|
-
|
|
48
|
+
if (treeNode.children.length > 1) {
|
|
49
|
+
const indexPath: KubbFile.Path = path.resolve(treeNode.data.path, 'index.ts')
|
|
50
|
+
|
|
51
|
+
const exports: KubbFile.Export[] = treeNode.children
|
|
58
52
|
.filter(Boolean)
|
|
59
53
|
.map((file) => {
|
|
60
|
-
const importPath: string = file.data.type === 'directory' ? `./${file.data.name}/index` : `./${file.data.name
|
|
54
|
+
const importPath: string = file.data.type === 'directory' ? `./${file.data.name}/index` : `./${transformers.trimExtName(file.data.name)}`
|
|
61
55
|
|
|
62
|
-
if (importPath.
|
|
56
|
+
if (importPath.endsWith('index') && file.data.type === 'file') {
|
|
63
57
|
return undefined
|
|
64
58
|
}
|
|
65
59
|
|
|
66
60
|
return {
|
|
67
|
-
path:
|
|
61
|
+
path: extName ? `${importPath}${extName}` : importPath,
|
|
68
62
|
isTypeOnly,
|
|
69
63
|
} as KubbFile.Export
|
|
70
64
|
})
|
|
@@ -74,50 +68,46 @@ export class BarrelManager {
|
|
|
74
68
|
path: indexPath,
|
|
75
69
|
baseName: 'index.ts',
|
|
76
70
|
source: '',
|
|
77
|
-
exports
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
: exports,
|
|
71
|
+
exports,
|
|
72
|
+
meta: {
|
|
73
|
+
treeNode,
|
|
74
|
+
},
|
|
82
75
|
})
|
|
83
|
-
} else {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
76
|
+
} else if (treeNode.children.length === 1) {
|
|
77
|
+
const [treeNodeChild] = treeNode.children as [TreeNode]
|
|
78
|
+
|
|
79
|
+
const indexPath = path.resolve(treeNode.data.path, 'index.ts')
|
|
80
|
+
const importPath = treeNodeChild.data.type === 'directory'
|
|
81
|
+
? `./${treeNodeChild.data.name}/index`
|
|
82
|
+
: `./${transformers.trimExtName(treeNodeChild.data.name)}`
|
|
83
|
+
|
|
84
|
+
const exports = [
|
|
85
|
+
{
|
|
86
|
+
path: extName
|
|
87
|
+
? `${importPath}${extName}`
|
|
88
|
+
: importPath,
|
|
89
|
+
isTypeOnly,
|
|
90
|
+
},
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
files.push({
|
|
94
|
+
path: indexPath,
|
|
95
|
+
baseName: 'index.ts',
|
|
96
|
+
source: '',
|
|
97
|
+
exports,
|
|
98
|
+
meta: {
|
|
99
|
+
treeNode,
|
|
100
|
+
},
|
|
107
101
|
})
|
|
108
102
|
}
|
|
109
103
|
|
|
110
|
-
|
|
104
|
+
treeNode.children.forEach((childItem) => {
|
|
111
105
|
fileReducer(files, childItem)
|
|
112
106
|
})
|
|
113
107
|
|
|
114
108
|
return files
|
|
115
109
|
}
|
|
116
110
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
const filteredFiles = filter ? files.filter(filter) : files
|
|
120
|
-
|
|
121
|
-
return map ? filteredFiles.map(map) : filteredFiles
|
|
111
|
+
return fileReducer([], tree).reverse()
|
|
122
112
|
}
|
|
123
113
|
}
|
package/src/FileManager.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
2
2
|
import crypto from 'node:crypto'
|
|
3
|
-
import { extname } from 'node:path'
|
|
3
|
+
import { extname, resolve } from 'node:path'
|
|
4
4
|
|
|
5
5
|
import { print } from '@kubb/parser'
|
|
6
6
|
import * as factory from '@kubb/parser/factory'
|
|
@@ -8,9 +8,9 @@ import * as factory from '@kubb/parser/factory'
|
|
|
8
8
|
import isEqual from 'lodash.isequal'
|
|
9
9
|
import { orderBy } from 'natural-orderby'
|
|
10
10
|
|
|
11
|
-
import
|
|
11
|
+
import transformers from './transformers/index.ts'
|
|
12
|
+
import { getRelativePath, read } from './utils/read.ts'
|
|
12
13
|
import { timeout } from './utils/timeout.ts'
|
|
13
|
-
import { transformers } from './utils/transformers/index.ts'
|
|
14
14
|
import { write } from './utils/write.ts'
|
|
15
15
|
import { BarrelManager } from './BarrelManager.ts'
|
|
16
16
|
|
|
@@ -26,38 +26,53 @@ export namespace KubbFile {
|
|
|
26
26
|
/**
|
|
27
27
|
* Import name to be used
|
|
28
28
|
* @example ["useState"]
|
|
29
|
-
* @
|
|
29
|
+
* @example "React"
|
|
30
30
|
*/
|
|
31
|
-
name:
|
|
31
|
+
name:
|
|
32
|
+
| string
|
|
33
|
+
| Array<
|
|
34
|
+
string | {
|
|
35
|
+
propertyName: string
|
|
36
|
+
name?: string
|
|
37
|
+
}
|
|
38
|
+
>
|
|
32
39
|
/**
|
|
33
40
|
* Path for the import
|
|
34
41
|
* @xample '@kubb/core'
|
|
35
42
|
*/
|
|
36
43
|
path: string
|
|
37
44
|
/**
|
|
38
|
-
* Add `type` prefix to the import, this will result in: `import type { Type } from './path'
|
|
45
|
+
* Add `type` prefix to the import, this will result in: `import type { Type } from './path'`.
|
|
39
46
|
*/
|
|
40
47
|
isTypeOnly?: boolean
|
|
48
|
+
/**
|
|
49
|
+
* Add `* as` prefix to the import, this will result in: `import * as path from './path'`.
|
|
50
|
+
*/
|
|
51
|
+
isNameSpace?: boolean
|
|
52
|
+
/**
|
|
53
|
+
* When root is set it will get the path with relative getRelativePath(root, path).
|
|
54
|
+
*/
|
|
55
|
+
root?: string
|
|
41
56
|
}
|
|
42
57
|
|
|
43
58
|
export type Export = {
|
|
44
59
|
/**
|
|
45
|
-
* Export name to be used
|
|
60
|
+
* Export name to be used.
|
|
46
61
|
* @example ["useState"]
|
|
47
|
-
* @
|
|
62
|
+
* @example "React"
|
|
48
63
|
*/
|
|
49
64
|
name?: string | Array<string>
|
|
50
65
|
/**
|
|
51
|
-
* Path for the import
|
|
66
|
+
* Path for the import.
|
|
52
67
|
* @xample '@kubb/core'
|
|
53
68
|
*/
|
|
54
69
|
path: string
|
|
55
70
|
/**
|
|
56
|
-
* Add `type` prefix to the export, this will result in: `export type { Type } from './path'
|
|
71
|
+
* Add `type` prefix to the export, this will result in: `export type { Type } from './path'`.
|
|
57
72
|
*/
|
|
58
73
|
isTypeOnly?: boolean
|
|
59
74
|
/**
|
|
60
|
-
* Make it possible to override the name, this will result in: `export * as aliasName from './path'
|
|
75
|
+
* Make it possible to override the name, this will result in: `export * as aliasName from './path'`.
|
|
61
76
|
*/
|
|
62
77
|
asAlias?: boolean
|
|
63
78
|
}
|
|
@@ -104,8 +119,8 @@ export namespace KubbFile {
|
|
|
104
119
|
*/
|
|
105
120
|
id?: string
|
|
106
121
|
/**
|
|
107
|
-
* Name to be used to
|
|
108
|
-
* Based on UNIX basename
|
|
122
|
+
* Name to be used to create the path
|
|
123
|
+
* Based on UNIX basename, `${name}.extName`
|
|
109
124
|
* @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
|
|
110
125
|
*/
|
|
111
126
|
baseName: TBaseName
|
|
@@ -130,10 +145,6 @@ export namespace KubbFile {
|
|
|
130
145
|
* This will override `process.env[key]` inside the `source`, see `getFileSource`.
|
|
131
146
|
*/
|
|
132
147
|
env?: NodeJS.ProcessEnv
|
|
133
|
-
/**
|
|
134
|
-
* @deprecated
|
|
135
|
-
*/
|
|
136
|
-
validate?: boolean
|
|
137
148
|
}
|
|
138
149
|
|
|
139
150
|
export type ResolvedFile<
|
|
@@ -144,6 +155,12 @@ export namespace KubbFile {
|
|
|
144
155
|
* @default crypto.randomUUID()
|
|
145
156
|
*/
|
|
146
157
|
id: UUID
|
|
158
|
+
/**
|
|
159
|
+
* Contains the first part of the baseName, generated based on baseName
|
|
160
|
+
* @link https://nodejs.org/api/path.html#pathformatpathobject
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
name: string
|
|
147
164
|
}
|
|
148
165
|
}
|
|
149
166
|
|
|
@@ -156,8 +173,19 @@ type AddResult<T extends Array<KubbFile.File>> = Promise<
|
|
|
156
173
|
>
|
|
157
174
|
|
|
158
175
|
type AddIndexesProps = {
|
|
159
|
-
|
|
160
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Root based on root and output.path specified in the config
|
|
178
|
+
*/
|
|
179
|
+
root: string
|
|
180
|
+
/**
|
|
181
|
+
* Output for plugin
|
|
182
|
+
*/
|
|
183
|
+
output: {
|
|
184
|
+
path: string
|
|
185
|
+
exportAs?: string
|
|
186
|
+
extName?: KubbFile.Extname
|
|
187
|
+
exportType?: 'barrel' | false
|
|
188
|
+
}
|
|
161
189
|
options?: BarrelManagerOptions
|
|
162
190
|
meta?: KubbFile.File['meta']
|
|
163
191
|
}
|
|
@@ -205,10 +233,6 @@ export class FileManager {
|
|
|
205
233
|
}
|
|
206
234
|
|
|
207
235
|
#validate(file: KubbFile.File): void {
|
|
208
|
-
if (!file.validate) {
|
|
209
|
-
return
|
|
210
|
-
}
|
|
211
|
-
|
|
212
236
|
if (!file.path.toLowerCase().endsWith(file.baseName.toLowerCase())) {
|
|
213
237
|
throw new Error(`${file.path} should end with the baseName ${file.baseName}`)
|
|
214
238
|
}
|
|
@@ -218,7 +242,7 @@ export class FileManager {
|
|
|
218
242
|
...files: T
|
|
219
243
|
): AddResult<T> {
|
|
220
244
|
const promises = files.map((file) => {
|
|
221
|
-
this.#validate(file)
|
|
245
|
+
// this.#validate(file)
|
|
222
246
|
|
|
223
247
|
if (file.override) {
|
|
224
248
|
return this.#add(file)
|
|
@@ -238,7 +262,7 @@ export class FileManager {
|
|
|
238
262
|
|
|
239
263
|
async #add(file: KubbFile.File): Promise<KubbFile.ResolvedFile> {
|
|
240
264
|
const controller = new AbortController()
|
|
241
|
-
const resolvedFile: KubbFile.ResolvedFile = { id: crypto.randomUUID(), ...file }
|
|
265
|
+
const resolvedFile: KubbFile.ResolvedFile = { id: crypto.randomUUID(), name: transformers.trimExtName(file.baseName), ...file }
|
|
242
266
|
|
|
243
267
|
this.#cache.set(resolvedFile.path, [{ cancel: () => controller.abort(), ...resolvedFile }])
|
|
244
268
|
|
|
@@ -272,15 +296,45 @@ export class FileManager {
|
|
|
272
296
|
return this.#add(file)
|
|
273
297
|
}
|
|
274
298
|
|
|
275
|
-
async addIndexes({ root,
|
|
276
|
-
const
|
|
299
|
+
async addIndexes({ root, output, meta, options = {} }: AddIndexesProps): Promise<Array<KubbFile.File> | undefined> {
|
|
300
|
+
const { exportType = 'barrel' } = output
|
|
301
|
+
|
|
302
|
+
if (!exportType) {
|
|
303
|
+
return undefined
|
|
304
|
+
}
|
|
277
305
|
|
|
278
|
-
const
|
|
306
|
+
const exportPath = output.path.startsWith('./') ? output.path : `./${output.path}`
|
|
307
|
+
const barrelManager = new BarrelManager({ extName: output.extName, ...options })
|
|
308
|
+
const files = barrelManager.getIndexes(resolve(root, output.path))
|
|
279
309
|
|
|
280
310
|
if (!files) {
|
|
281
311
|
return undefined
|
|
282
312
|
}
|
|
283
313
|
|
|
314
|
+
const rootFile: KubbFile.File = {
|
|
315
|
+
path: resolve(root, 'index.ts'),
|
|
316
|
+
baseName: 'index.ts',
|
|
317
|
+
source: '',
|
|
318
|
+
exports: [
|
|
319
|
+
output.exportAs
|
|
320
|
+
? {
|
|
321
|
+
name: output.exportAs,
|
|
322
|
+
asAlias: true,
|
|
323
|
+
path: exportPath,
|
|
324
|
+
isTypeOnly: options.isTypeOnly,
|
|
325
|
+
}
|
|
326
|
+
: {
|
|
327
|
+
path: exportPath,
|
|
328
|
+
isTypeOnly: options.isTypeOnly,
|
|
329
|
+
},
|
|
330
|
+
],
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
await this.#addOrAppend({
|
|
334
|
+
...rootFile,
|
|
335
|
+
meta: meta ? meta : rootFile.meta,
|
|
336
|
+
})
|
|
337
|
+
|
|
284
338
|
return await Promise.all(
|
|
285
339
|
files.map((file) => {
|
|
286
340
|
return this.#addOrAppend({
|
|
@@ -342,9 +396,24 @@ export class FileManager {
|
|
|
342
396
|
const exports = file.exports ? combineExports(file.exports) : []
|
|
343
397
|
const imports = file.imports ? combineImports(file.imports, exports, file.source) : []
|
|
344
398
|
|
|
345
|
-
const importNodes = imports.
|
|
399
|
+
const importNodes = imports.filter(item => {
|
|
400
|
+
// isImportNotNeeded
|
|
401
|
+
// trim extName
|
|
402
|
+
return item.path !== transformers.trimExtName(file.path)
|
|
403
|
+
}).map((item) => {
|
|
404
|
+
return factory.createImportDeclaration({
|
|
405
|
+
name: item.name,
|
|
406
|
+
path: item.root ? getRelativePath(item.root, item.path) : item.path,
|
|
407
|
+
isTypeOnly: item.isTypeOnly,
|
|
408
|
+
})
|
|
409
|
+
})
|
|
346
410
|
const exportNodes = exports.map((item) =>
|
|
347
|
-
factory.createExportDeclaration({
|
|
411
|
+
factory.createExportDeclaration({
|
|
412
|
+
name: item.name,
|
|
413
|
+
path: item.path,
|
|
414
|
+
isTypeOnly: item.isTypeOnly,
|
|
415
|
+
asAlias: item.asAlias,
|
|
416
|
+
})
|
|
348
417
|
)
|
|
349
418
|
|
|
350
419
|
return [print([...importNodes, ...exportNodes]), getEnvSource(file.source, file.env)].join('\n')
|
|
@@ -452,7 +521,7 @@ export function combineImports(imports: Array<KubbFile.Import>, exports: Array<K
|
|
|
452
521
|
}
|
|
453
522
|
|
|
454
523
|
if (Array.isArray(name)) {
|
|
455
|
-
name = name.filter((item) => hasImportInSource(item))
|
|
524
|
+
name = name.filter((item) => typeof item === 'string' ? hasImportInSource(item) : hasImportInSource(item.propertyName))
|
|
456
525
|
}
|
|
457
526
|
|
|
458
527
|
const prevByPath = prev.findLast((imp) => imp.path === curr.path && imp.isTypeOnly === curr.isTypeOnly)
|
package/src/PluginManager.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-types, @typescript-eslint/no-unsafe-argument */
|
|
2
2
|
|
|
3
|
+
import { transformReservedWord } from './transformers/transformReservedWord.ts'
|
|
3
4
|
import { EventEmitter } from './utils/EventEmitter.ts'
|
|
4
5
|
import { LogLevel } from './utils/logger.ts'
|
|
5
6
|
import { Queue } from './utils/Queue.ts'
|
|
6
|
-
import { transformReservedWord } from './utils/transformers/transformReservedWord.ts'
|
|
7
7
|
import { setUniqueName } from './utils/uniqueName.ts'
|
|
8
8
|
import { ValidationPluginError } from './errors.ts'
|
|
9
9
|
import { FileManager } from './FileManager.ts'
|
|
@@ -129,11 +129,11 @@ export class PluginManager {
|
|
|
129
129
|
parameters: [params.baseName, params.directory, params.options as object],
|
|
130
130
|
})
|
|
131
131
|
|
|
132
|
-
if (paths && paths?.length > 1) {
|
|
133
|
-
|
|
132
|
+
if (paths && paths?.length > 1 && this.logger.logLevel === LogLevel.debug) {
|
|
133
|
+
this.logger.warn(
|
|
134
134
|
`Cannot return a path where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough\n\nPaths: ${
|
|
135
135
|
JSON.stringify(paths, undefined, 2)
|
|
136
|
-
}`,
|
|
136
|
+
}\n\nFalling back on the first item.\n`,
|
|
137
137
|
)
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -153,11 +153,11 @@ export class PluginManager {
|
|
|
153
153
|
parameters: [params.name, params.type],
|
|
154
154
|
})
|
|
155
155
|
|
|
156
|
-
if (names && names?.length > 1) {
|
|
157
|
-
|
|
156
|
+
if (names && names?.length > 1 && this.logger.logLevel === LogLevel.debug) {
|
|
157
|
+
this.logger.warn(
|
|
158
158
|
`Cannot return a name where the 'pluginKey' ${params.pluginKey ? JSON.stringify(params.pluginKey) : '"'} is not unique enough\n\nNames: ${
|
|
159
159
|
JSON.stringify(names, undefined, 2)
|
|
160
|
-
}`,
|
|
160
|
+
}\n\nFalling back on the first item.\n`,
|
|
161
161
|
)
|
|
162
162
|
}
|
|
163
163
|
|
|
@@ -308,27 +308,11 @@ export class PluginManager {
|
|
|
308
308
|
hookName: H
|
|
309
309
|
parameters?: Parameters<RequiredPluginLifecycle[H]> | undefined
|
|
310
310
|
}): Promise<Awaited<TOuput>[]> {
|
|
311
|
-
const
|
|
311
|
+
const promises = this.#getSortedPlugins().map((plugin) => {
|
|
312
|
+
return () => this.#execute({ strategy: 'hookParallel', hookName, parameters, plugin }) as Promise<TOuput>
|
|
313
|
+
})
|
|
312
314
|
|
|
313
|
-
|
|
314
|
-
// TODO implement sequential with `buildStart` as an object({ sequential: boolean; handler: PluginContext["buildStart"] })
|
|
315
|
-
// if ((plugin[hookName] as { sequential?: boolean })?.sequential) {
|
|
316
|
-
// await Promise.all(parallelPromises)
|
|
317
|
-
// parallelPromises.length = 0
|
|
318
|
-
// await this.execute({
|
|
319
|
-
// strategy: 'hookParallel',
|
|
320
|
-
// hookName,
|
|
321
|
-
// parameters,
|
|
322
|
-
// plugin,
|
|
323
|
-
// })
|
|
324
|
-
// }
|
|
325
|
-
const promise: Promise<TOuput> | null = this.#execute({ strategy: 'hookParallel', hookName, parameters, plugin }) as Promise<TOuput>
|
|
326
|
-
|
|
327
|
-
if (promise) {
|
|
328
|
-
parallelPromises.push(promise)
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
const results = await Promise.allSettled(parallelPromises)
|
|
315
|
+
const results = await this.#promiseManager.run('parallel', promises)
|
|
332
316
|
|
|
333
317
|
results
|
|
334
318
|
.forEach((result, index) => {
|
|
@@ -338,7 +322,7 @@ export class PluginManager {
|
|
|
338
322
|
this.#catcher<H>(result.reason, plugin, hookName)
|
|
339
323
|
}
|
|
340
324
|
})
|
|
341
|
-
|
|
325
|
+
|
|
342
326
|
return results.filter((result) => result.status === 'fulfilled').map((result) => (result as PromiseFulfilledResult<Awaited<TOuput>>).value)
|
|
343
327
|
}
|
|
344
328
|
|
|
@@ -370,7 +354,7 @@ export class PluginManager {
|
|
|
370
354
|
})
|
|
371
355
|
.then((result) => reduce.call(this.#core.api, argument0, result as ReturnType<ParseResult<H>>, plugin)) as Promise<Argument0<H>>
|
|
372
356
|
}
|
|
373
|
-
|
|
357
|
+
|
|
374
358
|
return promise
|
|
375
359
|
}
|
|
376
360
|
|
|
@@ -395,7 +379,7 @@ export class PluginManager {
|
|
|
395
379
|
const plugins = [...this.plugins].filter((plugin) => plugin.name !== 'core')
|
|
396
380
|
|
|
397
381
|
if (hookName) {
|
|
398
|
-
if (this.logger.logLevel ===
|
|
382
|
+
if (this.logger.logLevel === LogLevel.info) {
|
|
399
383
|
const containsHookName = plugins.some((item) => item[hookName])
|
|
400
384
|
if (!containsHookName) {
|
|
401
385
|
this.logger.warn(`No hook ${hookName} found`)
|
|
@@ -404,28 +388,46 @@ export class PluginManager {
|
|
|
404
388
|
|
|
405
389
|
return plugins.filter((item) => item[hookName])
|
|
406
390
|
}
|
|
391
|
+
// TODO add test case for sorting with pre/post
|
|
407
392
|
|
|
408
|
-
return plugins
|
|
393
|
+
return plugins.map(plugin => {
|
|
394
|
+
if (plugin.pre) {
|
|
395
|
+
const isValid = plugin.pre.every(pluginName => plugins.find(pluginToFind => pluginToFind.name === pluginName))
|
|
396
|
+
|
|
397
|
+
if (!isValid) {
|
|
398
|
+
throw new ValidationPluginError(`This plugin has a pre set that is not valid(${JSON.stringify(plugin.pre, undefined, 2)})`)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return plugin
|
|
403
|
+
}).sort((a, b) => {
|
|
404
|
+
if (b.pre?.includes(a.name)) {
|
|
405
|
+
return 1
|
|
406
|
+
}
|
|
407
|
+
if (b.post?.includes(a.name)) {
|
|
408
|
+
return -1
|
|
409
|
+
}
|
|
410
|
+
return 0
|
|
411
|
+
})
|
|
409
412
|
}
|
|
410
413
|
|
|
411
414
|
getPluginsByKey(hookName: keyof PluginLifecycle, pluginKey: KubbPlugin['key']): KubbPlugin[] {
|
|
412
415
|
const plugins = [...this.plugins]
|
|
413
|
-
const [
|
|
416
|
+
const [searchPluginName, searchIdentifier] = pluginKey
|
|
414
417
|
|
|
415
418
|
const pluginByPluginName = plugins
|
|
416
419
|
.filter((plugin) => plugin[hookName])
|
|
417
420
|
.filter((item) => {
|
|
418
|
-
const [
|
|
421
|
+
const [name, identifier] = item.key
|
|
419
422
|
|
|
420
423
|
const identifierCheck = identifier?.toString() === searchIdentifier?.toString()
|
|
421
|
-
const kindCheck = kind === searchKind
|
|
422
424
|
const nameCheck = name === searchPluginName
|
|
423
425
|
|
|
424
426
|
if (searchIdentifier) {
|
|
425
|
-
return identifierCheck &&
|
|
427
|
+
return identifierCheck && nameCheck
|
|
426
428
|
}
|
|
427
429
|
|
|
428
|
-
return
|
|
430
|
+
return nameCheck
|
|
429
431
|
})
|
|
430
432
|
|
|
431
433
|
if (!pluginByPluginName?.length) {
|
|
@@ -433,7 +435,7 @@ export class PluginManager {
|
|
|
433
435
|
|
|
434
436
|
const corePlugin = plugins.find((plugin) => plugin.name === 'core' && plugin[hookName])
|
|
435
437
|
|
|
436
|
-
if (this.logger.logLevel ===
|
|
438
|
+
if (this.logger.logLevel === LogLevel.debug) {
|
|
437
439
|
if (corePlugin) {
|
|
438
440
|
this.logger.warn(`No hook '${hookName}' for pluginKey '${JSON.stringify(pluginKey)}' found, falling back on the '@kubb/core' plugin`)
|
|
439
441
|
} else {
|
|
@@ -585,7 +587,7 @@ export class PluginManager {
|
|
|
585
587
|
|
|
586
588
|
setUniqueName(plugin.name, usedPluginNames)
|
|
587
589
|
|
|
588
|
-
const key =
|
|
590
|
+
const key = [plugin.name, usedPluginNames[plugin.name]].filter(Boolean) as [typeof plugin.name, string]
|
|
589
591
|
|
|
590
592
|
if (plugin.name !== 'core' && usedPluginNames[plugin.name]! >= 2) {
|
|
591
593
|
pluginManager.logger.warn('Using multiple of the same plugin is an experimental feature')
|
|
@@ -640,6 +642,6 @@ export class PluginManager {
|
|
|
640
642
|
|
|
641
643
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
642
644
|
static get hooks() {
|
|
643
|
-
return ['
|
|
645
|
+
return ['buildStart', 'resolvePath', 'resolveName', 'load', 'transform', 'writeFile', 'buildEnd'] as const
|
|
644
646
|
}
|
|
645
647
|
}
|
package/src/PromiseManager.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { hookFirst, hookSeq } from './utils/executeStrategies.ts'
|
|
1
|
+
import { hookFirst, hookParallel, hookSeq } from './utils/executeStrategies.ts'
|
|
2
2
|
|
|
3
3
|
import type { PossiblePromise } from '@kubb/types'
|
|
4
4
|
import type { Strategy, StrategySwitch } from './utils/executeStrategies.ts'
|
|
@@ -30,6 +30,10 @@ export class PromiseManager<TState = any> {
|
|
|
30
30
|
return hookFirst<TInput, TValue, TOutput>(promises, this.#options.nullCheck)
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
+
if (strategy === 'parallel') {
|
|
34
|
+
return hookParallel<TInput, TValue, TOutput>(promises)
|
|
35
|
+
}
|
|
36
|
+
|
|
33
37
|
throw new Error(`${strategy} not implemented`)
|
|
34
38
|
}
|
|
35
39
|
}
|
package/src/build.ts
CHANGED
|
@@ -116,7 +116,7 @@ async function setup(options: BuildOptions): Promise<PluginManager> {
|
|
|
116
116
|
logger.spinner.start(`💾 Writing`)
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
if (logger.logLevel ===
|
|
119
|
+
if (logger.logLevel === LogLevel.debug) {
|
|
120
120
|
logger.info(`PluginKey ${pc.dim(JSON.stringify(plugin.key))} \nwith source\n\n${code}`)
|
|
121
121
|
}
|
|
122
122
|
}
|
|
@@ -157,11 +157,6 @@ export async function build(options: BuildOptions): Promise<BuildOutput> {
|
|
|
157
157
|
|
|
158
158
|
const { fileManager, logger } = pluginManager
|
|
159
159
|
|
|
160
|
-
await pluginManager.hookParallel<'validate', true>({
|
|
161
|
-
hookName: 'validate',
|
|
162
|
-
parameters: [pluginManager.plugins],
|
|
163
|
-
})
|
|
164
|
-
|
|
165
160
|
await pluginManager.hookParallel({
|
|
166
161
|
hookName: 'buildStart',
|
|
167
162
|
parameters: [options.config],
|
|
@@ -183,11 +178,6 @@ export async function safeBuild(options: BuildOptions): Promise<BuildOutput> {
|
|
|
183
178
|
const { fileManager, logger } = pluginManager
|
|
184
179
|
|
|
185
180
|
try {
|
|
186
|
-
await pluginManager.hookParallel<'validate', true>({
|
|
187
|
-
hookName: 'validate',
|
|
188
|
-
parameters: [pluginManager.plugins],
|
|
189
|
-
})
|
|
190
|
-
|
|
191
181
|
await pluginManager.hookParallel({
|
|
192
182
|
hookName: 'buildStart',
|
|
193
183
|
parameters: [options.config],
|
package/src/index.ts
CHANGED
|
@@ -12,7 +12,6 @@ export { PackageManager } from './PackageManager.ts'
|
|
|
12
12
|
export { createPlugin, pluginName as name, pluginName } from './plugin.ts'
|
|
13
13
|
export { PluginManager } from './PluginManager.ts'
|
|
14
14
|
export { PromiseManager } from './PromiseManager.ts'
|
|
15
|
-
export { SchemaGenerator } from './SchemaGenerator.ts'
|
|
16
15
|
export * from './types.ts'
|
|
17
16
|
|
|
18
17
|
export interface _Register {}
|