@reelout/core 0.10.0 → 0.11.0
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/analyzer.js +1 -1
- package/dist/builder.js +1 -1
- package/dist/changelog.js +1 -1
- package/dist/index.js +1 -1
- package/dist/{workflow-Z9WRyGi8.js → workflow-Dk-fKaAx.js} +120 -75
- package/dist/{workflow-Z9WRyGi8.js.map → workflow-Dk-fKaAx.js.map} +1 -1
- package/package.json +1 -1
- package/src/plugin/HookManager.ts +52 -14
- package/src/types.ts +1 -0
- package/src/workflow/executeReleaseWorkflow.ts +94 -65
package/package.json
CHANGED
|
@@ -15,11 +15,24 @@ export const HookPriority = {
|
|
|
15
15
|
|
|
16
16
|
interface HookRegistration<HookName, Context> {
|
|
17
17
|
name: HookName
|
|
18
|
-
handler: HookHandler<Context>
|
|
19
18
|
priority: number
|
|
19
|
+
handler: HookHandler<Context>
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Intercept a registered handler during emission.
|
|
24
|
+
*
|
|
25
|
+
* Runs in place of every registered handler, receiving the shared `context`,
|
|
26
|
+
* the `registration` being dispatched, and the `next` function that continues
|
|
27
|
+
* with the following handler.
|
|
28
|
+
*/
|
|
29
|
+
export type HandlerMiddleware<HookName, Context> = (
|
|
30
|
+
context: Context,
|
|
31
|
+
registration: HookRegistration<HookName, Context>,
|
|
32
|
+
next: NextFunction<Context>
|
|
33
|
+
) => void | Promise<void>
|
|
34
|
+
|
|
35
|
+
type InferContext<Handler extends HookHandler<unknown>> =
|
|
23
36
|
Handler extends HookHandler<infer Context> ? Context : never
|
|
24
37
|
|
|
25
38
|
/**
|
|
@@ -33,9 +46,28 @@ export class HookManager<
|
|
|
33
46
|
> {
|
|
34
47
|
readonly #registry = new Map<
|
|
35
48
|
keyof HookMap,
|
|
36
|
-
HookRegistration<keyof HookMap,
|
|
49
|
+
HookRegistration<keyof HookMap, InferContext<HookMap[keyof HookMap]>>[]
|
|
37
50
|
>()
|
|
38
51
|
|
|
52
|
+
readonly #middleware:
|
|
53
|
+
| HandlerMiddleware<keyof HookMap, InferContext<HookMap[keyof HookMap]>>
|
|
54
|
+
| undefined
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create a hook manager.
|
|
58
|
+
*
|
|
59
|
+
* @param middleware Wrapper invoked in place of every registered handler
|
|
60
|
+
* during emission
|
|
61
|
+
*/
|
|
62
|
+
constructor(
|
|
63
|
+
middleware?: HandlerMiddleware<
|
|
64
|
+
keyof HookMap,
|
|
65
|
+
InferContext<HookMap[keyof HookMap]>
|
|
66
|
+
>
|
|
67
|
+
) {
|
|
68
|
+
this.#middleware = middleware
|
|
69
|
+
}
|
|
70
|
+
|
|
39
71
|
/**
|
|
40
72
|
* Register a handler function for a specific hook.
|
|
41
73
|
*
|
|
@@ -48,7 +80,7 @@ export class HookManager<
|
|
|
48
80
|
*/
|
|
49
81
|
on<HookName extends keyof HookMap>(
|
|
50
82
|
name: HookName,
|
|
51
|
-
handler: HookMap[HookName]
|
|
83
|
+
handler: HookHandler<InferContext<HookMap[HookName]>>,
|
|
52
84
|
priority: number = HookPriority.Normal
|
|
53
85
|
): void {
|
|
54
86
|
const registrations = this.#registry.get(name) ?? []
|
|
@@ -71,7 +103,7 @@ export class HookManager<
|
|
|
71
103
|
*/
|
|
72
104
|
off<HookName extends keyof HookMap>(
|
|
73
105
|
name: HookName,
|
|
74
|
-
handler: HookMap[HookName]
|
|
106
|
+
handler: HookHandler<InferContext<HookMap[HookName]>>
|
|
75
107
|
): void {
|
|
76
108
|
const registrations = this.#registry.get(name)
|
|
77
109
|
|
|
@@ -86,9 +118,10 @@ export class HookManager<
|
|
|
86
118
|
/**
|
|
87
119
|
* Trigger execution of all handlers registered to a hook.
|
|
88
120
|
*
|
|
89
|
-
* Passes `context` through the handler pipeline in descending priority order
|
|
90
|
-
*
|
|
91
|
-
*
|
|
121
|
+
* Passes `context` through the handler pipeline in descending priority order
|
|
122
|
+
* and dispatches each registration through the constructor middleware when
|
|
123
|
+
* one is provided. Enables handlers to control execution flow using `next`
|
|
124
|
+
* and executes `done` after the last handler completes.
|
|
92
125
|
*
|
|
93
126
|
* @param name Name of the hook to trigger
|
|
94
127
|
* @param context Shared payload passed into each handler
|
|
@@ -102,10 +135,9 @@ export class HookManager<
|
|
|
102
135
|
): Promise<void> {
|
|
103
136
|
type Context = InferContext<HookMap[HookName]>
|
|
104
137
|
|
|
105
|
-
const registrations =
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
>[]
|
|
138
|
+
const registrations = this.#registry.get(name) ?? []
|
|
139
|
+
|
|
140
|
+
const middleware = this.#middleware
|
|
109
141
|
|
|
110
142
|
const dispatch = async (index: number, context: Context): Promise<void> => {
|
|
111
143
|
const registration = registrations[index]
|
|
@@ -116,11 +148,17 @@ export class HookManager<
|
|
|
116
148
|
|
|
117
149
|
let isNextCalled = false
|
|
118
150
|
|
|
119
|
-
|
|
151
|
+
const next: NextFunction<Context> = async (context) => {
|
|
120
152
|
if (isNextCalled) return
|
|
121
153
|
isNextCalled = true
|
|
122
154
|
return await dispatch(index + 1, context)
|
|
123
|
-
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const handler: HookHandler<Context> = middleware
|
|
158
|
+
? (context, next) => middleware(context, registration, next)
|
|
159
|
+
: registration.handler
|
|
160
|
+
|
|
161
|
+
return await handler(context, next)
|
|
124
162
|
}
|
|
125
163
|
|
|
126
164
|
return await dispatch(0, context)
|
package/src/types.ts
CHANGED
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
|
|
4
4
|
import { RELEASE_HOOK_NAMES } from '#config'
|
|
5
5
|
import { HookManager, type HookHandler } from '#plugin'
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
ReleaseContext,
|
|
8
|
+
ReleaseHookDefinition,
|
|
9
|
+
ReleaseHookName,
|
|
10
|
+
ReleasePlugin,
|
|
11
|
+
} from '#types'
|
|
7
12
|
import { colors } from 'consola/utils'
|
|
8
13
|
import {
|
|
9
14
|
appendReleaseReportSummary,
|
|
@@ -12,45 +17,33 @@ import {
|
|
|
12
17
|
printReleaseReportAnnotation,
|
|
13
18
|
} from './report'
|
|
14
19
|
|
|
15
|
-
/** Failure of a
|
|
16
|
-
interface
|
|
20
|
+
/** Failure of a hook handler, recorded with the owning hook and plugin. */
|
|
21
|
+
interface HandlerFailure {
|
|
22
|
+
hook: ReleaseHookName
|
|
17
23
|
plugin: string
|
|
18
24
|
error: unknown
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
interface PluginHookRegistration {
|
|
22
|
-
/** Hook name the plugin handler registers under. */
|
|
23
|
-
hookName: ReleaseHookName
|
|
24
|
-
/** Plugin owning the hook entry. */
|
|
25
|
-
plugin: ReleasePlugin
|
|
26
|
-
/**
|
|
27
|
-
* Handle a thrown handler error instead of rethrowing it.
|
|
28
|
-
*
|
|
29
|
-
* When provided, the wrapper passes a thrown error to this callback and
|
|
30
|
-
* continues the middleware chain, so later handlers still run.
|
|
31
|
-
*/
|
|
32
|
-
onHandlerError?: (error: unknown) => void
|
|
33
|
-
}
|
|
34
|
-
|
|
35
27
|
/**
|
|
36
|
-
*
|
|
28
|
+
* Resolve a plugin's hook entry into a normalized hook definition.
|
|
37
29
|
*
|
|
38
|
-
* Normalizes the plugin's hook entry
|
|
39
|
-
* handler
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
30
|
+
* Normalizes the plugin's hook entry — a bare handler function or a
|
|
31
|
+
* `{ priority, handler }` definition — and wraps the handler in shared timing
|
|
32
|
+
* and logging middleware. When the wrapped handler throws, the failure is
|
|
33
|
+
* attributed to the current hook and plugin on `context.outcome` before
|
|
34
|
+
* rethrowing it. The `??=` assignment never overwrites a recorded outcome,
|
|
35
|
+
* so `finalize` failures — which run after the outcome is recorded — leave it
|
|
36
|
+
* unchanged.
|
|
44
37
|
*
|
|
45
|
-
* @param
|
|
46
|
-
* @param
|
|
38
|
+
* @param hookName Name of the hook to resolve
|
|
39
|
+
* @param plugin Plugin owning the hook entry
|
|
40
|
+
* @returns Normalized hook definition, or `undefined` when the plugin does not
|
|
41
|
+
* implement the hook
|
|
47
42
|
*/
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
):
|
|
52
|
-
const { hookName, plugin } = registration
|
|
53
|
-
|
|
43
|
+
function resolvePluginHook(
|
|
44
|
+
hookName: ReleaseHookName,
|
|
45
|
+
plugin: ReleasePlugin
|
|
46
|
+
): ReleaseHookDefinition | undefined {
|
|
54
47
|
let hook = plugin[hookName]
|
|
55
48
|
|
|
56
49
|
if (typeof hook === 'function') {
|
|
@@ -64,9 +57,9 @@ function registerPluginHook(
|
|
|
64
57
|
|
|
65
58
|
const isSilent = import.meta.env.MODE === 'test'
|
|
66
59
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
async (context, next) => {
|
|
60
|
+
return {
|
|
61
|
+
priority,
|
|
62
|
+
handler: async (context, next) => {
|
|
70
63
|
const current = {
|
|
71
64
|
hook: hookName,
|
|
72
65
|
plugin: plugin.name,
|
|
@@ -99,11 +92,6 @@ function registerPluginHook(
|
|
|
99
92
|
} catch (err) {
|
|
100
93
|
hasError = true
|
|
101
94
|
|
|
102
|
-
if (registration.onHandlerError) {
|
|
103
|
-
registration.onHandlerError(err)
|
|
104
|
-
return await runNext()
|
|
105
|
-
}
|
|
106
|
-
|
|
107
95
|
context.outcome ??= {
|
|
108
96
|
status: 'failure',
|
|
109
97
|
error: err,
|
|
@@ -129,8 +117,37 @@ function registerPluginHook(
|
|
|
129
117
|
}
|
|
130
118
|
}
|
|
131
119
|
},
|
|
132
|
-
|
|
133
|
-
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Wrap a hook handler so a thrown error is recorded before rethrowing.
|
|
125
|
+
*
|
|
126
|
+
* Each recorded failure keeps the owning hook and plugin. The hook manager
|
|
127
|
+
* middleware rescues rethrown `finalize` errors so the remaining
|
|
128
|
+
* `finalize` handlers still run; other hook errors keep the fail-fast
|
|
129
|
+
* unwinding behavior.
|
|
130
|
+
*
|
|
131
|
+
* @param handler Hook handler to wrap
|
|
132
|
+
* @param hookName Name of the hook the handler is registered for
|
|
133
|
+
* @param plugin Plugin owning the hook handler
|
|
134
|
+
* @param failures Array of recorded handler failures
|
|
135
|
+
* @returns Wrapped hook handler
|
|
136
|
+
*/
|
|
137
|
+
function wrapHandlerWithFailureCollection(
|
|
138
|
+
handler: HookHandler<ReleaseContext>,
|
|
139
|
+
hookName: ReleaseHookName,
|
|
140
|
+
plugin: ReleasePlugin,
|
|
141
|
+
failures: HandlerFailure[]
|
|
142
|
+
): HookHandler<ReleaseContext> {
|
|
143
|
+
return async (context, next) => {
|
|
144
|
+
try {
|
|
145
|
+
return await handler(context, next)
|
|
146
|
+
} catch (error) {
|
|
147
|
+
failures.push({ hook: hookName, plugin: plugin.name, error })
|
|
148
|
+
throw error
|
|
149
|
+
}
|
|
150
|
+
}
|
|
134
151
|
}
|
|
135
152
|
|
|
136
153
|
/**
|
|
@@ -163,9 +180,10 @@ function throwIfCancelled(context: ReleaseContext): void {
|
|
|
163
180
|
* send notifications and clean up: when a phase hook throws, plugin rollbacks
|
|
164
181
|
* run while the error unwinds, then every `finalize` handler runs with the
|
|
165
182
|
* failure recorded on `context.outcome`. When a `finalize` handler throws,
|
|
166
|
-
* the
|
|
167
|
-
*
|
|
168
|
-
*
|
|
183
|
+
* the hook manager middleware rescues the error so the remaining handlers
|
|
184
|
+
* still run, and each `finalize` failure is logged as a warning after the
|
|
185
|
+
* report. A failing `finalize` handler never changes the workflow outcome
|
|
186
|
+
* and never masks the original workflow error.
|
|
169
187
|
*
|
|
170
188
|
* @param context Release execution context containing runtime options and logger
|
|
171
189
|
* @param plugins List of release plugins to execute
|
|
@@ -177,19 +195,38 @@ export async function executeReleaseWorkflow(
|
|
|
177
195
|
context: ReleaseContext,
|
|
178
196
|
plugins: readonly ReleasePlugin[]
|
|
179
197
|
): Promise<void> {
|
|
180
|
-
const phaseHookNames = RELEASE_HOOK_NAMES.filter(
|
|
181
|
-
(name) => name !== 'finalize'
|
|
182
|
-
)
|
|
183
|
-
|
|
184
198
|
context.outcome = null
|
|
185
199
|
|
|
200
|
+
const handlerFailures: HandlerFailure[] = []
|
|
201
|
+
|
|
186
202
|
const manager = new HookManager<
|
|
187
203
|
Record<ReleaseHookName, HookHandler<ReleaseContext>>
|
|
188
|
-
>()
|
|
204
|
+
>(async (context, registration, next) => {
|
|
205
|
+
if (registration.name !== 'finalize') {
|
|
206
|
+
return registration.handler(context, next)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
return await registration.handler(context, next)
|
|
211
|
+
} catch {
|
|
212
|
+
return next(context)
|
|
213
|
+
}
|
|
214
|
+
})
|
|
189
215
|
|
|
190
|
-
for (const hookName of
|
|
216
|
+
for (const hookName of RELEASE_HOOK_NAMES) {
|
|
191
217
|
for (const plugin of plugins) {
|
|
192
|
-
|
|
218
|
+
const definition = resolvePluginHook(hookName, plugin)
|
|
219
|
+
|
|
220
|
+
if (!definition) continue
|
|
221
|
+
|
|
222
|
+
const handler = wrapHandlerWithFailureCollection(
|
|
223
|
+
definition.handler,
|
|
224
|
+
hookName,
|
|
225
|
+
plugin,
|
|
226
|
+
handlerFailures
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
manager.on(hookName, handler, definition.priority)
|
|
193
230
|
}
|
|
194
231
|
}
|
|
195
232
|
|
|
@@ -197,10 +234,12 @@ export async function executeReleaseWorkflow(
|
|
|
197
234
|
index: number,
|
|
198
235
|
context: ReleaseContext
|
|
199
236
|
): Promise<void> => {
|
|
200
|
-
const hook =
|
|
237
|
+
const hook = RELEASE_HOOK_NAMES[index]
|
|
201
238
|
|
|
202
239
|
if (!hook) return
|
|
203
240
|
|
|
241
|
+
if (hook === 'finalize') return await dispatch(index + 1, context)
|
|
242
|
+
|
|
204
243
|
throwIfCancelled(context)
|
|
205
244
|
|
|
206
245
|
return await manager.emit(hook, context, (context) =>
|
|
@@ -224,17 +263,6 @@ export async function executeReleaseWorkflow(
|
|
|
224
263
|
}
|
|
225
264
|
}
|
|
226
265
|
|
|
227
|
-
const finalizeFailures: FinalizeFailure[] = []
|
|
228
|
-
|
|
229
|
-
for (const plugin of plugins) {
|
|
230
|
-
registerPluginHook(manager, {
|
|
231
|
-
hookName: 'finalize',
|
|
232
|
-
plugin,
|
|
233
|
-
onHandlerError: (error) =>
|
|
234
|
-
finalizeFailures.push({ plugin: plugin.name, error }),
|
|
235
|
-
})
|
|
236
|
-
}
|
|
237
|
-
|
|
238
266
|
await manager.emit('finalize', context)
|
|
239
267
|
|
|
240
268
|
const report = generateReleaseReport(context)
|
|
@@ -243,7 +271,8 @@ export async function executeReleaseWorkflow(
|
|
|
243
271
|
appendReleaseReportSummary(context.logger, context.env, report)
|
|
244
272
|
printReleaseReportAnnotation(context.env, context.stdout, report)
|
|
245
273
|
|
|
246
|
-
for (const failure of
|
|
274
|
+
for (const failure of handlerFailures) {
|
|
275
|
+
if (failure.hook !== 'finalize') continue
|
|
247
276
|
if (failure.error === context.outcome.error) continue
|
|
248
277
|
|
|
249
278
|
context.logger.warn(
|