feat: rewrite remove function and add remove test

This commit is contained in:
Chen, Tai-Lin committed 2023-08-28 16:17:23 +08:00
1 parent 887bbf5c1b
commit 190a5abf3c
3 files changed
+58 -36

No files matched your search

+3 -36
View File
@@ -1,5 +1,4 @@
import FileNode from './fileNode.js'
import { FileNotExistsError } from './errors.js'
export default class FileGraph {
constructor(root) {
@@ -22,32 +21,7 @@ export default class FileGraph {
// If the file node doesn't exist, throw an error
files.forEach((file) => {
const pathFolders = file.path
.split('/')
.filter((folder) => folder.length > 0)
const fileName = pathFolders.pop()
let currentNode = this.root
for (const folder of pathFolders) {
currentNode = currentNode.children.find(
(child) => child.name === folder
)
if (!currentNode) {
throw new FileNotExistsError(
`File not found: ${fileToFind.path}, ${this.toString()}`
)
}
}
if (!currentNode.children.some((child) => child.name === fileName)) {
throw new FileNotExistsError(
`File not found: ${file.path}, ${this.toString()}`
)
} else {
currentNode.children = currentNode.children.filter(
(child) => child.name !== fileName
)
}
this.root.removeChild(file)
})
}
@@ -56,20 +30,13 @@ export default class FileGraph {
// Overwrite the file graph with the provided files
// Remove file nodes that don't exist anymore and add new ones
const newRoot = new FileNode({
path: '/',
type: 'folder',
discovered: true,
})
eventFiles.forEach((file) => {
newRoot.addChild(file, { makeParents: true })
this.root.merge(file)
})
this.root = newRoot
}
handleEvent(event) {
// eslint-disable-next-line default-case
switch (true) {
case !!event.add:
this.add(event.add)