feat: add run command

This commit is contained in:
Justin Lin committed 2025-04-18 15:04:28 +08:00
1 parent e622011b21
commit dae9e841ca
3 files changed
+77 -9

No files matched your search

+20 -1
View File
@@ -1,11 +1,16 @@
/* eslint-disable no-await-in-loop */
import { IQuest, IStage } from './schema'
import { Stage } from './Stage'
import { ICommand, IFileExistenceChecker } from './types'
export class Quest {
private stages: Stage[]
private quest: IQuest
constructor(quest: IQuest) {
constructor(
quest: IQuest,
private checker: IFileExistenceChecker
) {
this.quest = quest
this.stages = quest.stages.map((stage: IStage) => new Stage(stage))
}
@@ -13,4 +18,18 @@ export class Quest {
getActiveStages(completed: string[]) {
return this.stages.filter((stage) => stage.active(completed))
}
async findSatisfiedStage(
command: ICommand,
completed: string[]
): Promise<string | null> {
const stages = this.getActiveStages(completed)
for (const stage of stages) {
const satisfies = await stage.satisfies(command, this.checker)
if (satisfies) {
return stage.id
}
}
return null
}
}
+15 -3
View File
@@ -1,14 +1,18 @@
import { Quest } from './Quest'
import { IQuest } from './schema'
import { ISession } from './types'
import { ICommand, ISession, IFileExistenceChecker } from './types'
export class Session implements ISession {
private completed: string[] = []
private quest: Quest
constructor({ completedStages }: ISession, quest: IQuest) {
constructor(
{ completedStages }: ISession,
quest: IQuest,
checker: IFileExistenceChecker
) {
this.completed = completedStages
this.quest = new Quest(quest)
this.quest = new Quest(quest, checker)
}
get completedStages() {
@@ -22,4 +26,12 @@ export class Session implements ISession {
getActiveStages(): string[] {
return this.quest.getActiveStages(this.completed).map((stage) => stage.id)
}
async runCommand(command: ICommand) {
const stageId = await this.quest.findSatisfiedStage(command, this.completed)
if (stageId) {
this.complete(stageId)
}
return stageId
}
}