1
0
Fork 0

Refactor the code inside the constructor

pull/1562/head
Dusan Trickovic 2023-09-19 12:28:23 +02:00
parent 72a2612e0c
commit 31cd25530e
1 changed files with 12 additions and 21 deletions

View File

@ -2,20 +2,11 @@ import * as exec from '@actions/exec'
import * as core from '@actions/core' import * as core from '@actions/core'
export default class CommandHelper { export default class CommandHelper {
private commandText: string
private args: string[]
private options: exec.ExecOptions | undefined
private throwOnError: boolean
private throwOnEmptyOutput: boolean
private failOnError: boolean
private failOnEmptyOutput: boolean
constructor( constructor(
commandText: string, private commandText: string,
args: string[] = [], private args: string[] = [],
options: exec.ExecOptions | undefined = {}, private options: exec.ExecOptions | undefined = {},
config: { private config: {
throwOnError?: boolean throwOnError?: boolean
throwOnEmptyOutput?: boolean throwOnEmptyOutput?: boolean
failOnError?: boolean failOnError?: boolean
@ -25,10 +16,10 @@ export default class CommandHelper {
this.commandText = commandText this.commandText = commandText
this.args = args this.args = args
this.options = options this.options = options
this.throwOnError = config.throwOnError ?? false this.config.throwOnError = config.throwOnError ?? false
this.throwOnEmptyOutput = config.throwOnEmptyOutput ?? false this.config.throwOnEmptyOutput = config.throwOnEmptyOutput ?? false
this.failOnError = config.failOnError ?? false this.config.failOnError = config.failOnError ?? false
this.failOnEmptyOutput = config.failOnEmptyOutput ?? false this.config.failOnEmptyOutput = config.failOnEmptyOutput ?? false
} }
async execute(): Promise<exec.ExecOutput> { async execute(): Promise<exec.ExecOutput> {
@ -39,19 +30,19 @@ export default class CommandHelper {
this.options this.options
) )
if (this.throwOnError && output.stderr) { if (this.config.throwOnError && output.stderr) {
this.onError(output.stderr).throw() this.onError(output.stderr).throw()
} }
if (this.throwOnEmptyOutput && output.stdout.trim() === '') { if (this.config.throwOnEmptyOutput && output.stdout.trim() === '') {
this.onError('Command produced empty output.').throw() this.onError('Command produced empty output.').throw()
} }
if (this.failOnError && output.stderr) { if (this.config.failOnError && output.stderr) {
this.onError(output.stderr).fail() this.onError(output.stderr).fail()
} }
if (this.failOnEmptyOutput && output.stdout.trim() === '') { if (this.config.failOnEmptyOutput && output.stdout.trim() === '') {
this.onError('Command produced empty output.').fail() this.onError('Command produced empty output.').fail()
} }