Files
linux-odyssey/packages/file-graph/fileGraph.ts
T
Justin Lin f0a09ac4d9 Feat filegraph (#194)
* feat: update filegraph on touch command

* feat: update filegraph on mkdir

* feat: add different parsing opts for each command

* refactor: replace axios with fetch. put handleCommand to a new file

* feat: add config for mounting cli in dev stage

* refactor: clean up unused validators

* feat: add command handler for copy files. reuse resolvePath function

* feat: add rm handler, support specific files deletion. Glob not supported yet

* feat: handle discover cases that the folder is actually empty

* feat: handle discover empty folder better and support rmdir

* feat: handle mv command

* refactor: remove unused console logs
2025-04-29 20:40:02 +08:00

59 lines
1.5 KiB
TypeScript

import { FileObject } from './file.js'
import FileNode from './fileNode.js'
export interface FileGraphUpdateEvent {
add?: FileObject[]
remove?: FileObject[]
discover?: FileObject[]
pwd?: string
}
export default class FileGraph extends FileNode {
add(files: FileObject[]) {
// Logic to add the file node to the graph
// You can implement the logic to find the appropriate location in the file graph
// If the file node already exists, throw an error
files.forEach((file) => {
this.addChild(file, { makeParents: true })
})
}
remove(files: FileObject[]) {
// Logic to remove a file node from the graph
// If the file node is a directory, also remove all children
// If the file node doesn't exist, throw an error
files.forEach((file) => {
this.removeChild(file)
})
}
discover(eventFiles: FileObject[]) {
// Logic to handle the discover event
// Overwrite the file graph with the provided files
// Remove file nodes that don't exist anymore and add new ones
const newNode = new FileNode(eventFiles[0])
eventFiles.slice(1).forEach((file) => {
newNode.addChild(file, { makeParents: true })
})
this.merge(newNode)
}
handleEvent(event: FileGraphUpdateEvent) {
// eslint-disable-next-line default-case
if (event.add) {
this.add(event.add)
}
if (event.remove) {
this.remove(event.remove)
}
if (event.discover) {
this.discover(event.discover)
}
}
}