Created
January 29, 2015 17:30
-
-
Save naholyr/510ba182a26293ee560a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dirTree (filename, cb) { | |
fs.lstat(filename, function (err, stat) { | |
if (err) { | |
return cb(err); | |
} | |
var info = { | |
"path": filename, | |
"name": path.basename(filename) | |
}; | |
if (stat.isDirectory()) { | |
info.type = "folder"; | |
fs.readdir(filename, function (err, children) { | |
if (err) { | |
return cb(err); | |
} | |
info.children = []; | |
var browsed = 0; | |
children.forEach(function (child, index) { | |
dirTree(path.join(filename, child), function (err, childInfo) { | |
if (err) { | |
return cb(err); | |
} | |
info.children[index] = childInfo; | |
if (++browsed === children.length) { | |
cb(null, info); | |
} | |
}); | |
}); | |
}); | |
} else { | |
info.type = "file"; | |
cb(null, info); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment