-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
141 lines (130 loc) · 3.75 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var fs = require('fs')
, path = require('path')
, async = require('async')
/**
* Asynchronously list `dir` and all its descendents,
* calling `iterator` for each entry. Once all entries have
* been listed, calls `callback`.
*
* @param {String} dir Path to the base directory of the listing.
* @param {Function} iterator Function that will be called
* for each entry in the listing, passing 4 arguments:
* * `basedir`: Base directory of the entry (relative to `dir`).
* * `file`: Name of the file
* * `stat`: Result of calling `fs.stat`
* * `next`: Asynchronous callback
* Passing an error to the iterator `next` callback will stop iteration
* and the main `callback` will be called.
* @param {Function} callback Asynchronous callback.
*/
var walk = exports.walk = function(dir, iterator, callback) {
var dirs = [dir];
async.whilst(
function test(cb) { cb(null, dirs.length > 0); },
function(nextd) {
var dir = dirs.shift();
async.waterfall([
function(nextrd) {
fs.readdir(dir, nextrd);
},
function(files, nextf) {
async.each(files, function(file, nexts) {
var f = path.join(dir, file);
fs.stat(f, function(err, stat) {
if(err){
nexts(f + ' is not a valid file or directory');
} else {
if (stat && stat.isDirectory()) {
dirs.push(f);
}
if (stat) {
iterator(dir, file, stat, nexts);
} else {
nexts(f + ' is not a valid file or directory');
}
}
});
}, nextf);
}
],
nextd);
},
callback);
};
/**
* Synchronously list `dir` and all its descendents,
* calling `iterator` for each entry.
*
* @param {String} dir Path to the base directory of the listing.
* @param {Function} iterator Function that will be called
* for each entry in the listing, passing 4 arguments:
* * `basedir`: Base directory of the entry (relative to `dir`).
* * `file`: Name of the file
* * `stat`: Result of calling `fs.stat`
*
*/
var walkSync = exports.walkSync = function(dir, iterator) {
var dirs = [dir];
while (dirs.length) {
var dir = dirs.shift();
var files = fs.readdirSync(dir);
files.forEach(function(file) {
var f = path.join(dir, file);
var stat = fs.statSync(f);
if (stat && stat.isDirectory()) {
dirs.push(f);
}
if (stat) {
iterator(dir, file, stat);
}
});
}
};
/**
* Asynchronously list all files in `dir` and its
* descendants
*/
var files = exports.files = function(dir, iterator, callback) {
walk(dir, function(root, file, stat, next) {
if (stat.isFile()) {
iterator(root, file, stat, next);
} else {
next();
}
}, callback);
};
/**
* Synchronously list all files in `dir` and its
* descendants
*/
var filesSync = exports.filesSync = function(dir, iterator) {
walkSync(dir, function(root, file, stat) {
if (stat.isFile()) {
iterator(root, file, stat);
}
});
};
/**
* Asynchronously list all directories in `dir` and its
* descendants
*/
var dirs = exports.dirs = function(dir, iterator, callback) {
walk(dir, function(root, file, stat, next) {
if (stat.isDirectory()) {
iterator(root, file, stat, next);
} else {
next();
}
}, callback);
};
/**
* Synchronously list all directories in `dir` and its
* descendants
*/
var dirsSync = exports.dirsSync = function(dir, iterator) {
walkSync(dir, function(root, file, stat) {
if (stat.isDirectory()) {
iterator(root, file, stat);
}
});
};