1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-09 23:40:06 +00:00

fix: modify the directory tree algorithm to include relative path with each node

This commit is contained in:
2021-10-18 19:48:25 +00:00
parent bc3cb7bb20
commit 91e2e2bc4a
2 changed files with 45 additions and 11 deletions

View File

@@ -1,7 +1,40 @@
import dirTree from 'directory-tree'
import path from 'path'
import fs from 'fs'
import { TreeNode } from '../types'
import { getTmpFilesFolderPath } from '../utils'
export const sasjsExecutor = () => {
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
}