Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created August 27, 2010 03:24

Revisions

  1. isaacs revised this gist Sep 17, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions tree.js
    Original 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
  2. isaacs revised this gist Sep 17, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tree.js
    Original 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 = false
    , error = null
    children.forEach(function (child) {
    var i = index ++
    if (error) return
  3. isaacs revised this gist Sep 17, 2010. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions tree.js
    Original 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) {
    error = true
    return cb(er)
    }
    if (er) return cb(error = er)
    t.name = child
    s.children[i] = t
    if (--childrenWaiting === 0) cb(null, s)
  4. isaacs created this gist Aug 27, 2010.
    56 changes: 56 additions & 0 deletions tree.js
    Original 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)
    })