Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1.  
  2. function dirTree (filename, cb) {
  3. fs.lstat(filename, function (err, stat) {
  4. if (err) {
  5. return cb(err);
  6. }
  7.  
  8. var info = {
  9. "path": filename,
  10. "name": path.basename(filename)
  11. };
  12.  
  13. if (stat.isDirectory()) {
  14. info.type = "folder";
  15. fs.readdir(filename, function (err, children) {
  16. if (err) {
  17. return cb(err);
  18. }
  19.  
  20. info.children = [];
  21.  
  22. var browsed = 0;
  23. children.forEach(function (child, index) {
  24. dirTree(path.join(filename, child), function (err, childInfo) {
  25. if (err) {
  26. return cb(err);
  27. }
  28.  
  29. info.children[index] = childInfo;
  30.  
  31. if (++browsed === children.length) {
  32. cb(null, info);
  33. }
  34. });
  35. });
  36. });
  37. } else {
  38. info.type = "file";
  39. cb(null, info);
  40. }
  41. });
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement