mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
finish file-graph and solve jest ts
This commit is contained in:
1 parent
2a542302bc
commit
253bd04588
9 files changed
+40
-28
No files matched your search
@@ -45,6 +45,7 @@ module.exports = {
|
|||||||
'always',
|
'always',
|
||||||
{ exceptAfterSingleLine: true },
|
{ exceptAfterSingleLine: true },
|
||||||
],
|
],
|
||||||
|
'no-use-before-define': 'off',
|
||||||
|
|
||||||
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
||||||
|
|
||||||
|
|||||||
+4
-2
@@ -2,6 +2,8 @@
|
|||||||
// const { jsWithTs: tsjPreset } = require('ts-jest/presets')
|
// const { jsWithTs: tsjPreset } = require('ts-jest/presets')
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
preset: 'ts-jest/presets/js-with-ts',
|
preset: 'ts-jest/presets/default-esm',
|
||||||
testEnvironment: 'node',
|
moduleNameMapper: {
|
||||||
|
'^(\\.{1,2}/.*)\\.js$': '$1', // Redirect .js imports to .ts files
|
||||||
|
},
|
||||||
}
|
}
|
||||||
+1
-1
@@ -20,7 +20,7 @@
|
|||||||
"cli": "yarn workspace client start",
|
"cli": "yarn workspace client start",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"format": "prettier --ignore-path .prettierignore --write app server",
|
"format": "prettier --ignore-path .prettierignore --write app server",
|
||||||
"test": "yarn jest",
|
"test": "yarn node --experimental-vm-modules --no-warnings $(yarn bin jest)",
|
||||||
"test:e2e": "yarn workspace tests test",
|
"test:e2e": "yarn workspace tests test",
|
||||||
"test:open": "yarn workspace tests test:open",
|
"test:open": "yarn workspace tests test:open",
|
||||||
"analytics": "yarn workspace analytics dev",
|
"analytics": "yarn workspace analytics dev",
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
/* eslint-disable lines-between-class-members */
|
|
||||||
import { basename } from './utils.js'
|
import { basename } from './utils.js'
|
||||||
|
|
||||||
interface FileObject {
|
export interface FileObject {
|
||||||
path: string
|
path: string
|
||||||
type: string
|
type: string
|
||||||
discovered: boolean
|
discovered: boolean
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
|
import { FileObject } from './file.js'
|
||||||
import FileNode from './fileNode.js'
|
import FileNode from './fileNode.js'
|
||||||
|
|
||||||
|
export interface Event {
|
||||||
|
add?: FileObject[]
|
||||||
|
remove?: FileObject[]
|
||||||
|
discover?: FileObject[]
|
||||||
|
}
|
||||||
|
|
||||||
export default class FileGraph extends FileNode {
|
export default class FileGraph extends FileNode {
|
||||||
add(files) {
|
add(files: FileObject[]) {
|
||||||
// Logic to add the file node to the graph
|
// Logic to add the file node to the graph
|
||||||
// You can implement the logic to find the appropriate location in the file graph
|
// You can implement the logic to find the appropriate location in the file graph
|
||||||
// If the file node already exists, throw an error
|
// 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
|
// Logic to remove a file node from the graph
|
||||||
// If the file node is a folder, also remove all children
|
// If the file node is a folder, also remove all children
|
||||||
// If the file node doesn't exist, throw an error
|
// 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
|
// Logic to handle the discover event
|
||||||
// Overwrite the file graph with the provided files
|
// Overwrite the file graph with the provided files
|
||||||
// Remove file nodes that don't exist anymore and add new ones
|
// 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)
|
this.merge(newNode)
|
||||||
}
|
}
|
||||||
|
|
||||||
handleEvent(event) {
|
handleEvent(event: Event) {
|
||||||
// eslint-disable-next-line default-case
|
// eslint-disable-next-line default-case
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case !!event.add:
|
case !!event.add:
|
||||||
@@ -1,19 +1,25 @@
|
|||||||
import File from './file.js'
|
import File, { FileObject } from './file.js'
|
||||||
import {
|
import {
|
||||||
DuplicateItemError,
|
DuplicateItemError,
|
||||||
ParentNotExistsError,
|
ParentNotExistsError,
|
||||||
FileNotExistsError,
|
FileNotExistsError,
|
||||||
} from './errors.js'
|
} from './errors.js'
|
||||||
import { basename, dirname } from './utils.js'
|
import { dirname } from './utils.js'
|
||||||
|
|
||||||
export default class FileNode extends File {
|
export default class FileNode extends File {
|
||||||
constructor(file) {
|
children: FileNode[]
|
||||||
|
|
||||||
|
constructor(file: FileObject | FileNode) {
|
||||||
super(file)
|
super(file)
|
||||||
this.children = []
|
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)
|
const file = new FileNode(fileInput)
|
||||||
if (file.path === this.path) {
|
if (file.path === this.path) {
|
||||||
throw new DuplicateItemError(
|
throw new DuplicateItemError(
|
||||||
@@ -39,7 +45,6 @@ export default class FileNode extends File {
|
|||||||
} else if (makeParents) {
|
} else if (makeParents) {
|
||||||
const parent = new FileNode({
|
const parent = new FileNode({
|
||||||
path: parentPath,
|
path: parentPath,
|
||||||
name: basename(parentPath),
|
|
||||||
type: 'folder',
|
type: 'folder',
|
||||||
discovered: false,
|
discovered: false,
|
||||||
children: [file],
|
children: [file],
|
||||||
@@ -53,7 +58,7 @@ export default class FileNode extends File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeChild(fileToRemove) {
|
removeChild(fileToRemove: FileObject) {
|
||||||
const file = new FileNode(fileToRemove)
|
const file = new FileNode(fileToRemove)
|
||||||
const parentPath = dirname(file.path)
|
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
|
// 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 already exists, merge the children of that child
|
||||||
// If a child does not exist, add it to this node
|
// If a child does not exist, add it to this node
|
||||||
@@ -88,7 +93,7 @@ export default class FileNode extends File {
|
|||||||
if (existingChild) {
|
if (existingChild) {
|
||||||
existingChild.merge(child)
|
existingChild.merge(child)
|
||||||
} else {
|
} else {
|
||||||
this.children.push(child)
|
this.children.push(new FileNode(child))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -112,14 +117,12 @@ export default class FileNode extends File {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Cannot merge file nodes with different paths',
|
`Cannot merge file nodes with different paths: ${this.path}, ${fileNode.path}`
|
||||||
this.path,
|
|
||||||
fileNode.path
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toString(level = 0) {
|
toString(level = 0): string {
|
||||||
let { name } = this
|
let { name } = this
|
||||||
const indent = ' '.repeat(level)
|
const indent = ' '.repeat(level)
|
||||||
name = `${indent}${name}`
|
name = `${indent}${name}`
|
||||||
@@ -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'
|
|
||||||
@@ -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('/')
|
const parts = path.split('/')
|
||||||
if (parts.length <= 2) {
|
if (parts.length <= 2) {
|
||||||
return '/'
|
return '/'
|
||||||
@@ -7,7 +7,7 @@ export function dirname(path) {
|
|||||||
return parts.join('/')
|
return parts.join('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function basename(path) {
|
export function basename(path: string): string {
|
||||||
const parts = path.split('/')
|
const parts = path.split('/')
|
||||||
return parts.pop()
|
return parts.pop() ?? ''
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user