DEV Community

Discussion on: Loading a directory as a tree structure in Node

Collapse
 
peaonunes profile image
Rafael Nunes

Thanks!

Do you mean a limited number of subfolders as in the depth, on how deep you go down in the folder structure?
Or is it more like the total number of subfolders explored?

I believe for both ways we would need to work in our code to solve this problem.

The file system methods like readdirSync do not accept a limit number as far as I know. So to implement it we could probably define a constant called LIMIT and keep track of the depth of subfolders or the count of subfolders depending on your case.

        const isDirectory = fs.statSync(childNode.path).isDirectory();
        const isUnderLimit = depth <= LIMIT;

        if (isDirectory && isUnderLimit) {
          stack.push(childNode);
        } 
Enter fullscreen mode Exit fullscreen mode

It could be something like the code above to prevent adding more nodes after a certain threshold. Of course, we would need to increment the depth or count every time we explore a new subfolder.

I hope that helps 😄