feat: rewrite remove function and add remove test

This commit is contained in:
Chen, Tai-Lin committed 2023-08-28 16:13:10 +08:00
1 parent 887bbf5c1b
commit 7f2a57824b
3 files changed
+57 -35

No files matched your search

+2 -35
View File
@@ -22,32 +22,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,17 +31,9 @@ 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) {