From 0f193849994f1ac8a071afa8f10af5b46f86663d Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 19 Jul 2022 16:13:46 +0500 Subject: [PATCH] fix: fileTree api response to include an additional attribute isFolder --- api/src/controllers/internal/Execution.ts | 10 +++++++++- api/src/types/TreeNode.ts | 1 + web/src/components/tree.tsx | 2 +- web/src/utils/types.ts | 1 + 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/api/src/controllers/internal/Execution.ts b/api/src/controllers/internal/Execution.ts index 7f3541d..1beb046 100644 --- a/api/src/controllers/internal/Execution.ts +++ b/api/src/controllers/internal/Execution.ts @@ -143,6 +143,7 @@ export class ExecutionController { name: 'files', relativePath: '', absolutePath: getFilesFolder(), + isFolder: true, children: [] } @@ -152,15 +153,22 @@ export class ExecutionController { const currentNode = stack.pop() if (currentNode) { + currentNode.isFolder = fs + .statSync(currentNode.absolutePath) + .isDirectory() + const children = fs.readdirSync(currentNode.absolutePath) for (let child of children) { - const absoluteChildPath = `${currentNode.absolutePath}/${child}` + const absoluteChildPath = path.join(currentNode.absolutePath, child) + // relative path will only be used in frontend component + // so, no need to convert '/' to platform specific separator const relativeChildPath = `${currentNode.relativePath}/${child}` const childNode: TreeNode = { name: child, relativePath: relativeChildPath, absolutePath: absoluteChildPath, + isFolder: false, children: [] } currentNode.children.push(childNode) diff --git a/api/src/types/TreeNode.ts b/api/src/types/TreeNode.ts index 7d902ac..71fd803 100644 --- a/api/src/types/TreeNode.ts +++ b/api/src/types/TreeNode.ts @@ -2,5 +2,6 @@ export interface TreeNode { name: string relativePath: string absolutePath: string + isFolder: boolean children: Array } diff --git a/web/src/components/tree.tsx b/web/src/components/tree.tsx index e728b51..f8c7376 100644 --- a/web/src/components/tree.tsx +++ b/web/src/components/tree.tsx @@ -126,7 +126,7 @@ const TreeViewNode = ({ : undefined } > - {hasChild && + {node.isFolder && ['Add Folder', 'Add File'].map((item) => ( {item} ))} diff --git a/web/src/utils/types.ts b/web/src/utils/types.ts index 6d48d9b..99c6fc4 100644 --- a/web/src/utils/types.ts +++ b/web/src/utils/types.ts @@ -34,5 +34,6 @@ export interface RegisterPermissionPayload { export interface TreeNode { name: string relativePath: string + isFolder: boolean children: Array }