wip: integrate filegraph into session

This commit is contained in:
Justin Lin committed 2025-04-24 22:05:10 +08:00
1 parent cdd22634e0
commit 2d95ca382f
13 files changed
+105 -42

No files matched your search

+3 -1
View File
@@ -1,17 +1,19 @@
{
"name": "@linux-odyssey/file-graph",
"version": "1.0.0",
"type": "module",
"main": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts"
}
},
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.cjs.json",
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json",
"dev": "tsc -w",
"test": "yarn build && jest"
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { FileObject } from './file.js'
import FileNode from './fileNode.js'
export interface Event {
export interface FileGraphUpdateEvent {
add?: FileObject[]
remove?: FileObject[]
discover?: FileObject[]
@@ -42,7 +42,7 @@ export default class FileGraph extends FileNode {
this.merge(newNode)
}
handleEvent(event: Event) {
handleEvent(event: FileGraphUpdateEvent) {
// eslint-disable-next-line default-case
switch (true) {
case !!event.add:
+9 -2
View File
@@ -6,10 +6,17 @@ import {
} from './errors.js'
import { dirname } from './utils.js'
export default class FileNode extends File {
export interface IFileNode {
path: string
type: 'file' | 'directory'
discovered: boolean
children?: IFileNode[]
}
export default class FileNode extends File implements IFileNode {
children: FileNode[]
constructor(file: FileObject | FileNode) {
constructor(file: FileObject | IFileNode) {
super(file)
this.children = []
if ((<FileNode>file).children)
+2 -9
View File
@@ -1,10 +1,3 @@
export { default as File, FileObject } from './file.js'
export { default as FileNode } from './fileNode.js'
export { default as FileGraph, Event } from './fileGraph.js'
export interface IFileNode {
path: string
type: string
discovered: boolean
children?: IFileNode[]
}
export { default as FileNode, IFileNode } from './fileNode.js'
export { default as FileGraph, FileGraphUpdateEvent } from './fileGraph.js'
+4 -1
View File
@@ -4,8 +4,11 @@
"rootDir": "src",
"composite": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "./dist/esm",
"declarationDir": "./dist/types"
"declarationDir": "./dist/types",
"target": "ES2020",
"useDefineForClassFields": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
+5 -1
View File
@@ -7,6 +7,7 @@ import {
IFileExistenceChecker,
IResponse,
ITask,
ICommandParams,
} from './schema'
export class GameSession {
@@ -22,7 +23,7 @@ export class GameSession {
this.completed = completedEvents || []
this.quest = new Quest(quest, checker)
this.graph = new FileGraph(
graph ?? new FileNode({ path: '/', type: 'root', discovered: false })
graph ?? { path: '/', type: 'directory', discovered: false }
)
}
@@ -39,6 +40,9 @@ export class GameSession {
}
async runCommand(command: ICommand) {
if (command.params) {
this.graph.handleEvent(command.params)
}
const stageId = await this.quest.findSatisfiedEvent(command, this.completed)
if (stageId) {
this.complete(stageId)
+43
View File
@@ -168,4 +168,47 @@ describe('Session', () => {
{ id: 'stage4', name: 'Stage 4', completed: false },
])
})
it('should update graph', () => {
const session = new GameSession({}, quest, new MockFileChecker())
expect(session.getGraph()).toMatchObject({
path: '/',
type: 'directory',
discovered: false,
})
session.runCommand({
command: 'ls',
params: {
discover: [
{
path: '/home/user',
type: 'directory',
discovered: true,
},
],
},
})
expect(session.getGraph()).toMatchObject({
path: '/',
type: 'directory',
discovered: false,
children: [
{
path: '/home',
type: 'directory',
discovered: false,
children: [
{
path: '/home/user',
type: 'directory',
discovered: true,
},
],
},
],
})
})
})
+5 -2
View File
@@ -2,14 +2,17 @@ import { model, Schema, Types } from 'mongoose'
export interface INode {
path: string
type: string
type: 'file' | 'directory'
discovered: boolean
children?: INode[]
}
const nodeSchema = new Schema<INode>({
path: String,
type: String,
type: {
type: String,
enum: ['file', 'directory'],
},
discovered: {
type: Boolean,
default: false,