I've tried implementing the code from here, but I have problem with asynchronous function.
I wanted to get a folder map through a function like this:
getFolderMap("./public", function(folderMap){
/* Call back */
});
And the function would return sth. like this:
{
"index":{"fileType":"html", blah blah blah...}
"assets":{
"plants":{
"plant1":{"fileType":"png", blah blah blah...}
}
}
"css":{
"style":{"fileType":"css", blah blah blah...}
"flickity":{"fileType":"css", blah blah blah...}
},
"js":{
"main":{"fileType":"js", blah blah blah...}
}
}
The code was:
var getFolderMap = function(rootDir, cb) {
fs.readdir(rootDir, function(err, files) {
var dirs = {};
for (var index = 0; index < files.length; index++) {
var file = files[index];
if (file[0] !== '.') {
var filePath = rootDir + '/' + file;
fs.stat(filePath, function(err, stat) {
if (stat.isDirectory()) {
var file = this.file;
//Recursion...
getFolderMap(this.filePath,function(d){
dirs[file]=d;
});
}else{
dirs[this.file] = {
fileType: ""
};
}
if (files.length === (this.index + 1)) {
return cb(dirs);
}
}.bind({index: index, file: file, filePath:filePath}));
}
}
});
};