I want to get all directories synchronize within a given directory.
<MyFolder>
|- Folder1
|- Folder11
|- Folder12
|- Folder2
|- File1.txt
|- File2.txt
|- Folder3
|- Folder31
|- Folder32
I would expect to get an array of:
["Folder1/Folder11", "Folder1/Folder12", "Folder2", "Folder3/Folder31", "Folder3/Folder32"]
This is my code:
const fs = require('fs');
const path = require('path');
function flatten(lists) {
return lists.reduce((a, b) => a.concat(b), []);
}
function getDirectories(srcpath) {
return fs.readdirSync(srcpath)
.map(file => path.join(srcpath, file))
.filter(path => fs.statSync(path).isDirectory());
}
function getDirectoriesRecursive(srcpath) {
return [srcpath, ...flatten(getDirectories(srcpath).map(getDirectoriesRecursive))];
}
Would anyone help me solve the problem above?