mirror of https://github.com/actions/toolkit
Remove state from context
parent
b4df76861f
commit
36629a3962
|
@ -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)
|
||||
)
|
||||
|
|
|
@ -25,10 +25,10 @@ const commandRunnerActions = {
|
|||
log: produceLog
|
||||
} as const
|
||||
|
||||
export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
||||
export class CommandRunner extends CommandRunnerBase {
|
||||
on(
|
||||
event: CommandRunnerEventTypeExtended | CommandRunnerEventTypeExtended[],
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
const middleware =
|
||||
|
@ -36,12 +36,12 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
? [commandRunnerActions[action](message)]
|
||||
: [action]
|
||||
|
||||
this.use(matchEvent(event, middleware as CommandRunnerMiddleware[]))
|
||||
this.use(matchEvent(event, middleware))
|
||||
return this
|
||||
}
|
||||
|
||||
onEmptyOutput(
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
this.onOutput(stdout => stdout?.trim() === '', action, message)
|
||||
|
@ -49,7 +49,7 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
}
|
||||
|
||||
onExecutionError(
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
const middleware =
|
||||
|
@ -57,18 +57,13 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
? [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<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
const middleware =
|
||||
|
@ -76,18 +71,13 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
? [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<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
return this.on(['execerr', 'stderr'], action, message)
|
||||
|
@ -95,7 +85,7 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
|
||||
onSpecificError(
|
||||
matcher: ErrorMatcher,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
const middleware =
|
||||
|
@ -103,15 +93,13 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
? [commandRunnerActions[action](message)]
|
||||
: [action]
|
||||
|
||||
this.use(
|
||||
matchSpecificError(matcher, middleware as CommandRunnerMiddleware[])
|
||||
)
|
||||
this.use(matchSpecificError(matcher, middleware))
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
onSuccess(
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
return this.on('ok', action, message)
|
||||
|
@ -119,7 +107,7 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
|
||||
onExitCode(
|
||||
matcher: ExitCodeMatcher,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
const middleware =
|
||||
|
@ -127,14 +115,14 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
? [commandRunnerActions[action](message)]
|
||||
: [action]
|
||||
|
||||
this.use(matchExitCode(matcher, middleware as CommandRunnerMiddleware[]))
|
||||
this.use(matchExitCode(matcher, middleware))
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
onOutput(
|
||||
matcher: OutputMatcher,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware<S>,
|
||||
action: CommandRunnerActionType | CommandRunnerMiddleware,
|
||||
message?: string
|
||||
): this {
|
||||
const middleware =
|
||||
|
@ -142,14 +130,14 @@ export class CommandRunner<S = unknown> extends CommandRunnerBase<S> {
|
|||
? [commandRunnerActions[action](message)]
|
||||
: [action]
|
||||
|
||||
this.use(matchOutput(matcher, middleware as CommandRunnerMiddleware[]))
|
||||
this.use(matchOutput(matcher, middleware))
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
export const createCommandRunner = <S = unknown>(
|
||||
export const createCommandRunner = (
|
||||
commandLine: string,
|
||||
args: string[] = [],
|
||||
options: CommandRunnerOptions = {}
|
||||
): CommandRunner<S> => new CommandRunner(commandLine, args, options, exec.exec)
|
||||
): CommandRunner => new CommandRunner(commandLine, args, options, exec.exec)
|
||||
|
|
|
@ -8,9 +8,7 @@ import {
|
|||
} from './types'
|
||||
|
||||
export const promisifyCommandRunnerMiddleware =
|
||||
(
|
||||
middleware: CommandRunnerMiddleware<unknown>
|
||||
): 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<S = unknown> {
|
||||
export class CommandRunnerBase {
|
||||
private middleware: CommandRunnerMiddlewarePromisified[] = []
|
||||
|
||||
constructor(
|
||||
|
@ -46,12 +44,8 @@ export class CommandRunnerBase<S = unknown> {
|
|||
private executor: typeof exec.exec = exec.exec
|
||||
) {}
|
||||
|
||||
use(middleware: CommandRunnerMiddleware<S>): this {
|
||||
this.middleware.push(
|
||||
promisifyCommandRunnerMiddleware(
|
||||
middleware as CommandRunnerMiddleware<unknown>
|
||||
)
|
||||
)
|
||||
use(middleware: CommandRunnerMiddleware): this {
|
||||
this.middleware.push(promisifyCommandRunnerMiddleware(middleware))
|
||||
return this
|
||||
}
|
||||
|
||||
|
@ -64,21 +58,20 @@ export class CommandRunnerBase<S = unknown> {
|
|||
|
||||
/* overrides options for this specific execution if not undefined */
|
||||
options?: CommandRunnerOptions
|
||||
): Promise<CommandRunnerContext<S>> {
|
||||
): Promise<CommandRunnerContext> {
|
||||
const requiredOptions: exec.ExecOptions = {
|
||||
ignoreReturnCode: true,
|
||||
failOnStdErr: false
|
||||
}
|
||||
|
||||
const context: CommandRunnerContext<S> = {
|
||||
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 {
|
||||
|
|
|
@ -2,7 +2,7 @@ import * as exec from '@actions/exec'
|
|||
|
||||
/* CommandRunner core */
|
||||
|
||||
export interface CommandRunnerContext<S = unknown> {
|
||||
export interface CommandRunnerContext {
|
||||
/* Inputs with which command was executed */
|
||||
commandLine: string
|
||||
args: string[]
|
||||
|
@ -13,9 +13,6 @@ export interface CommandRunnerContext<S = unknown> {
|
|||
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<void>
|
||||
|
||||
/* Middlewares as used by the user */
|
||||
export type CommandRunnerMiddleware<S = unknown> = (
|
||||
ctx: CommandRunnerContext<S>,
|
||||
export type CommandRunnerMiddleware = (
|
||||
ctx: CommandRunnerContext,
|
||||
next: () => Promise<void>
|
||||
) => void | Promise<void>
|
||||
|
||||
|
|
Loading…
Reference in New Issue