From 36629a3962343189e7d67e487793f04120b69902 Mon Sep 17 00:00:00 2001 From: Nikolai Laevskii Date: Thu, 12 Oct 2023 05:54:13 +0200 Subject: [PATCH] Remove state from context --- .../helpers/__tests__/command-runner.test.ts | 13 ++--- .../src/command-runner/command-runner.ts | 48 +++++++------------ packages/helpers/src/command-runner/core.ts | 21 +++----- packages/helpers/src/command-runner/types.ts | 9 ++-- 4 files changed, 33 insertions(+), 58 deletions(-) diff --git a/packages/helpers/__tests__/command-runner.test.ts b/packages/helpers/__tests__/command-runner.test.ts index cbd6ba6a..d4ec6746 100644 --- a/packages/helpers/__tests__/command-runner.test.ts +++ b/packages/helpers/__tests__/command-runner.test.ts @@ -74,16 +74,13 @@ describe('command-runner', () => { expect(middleware).toHaveBeenCalledWith( expect.objectContaining({ - commandLine: 'echo', args: ['hello', 'world'], - options: expect.objectContaining({ - silent: true - }), - stdout: 'hello', - stderr: '', - exitCode: 0, + commandLine: 'echo', execerr: null, - state: null + exitCode: 0, + options: {failOnStdErr: false, ignoreReturnCode: true, silent: true}, + stderr: '', + stdout: 'hello' }), expect.any(Function) ) diff --git a/packages/helpers/src/command-runner/command-runner.ts b/packages/helpers/src/command-runner/command-runner.ts index d671d8c7..ac2120c4 100644 --- a/packages/helpers/src/command-runner/command-runner.ts +++ b/packages/helpers/src/command-runner/command-runner.ts @@ -25,10 +25,10 @@ const commandRunnerActions = { log: produceLog } as const -export class CommandRunner extends CommandRunnerBase { +export class CommandRunner extends CommandRunnerBase { on( event: CommandRunnerEventTypeExtended | CommandRunnerEventTypeExtended[], - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { const middleware = @@ -36,12 +36,12 @@ export class CommandRunner extends CommandRunnerBase { ? [commandRunnerActions[action](message)] : [action] - this.use(matchEvent(event, middleware as CommandRunnerMiddleware[])) + this.use(matchEvent(event, middleware)) return this } onEmptyOutput( - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { this.onOutput(stdout => stdout?.trim() === '', action, message) @@ -49,7 +49,7 @@ export class CommandRunner extends CommandRunnerBase { } onExecutionError( - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { const middleware = @@ -57,18 +57,13 @@ export class CommandRunner extends CommandRunnerBase { ? [commandRunnerActions[action](message)] : [action] - this.use( - matchSpecificError( - ({type}) => type === 'execerr', - middleware as CommandRunnerMiddleware[] - ) - ) + this.use(matchSpecificError(({type}) => type === 'execerr', middleware)) return this } onStdError( - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { const middleware = @@ -76,18 +71,13 @@ export class CommandRunner extends CommandRunnerBase { ? [commandRunnerActions[action](message)] : [action] - this.use( - matchSpecificError( - ({type}) => type === 'stderr', - middleware as CommandRunnerMiddleware[] - ) - ) + this.use(matchSpecificError(({type}) => type === 'stderr', middleware)) return this } onError( - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { return this.on(['execerr', 'stderr'], action, message) @@ -95,7 +85,7 @@ export class CommandRunner extends CommandRunnerBase { onSpecificError( matcher: ErrorMatcher, - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { const middleware = @@ -103,15 +93,13 @@ export class CommandRunner extends CommandRunnerBase { ? [commandRunnerActions[action](message)] : [action] - this.use( - matchSpecificError(matcher, middleware as CommandRunnerMiddleware[]) - ) + this.use(matchSpecificError(matcher, middleware)) return this } onSuccess( - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { return this.on('ok', action, message) @@ -119,7 +107,7 @@ export class CommandRunner extends CommandRunnerBase { onExitCode( matcher: ExitCodeMatcher, - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { const middleware = @@ -127,14 +115,14 @@ export class CommandRunner extends CommandRunnerBase { ? [commandRunnerActions[action](message)] : [action] - this.use(matchExitCode(matcher, middleware as CommandRunnerMiddleware[])) + this.use(matchExitCode(matcher, middleware)) return this } onOutput( matcher: OutputMatcher, - action: CommandRunnerActionType | CommandRunnerMiddleware, + action: CommandRunnerActionType | CommandRunnerMiddleware, message?: string ): this { const middleware = @@ -142,14 +130,14 @@ export class CommandRunner extends CommandRunnerBase { ? [commandRunnerActions[action](message)] : [action] - this.use(matchOutput(matcher, middleware as CommandRunnerMiddleware[])) + this.use(matchOutput(matcher, middleware)) return this } } -export const createCommandRunner = ( +export const createCommandRunner = ( commandLine: string, args: string[] = [], options: CommandRunnerOptions = {} -): CommandRunner => new CommandRunner(commandLine, args, options, exec.exec) +): CommandRunner => new CommandRunner(commandLine, args, options, exec.exec) diff --git a/packages/helpers/src/command-runner/core.ts b/packages/helpers/src/command-runner/core.ts index 894fa05b..b93dd26d 100644 --- a/packages/helpers/src/command-runner/core.ts +++ b/packages/helpers/src/command-runner/core.ts @@ -8,9 +8,7 @@ import { } from './types' export const promisifyCommandRunnerMiddleware = - ( - middleware: CommandRunnerMiddleware - ): CommandRunnerMiddlewarePromisified => + (middleware: CommandRunnerMiddleware): CommandRunnerMiddlewarePromisified => async (ctx, next) => { return Promise.resolve(middleware(ctx, next)) } @@ -36,7 +34,7 @@ export const composeCommandRunnerMiddleware = await nextLocal() } -export class CommandRunnerBase { +export class CommandRunnerBase { private middleware: CommandRunnerMiddlewarePromisified[] = [] constructor( @@ -46,12 +44,8 @@ export class CommandRunnerBase { private executor: typeof exec.exec = exec.exec ) {} - use(middleware: CommandRunnerMiddleware): this { - this.middleware.push( - promisifyCommandRunnerMiddleware( - middleware as CommandRunnerMiddleware - ) - ) + use(middleware: CommandRunnerMiddleware): this { + this.middleware.push(promisifyCommandRunnerMiddleware(middleware)) return this } @@ -64,21 +58,20 @@ export class CommandRunnerBase { /* overrides options for this specific execution if not undefined */ options?: CommandRunnerOptions - ): Promise> { + ): Promise { const requiredOptions: exec.ExecOptions = { ignoreReturnCode: true, failOnStdErr: false } - const context: CommandRunnerContext = { + const context: CommandRunnerContext = { commandLine: commandLine ?? this.commandLine, args: args ?? this.args, options: {...(options ?? this.options), ...requiredOptions}, stdout: null, stderr: null, execerr: null, - exitCode: null, - state: null + exitCode: null } try { diff --git a/packages/helpers/src/command-runner/types.ts b/packages/helpers/src/command-runner/types.ts index 54e7fad7..12e0eb7f 100644 --- a/packages/helpers/src/command-runner/types.ts +++ b/packages/helpers/src/command-runner/types.ts @@ -2,7 +2,7 @@ import * as exec from '@actions/exec' /* CommandRunner core */ -export interface CommandRunnerContext { +export interface CommandRunnerContext { /* Inputs with which command was executed */ commandLine: string args: string[] @@ -13,9 +13,6 @@ export interface CommandRunnerContext { stderr: string | null stdout: string | null exitCode: number | null - - /* Arbitrary state that can be change during middleware execution if needed */ - state: S | null } /* Middlewares as used internally in CommandRunner */ @@ -25,8 +22,8 @@ export type CommandRunnerMiddlewarePromisified = ( ) => Promise /* Middlewares as used by the user */ -export type CommandRunnerMiddleware = ( - ctx: CommandRunnerContext, +export type CommandRunnerMiddleware = ( + ctx: CommandRunnerContext, next: () => Promise ) => void | Promise