-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan-fs.js
More file actions
209 lines (179 loc) · 4.06 KB
/
Copy pathscan-fs.js
File metadata and controls
209 lines (179 loc) · 4.06 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* 扫描文件系统
* User: lg
* Date: 8/16/13
* Time: 3:34 PM
* Version: v0.0.1
*/
var fs = require('fs')
, join = require('path' ).join
, events = require('events' )
, util = require('util');
ScanFS = function(){
events.EventEmitter.call(this);
this.dirCount = 0;
this.fileCount = 0;
this.__pending = 0;
this.__recursive = true;
this._exclusions = require('path-filters').create();
}
util.inherits(ScanFS, events.EventEmitter);
/**
* 开始执行扫描
* @param path 扫描路径
* @param parent 父路径,不需要传
*/
ScanFS.prototype.scan = function(path, parent){
if(this._exclusions.hasMatch(path)){
if(this.__pending === 0)
this.emit('complete', this.fileCount, this.dirCount);
return ;
}
this.__pending++;
var me = this;
fs.stat(path, function(err, stat){
me.__pending--;
var eOpts = {
path: path,
stat: stat,
parent: parent
};
if(err)
me.emit('error', err);
else{
me.emit('path', path, eOpts);
if(!parent)
me.emit('root', path, eOpts);
if(stat.isFile()){
me.fileCount ++ ;
me.emit('file', path, eOpts);
} else if( stat.isDirectory()){
//if(parent){
me.dirCount ++ ;
me.emit('directory', path, eOpts);
//}
if(me.__recursive || parent === undefined){
me.__pending++;
fs.readdir(path, function(err, fileNames){
if(err)
me.emit('error', err);
else {
me.__pending--;
fileNames.forEach(function(fileName, idx, all){
me.scan(join(path, fileName), path);
});
if(me.__pending === 0)
me.emit('complete', me.fileCount, me.dirCount);
}
});
}
}
}
if(me.__pending === 0)
me.emit('complete', me.fileCount, me.dirCount);
});
};
/**
* 添加不包含的规则,可以使用带*的字符串、正则表达式、方法等,详细参考path-filters
* @param filter 过滤条件
* @param recursive 递归
* @returns ScanFS
*/
ScanFS.prototype.exclude = function(filter, recursive) {
this._exclusions.add(filter, recursive);
return this;
};
/**
* 得到path-filters
* @returns PathFilters
*/
ScanFS.prototype.getExclusions = function() {
return this._exclusions;
};
/**
* 注册扫描到跟目录时的处理事件
* @param callback 事件处理函数
* @returns ScanFS
*/
ScanFS.prototype.onRoot = function(callback) {
this.on('root', callback);
return this;
};
/**
* 注册扫描到每个路径时的处理事件
* @param callback 事件处理函数
* @returns ScanFS
*/
ScanFS.prototype.onPath = function(callback) {
this.on('path', callback);
return this;
};
/**
* 注册扫描到文件时的处理事件
* @param callback 事件处理函数
* @returns ScanFS
*/
ScanFS.prototype.onFile = function(callback) {
this.on('file', callback);
return this;
};
/**
* 注册扫描到目录时的处理事件
* @param callback 事件处理函数
* @returns ScanFS
*/
ScanFS.prototype.onDirectory = function(callback) {
this.on('directory', callback);
return this;
};
/**
* 注册错误处理事件
* @param callback 事件处理函数
* @returns ScanFS
*/
ScanFS.prototype.onError = function(callback) {
this.on('error', callback);
return this;
};
/**
* 注册扫描完成时的处理事件
* @param callback 事件处理函数
* @returns ScanFS
*/
ScanFS.prototype.onComplete = function(callback) {
this.on('complete', callback);
return this;
};
/**
* 设置是否递归子目录
* @param recursive 布尔值,true-递归子目录;false-不递归子目录
* @returns ScanFS
*/
ScanFS.prototype.setRecursive = function(recursive) {
this.__recursive = recursive;
return this;
};
/**
* 添加listeners(root, path, file, directory,error,complete),格式
* {
* root: function(){},
* path: function(){},
* ...
* }
* @param listeners
* @returns {*}
*/
ScanFS.prototype.listeners = function(listeners) {
var me = this;
Object.keys(listeners ).forEach(function(key, idx, all){
me.on(key, listeners[key]);
});
return this;
};
/**
* 创建扫描文件系统的对象。
* @returns {ScanFS}
*/
exports.create = function(){
return new ScanFS();
}