-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
62 lines (53 loc) · 1.67 KB
/
index.js
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
var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var Seq = require('seq');
exports = module.exports = find;
exports.find = find;
function find (base, cb) {
var em = new EventEmitter;
(function find (dir, f) {
Seq()
.seq(fs.readdir, dir, Seq)
.flatten()
.seqEach(function (file) {
var p = dir + '/' + file;
fs.stat(p, this.into(p));
})
.seq(function () {
this(null, Object.keys(this.vars));
})
.flatten()
.seqEach(function (file) {
var stat = this.vars[file];
if (cb) cb(file, stat);
em.emit('path', file, stat);
if (stat.isDirectory()) {
em.emit('directory', file, stat);
find(file, this);
}
else {
em.emit('file', file, stat);
this(null);
}
})
.seq(f.bind({}, null))
.catch(em.emit.bind(em, 'error'))
;
})(base, em.emit.bind(em, 'end'));
return em;
};
exports.findSync = function findSync (dir) {
if (!fs.statSync(dir).isDirectory()) return [dir];
return fs.readdirSync(dir)
.reduce(function (files, file) {
var p = dir + '/' + file;
var stat = fs.statSync(p);
files.push(p);
if (stat.isDirectory()) {
files.push.apply(files, findSync(p));
}
return files;
}, [])
;
};
exports.find.sync = exports.findSync;