feat: add pwdmatcher and update error matcher

This commit is contained in:
wancat committed 2024-03-06 23:28:28 +08:00
1 parent 3b4a808131
commit 9eb08b45f4
3 files changed
+81 -15

No files matched your search

+30 -7
View File
@@ -3,15 +3,12 @@ import { Command } from './command'
import { Matcher } from './matcher'
export class PatternMatcher {
protected matcher: RegExp | null
constructor(pattern?: string, args?: string) {
this.matcher = pattern ? new RegExp(pattern, args) : null
protected matcher: RegExp
constructor(pattern: string, args?: string) {
this.matcher = new RegExp(pattern, args)
}
test(input: string): boolean {
if (!this.matcher) {
return true
}
return this.matcher.test(input)
}
}
@@ -23,14 +20,40 @@ export class CommandMatcher extends PatternMatcher implements Matcher {
}
export class ErrorMatcher extends PatternMatcher implements Matcher {
constructor(private errorPattern?: string) {
super(errorPattern || '')
}
match(command: Partial<Command>): boolean {
if (!this.matcher) {
if (!this.errorPattern) {
return command.error === ''
}
return !!command.error && this.test(command.error)
}
}
export class PwdMatcher implements Matcher {
constructor(private pwd: string) {}
match(command: Partial<Command>): boolean {
return command.pwd === this.pwd
}
}
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
}
}