Created
August 27, 2010 03:24
Revisions
-
isaacs revised this gist
Sep 17, 2010 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,7 @@ function tree (root, cb) { if (error) return if (er) return cb(error = er) t.name = child t.path = path.join(root, child) s.children[i] = t if (--childrenWaiting === 0) cb(null, s) }) @@ -40,6 +41,7 @@ function treeSync (root) { for (var i = 0, l = children.length; i < l; i ++) { var child = treeSync(path.join(root, children[i])) child.name = children[i] child.path = path.join(root, children[i]) s.children.push(child) } return s -
isaacs revised this gist
Sep 17, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ function tree (root, cb) { s.children = [] var childrenWaiting = children.length , index = 0 , error = null children.forEach(function (child) { var i = index ++ if (error) return -
isaacs revised this gist
Sep 17, 2010 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ module.exports = tree tree.sync = treeSync @@ -22,10 +21,7 @@ function tree (root, cb) { if (error) return tree(path.join(root, child), function (er, t) { if (error) return if (er) return cb(error = er) t.name = child s.children[i] = t if (--childrenWaiting === 0) cb(null, s) -
isaacs created this gist
Aug 27, 2010 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ module.exports = tree tree.sync = treeSync var fs = require("fs") , path = require("path") function tree (root, cb) { fs.lstat(root, function (er, s) { if (er) return cb(er) s.name = root // if it's a dir, then get the list of children. if (!s.isDirectory()) return cb(null, s) fs.readdir(root, function (er, children) { if (er) return cb(er) s.children = [] var childrenWaiting = children.length , index = 0 , error = false children.forEach(function (child) { var i = index ++ if (error) return tree(path.join(root, child), function (er, t) { if (error) return if (er) { error = true return cb(er) } t.name = child s.children[i] = t if (--childrenWaiting === 0) cb(null, s) }) }) }) }) } function treeSync (root) { var s = fs.lstatSync(root) s.name = root if (!s.isDirectory()) return s var children = fs.readdirSync(root) s.children = [] for (var i = 0, l = children.length; i < l; i ++) { var child = treeSync(path.join(root, children[i])) child.name = children[i] s.children.push(child) } return s } if (module !== process.mainModule) return tree("/Users", function (er, ok) { console.error((er && er.stack) || ok) })