Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created January 29, 2015 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naholyr/510ba182a26293ee560a to your computer and use it in GitHub Desktop.
Save naholyr/510ba182a26293ee560a to your computer and use it in GitHub Desktop.
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