From 91e2e2bc4a46da0d149578593559efdb87681bd4 Mon Sep 17 00:00:00 2001 From: sabhas Date: Mon, 18 Oct 2021 19:48:25 +0000 Subject: [PATCH] fix: modify the directory tree algorithm to include relative path with each node --- Web/src/containers/SASjsDrive/sideBar.tsx | 15 +++++---- src/controllers/sasjsExecutor.ts | 41 ++++++++++++++++++++--- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/Web/src/containers/SASjsDrive/sideBar.tsx b/Web/src/containers/SASjsDrive/sideBar.tsx index 389eb91..5f76d2e 100644 --- a/Web/src/containers/SASjsDrive/sideBar.tsx +++ b/Web/src/containers/SASjsDrive/sideBar.tsx @@ -16,9 +16,10 @@ import ExpandMoreIcon from '@mui/icons-material/ExpandMore' import ChevronRightIcon from '@mui/icons-material/ChevronRight' interface TreeNode { - path: string name: string - children?: readonly TreeNode[] + relativePath: string + absolutePath: string + children: Array } const useStyles = makeStyles(() => ({ @@ -49,21 +50,21 @@ const SideBar = (props: any) => { }, []) const handleSelect = (node: TreeNode) => { - if (!node.children) { + if (!node.children.length) { window.history.pushState( '', '', - `${baseUrl}/SASjsDrive?filepath=${node.path}` + `${baseUrl}/SASjsDrive?filepath=${node.relativePath}` ) - props.setSelectedFilePath(node.path) + props.setSelectedFilePath(node.relativePath) } } const renderTree = (nodes: TreeNode) => ( { - const tree = dirTree(path.join(__dirname, '..', '..', 'tmp')) - return tree + const root: TreeNode = { + name: 'files', + relativePath: '', + absolutePath: getTmpFilesFolderPath(), + children: [] + } + + const stack = [root] + + while (stack.length) { + const currentNode = stack.pop() + + if (currentNode) { + const children = fs.readdirSync(currentNode.absolutePath) + + for (let child of children) { + const absoluteChildPath = `${currentNode.absolutePath}/${child}` + const relativeChildPath = `${currentNode.relativePath}/${child}` + const childNode: TreeNode = { + name: child, + relativePath: relativeChildPath, + absolutePath: absoluteChildPath, + children: [] + } + currentNode.children.push(childNode) + + if (fs.statSync(childNode.absolutePath).isDirectory()) { + stack.push(childNode) + } + } + } + } + + return root }