From b5e20766a35d8635b13f140144a70cab61d76f7d Mon Sep 17 00:00:00 2001 From: wancat Date: Thu, 7 Mar 2024 00:50:42 +0800 Subject: [PATCH] refactor: rename paths --- packages/game/src/command.ts | 6 ----- .../{commandMatcher.ts => CommandMatcher.ts} | 6 ++--- .../condition/{condition.ts => Condition.ts} | 27 +++++++------------ .../{errorMatcher.ts => ErrorMatcher.ts} | 6 ++--- packages/game/src/condition/Matcher.ts | 5 ++++ .../{notMatcher.ts => NotMatcher.ts} | 6 ++--- .../condition/{orMatcher.ts => OrMatcher.ts} | 6 ++--- .../{outputMatcher.ts => OutputMatcher.ts} | 6 ++--- .../{pwdMatcher.ts => PwdMatcher.ts} | 6 ++--- packages/game/src/condition/matcher.ts | 5 ---- packages/game/src/types.ts | 15 +++++++++++ packages/game/test/condition.test.ts | 10 +++---- 12 files changed, 52 insertions(+), 52 deletions(-) delete mode 100644 packages/game/src/command.ts rename packages/game/src/condition/{commandMatcher.ts => CommandMatcher.ts} (66%) rename packages/game/src/condition/{condition.ts => Condition.ts} (57%) rename packages/game/src/condition/{errorMatcher.ts => ErrorMatcher.ts} (71%) create mode 100644 packages/game/src/condition/Matcher.ts rename packages/game/src/condition/{notMatcher.ts => NotMatcher.ts} (52%) rename packages/game/src/condition/{orMatcher.ts => OrMatcher.ts} (58%) rename packages/game/src/condition/{outputMatcher.ts => OutputMatcher.ts} (66%) rename packages/game/src/condition/{pwdMatcher.ts => PwdMatcher.ts} (50%) delete mode 100644 packages/game/src/condition/matcher.ts create mode 100644 packages/game/src/types.ts diff --git a/packages/game/src/command.ts b/packages/game/src/command.ts deleted file mode 100644 index 6faf783..0000000 --- a/packages/game/src/command.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface Command { - command: string - output: string - error: string - pwd: string -} diff --git a/packages/game/src/condition/commandMatcher.ts b/packages/game/src/condition/CommandMatcher.ts similarity index 66% rename from packages/game/src/condition/commandMatcher.ts rename to packages/game/src/condition/CommandMatcher.ts index 2d102b3..1387403 100644 --- a/packages/game/src/condition/commandMatcher.ts +++ b/packages/game/src/condition/CommandMatcher.ts @@ -1,5 +1,5 @@ -import { Command } from '../command.js' -import { Matcher } from './matcher.js' +import { Command } from '../types.js' +import { Matcher } from './Matcher.js' export class CommandMatcher implements Matcher { private matcher: RegExp @@ -7,7 +7,7 @@ export class CommandMatcher implements Matcher { this.matcher = new RegExp(command, flags) } - match(command: Partial): boolean { + match(command: Command): boolean { return !!command.command && this.matcher.test(command.command) } } diff --git a/packages/game/src/condition/condition.ts b/packages/game/src/condition/Condition.ts similarity index 57% rename from packages/game/src/condition/condition.ts rename to packages/game/src/condition/Condition.ts index 6188ef3..5227413 100644 --- a/packages/game/src/condition/condition.ts +++ b/packages/game/src/condition/Condition.ts @@ -1,21 +1,12 @@ /* eslint-disable max-classes-per-file */ -import { Command } from '../command.js' -import { PwdMatcher } from './pwdMatcher.js' -import { CommandMatcher } from './commandMatcher.js' -import { ErrorMatcher } from './errorMatcher.js' -import { Matcher } from './matcher.js' -import { OutputMatcher } from './outputMatcher.js' -import { OrMatcher } from './orMatcher.js' -import { NotMatcher } from './notMatcher.js' - -export interface ICondition { - command?: string - output?: string - error?: string - pwd?: string - or?: ICondition[] - not?: ICondition -} +import { Command, ICondition } from '../types.js' +import { PwdMatcher } from './PwdMatcher.js' +import { CommandMatcher } from './CommandMatcher.js' +import { ErrorMatcher } from './ErrorMatcher.js' +import { Matcher } from './Matcher.js' +import { OutputMatcher } from './OutputMatcher.js' +import { OrMatcher } from './OrMatcher.js' +import { NotMatcher } from './NotMatcher.js' export class Condition { private matchers: Matcher[] = [] @@ -32,7 +23,7 @@ export class Condition { this.matchers.push(new ErrorMatcher(error)) // always check unexpected error } - match(command: Partial): boolean { + match(command: Command): boolean { return this.matchers.every((matcher) => { return matcher.match(command) }) diff --git a/packages/game/src/condition/errorMatcher.ts b/packages/game/src/condition/ErrorMatcher.ts similarity index 71% rename from packages/game/src/condition/errorMatcher.ts rename to packages/game/src/condition/ErrorMatcher.ts index 53898fb..b77a2a8 100644 --- a/packages/game/src/condition/errorMatcher.ts +++ b/packages/game/src/condition/ErrorMatcher.ts @@ -1,5 +1,5 @@ -import { Matcher } from './matcher.js' -import { Command } from '../command.js' +import { Matcher } from './Matcher.js' +import { Command } from '../types.js' export class ErrorMatcher implements Matcher { private matcher?: RegExp @@ -9,7 +9,7 @@ export class ErrorMatcher implements Matcher { } } - match(command: Partial): boolean { + match(command: Command): boolean { if (!this.matcher) { return !command.error } diff --git a/packages/game/src/condition/Matcher.ts b/packages/game/src/condition/Matcher.ts new file mode 100644 index 0000000..862942a --- /dev/null +++ b/packages/game/src/condition/Matcher.ts @@ -0,0 +1,5 @@ +import { Command } from '../types.js' + +export interface Matcher { + match(command: Command): boolean +} diff --git a/packages/game/src/condition/notMatcher.ts b/packages/game/src/condition/NotMatcher.ts similarity index 52% rename from packages/game/src/condition/notMatcher.ts rename to packages/game/src/condition/NotMatcher.ts index ac3ae99..f37f746 100644 --- a/packages/game/src/condition/notMatcher.ts +++ b/packages/game/src/condition/NotMatcher.ts @@ -1,10 +1,10 @@ -import { Command } from '../command.js' -import { Matcher } from './matcher.js' +import { Command } from '../types.js' +import { Matcher } from './Matcher.js' export class NotMatcher implements Matcher { constructor(private condition: Matcher) {} - match(command: Partial): boolean { + match(command: Command): boolean { return !this.condition.match(command) } } diff --git a/packages/game/src/condition/orMatcher.ts b/packages/game/src/condition/OrMatcher.ts similarity index 58% rename from packages/game/src/condition/orMatcher.ts rename to packages/game/src/condition/OrMatcher.ts index 1703c6c..eb51286 100644 --- a/packages/game/src/condition/orMatcher.ts +++ b/packages/game/src/condition/OrMatcher.ts @@ -1,10 +1,10 @@ -import { Command } from '../command.js' -import { Matcher } from './matcher.js' +import { Command } from '../types.js' +import { Matcher } from './Matcher.js' export class OrMatcher implements Matcher { constructor(private conditions: Matcher[]) {} - match(command: Partial): boolean { + match(command: Command): boolean { return this.conditions.some((condition) => condition.match(command)) } } diff --git a/packages/game/src/condition/outputMatcher.ts b/packages/game/src/condition/OutputMatcher.ts similarity index 66% rename from packages/game/src/condition/outputMatcher.ts rename to packages/game/src/condition/OutputMatcher.ts index 3bedb55..643c558 100644 --- a/packages/game/src/condition/outputMatcher.ts +++ b/packages/game/src/condition/OutputMatcher.ts @@ -1,5 +1,5 @@ -import { Command } from '../command.js' -import { Matcher } from './matcher.js' +import { Command } from '../types.js' +import { Matcher } from './Matcher.js' export class OutputMatcher implements Matcher { private matcher: RegExp @@ -7,7 +7,7 @@ export class OutputMatcher implements Matcher { this.matcher = new RegExp(pattern, args) } - match(command: Partial): boolean { + match(command: Command): boolean { return !!command.output && this.matcher.test(command.output.trim()) } } diff --git a/packages/game/src/condition/pwdMatcher.ts b/packages/game/src/condition/PwdMatcher.ts similarity index 50% rename from packages/game/src/condition/pwdMatcher.ts rename to packages/game/src/condition/PwdMatcher.ts index f4e276a..dac9c78 100644 --- a/packages/game/src/condition/pwdMatcher.ts +++ b/packages/game/src/condition/PwdMatcher.ts @@ -1,10 +1,10 @@ -import { Command } from '../command.js' -import { Matcher } from './matcher.js' +import { Command } from '../types.js' +import { Matcher } from './Matcher.js' export class PwdMatcher implements Matcher { constructor(private pwd: string) {} - match(command: Partial): boolean { + match(command: Command): boolean { return command.pwd === this.pwd } } diff --git a/packages/game/src/condition/matcher.ts b/packages/game/src/condition/matcher.ts deleted file mode 100644 index 29afe3f..0000000 --- a/packages/game/src/condition/matcher.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Command } from '../command.js' - -export interface Matcher { - match(command: Partial): boolean -} diff --git a/packages/game/src/types.ts b/packages/game/src/types.ts new file mode 100644 index 0000000..8227198 --- /dev/null +++ b/packages/game/src/types.ts @@ -0,0 +1,15 @@ +export interface Command { + command?: string + output?: string + error?: string + pwd?: string +} + +export interface ICondition { + command?: string + output?: string + error?: string + pwd?: string + or?: ICondition[] + not?: ICondition +} diff --git a/packages/game/test/condition.test.ts b/packages/game/test/condition.test.ts index 554a0ab..11dd056 100644 --- a/packages/game/test/condition.test.ts +++ b/packages/game/test/condition.test.ts @@ -1,9 +1,9 @@ import { test, expect } from '@jest/globals' -import { Condition } from '../src/condition/condition' -import { OutputMatcher } from '../src/condition/outputMatcher' -import { PwdMatcher } from '../src/condition/pwdMatcher' -import { CommandMatcher } from '../src/condition/commandMatcher' -import { ErrorMatcher } from '../src/condition/errorMatcher.js' +import { Condition } from '../src/condition/Condition' +import { OutputMatcher } from '../src/condition/OutputMatcher' +import { PwdMatcher } from '../src/condition/PwdMatcher' +import { CommandMatcher } from '../src/condition/CommandMatcher' +import { ErrorMatcher } from '../src/condition/ErrorMatcher.js' test('command match', () => { const matcher = new CommandMatcher('^echo start$')