directory-walker
Advanced tools
Comparing version 1.2.1 to 1.2.3
@@ -7,79 +7,121 @@ // third-party dependencies | ||
function stop() { | ||
this.stopped = true; | ||
} | ||
function DirectoryWalker(config) { | ||
events.EventEmitter.call(this); | ||
this.onFile = config.onFile; | ||
this.onDirectory = config.onDirectory; | ||
this.pending = 0; | ||
if (config.excludes) { | ||
this._excludesMap = {}; | ||
this._recursive = true; | ||
this._pending = 0; | ||
this._exclusions = require('path-filters').create(); | ||
} | ||
for (var i = 0; i < config.excludes.length; i++) { | ||
var exclude = path.resolve(config.excludes[i]); | ||
this._excludesMap[exclude] = true; | ||
} | ||
} | ||
util.inherits(DirectoryWalker, events.EventEmitter); | ||
if (config.listeners) { | ||
var listeners = config.listeners; | ||
for (var key in listeners) { | ||
if (listeners.hasOwnProperty(key)) { | ||
this.on(key, listeners[key]); | ||
} | ||
DirectoryWalker.prototype.listeners = function(listeners) { | ||
for (var key in listeners) { | ||
if (listeners.hasOwnProperty(key)) { | ||
this.on(key, listeners[key]); | ||
} | ||
} | ||
return this; | ||
} | ||
util.inherits(DirectoryWalker, events.EventEmitter); | ||
DirectoryWalker.prototype.onRoot = function(callback) { | ||
this.on('root', callback); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.walk = function(dir) { | ||
this.visit(dir); | ||
DirectoryWalker.prototype.onPath = function(callback) { | ||
this.on('path', callback); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.notifyComplete = function() { | ||
this.emit('complete'); | ||
DirectoryWalker.prototype.onFile = function(callback) { | ||
this.on('file', callback); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.isExplicitExclude = function(path) { | ||
return this._excludesMap && this._excludesMap[path]; | ||
DirectoryWalker.prototype.onDirectory = function(callback) { | ||
this.on('directory', callback); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.visit = function(file, parent) { | ||
DirectoryWalker.prototype.onError = function(callback) { | ||
this.on('error', callback); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.onComplete = function(callback) { | ||
this.on('complete', callback); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.recursive = function(recursive) { | ||
this._recursive = recursive; | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.exclude = function(filter, recursive) { | ||
this._exclusions.add(filter, recursive); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.walk = function(dir) { | ||
this._visit(dir); | ||
return this; | ||
}; | ||
DirectoryWalker.prototype.getExclusions = function() { | ||
return this._exclusions; | ||
}; | ||
DirectoryWalker.prototype._visit = function(path, parent) { | ||
var self = this; | ||
this.pending++; | ||
this._pending++; | ||
fs.stat(file, | ||
function(err, stat) { | ||
self.pending--; | ||
fs.stat(path, | ||
if (stat && stat.isDirectory()) { | ||
self.readdir(file); | ||
} else { | ||
// if onFile returns false then don't visit file | ||
if (!self.onFile || (self.onFile(file) !== false)) { | ||
self.emit('file', file, parent); | ||
} | ||
function(err, stat) { | ||
self._pending--; | ||
var eventArgs = { | ||
path: path, | ||
stat: stat, | ||
parent: parent | ||
}; | ||
self.emit('path', path, eventArgs); | ||
if (!parent) { | ||
self.emit('root', path, eventArgs); | ||
} | ||
if (stat && stat.isDirectory()) { | ||
eventArgs.stop = stop; | ||
self.emit('directory', path, eventArgs); | ||
if (!eventArgs.stopped && self.recursive) { | ||
self._walkDir(path); | ||
} | ||
} else { | ||
self.emit('file', path, eventArgs); | ||
} | ||
if (self.pending === 0) { | ||
self.notifyComplete(); | ||
} | ||
}); | ||
if (self._pending === 0) { | ||
self.emit('complete'); | ||
} | ||
}); | ||
}; | ||
DirectoryWalker.prototype.readdir = function(dir) { | ||
DirectoryWalker.prototype._walkDir = function(dir) { | ||
var dir = path.resolve(dir); | ||
if (this._excludesMap && this._excludesMap[dir]) { | ||
return; | ||
} | ||
var self = this; | ||
this.pending++; | ||
this._pending++; | ||
fs.readdir(dir, function(err, files) { | ||
self.pending--; | ||
fs.readdir(dir, function(err, filenames) { | ||
self._pending--; | ||
@@ -91,15 +133,12 @@ if (err) { | ||
if (!self.onDirectory || (self.onDirectory(dir) !== false)) { | ||
self.emit('directory', dir); | ||
for (var i = 0; i < filenames.length; i++) { | ||
var filename = path.join(dir, filenames[i]); | ||
for ( var i = 0; i < files.length; i++) { | ||
var file = path.resolve(dir + '/' + files[i]); | ||
if (!this._excludesMap || !this._excludesMap[dir]) { | ||
self.visit(file, dir); | ||
} | ||
if (!self._exclusions.hasMatch(filename)) { | ||
self._visit(filename, dir); | ||
} | ||
} | ||
if (self.pending === 0) { | ||
self.notifyComplete(); | ||
if (self._pending === 0) { | ||
self.emit('complete'); | ||
} | ||
@@ -109,9 +148,4 @@ }); | ||
exports.createDirectoryWalker = function(config) { | ||
return new DirectoryWalker(config); | ||
}; | ||
exports.walk = function(options) { | ||
var walker = new DirectoryWalker(options); | ||
walker.walk(options.basedir); | ||
} | ||
exports.create = function() { | ||
return new DirectoryWalker(); | ||
}; |
{ | ||
"name": "directory-walker", | ||
"description": "Directory walker", | ||
"version": "1.2.1", | ||
"version": "1.2.3", | ||
"homepage": "https://github.com/philidem/node-directory-walker", | ||
@@ -14,3 +14,4 @@ "authors": [ | ||
"dependencies": { | ||
"path-filters": "1.0.x" | ||
} | ||
} |
3745
113
1
+ Addedpath-filters@1.0.x
+ Addedpath-filters@1.0.6(transitive)