refactor: separate matchers

This commit is contained in:
wancat committed 2024-03-07 00:25:50 +08:00
1 parent 8280f7073d
commit e83d913852
10 files changed
+114 -92

No files matched your search

-73
View File
@@ -1,73 +0,0 @@
/* eslint-disable max-classes-per-file */
import { Command } from './command'
import { Matcher } from './matcher'
export class CommandMatcher implements Matcher {
private matcher: RegExp
constructor(pattern: string, args?: string) {
this.matcher = new RegExp(pattern, args)
}
match(command: Partial<Command>): boolean {
return !!command.command && this.matcher.test(command.command)
}
}
export class ErrorMatcher implements Matcher {
private matcher?: RegExp
constructor(errorPattern?: string) {
if (errorPattern) {
this.matcher = new RegExp(errorPattern)
}
}
match(command: Partial<Command>): boolean {
if (!this.matcher) {
return !command.error
}
return this.matcher.test(command.error || '')
}
}
export class PwdMatcher implements Matcher {
constructor(private pwd: string) {}
match(command: Partial<Command>): boolean {
return command.pwd === this.pwd
}
}
export class OutputMatcher implements Matcher {
private matcher: RegExp
constructor(pattern: string, args?: string) {
this.matcher = new RegExp(pattern, args)
}
match(command: Partial<Command>): boolean {
return !!command.output && this.matcher.test(command.output.trim())
}
}
export interface ICondition {
command?: string
output?: string
error?: string
pwd?: string
}
export class Condition {
private matchers: Matcher[] = []
constructor({ command, output, error, pwd }: ICondition) {
if (command) this.matchers.push(new CommandMatcher(command))
if (output) this.matchers.push(new OutputMatcher(output))
if (pwd) this.matchers.push(new PwdMatcher(pwd))
this.matchers.push(new ErrorMatcher(error)) // always check unexpected error
}
match(command: Partial<Command>): boolean {
return this.matchers.every((matcher) => {
return matcher.match(command)
})
}
}
@@ -0,0 +1,13 @@
import { Command } from '../command.js'
import { Matcher } from './matcher.js'
export class CommandMatcher implements Matcher {
private matcher: RegExp
constructor(command: string, flags?: string) {
this.matcher = new RegExp(command, flags)
}
match(command: Partial<Command>): boolean {
return !!command.command && this.matcher.test(command.command)
}
}
+30
View File
@@ -0,0 +1,30 @@
import { Command } from '../command.js'
import { PwdMatcher } from './pwdMatcher.js'
import { CommandMatcher } from './commandMatcher.js'
import { ErrorMatcher } from './errorMatcher.js'
import { Matcher } from './matcher.js'
import { OutputMatcher } from './outputMatcher.js'
export interface ICondition {
command?: string
output?: string
error?: string
pwd?: string
}
export class Condition {
private matchers: Matcher[] = []
constructor({ command, output, error, pwd }: ICondition) {
if (command) this.matchers.push(new CommandMatcher(command))
if (output) this.matchers.push(new OutputMatcher(output))
if (pwd) this.matchers.push(new PwdMatcher(pwd))
this.matchers.push(new ErrorMatcher(error)) // always check unexpected error
}
match(command: Partial<Command>): boolean {
return this.matchers.every((matcher) => {
return matcher.match(command)
})
}
}
@@ -0,0 +1,18 @@
import { Matcher } from './matcher.js'
import { Command } from '../command.js'
export class ErrorMatcher implements Matcher {
private matcher?: RegExp
constructor(errorPattern?: string) {
if (errorPattern) {
this.matcher = new RegExp(errorPattern)
}
}
match(command: Partial<Command>): boolean {
if (!this.matcher) {
return !command.error
}
return this.matcher.test(command.error || '')
}
}
@@ -1,4 +1,4 @@
import { Command } from './command'
import { Command } from '../command.js'
export interface Matcher {
match(command: Partial<Command>): boolean
@@ -0,0 +1,13 @@
import { Command } from '../command.js'
import { Matcher } from './matcher.js'
export class OutputMatcher implements Matcher {
private matcher: RegExp
constructor(pattern: string, args?: string) {
this.matcher = new RegExp(pattern, args)
}
match(command: Partial<Command>): boolean {
return !!command.output && this.matcher.test(command.output.trim())
}
}
+10
View File
@@ -0,0 +1,10 @@
import { Command } from '../command.js'
import { Matcher } from './matcher.js'
export class PwdMatcher implements Matcher {
constructor(private pwd: string) {}
match(command: Partial<Command>): boolean {
return command.pwd === this.pwd
}
}