|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as os from 'node:os'; |
| 5 | + |
| 6 | +import type { IReporterEventEnvelope } from '../events/IReporterEventEnvelope'; |
| 7 | + |
| 8 | +/** |
| 9 | + * The exit code returned for success, including warning-only success. |
| 10 | + * |
| 11 | + * @beta |
| 12 | + */ |
| 13 | +export const EXIT_CODE_SUCCESS: 0 = 0; |
| 14 | + |
| 15 | +/** |
| 16 | + * The exit code returned for a Rush or operation failure and for logical cancellation. |
| 17 | + * |
| 18 | + * @beta |
| 19 | + */ |
| 20 | +export const EXIT_CODE_FAILURE: 1 = 1; |
| 21 | + |
| 22 | +/** |
| 23 | + * The logical outcome of a command, independent of presentation. |
| 24 | + * |
| 25 | + * @beta |
| 26 | + */ |
| 27 | +export type RushCommandOutcome = 'succeeded' | 'failed' | 'cancelled' | 'signal'; |
| 28 | + |
| 29 | +/** |
| 30 | + * The resolved exit status of a command. |
| 31 | + * |
| 32 | + * @beta |
| 33 | + */ |
| 34 | +export interface IRushExitStatus { |
| 35 | + /** |
| 36 | + * The process exit code. |
| 37 | + */ |
| 38 | + readonly exitCode: number; |
| 39 | + |
| 40 | + /** |
| 41 | + * The logical outcome. |
| 42 | + */ |
| 43 | + readonly outcome: RushCommandOutcome; |
| 44 | + |
| 45 | + /** |
| 46 | + * The terminating OS signal, when the outcome is `signal`. |
| 47 | + */ |
| 48 | + readonly signal?: NodeJS.Signals; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Returns the conventional exit code for an OS signal, `128 + signalNumber`. |
| 53 | + * |
| 54 | + * @param signal - the terminating signal |
| 55 | + * |
| 56 | + * @beta |
| 57 | + */ |
| 58 | +export function getSignalExitCode(signal: NodeJS.Signals): number { |
| 59 | + const signalNumber: number | undefined = (os.constants.signals as Record<string, number>)[signal]; |
| 60 | + return 128 + (signalNumber ?? 0); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Options for {@link resolveExitStatus}. |
| 65 | + * |
| 66 | + * @beta |
| 67 | + */ |
| 68 | +export interface IResolveExitStatusOptions { |
| 69 | + /** |
| 70 | + * Whether the command had a Rush or operation failure. Warnings alone are not failures. |
| 71 | + */ |
| 72 | + readonly hasFailures?: boolean; |
| 73 | + |
| 74 | + /** |
| 75 | + * Whether the command was logically cancelled or aborted. |
| 76 | + */ |
| 77 | + readonly cancelled?: boolean; |
| 78 | + |
| 79 | + /** |
| 80 | + * The terminating OS signal, if any. |
| 81 | + */ |
| 82 | + readonly signal?: NodeJS.Signals; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Resolves a command's exit status from its outcome. |
| 87 | + * |
| 88 | + * @remarks |
| 89 | + * A signal termination yields the conventional signal-derived status. |
| 90 | + * Cancellation and failure yield `1`. Everything else, including warning-only |
| 91 | + * success, yields `0`. The reporter mode and diagnostic categories are never |
| 92 | + * inputs, so they can never select the exit code. |
| 93 | + * |
| 94 | + * @param options - the command outcome |
| 95 | + * |
| 96 | + * @beta |
| 97 | + */ |
| 98 | +export function resolveExitStatus(options: IResolveExitStatusOptions): IRushExitStatus { |
| 99 | + if (options.signal) { |
| 100 | + return { exitCode: getSignalExitCode(options.signal), outcome: 'signal', signal: options.signal }; |
| 101 | + } |
| 102 | + if (options.cancelled) { |
| 103 | + return { exitCode: EXIT_CODE_FAILURE, outcome: 'cancelled' }; |
| 104 | + } |
| 105 | + if (options.hasFailures) { |
| 106 | + return { exitCode: EXIT_CODE_FAILURE, outcome: 'failed' }; |
| 107 | + } |
| 108 | + return { exitCode: EXIT_CODE_SUCCESS, outcome: 'succeeded' }; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Options for {@link resolveExitStatusFromEvents}. |
| 113 | + * |
| 114 | + * @beta |
| 115 | + */ |
| 116 | +export interface IResolveExitStatusFromEventsOptions { |
| 117 | + /** |
| 118 | + * Whether the command was logically cancelled or aborted. |
| 119 | + */ |
| 120 | + readonly cancelled?: boolean; |
| 121 | + |
| 122 | + /** |
| 123 | + * The terminating OS signal, if any. |
| 124 | + */ |
| 125 | + readonly signal?: NodeJS.Signals; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Resolves a command's exit status from its structured event stream. |
| 130 | + * |
| 131 | + * @remarks |
| 132 | + * A failure is any failed command result, any error-severity diagnostic, or any |
| 133 | + * failed operation. Diagnostic categories and the selected reporter are never |
| 134 | + * consulted, so they cannot influence the exit code. Warning-severity |
| 135 | + * diagnostics never cause failure. |
| 136 | + * |
| 137 | + * @param events - the structured events emitted during the command |
| 138 | + * @param options - cancellation and signal state |
| 139 | + * |
| 140 | + * @beta |
| 141 | + */ |
| 142 | +export function resolveExitStatusFromEvents( |
| 143 | + events: readonly IReporterEventEnvelope<unknown>[], |
| 144 | + options: IResolveExitStatusFromEventsOptions = {} |
| 145 | +): IRushExitStatus { |
| 146 | + let hasFailures: boolean = false; |
| 147 | + for (const event of events) { |
| 148 | + if (event.type === 'commandResult') { |
| 149 | + if ((event.payload as { succeeded: boolean }).succeeded === false) { |
| 150 | + hasFailures = true; |
| 151 | + } |
| 152 | + } else if (event.type === 'diagnosticEmitted') { |
| 153 | + if ((event.payload as { severity?: string }).severity === 'error') { |
| 154 | + hasFailures = true; |
| 155 | + } |
| 156 | + } else if (event.type === 'operationStatusChanged') { |
| 157 | + if ((event.payload as { status?: string }).status === 'failure') { |
| 158 | + hasFailures = true; |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return resolveExitStatus({ hasFailures, cancelled: options.cancelled, signal: options.signal }); |
| 164 | +} |
0 commit comments