summaryrefslogtreecommitdiff
path: root/node_modules/walk/lib/walk-async-only.js
blob: 41b30ca538008579c77b2fcb9f7410f28c15826e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function () {
  "use strict"

  // Array.prototype.forEachAsync(next, item, i, collection)
  require('futures/forEachAsync');

  function noop() {}

  var fs = require('fs'),
    path = require('path'),
    EventEmitter = require('events').EventEmitter,
    TypeEmitter = require('./node-type-emitter');

  // 2010-11-25 jorge@jorgechamorro.com
  function create(pathname, cb) {
    var emitter = new EventEmitter(),
      q = [],
      queue = [q],
      curpath;

    function walk() { 
      fs.readdir(curpath, function(err, files) {
        if (err) {
          emitter.emit('directoryError', curpath, { error: err }, noop);
          //emitter.emit('error', curpath, { error: err });
        }
        // XXX bug was here. next() was omitted
        if (!files || 0 == files.length) {
          return next();
        }

        var fnodeGroups = TypeEmitter.createNodeGroups();

        // TODO could allow user to selectively stat
        // and don't stat if there are no stat listeners
        emitter.emit('names', curpath, files, noop);
        files.forEachAsync(function (cont, file) {
          emitter.emit('name', curpath, file, noop);
          fs.lstat(curpath + path.sep + file, function (err, stat) {
            stat = stat || {};
            stat.name = file;
            if (err) {
              stat.error = err;
              //emitter.emit('error', curpath, stat);
              emitter.emit('nodeError', curpath, stat, noop);
              fnodeGroups.errors.push(stat);
              cont();
            } else {
              TypeEmitter.sortFnodesByType(stat, fnodeGroups);
              TypeEmitter.emitNodeType(emitter, curpath, stat, cont);
            }
          });
        }).then(function () {
          if (fnodeGroups.errors.length) {
            emitter.emit('errors', curpath, fnodeGroups.errors, noop);
          }
          TypeEmitter.emitNodeTypeGroups(emitter, curpath, fnodeGroups, function () {
            var dirs = [];
            fnodeGroups.directories.forEach(function (stat) {
              dirs.push(stat.name);
            });
            dirs.forEach(fullPath);
            queue.push(q = dirs);
            next();
          });
        });
      });
    }
    
    function next() {
      if (q.length) {
        curpath = q.pop();
        return walk();
      }
      if (queue.length -= 1) {
        q = queue[queue.length-1];
        return next();
      }
      emitter.emit('end');
    }
    
    function fullPath(v,i,o) {
      o[i]= [curpath, path.sep, v].join('');
    }
    
    curpath = pathname;
    walk();
    
    return emitter;
  }

  module.exports = create;
}());