finish file-graph and solve jest ts

This commit is contained in:
wancat committed 2024-02-07 10:03:53 +08:00
1 parent 2a542302bc
commit 253bd04588
9 files changed
+40 -28

No files matched your search

+1 -2
View File
@@ -1,7 +1,6 @@
/* eslint-disable lines-between-class-members */
import { basename } from './utils.js'
interface FileObject {
export interface FileObject {
path: string
type: string
discovered: boolean
@@ -1,7 +1,14 @@
import { FileObject } from './file.js'
import FileNode from './fileNode.js'
export interface Event {
add?: FileObject[]
remove?: FileObject[]
discover?: FileObject[]
}
export default class FileGraph extends FileNode {
add(files) {
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
@@ -11,7 +18,7 @@ export default class FileGraph extends FileNode {
})
}
remove(files) {
remove(files: FileObject[]) {
// Logic to remove a file node from the graph
// If the file node is a folder, also remove all children
// If the file node doesn't exist, throw an error
@@ -21,7 +28,7 @@ export default class FileGraph extends FileNode {
})
}
discover(eventFiles) {
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
@@ -35,7 +42,7 @@ export default class FileGraph extends FileNode {
this.merge(newNode)
}
handleEvent(event) {
handleEvent(event: Event) {
// eslint-disable-next-line default-case
switch (true) {
case !!event.add:
@@ -1,19 +1,25 @@
import File from './file.js'
import File, { FileObject } from './file.js'
import {
DuplicateItemError,
ParentNotExistsError,
FileNotExistsError,
} from './errors.js'
import { basename, dirname } from './utils.js'
import { dirname } from './utils.js'
export default class FileNode extends File {
constructor(file) {
children: FileNode[]
constructor(file: FileObject | FileNode) {
super(file)
this.children = []
if (file.children) this.children = file.children.map((f) => new FileNode(f))
if ((<FileNode>file).children)
this.children = (<FileNode>file).children.map((f) => new FileNode(f))
}
addChild(fileInput, { makeParents } = {}) {
addChild(
fileInput: FileObject,
{ makeParents }: { makeParents?: boolean } = {}
) {
const file = new FileNode(fileInput)
if (file.path === this.path) {
throw new DuplicateItemError(
@@ -39,7 +45,6 @@ export default class FileNode extends File {
} else if (makeParents) {
const parent = new FileNode({
path: parentPath,
name: basename(parentPath),
type: 'folder',
discovered: false,
children: [file],
@@ -53,7 +58,7 @@ export default class FileNode extends File {
}
}
removeChild(fileToRemove) {
removeChild(fileToRemove: FileObject) {
const file = new FileNode(fileToRemove)
const parentPath = dirname(file.path)
@@ -73,7 +78,7 @@ export default class FileNode extends File {
}
}
merge(fileNode) {
merge(fileNode: FileNode) {
// Merge the children of the provided file node into this node
// If a child already exists, merge the children of that child
// If a child does not exist, add it to this node
@@ -88,7 +93,7 @@ export default class FileNode extends File {
if (existingChild) {
existingChild.merge(child)
} else {
this.children.push(child)
this.children.push(new FileNode(child))
}
})
@@ -112,14 +117,12 @@ export default class FileNode extends File {
}
} else {
throw new Error(
'Cannot merge file nodes with different paths',
this.path,
fileNode.path
`Cannot merge file nodes with different paths: ${this.path}, ${fileNode.path}`
)
}
}
toString(level = 0) {
toString(level = 0): string {
let { name } = this
const indent = ' '.repeat(level)
name = `${indent}${name}`
-3
View File
@@ -1,3 +0,0 @@
export { default as File } from './file.js'
export { default as FileNode } from './fileNode.js'
export { default as FileGraph } from './fileGraph.js'
+3
View File
@@ -0,0 +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'
@@ -1,4 +1,4 @@
export function dirname(path) {
export function dirname(path: string) {
const parts = path.split('/')
if (parts.length <= 2) {
return '/'
@@ -7,7 +7,7 @@ export function dirname(path) {
return parts.join('/')
}
export function basename(path) {
export function basename(path: string): string {
const parts = path.split('/')
return parts.pop()
return parts.pop() ?? ''
}