mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: create pattern and command matcher
This commit is contained in:
1 parent
a8a19ca902
commit
3b4a808131
5 files changed
+92
No files matched your search
@@ -0,0 +1,6 @@
|
||||
export interface Command {
|
||||
command: string
|
||||
output: string
|
||||
error: string
|
||||
pwd: string
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/* eslint-disable max-classes-per-file */
|
||||
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
|
||||
}
|
||||
|
||||
test(input: string): boolean {
|
||||
if (!this.matcher) {
|
||||
return true
|
||||
}
|
||||
return this.matcher.test(input)
|
||||
}
|
||||
}
|
||||
|
||||
export class CommandMatcher extends PatternMatcher implements Matcher {
|
||||
match(command: Partial<Command>): boolean {
|
||||
return !!command.command && this.test(command.command)
|
||||
}
|
||||
}
|
||||
|
||||
export class ErrorMatcher extends PatternMatcher implements Matcher {
|
||||
match(command: Partial<Command>): boolean {
|
||||
if (!this.matcher) {
|
||||
return command.error === ''
|
||||
}
|
||||
return !!command.error && this.test(command.error)
|
||||
}
|
||||
}
|
||||
|
||||
export class Condition {
|
||||
private matchers: Matcher[] = []
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { Command } from './command'
|
||||
|
||||
export interface Matcher {
|
||||
match(command: Partial<Command>): boolean
|
||||
}
|
||||
Reference in new issue
Block a user