Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Directory as JSON object
var fs = require("fs");
var tree = function(dir, done) {
var results = {
"path": dir
,"children": []
};
fs.readdir(dir, function(err, list) {
if (err) { return done(err); }
var pending = list.length;
if (!pending) { return done(null, results); }
list.forEach(function(file) {
fs.stat(dir + '/' + file, function(err, stat) {
if (stat && stat.isDirectory()) {
tree(dir + '/' + file, function(err, res) {
results.children.push(res);
if (!--pending){ done(null, results); }
});
} else {
results.children.push({"path": dir + "/" + file});
if (!--pending) { done(null, results); }
}
});
});
});
};
module.exports = tree;
@Burnett01

This comment has been minimized.

Copy link

Burnett01 commented Jul 4, 2015

BEST!!!!!!

@logoscoder

This comment has been minimized.

Copy link

logoscoder commented Nov 26, 2019

Thankz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.