feat: add checkfiles into Condition

This commit is contained in:
wancat committed 2024-03-08 00:24:51 +08:00
1 parent d612b2d6af
commit 65ad72223b
4 files changed
+36 -8

No files matched your search

+15 -3
View File
@@ -1,5 +1,9 @@
/* eslint-disable max-classes-per-file */
import { Command, ICondition } from '../types.js'
import {
Command,
ICondition,
IFileExistenceChecker,
FileExistenceInput,
} from '../types.js'
import { PwdMatcher } from './PwdMatcher.js'
import { CommandMatcher } from './CommandMatcher.js'
import { ErrorMatcher } from './ErrorMatcher.js'
@@ -7,11 +11,13 @@ import { Matcher } from './Matcher.js'
import { OutputMatcher } from './OutputMatcher.js'
import { OrMatcher } from './OrMatcher.js'
import { NotMatcher } from './NotMatcher.js'
import { checkFiles } from './FileMatcher.js'
export class Condition {
private files: FileExistenceInput[] = []
private matchers: Matcher[] = []
constructor({ command, output, error, pwd, or, not }: ICondition) {
constructor({ command, output, error, pwd, or, not, files }: 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))
@@ -20,6 +26,7 @@ export class Condition {
this.matchers.push(new OrMatcher(conditions))
}
if (not) this.matchers.push(new NotMatcher(new Condition(not)))
if (files) this.files = files
this.matchers.push(new ErrorMatcher(error)) // always check unexpected error
}
@@ -28,4 +35,9 @@ export class Condition {
return matcher.match(command)
})
}
async check(checker: IFileExistenceChecker): Promise<boolean> {
if (this.matchers.length === 0) return true
return checkFiles(checker, this.files)
}
}
+1 -5
View File
@@ -1,8 +1,4 @@
import { FileInput, IFileExistenceChecker } from '../types'
interface FileExistenceInput extends FileInput {
exists: boolean
}
import { FileExistenceInput, IFileExistenceChecker } from '../types'
export async function checkFiles(
checker: IFileExistenceChecker,
+5
View File
@@ -12,6 +12,7 @@ export interface ICondition {
pwd?: string
or?: ICondition[]
not?: ICondition
files: FileExistenceInput[]
}
export enum FileType {
@@ -24,6 +25,10 @@ export interface FileInput {
type: FileType
}
export interface FileExistenceInput extends FileInput {
exists: boolean
}
export interface IFileExistenceChecker {
exists(file: FileInput): Promise<boolean>
}
+15
View File
@@ -1,6 +1,7 @@
import { test, expect } from '@jest/globals'
import { IFileExistenceChecker, FileType, FileInput } from '../src/types.js'
import { checkFiles } from '../src/condition/FileMatcher.js'
import { Condition } from '../src/condition/Condition.js'
class MockFileChecker implements IFileExistenceChecker {
private files: FileInput[] = [
@@ -90,3 +91,17 @@ test('file matcher', async () => {
])
).toBe(false)
})
test('condition with file matcher', async () => {
const checker = new MockFileChecker()
const condition = new Condition({
files: [
{
path: '/home/user/hello.txt',
type: FileType.FILE,
exists: true,
},
],
})
expect(await condition.check(checker)).toBe(true)
})