This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
ndir.js
284 lines (270 loc) · 6.46 KB
/
ndir.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*!
* ndir - lib/ndir.js
* Copyright(c) 2012 fengmk2 <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var fs = require('fs');
var path = require('path');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
fs.exists = fs.exists || path.exists;
/**
* dir Walker Class.
*
* @constructor
* @param {String} root Root path.
* @param {Function(dirpath, files)} [onDir] The `dir` event callback.
* @param {Function} [onEnd] The `end` event callback.
* @param {Function(err)} [onError] The `error` event callback.
* @public
*/
function Walk(root, onDir, onEnd, onError) {
if (!(this instanceof Walk)) {
return new Walk(root, onDir, onEnd, onError);
}
this.dirs = [path.resolve(root)];
if (onDir) {
this.on('dir', onDir);
}
if (onEnd) {
this.on('end', onEnd);
}
onError && this.on('error', onError);
var self = this;
// let listen `files` Event first.
process.nextTick(function () {
self.next();
});
}
util.inherits(Walk, EventEmitter);
exports.Walk = Walk;
/**
* Walking dir base on `Event`.
*
* @param {String} dir Start walking path.
* @param {Function(dir)} [onDir] When a dir walk through, `emit('dir', dirpath, files)`.
* @param {Function} [onEnd] When a dir walk over, `emit('end')`.
* @param {Function(err)} [onError] When stat a path error, `emit('error', err, path)`.
* @return {Walk} dir walker instance.
* @public
*/
exports.walk = function walk(dir, onDir, onEnd, onError) {
return new Walk(dir, onDir, onEnd, onError);
};
/**
* Next move, if move to the end,
* will `emit('end')` event.
*
* @private
*/
Walk.prototype.next = function () {
var dir = this.dirs.shift();
if (!dir) {
return this.emit('end');
}
this._dir(dir);
};
/**
* @private
*/
Walk.prototype._dir = function (dir) {
var self = this;
fs.readdir(dir, function (err, files) {
if (err) {
self.emit('error', err, dir);
return self.next();
}
var infos = [];
if (files.length === 0) {
self.emit('dir', dir, infos);
return self.next();
}
var counter = 0;
files.forEach(function (file) {
var p = path.join(dir, file);
fs.lstat(p, function (err, stats) {
counter++;
if (err) {
self.emit('error', err, p);
} else {
infos.push([p, stats]);
if (stats.isDirectory()) {
self.dirs.push(p);
}
}
if (counter === files.length) {
self.emit('dir', dir, infos);
self.next();
}
});
});
});
};
/**
* Copy file, auto create tofile dir if dir not exists.
*
* @param {String} fromfile, Source file path.
* @param {String} tofile, Target file path.
* @param {Function(err)} callback
* @public
*/
exports.copyfile = function copyfile(fromfile, tofile, callback) {
fromfile = path.resolve(fromfile);
tofile = path.resolve(tofile);
if (fromfile === tofile) {
var msg = 'cp: "' + fromfile + '" and "' + tofile + '" are identical (not copied).';
return callback(new Error(msg));
}
exports.mkdir(path.dirname(tofile), function (err) {
if (err) {
return callback(err);
}
var ws = fs.createWriteStream(tofile);
var rs = fs.createReadStream(fromfile);
var onerr = function (err) {
callback && callback(err);
callback = null;
};
ws.once('error', onerr); // if file not open, these is only error event will be emit.
rs.once('error', onerr);
ws.on('close', function () {
// after file open, error event could be fire close event before.
callback && callback();
callback = null;
});
rs.pipe(ws);
});
};
/**
* @private
*/
function _mkdir(dir, mode, callback) {
fs.exists(dir, function (exists) {
if (exists) {
return callback();
}
fs.mkdir(dir, mode, callback);
});
}
/**
* mkdir if dir not exists, equal mkdir -p /path/foo/bar
*
* @param {String} dir
* @param {Number} [mode] file mode, default is 0777.
* @param {Function(err)} callback
* @public
*/
exports.mkdir = function mkdir(dir, mode, callback) {
if (typeof mode === 'function') {
callback = mode;
mode = 0777 & (~process.umask());
}
var parent = path.dirname(dir);
fs.exists(parent, function (exists) {
if (exists) {
return _mkdir(dir, mode, callback);
}
exports.mkdir(parent, mode, function (err) {
if (err) {
return callback(err);
}
_mkdir(dir, mode, callback);
});
});
};
exports.mkdirp = exports.mkdir;
/**
* Read stream data line by line.
*
* @constructor
* @param {String|ReadStream} file File path or data stream object.
*/
function LineReader(file) {
if (typeof file === 'string') {
this.readstream = fs.createReadStream(file);
} else {
this.readstream = file;
}
this.remainBuffers = [];
var self = this;
this.readstream.on('data', function (data) {
self.ondata(data);
});
this.readstream.on('error', function (err) {
self.emit('error', err);
});
this.readstream.on('end', function () {
self.emit('end');
});
}
util.inherits(LineReader, EventEmitter);
/**
* `Stream` data event handler.
*
* @param {Buffer} data
* @private
*/
LineReader.prototype.ondata = function (data) {
var i = 0;
var found = false;
for (var l = data.length; i < l; i++) {
if (data[i] === 10) {
found = true;
break;
}
}
if (!found) {
this.remainBuffers.push(data);
return;
}
var line = null;
if (this.remainBuffers.length > 0) {
var size = i;
var j, jl = this.remainBuffers.length;
for (j = 0; j < jl; j++) {
size += this.remainBuffers[j].length;
}
line = new Buffer(size);
var pos = 0;
for (j = 0; j < jl; j++) {
var buf = this.remainBuffers[j];
buf.copy(line, pos);
pos += buf.length;
}
// check if `\n` is the first char in `data`
if (i > 0) {
data.copy(line, pos, 0, i);
}
this.remainBuffers = [];
} else {
line = data.slice(0, i);
}
this.emit('line', line);
this.ondata(data.slice(i + 1));
};
/**
* Line data reader
*
* @example
* ```
* var ndir = require('ndir');
* ndir.createLineReader('/tmp/access.log')
* .on('line', function (line) {
* console.log(line.toString());
* })
* .on('end', function () {
* console.log('end');
* })
* .on('error', function (err) {
* console.error(err);
* });
* ```
*
* @param {String|ReadStream} file, file path or a `ReadStream` object.
*/
exports.createLineReader = function (file) {
return new LineReader(file);
};