@reelout/core 0.9.1 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reelout/core",
3
- "version": "0.9.1",
3
+ "version": "0.11.0",
4
4
  "description": "Release engine for commit analysis, version planning, lifecycle hooks, and release notes.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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
- export type InferContext<Handler> =
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, unknown>[]
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
- * Enables handlers to control execution flow using `next` and executes `done`
91
- * after the last handler completes.
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 = (this.#registry.get(name) ?? []) as HookRegistration<
106
- HookName,
107
- Context
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
- return await registration.handler(context, async (context) => {
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
@@ -178,6 +178,7 @@ export interface ReleaseContext<P extends Package = Package> {
178
178
  releaseChannel: string | undefined
179
179
  allowEmpty: boolean
180
180
  dryRun: boolean
181
+ strict: boolean
181
182
  packages: P[]
182
183
  plans: ReleasePlan<P>[]
183
184
  outcome: ReleaseOutcome | null
@@ -244,6 +245,7 @@ export interface ReleasePlugin<P extends Package = Package> {
244
245
  export type ReleaseHookName = Exclude<keyof ReleasePlugin, 'name' | 'version'>
245
246
 
246
247
  export interface ReleaseReport {
248
+ dryRun: boolean
247
249
  outcome: ReleaseOutcome | null
248
250
  packages: {
249
251
  location: string
@@ -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 { ReleaseContext, ReleaseHookName, ReleasePlugin } from '#types'
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 `finalize` handler, collected for the post-report warning. */
16
- interface FinalizeFailure {
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
- * Register a plugin's hook handler on the workflow hook manager.
28
+ * Resolve a plugin's hook entry into a normalized hook definition.
37
29
  *
38
- * Normalizes the plugin's hook entry into a handler and priority, wraps the
39
- * handler in shared timing and logging middleware, and registers it under
40
- * the registration's hook name. When `onHandlerError` is provided, a thrown
41
- * handler error is passed to it and the middleware chain continues so later
42
- * handlers still run; without it the wrapper attributes the failure to the
43
- * current hook and plugin on `context.outcome` before rethrowing.
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 manager Hook manager receiving the wrapped handler
46
- * @param registration Plugin hook registration carrying the error strategy
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 registerPluginHook(
49
- manager: HookManager<Record<ReleaseHookName, HookHandler<ReleaseContext>>>,
50
- registration: PluginHookRegistration
51
- ): void {
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
- manager.on(
68
- hookName,
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
- priority
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 remaining handlers still run and each failure is logged as a warning
167
- * after the report. A failing `finalize` handler never changes the workflow
168
- * outcome and never masks the original workflow error.
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 phaseHookNames) {
216
+ for (const hookName of RELEASE_HOOK_NAMES) {
191
217
  for (const plugin of plugins) {
192
- registerPluginHook(manager, { hookName, plugin })
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 = phaseHookNames[index]
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 finalizeFailures) {
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(
@@ -19,9 +19,11 @@ type ReportPlan = ReleaseReport['plans'][number]
19
19
  *
20
20
  * Detects a GitHub Actions runner through the `GITHUB_ACTIONS` environment
21
21
  * variable and appends the rendered `report` to the file referenced by
22
- * `GITHUB_STEP_SUMMARY`, so GitHub displays it on the run summary page. Does
23
- * nothing outside GitHub Actions or when no summary file is configured, and
24
- * logs a warning instead of throwing when the summary file cannot be written.
22
+ * `GITHUB_STEP_SUMMARY`, so GitHub displays it on the run summary page. Dry
23
+ * runs append a `(dry run)` marker to the status line; real releases render
24
+ * the status unmarked. Does nothing outside GitHub Actions or when no summary
25
+ * file is configured, and logs a warning instead of throwing when the summary
26
+ * file cannot be written.
25
27
  *
26
28
  * @param logger Logger instance for reporting summary write failures
27
29
  * @param env Readonly process environment variables object
@@ -57,7 +59,10 @@ const formatReleaseReportSummary = (report: ReleaseReport): string => {
57
59
  const lines: string[] = ['## ReelOut Release Report', '']
58
60
 
59
61
  if (report.outcome) {
60
- lines.push(`**Status:** ${STATUS_LABELS[report.outcome.status]}`, '')
62
+ const status = STATUS_LABELS[report.outcome.status]
63
+ const label = report.dryRun ? `${status} (dry run)` : status
64
+
65
+ lines.push(`**Status:** ${label}`, '')
61
66
  }
62
67
 
63
68
  if (report.outcome?.status === 'failure') {
@@ -8,14 +8,16 @@ import { compact } from '#util'
8
8
  * Generate a structured release report from the current workflow state.
9
9
  *
10
10
  * Extracts state information from `context` and organizes it into a report
11
- * containing the terminal release outcome, release plans, and package
12
- * metadata. Omits unversioned release plans from the finalized output.
11
+ * containing the `dryRun` flag, the terminal release outcome, release plans,
12
+ * and package metadata. Omits unversioned release plans from the finalized
13
+ * output.
13
14
  *
14
15
  * @param context Release execution context containing workflow state and plans
15
16
  * @returns Structured release report object containing workflow results
16
17
  */
17
18
  export function generateReleaseReport(context: ReleaseContext): ReleaseReport {
18
19
  return {
20
+ dryRun: context.dryRun,
19
21
  outcome: context.outcome,
20
22
  packages: context.packages.map((pkg) => ({
21
23
  location: pkg.location,
@@ -12,7 +12,8 @@ import { resolveOutcomeErrorMessage } from './resolveErrorMessage'
12
12
  *
13
13
  * Logs the terminal outcome status and details for planned package releases.
14
14
  * Displays package names, directories, and version changes on success, or
15
- * outputs the error message on failure.
15
+ * outputs the error message on failure. Dry runs append a `(dry run)` marker
16
+ * to the status line; real releases print the status unmarked.
16
17
  *
17
18
  * @param logger Logger instance for outputting messages
18
19
  * @param report Release report containing outcome and package details
@@ -27,7 +28,9 @@ export function printReleaseReport(
27
28
  failure: colors.red('FAILURE'),
28
29
  }[report.outcome.status]
29
30
 
30
- logger.info(`Status: ${statusLabel}`)
31
+ logger.info(
32
+ `Status: ${[statusLabel, report.dryRun && colors.yellow('(dry run)')].filter(Boolean).join(' ')}`
33
+ )
31
34
  }
32
35
 
33
36
  if (report.outcome?.status === 'failure') {