From fd26be5e646be631070ab036a2b2360515f05e5f Mon Sep 17 00:00:00 2001 From: wancat Date: Fri, 18 Aug 2023 21:45:05 +0800 Subject: [PATCH] feat: add makeParent options to addChild --- packages/file-graph/fileNode.js | 21 +++++++++++++++------ packages/file-graph/fileNode.test.js | 19 +++++++++++++++++++ packages/file-graph/utils.js | 10 ++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 packages/file-graph/utils.js diff --git a/packages/file-graph/fileNode.js b/packages/file-graph/fileNode.js index a224cd4..e537c94 100644 --- a/packages/file-graph/fileNode.js +++ b/packages/file-graph/fileNode.js @@ -1,15 +1,15 @@ -import path from 'path' import File from './file.js' import { DuplicateItemError, ParentNotExistsError } from './errors.js' +import { basename, dirname } from './utils.js' export default class FileNode extends File { constructor(file) { super(file) - this.children = [] + this.children = file.children || [] } - addChild(fileInput) { - const file = new File(fileInput) + addChild(fileInput, { makeParents } = {}) { + const file = new FileNode(fileInput) if (file.path === this.path) { throw new DuplicateItemError('File already exists in the tree') } @@ -18,15 +18,24 @@ export default class FileNode extends File { throw new DuplicateItemError('File already exists in the tree') } - const parentPath = path.dirname(file.path) + const parentPath = dirname(file.path) if (parentPath === this.path) { // Direct child of this node - this.children.push(new FileNode(file)) + this.children.push(file) } else { // Recursively call addChild on the correct child node const childNode = this.children.find((child) => child.contains(file)) if (childNode) { childNode.addChild(file) + } else if (makeParents) { + const parent = new FileNode({ + path: parentPath, + name: basename(parentPath), + type: 'folder', + discovered: false, + children: [file], + }) + this.addChild(parent, { makeParents }) } else { throw new ParentNotExistsError('Parent path not found in the tree') } diff --git a/packages/file-graph/fileNode.test.js b/packages/file-graph/fileNode.test.js index b1584c7..97d0c06 100644 --- a/packages/file-graph/fileNode.test.js +++ b/packages/file-graph/fileNode.test.js @@ -74,4 +74,23 @@ describe('FileNode', () => { expect(rootNode.children[1].children.length).toBe(1) console.log(rootNode.toString()) }) + + test('adds nested children to the tree with makeParents option', () => { + const file = { + path: '/home/rudeus/test/a/b/c/d/e', + name: 'a', + type: 'file', + discovered: false, + } + + rootNode.addChild(file, { makeParents: true }) + + console.log(rootNode.toString()) + let currentNode = rootNode + for (let i = 0; i < 5; i += 1) { + expect(currentNode.children.length).toBe(1) + // eslint-disable-next-line prefer-destructuring + currentNode = currentNode.children[0] + } + }) }) diff --git a/packages/file-graph/utils.js b/packages/file-graph/utils.js new file mode 100644 index 0000000..afc3f45 --- /dev/null +++ b/packages/file-graph/utils.js @@ -0,0 +1,10 @@ +export function dirname(path) { + const parts = path.split('/') + parts.pop() + return parts.join('/') +} + +export function basename(path) { + const parts = path.split('/') + return parts.pop() +}