@kubb/core 4.32.4 → 4.33.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.
- package/dist/hooks.d.ts +1 -1
- package/dist/index.cjs +1695 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +150 -20
- package/dist/index.js +1673 -65
- package/dist/index.js.map +1 -1
- package/dist/{types-f_no0d7G.d.ts → types-DfjjJb2r.d.ts} +70 -27
- package/package.json +3 -24
- package/src/BarrelManager.ts +10 -31
- package/src/PackageManager.ts +13 -21
- package/src/PluginManager.ts +65 -87
- package/src/PromiseManager.ts +3 -5
- package/src/build.ts +61 -47
- package/src/config.ts +1 -1
- package/src/constants.ts +60 -0
- package/src/errors.ts +1 -14
- package/src/index.ts +6 -3
- package/src/types.ts +5 -14
- package/src/utils/FunctionParams.ts +7 -8
- package/src/utils/TreeNode.ts +12 -23
- package/src/utils/executeStrategies.ts +5 -3
- package/src/utils/formatters.ts +3 -20
- package/src/utils/getBarrelFiles.ts +8 -2
- package/src/utils/getConfigs.ts +6 -15
- package/src/utils/getPlugins.ts +7 -7
- package/src/utils/linters.ts +3 -20
- package/dist/fs-D4eqq6bR.cjs +0 -103
- package/dist/fs-D4eqq6bR.cjs.map +0 -1
- package/dist/fs-TVBCPkE-.js +0 -67
- package/dist/fs-TVBCPkE-.js.map +0 -1
- package/dist/fs.cjs +0 -8
- package/dist/fs.d.ts +0 -23
- package/dist/fs.js +0 -2
- package/dist/packageManager-_7I0WFQU.d.ts +0 -82
- package/dist/packageManager-jzjuEj2U.cjs +0 -1103
- package/dist/packageManager-jzjuEj2U.cjs.map +0 -1
- package/dist/packageManager-wMCQlgd6.js +0 -1024
- package/dist/packageManager-wMCQlgd6.js.map +0 -1
- package/dist/transformers-BwSpAhvT.js +0 -267
- package/dist/transformers-BwSpAhvT.js.map +0 -1
- package/dist/transformers-BweFhqh-.cjs +0 -380
- package/dist/transformers-BweFhqh-.cjs.map +0 -1
- package/dist/transformers.cjs +0 -24
- package/dist/transformers.d.ts +0 -108
- package/dist/transformers.js +0 -2
- package/dist/utils.cjs +0 -430
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.ts +0 -290
- package/dist/utils.js +0 -402
- package/dist/utils.js.map +0 -1
- package/src/BaseGenerator.ts +0 -34
- package/src/fs/clean.ts +0 -5
- package/src/fs/exists.ts +0 -16
- package/src/fs/index.ts +0 -5
- package/src/fs/read.ts +0 -13
- package/src/fs/utils.ts +0 -32
- package/src/fs/write.ts +0 -46
- package/src/transformers/casing.ts +0 -62
- package/src/transformers/combineCodes.ts +0 -3
- package/src/transformers/createJSDocBlockText.ts +0 -9
- package/src/transformers/escape.ts +0 -31
- package/src/transformers/indent.ts +0 -3
- package/src/transformers/index.ts +0 -46
- package/src/transformers/nameSorter.ts +0 -9
- package/src/transformers/searchAndReplace.ts +0 -25
- package/src/transformers/stringify.ts +0 -25
- package/src/transformers/toRegExp.ts +0 -22
- package/src/transformers/transformReservedWord.ts +0 -106
- package/src/transformers/trim.ts +0 -18
- package/src/utils/AsyncEventEmitter.ts +0 -48
- package/src/utils/Cache.ts +0 -31
- package/src/utils/URLPath.ts +0 -146
- package/src/utils/buildJSDoc.ts +0 -34
- package/src/utils/checkOnlineStatus.ts +0 -40
- package/src/utils/formatHrtime.ts +0 -33
- package/src/utils/getNestedAccessor.ts +0 -25
- package/src/utils/index.ts +0 -26
- package/src/utils/packageManager.ts +0 -58
- package/src/utils/promise.ts +0 -13
- package/src/utils/renderTemplate.ts +0 -31
- package/src/utils/serializePluginOptions.ts +0 -29
- package/src/utils/timeout.ts +0 -11
- package/src/utils/tokenize.ts +0 -23
- package/src/utils/types.ts +0 -1
- package/src/utils/uniqueName.ts +0 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.33.1",
|
|
4
4
|
"description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -31,36 +31,15 @@
|
|
|
31
31
|
"import": "./dist/index.js",
|
|
32
32
|
"require": "./dist/index.cjs"
|
|
33
33
|
},
|
|
34
|
-
"./fs": {
|
|
35
|
-
"import": "./dist/fs.js",
|
|
36
|
-
"require": "./dist/fs.cjs"
|
|
37
|
-
},
|
|
38
34
|
"./hooks": {
|
|
39
35
|
"import": "./dist/hooks.js",
|
|
40
36
|
"require": "./dist/hooks.cjs"
|
|
41
37
|
},
|
|
42
|
-
"./transformers": {
|
|
43
|
-
"import": "./dist/transformers.js",
|
|
44
|
-
"require": "./dist/transformers.cjs"
|
|
45
|
-
},
|
|
46
|
-
"./utils": {
|
|
47
|
-
"import": "./dist/utils.js",
|
|
48
|
-
"require": "./dist/utils.cjs"
|
|
49
|
-
},
|
|
50
38
|
"./package.json": "./package.json"
|
|
51
39
|
},
|
|
52
40
|
"types": "./dist/index.d.ts",
|
|
53
41
|
"typesVersions": {
|
|
54
42
|
"*": {
|
|
55
|
-
"utils": [
|
|
56
|
-
"./dist/utils.d.ts"
|
|
57
|
-
],
|
|
58
|
-
"transformers": [
|
|
59
|
-
"./dist/transformers.d.ts"
|
|
60
|
-
],
|
|
61
|
-
"fs": [
|
|
62
|
-
"./dist/fs.d.ts"
|
|
63
|
-
],
|
|
64
43
|
"hooks": [
|
|
65
44
|
"./dist/hooks.d.ts"
|
|
66
45
|
]
|
|
@@ -87,14 +66,14 @@
|
|
|
87
66
|
"dependencies": {
|
|
88
67
|
"@kubb/react-fabric": "0.13.3",
|
|
89
68
|
"empathic": "^2.0.0",
|
|
90
|
-
"jiti": "2.5.1",
|
|
91
69
|
"remeda": "^2.33.6",
|
|
92
70
|
"semver": "^7.7.4",
|
|
93
71
|
"tinyexec": "^1.0.2"
|
|
94
72
|
},
|
|
95
73
|
"devDependencies": {
|
|
96
74
|
"@types/semver": "^7.7.1",
|
|
97
|
-
"p-limit": "^7.3.0"
|
|
75
|
+
"p-limit": "^7.3.0",
|
|
76
|
+
"@internals/utils": "0.0.0"
|
|
98
77
|
},
|
|
99
78
|
"peerDependencies": {
|
|
100
79
|
"@kubb/react-fabric": "0.13.3"
|
package/src/BarrelManager.ts
CHANGED
|
@@ -1,24 +1,18 @@
|
|
|
1
1
|
/** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */
|
|
2
2
|
import { join } from 'node:path'
|
|
3
|
+
import { getRelativePath } from '@internals/utils'
|
|
3
4
|
import type { KubbFile } from '@kubb/fabric-core/types'
|
|
4
|
-
import { getRelativePath } from './fs/index.ts'
|
|
5
5
|
|
|
6
6
|
import type { FileMetaBase } from './utils/getBarrelFiles.ts'
|
|
7
7
|
import { TreeNode } from './utils/TreeNode.ts'
|
|
8
8
|
|
|
9
|
-
type BarrelManagerOptions = {}
|
|
10
|
-
|
|
11
9
|
export class BarrelManager {
|
|
12
|
-
constructor(_options: BarrelManagerOptions = {}) {
|
|
13
|
-
return this
|
|
14
|
-
}
|
|
15
|
-
|
|
16
10
|
getFiles({ files: generatedFiles, root }: { files: KubbFile.File[]; root?: string; meta?: FileMetaBase | undefined }): Array<KubbFile.File> {
|
|
17
11
|
const cachedFiles = new Map<KubbFile.Path, KubbFile.File>()
|
|
18
12
|
|
|
19
13
|
TreeNode.build(generatedFiles, root)?.forEach((treeNode) => {
|
|
20
14
|
if (!treeNode || !treeNode.children || !treeNode.parent?.data.path) {
|
|
21
|
-
return
|
|
15
|
+
return
|
|
22
16
|
}
|
|
23
17
|
|
|
24
18
|
const barrelFile: KubbFile.File = {
|
|
@@ -33,43 +27,28 @@ export class BarrelManager {
|
|
|
33
27
|
|
|
34
28
|
leaves.forEach((item) => {
|
|
35
29
|
if (!item.data.name) {
|
|
36
|
-
return
|
|
30
|
+
return
|
|
37
31
|
}
|
|
38
32
|
|
|
39
33
|
const sources = item.data.file?.sources || []
|
|
40
34
|
|
|
41
35
|
sources.forEach((source) => {
|
|
42
36
|
if (!item.data.file?.path || !source.isIndexable || !source.name) {
|
|
43
|
-
return
|
|
37
|
+
return
|
|
44
38
|
}
|
|
45
39
|
const alreadyContainInPreviousBarrelFile = previousBarrelFile?.sources.some(
|
|
46
40
|
(item) => item.name === source.name && item.isTypeOnly === source.isTypeOnly,
|
|
47
41
|
)
|
|
48
42
|
|
|
49
43
|
if (alreadyContainInPreviousBarrelFile) {
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (!barrelFile.exports) {
|
|
54
|
-
barrelFile.exports = []
|
|
44
|
+
return
|
|
55
45
|
}
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
name: [source.name],
|
|
63
|
-
path: getRelativePath(treeNode.parent?.data.path, item.data.path),
|
|
64
|
-
isTypeOnly: source.isTypeOnly,
|
|
65
|
-
})
|
|
66
|
-
} else {
|
|
67
|
-
barrelFile.exports.push({
|
|
68
|
-
name: [source.name],
|
|
69
|
-
path: `./${item.data.file.baseName}`,
|
|
70
|
-
isTypeOnly: source.isTypeOnly,
|
|
71
|
-
})
|
|
72
|
-
}
|
|
47
|
+
barrelFile.exports!.push({
|
|
48
|
+
name: [source.name],
|
|
49
|
+
path: getRelativePath(treeNode.parent?.data.path, item.data.path),
|
|
50
|
+
isTypeOnly: source.isTypeOnly,
|
|
51
|
+
})
|
|
73
52
|
|
|
74
53
|
barrelFile.sources.push({
|
|
75
54
|
name: source.name,
|
package/src/PackageManager.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import mod from 'node:module'
|
|
2
2
|
import os from 'node:os'
|
|
3
3
|
import { pathToFileURL } from 'node:url'
|
|
4
|
-
|
|
4
|
+
import { read, readSync } from '@internals/utils'
|
|
5
5
|
import * as pkg from 'empathic/package'
|
|
6
6
|
import { coerce, satisfies } from 'semver'
|
|
7
|
-
|
|
8
|
-
import { read, readSync } from './fs/index.ts'
|
|
7
|
+
import { PATH_SEPARATORS } from './constants.ts'
|
|
9
8
|
|
|
10
9
|
type PackageJSON = {
|
|
11
10
|
dependencies?: Record<string, string>
|
|
@@ -20,13 +19,11 @@ export class PackageManager {
|
|
|
20
19
|
static #cache: Record<DependencyName, DependencyVersion> = {}
|
|
21
20
|
|
|
22
21
|
#cwd?: string
|
|
23
|
-
|
|
22
|
+
|
|
24
23
|
constructor(workspace?: string) {
|
|
25
24
|
if (workspace) {
|
|
26
25
|
this.#cwd = workspace
|
|
27
26
|
}
|
|
28
|
-
|
|
29
|
-
return this
|
|
30
27
|
}
|
|
31
28
|
|
|
32
29
|
set workspace(workspace: string) {
|
|
@@ -39,7 +36,7 @@ export class PackageManager {
|
|
|
39
36
|
|
|
40
37
|
normalizeDirectory(directory: string): string {
|
|
41
38
|
const lastChar = directory[directory.length - 1]
|
|
42
|
-
if (lastChar && !
|
|
39
|
+
if (lastChar && !(PATH_SEPARATORS as readonly string[]).includes(lastChar)) {
|
|
43
40
|
return `${directory}/`
|
|
44
41
|
}
|
|
45
42
|
|
|
@@ -57,21 +54,16 @@ export class PackageManager {
|
|
|
57
54
|
return location
|
|
58
55
|
}
|
|
59
56
|
|
|
60
|
-
async import(path: string): Promise<
|
|
61
|
-
|
|
62
|
-
let location = this.getLocation(path)
|
|
57
|
+
async import(path: string): Promise<unknown> {
|
|
58
|
+
let location = this.getLocation(path)
|
|
63
59
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
if (os.platform() === 'win32') {
|
|
61
|
+
location = pathToFileURL(location).href
|
|
62
|
+
}
|
|
67
63
|
|
|
68
|
-
|
|
64
|
+
const module = await import(location)
|
|
69
65
|
|
|
70
|
-
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error(error)
|
|
73
|
-
return undefined
|
|
74
|
-
}
|
|
66
|
+
return module?.default ?? module
|
|
75
67
|
}
|
|
76
68
|
|
|
77
69
|
async getPackageJSON(): Promise<PackageJSON | undefined> {
|
|
@@ -106,8 +98,8 @@ export class PackageManager {
|
|
|
106
98
|
|
|
107
99
|
#match(packageJSON: PackageJSON, dependency: DependencyName | RegExp): string | undefined {
|
|
108
100
|
const dependencies = {
|
|
109
|
-
...(packageJSON
|
|
110
|
-
...(packageJSON
|
|
101
|
+
...(packageJSON.dependencies || {}),
|
|
102
|
+
...(packageJSON.devDependencies || {}),
|
|
111
103
|
}
|
|
112
104
|
|
|
113
105
|
if (typeof dependency === 'string' && dependencies[dependency]) {
|
package/src/PluginManager.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { performance } from 'node:perf_hooks'
|
|
3
|
+
import type { AsyncEventEmitter } from '@internals/utils'
|
|
4
|
+
import { setUniqueName, transformReservedWord } from '@internals/utils'
|
|
3
5
|
import type { KubbFile } from '@kubb/fabric-core/types'
|
|
4
6
|
import type { Fabric } from '@kubb/react-fabric'
|
|
7
|
+
import { CORE_PLUGIN_NAME } from './constants.ts'
|
|
5
8
|
import { ValidationPluginError } from './errors.ts'
|
|
6
9
|
import { isPromiseRejectedResult, PromiseManager } from './PromiseManager.ts'
|
|
7
|
-
import { transformReservedWord } from './transformers/transformReservedWord.ts'
|
|
8
|
-
import { trim } from './transformers/trim.ts'
|
|
9
10
|
import type {
|
|
10
11
|
Config,
|
|
11
|
-
GetPluginFactoryOptions,
|
|
12
12
|
KubbEvents,
|
|
13
13
|
Plugin,
|
|
14
14
|
PluginContext,
|
|
@@ -21,8 +21,6 @@ import type {
|
|
|
21
21
|
ResolvePathParams,
|
|
22
22
|
UserPlugin,
|
|
23
23
|
} from './types.ts'
|
|
24
|
-
import type { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'
|
|
25
|
-
import { setUniqueName } from './utils/uniqueName.ts'
|
|
26
24
|
|
|
27
25
|
type RequiredPluginLifecycle = Required<PluginLifecycle>
|
|
28
26
|
|
|
@@ -65,9 +63,9 @@ export class PluginManager {
|
|
|
65
63
|
readonly config: Config
|
|
66
64
|
readonly options: Options
|
|
67
65
|
|
|
68
|
-
readonly #plugins = new Set<Plugin
|
|
66
|
+
readonly #plugins = new Set<Plugin>()
|
|
69
67
|
readonly #usedPluginNames: Record<string, number> = {}
|
|
70
|
-
readonly #promiseManager
|
|
68
|
+
readonly #promiseManager
|
|
71
69
|
|
|
72
70
|
constructor(config: Config, options: Options) {
|
|
73
71
|
this.config = config
|
|
@@ -80,15 +78,13 @@ export class PluginManager {
|
|
|
80
78
|
|
|
81
79
|
this.#plugins.add(parsedPlugin)
|
|
82
80
|
})
|
|
83
|
-
|
|
84
|
-
return this
|
|
85
81
|
}
|
|
86
82
|
|
|
87
83
|
get events() {
|
|
88
84
|
return this.options.events
|
|
89
85
|
}
|
|
90
86
|
|
|
91
|
-
getContext<TOptions extends PluginFactoryOptions>(plugin: Plugin<TOptions>): PluginContext<TOptions> & Record<string,
|
|
87
|
+
getContext<TOptions extends PluginFactoryOptions>(plugin: Plugin<TOptions>): PluginContext<TOptions> & Record<string, unknown> {
|
|
92
88
|
const plugins = [...this.#plugins]
|
|
93
89
|
const baseContext = {
|
|
94
90
|
fabric: this.options.fabric,
|
|
@@ -105,13 +101,14 @@ export class PluginManager {
|
|
|
105
101
|
},
|
|
106
102
|
} as unknown as PluginContext<TOptions>
|
|
107
103
|
|
|
108
|
-
const mergedExtras: Record<string,
|
|
104
|
+
const mergedExtras: Record<string, unknown> = {}
|
|
109
105
|
for (const p of plugins) {
|
|
110
106
|
if (typeof p.inject === 'function') {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
107
|
+
const result = (p.inject as (this: PluginContext, context: PluginContext) => unknown).call(
|
|
108
|
+
baseContext as unknown as PluginContext,
|
|
109
|
+
baseContext as unknown as PluginContext,
|
|
110
|
+
)
|
|
111
|
+
if (result !== null && typeof result === 'object') {
|
|
115
112
|
Object.assign(mergedExtras, result)
|
|
116
113
|
}
|
|
117
114
|
}
|
|
@@ -174,7 +171,7 @@ export class PluginManager {
|
|
|
174
171
|
const names = this.hookForPluginSync({
|
|
175
172
|
pluginKey: params.pluginKey,
|
|
176
173
|
hookName: 'resolveName',
|
|
177
|
-
parameters: [
|
|
174
|
+
parameters: [params.name.trim(), params.type],
|
|
178
175
|
})
|
|
179
176
|
|
|
180
177
|
const uniqueNames = new Set(names)
|
|
@@ -184,10 +181,10 @@ export class PluginManager {
|
|
|
184
181
|
|
|
185
182
|
const name = this.hookFirstSync({
|
|
186
183
|
hookName: 'resolveName',
|
|
187
|
-
parameters: [
|
|
188
|
-
})
|
|
184
|
+
parameters: [params.name.trim(), params.type],
|
|
185
|
+
})?.result
|
|
189
186
|
|
|
190
|
-
return transformReservedWord(name)
|
|
187
|
+
return transformReservedWord(name ?? params.name)
|
|
191
188
|
}
|
|
192
189
|
|
|
193
190
|
/**
|
|
@@ -252,7 +249,7 @@ export class PluginManager {
|
|
|
252
249
|
plugin,
|
|
253
250
|
})
|
|
254
251
|
})
|
|
255
|
-
.filter(
|
|
252
|
+
.filter((x): x is NonNullable<typeof x> => x !== null)
|
|
256
253
|
|
|
257
254
|
return result
|
|
258
255
|
}
|
|
@@ -270,7 +267,7 @@ export class PluginManager {
|
|
|
270
267
|
skipped?: ReadonlySet<Plugin> | null
|
|
271
268
|
}): Promise<SafeParseResult<H>> {
|
|
272
269
|
const plugins = this.#getSortedPlugins(hookName).filter((plugin) => {
|
|
273
|
-
return skipped ? skipped.has(plugin) : true
|
|
270
|
+
return skipped ? !skipped.has(plugin) : true
|
|
274
271
|
})
|
|
275
272
|
|
|
276
273
|
this.events.emit('plugins:hook:progress:start', { hookName, plugins })
|
|
@@ -309,10 +306,10 @@ export class PluginManager {
|
|
|
309
306
|
hookName: H
|
|
310
307
|
parameters: PluginParameter<H>
|
|
311
308
|
skipped?: ReadonlySet<Plugin> | null
|
|
312
|
-
}): SafeParseResult<H> {
|
|
313
|
-
let parseResult: SafeParseResult<H>
|
|
309
|
+
}): SafeParseResult<H> | null {
|
|
310
|
+
let parseResult: SafeParseResult<H> | null = null
|
|
314
311
|
const plugins = this.#getSortedPlugins(hookName).filter((plugin) => {
|
|
315
|
-
return skipped ? skipped.has(plugin) : true
|
|
312
|
+
return skipped ? !skipped.has(plugin) : true
|
|
316
313
|
})
|
|
317
314
|
|
|
318
315
|
for (const plugin of plugins) {
|
|
@@ -347,14 +344,18 @@ export class PluginManager {
|
|
|
347
344
|
const plugins = this.#getSortedPlugins(hookName)
|
|
348
345
|
this.events.emit('plugins:hook:progress:start', { hookName, plugins })
|
|
349
346
|
|
|
347
|
+
const pluginStartTimes = new Map<Plugin, number>()
|
|
348
|
+
|
|
350
349
|
const promises = plugins.map((plugin) => {
|
|
351
|
-
return () =>
|
|
352
|
-
|
|
350
|
+
return () => {
|
|
351
|
+
pluginStartTimes.set(plugin, performance.now())
|
|
352
|
+
return this.#execute({
|
|
353
353
|
strategy: 'hookParallel',
|
|
354
354
|
hookName,
|
|
355
355
|
parameters,
|
|
356
356
|
plugin,
|
|
357
357
|
}) as Promise<TOutput>
|
|
358
|
+
}
|
|
358
359
|
})
|
|
359
360
|
|
|
360
361
|
const results = await this.#promiseManager.run('parallel', promises, {
|
|
@@ -366,11 +367,12 @@ export class PluginManager {
|
|
|
366
367
|
const plugin = this.#getSortedPlugins(hookName)[index]
|
|
367
368
|
|
|
368
369
|
if (plugin) {
|
|
370
|
+
const startTime = pluginStartTimes.get(plugin) ?? performance.now()
|
|
369
371
|
this.events.emit('error', result.reason, {
|
|
370
372
|
plugin,
|
|
371
373
|
hookName,
|
|
372
374
|
strategy: 'hookParallel',
|
|
373
|
-
duration:
|
|
375
|
+
duration: Math.round(performance.now() - startTime),
|
|
374
376
|
parameters,
|
|
375
377
|
})
|
|
376
378
|
}
|
|
@@ -473,7 +475,7 @@ export class PluginManager {
|
|
|
473
475
|
if (!pluginByPluginName?.length) {
|
|
474
476
|
// fallback on the core plugin when there is no match
|
|
475
477
|
|
|
476
|
-
const corePlugin = plugins.find((plugin) => plugin.name ===
|
|
478
|
+
const corePlugin = plugins.find((plugin) => plugin.name === CORE_PLUGIN_NAME && hookName in plugin)
|
|
477
479
|
// Removed noisy debug logs for missing hooks - these are expected behavior, not errors
|
|
478
480
|
|
|
479
481
|
return corePlugin ? [corePlugin] : []
|
|
@@ -488,6 +490,31 @@ export class PluginManager {
|
|
|
488
490
|
* @param args Arguments passed to the plugin hook.
|
|
489
491
|
* @param plugin The actual pluginObject to run.
|
|
490
492
|
*/
|
|
493
|
+
#emitProcessingEnd<H extends PluginLifecycleHooks>({
|
|
494
|
+
startTime,
|
|
495
|
+
output,
|
|
496
|
+
strategy,
|
|
497
|
+
hookName,
|
|
498
|
+
plugin,
|
|
499
|
+
parameters,
|
|
500
|
+
}: {
|
|
501
|
+
startTime: number
|
|
502
|
+
output: unknown
|
|
503
|
+
strategy: Strategy
|
|
504
|
+
hookName: H
|
|
505
|
+
plugin: PluginWithLifeCycle
|
|
506
|
+
parameters: unknown[] | undefined
|
|
507
|
+
}): void {
|
|
508
|
+
this.events.emit('plugins:hook:processing:end', {
|
|
509
|
+
duration: Math.round(performance.now() - startTime),
|
|
510
|
+
parameters,
|
|
511
|
+
output,
|
|
512
|
+
strategy,
|
|
513
|
+
hookName,
|
|
514
|
+
plugin,
|
|
515
|
+
})
|
|
516
|
+
}
|
|
517
|
+
|
|
491
518
|
// Implementation signature
|
|
492
519
|
#execute<H extends PluginLifecycleHooks>({
|
|
493
520
|
strategy,
|
|
@@ -501,7 +528,6 @@ export class PluginManager {
|
|
|
501
528
|
plugin: PluginWithLifeCycle
|
|
502
529
|
}): Promise<ReturnType<ParseResult<H>> | null> | null {
|
|
503
530
|
const hook = plugin[hookName]
|
|
504
|
-
let output: unknown
|
|
505
531
|
|
|
506
532
|
if (!hook) {
|
|
507
533
|
return null
|
|
@@ -518,36 +544,12 @@ export class PluginManager {
|
|
|
518
544
|
|
|
519
545
|
const task = (async () => {
|
|
520
546
|
try {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
const result = await Promise.resolve((hook as Function).apply(context, parameters))
|
|
524
|
-
|
|
525
|
-
output = result
|
|
526
|
-
|
|
527
|
-
this.events.emit('plugins:hook:processing:end', {
|
|
528
|
-
duration: Math.round(performance.now() - startTime),
|
|
529
|
-
parameters,
|
|
530
|
-
output,
|
|
531
|
-
strategy,
|
|
532
|
-
hookName,
|
|
533
|
-
plugin,
|
|
534
|
-
})
|
|
535
|
-
|
|
536
|
-
return result
|
|
537
|
-
}
|
|
547
|
+
const output =
|
|
548
|
+
typeof hook === 'function' ? await Promise.resolve((hook as (...args: unknown[]) => unknown).apply(this.getContext(plugin), parameters ?? [])) : hook
|
|
538
549
|
|
|
539
|
-
output
|
|
540
|
-
|
|
541
|
-
this.events.emit('plugins:hook:processing:end', {
|
|
542
|
-
duration: Math.round(performance.now() - startTime),
|
|
543
|
-
parameters,
|
|
544
|
-
output,
|
|
545
|
-
strategy,
|
|
546
|
-
hookName,
|
|
547
|
-
plugin,
|
|
548
|
-
})
|
|
550
|
+
this.#emitProcessingEnd({ startTime, output, strategy, hookName, plugin, parameters })
|
|
549
551
|
|
|
550
|
-
return
|
|
552
|
+
return output as ReturnType<ParseResult<H>>
|
|
551
553
|
} catch (error) {
|
|
552
554
|
this.events.emit('error', error as Error, {
|
|
553
555
|
plugin,
|
|
@@ -568,7 +570,6 @@ export class PluginManager {
|
|
|
568
570
|
* @param hookName Name of the plugin hook. Must be in `PluginHooks`.
|
|
569
571
|
* @param args Arguments passed to the plugin hook.
|
|
570
572
|
* @param plugin The actual plugin
|
|
571
|
-
* @param replaceContext When passed, the plugin context can be overridden.
|
|
572
573
|
*/
|
|
573
574
|
#executeSync<H extends PluginLifecycleHooks>({
|
|
574
575
|
strategy,
|
|
@@ -582,7 +583,6 @@ export class PluginManager {
|
|
|
582
583
|
plugin: PluginWithLifeCycle
|
|
583
584
|
}): ReturnType<ParseResult<H>> | null {
|
|
584
585
|
const hook = plugin[hookName]
|
|
585
|
-
let output: unknown
|
|
586
586
|
|
|
587
587
|
if (!hook) {
|
|
588
588
|
return null
|
|
@@ -598,36 +598,14 @@ export class PluginManager {
|
|
|
598
598
|
const startTime = performance.now()
|
|
599
599
|
|
|
600
600
|
try {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
output = fn
|
|
606
|
-
|
|
607
|
-
this.events.emit('plugins:hook:processing:end', {
|
|
608
|
-
duration: Math.round(performance.now() - startTime),
|
|
609
|
-
parameters,
|
|
610
|
-
output,
|
|
611
|
-
strategy,
|
|
612
|
-
hookName,
|
|
613
|
-
plugin,
|
|
614
|
-
})
|
|
615
|
-
|
|
616
|
-
return fn
|
|
617
|
-
}
|
|
601
|
+
const output =
|
|
602
|
+
typeof hook === 'function'
|
|
603
|
+
? ((hook as (...args: unknown[]) => unknown).apply(this.getContext(plugin), parameters) as ReturnType<ParseResult<H>>)
|
|
604
|
+
: (hook as ReturnType<ParseResult<H>>)
|
|
618
605
|
|
|
619
|
-
output
|
|
620
|
-
|
|
621
|
-
this.events.emit('plugins:hook:processing:end', {
|
|
622
|
-
duration: Math.round(performance.now() - startTime),
|
|
623
|
-
parameters,
|
|
624
|
-
output,
|
|
625
|
-
strategy,
|
|
626
|
-
hookName,
|
|
627
|
-
plugin,
|
|
628
|
-
})
|
|
606
|
+
this.#emitProcessingEnd({ startTime, output, strategy, hookName, plugin, parameters })
|
|
629
607
|
|
|
630
|
-
return
|
|
608
|
+
return output
|
|
631
609
|
} catch (error) {
|
|
632
610
|
this.events.emit('error', error as Error, {
|
|
633
611
|
plugin,
|
package/src/PromiseManager.ts
CHANGED
|
@@ -3,17 +3,15 @@ import { hookFirst, hookParallel, hookSeq } from './utils/executeStrategies.ts'
|
|
|
3
3
|
|
|
4
4
|
type PromiseFunc<T = unknown, T2 = never> = () => T2 extends never ? Promise<T> : Promise<T> | T2
|
|
5
5
|
|
|
6
|
-
type Options<TState =
|
|
6
|
+
type Options<TState = unknown> = {
|
|
7
7
|
nullCheck?: (state: TState) => boolean
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export class PromiseManager<TState =
|
|
10
|
+
export class PromiseManager<TState = unknown> {
|
|
11
11
|
#options: Options<TState> = {}
|
|
12
12
|
|
|
13
13
|
constructor(options: Options<TState> = {}) {
|
|
14
14
|
this.#options = options
|
|
15
|
-
|
|
16
|
-
return this
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
run<TInput extends Array<PromiseFunc<TValue, null>>, TValue, TStrategy extends Strategy, TOutput = StrategySwitch<TStrategy, TInput, TValue>>(
|
|
@@ -26,7 +24,7 @@ export class PromiseManager<TState = any> {
|
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
if (strategy === 'first') {
|
|
29
|
-
return hookFirst<TInput, TValue, TOutput>(promises, this.#options.nullCheck)
|
|
27
|
+
return hookFirst<TInput, TValue, TOutput>(promises, this.#options.nullCheck as ((state: unknown) => boolean) | undefined)
|
|
30
28
|
}
|
|
31
29
|
|
|
32
30
|
if (strategy === 'parallel') {
|