feat: condition matcher

This commit is contained in:
wancat committed 2024-03-06 23:54:03 +08:00
1 parent cb26e1dcb0
commit 4f3752060d
3 files changed
+37 -26

No files matched your search

+17 -8
View File
@@ -3,7 +3,7 @@ import { Command } from './command'
import { Matcher } from './matcher'
export class PatternMatcher {
protected matcher: RegExp
private matcher: RegExp
constructor(pattern: string, args?: string) {
this.matcher = new RegExp(pattern, args)
}
@@ -19,16 +19,19 @@ export class CommandMatcher extends PatternMatcher implements Matcher {
}
}
export class ErrorMatcher extends PatternMatcher implements Matcher {
constructor(private errorPattern?: string) {
super(errorPattern || '')
export class ErrorMatcher implements Matcher {
private matcher?: PatternMatcher
constructor(errorPattern?: string) {
if (errorPattern) {
this.matcher = new PatternMatcher(errorPattern)
}
}
match(command: Partial<Command>): boolean {
if (!this.errorPattern) {
return command.error === ''
if (!this.matcher) {
return !command.error
}
return !!command.error && this.test(command.error)
return this.matcher.test(command.error || '')
}
}
@@ -58,8 +61,14 @@ export class Condition {
constructor({ command, output, error, pwd }: ICondition) {
if (command) this.matchers.push(new CommandMatcher(command))
// if (output) this.matchers.push(new OutputMatcher(output))
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)
})
}
}