diff --git a/packages/game/src/condition/Condition.ts b/packages/game/src/condition/Condition.ts index 5227413..fc8aea4 100644 --- a/packages/game/src/condition/Condition.ts +++ b/packages/game/src/condition/Condition.ts @@ -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 { + if (this.matchers.length === 0) return true + return checkFiles(checker, this.files) + } } diff --git a/packages/game/src/condition/FileMatcher.ts b/packages/game/src/condition/FileMatcher.ts index ac9f709..c364e09 100644 --- a/packages/game/src/condition/FileMatcher.ts +++ b/packages/game/src/condition/FileMatcher.ts @@ -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, diff --git a/packages/game/src/types.ts b/packages/game/src/types.ts index 57724a9..4bcbb1a 100644 --- a/packages/game/src/types.ts +++ b/packages/game/src/types.ts @@ -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 } diff --git a/packages/game/test/asyncCondition.test.ts b/packages/game/test/asyncCondition.test.ts index 1a6503a..da4399d 100644 --- a/packages/game/test/asyncCondition.test.ts +++ b/packages/game/test/asyncCondition.test.ts @@ -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) +})