Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions lib/DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,37 @@ DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, i
};

DirectoryWatcher.prototype.setDirectory = function setDirectory(directoryPath, exist, initial, type) {
var old = this.directories[directoryPath];
if(!old) {
if(exist) {
if(this.nestedWatching) {
this.createNestedWatcher(directoryPath);
} else {
this.directories[directoryPath] = true;
}
if(directoryPath === this.path) {
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
} else {
if(!exist) {
if(this.nestedWatching)
this.directories[directoryPath].close();
delete this.directories[directoryPath];
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
var old = this.directories[directoryPath];
if(!old) {
if(exist) {
if(this.nestedWatching) {
this.createNestedWatcher(directoryPath);
} else {
this.directories[directoryPath] = true;
}
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
}
} else {
if(!exist) {
if(this.nestedWatching)
this.directories[directoryPath].close();
delete this.directories[directoryPath];
if(!initial && this.watchers[withoutCase(this.path)]) {
this.watchers[withoutCase(this.path)].forEach(function(w) {
w.emit("change", directoryPath, w.data, initial ? "initial" : type);
});
}
}
}
}
Expand Down
50 changes: 50 additions & 0 deletions test/Watchpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,56 @@ describe("Watchpack", function() {
});
});

it("should watch a missing directory", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
});
var changeEvents = [];
w.on("change", function(file) {
if(changeEvents[changeEvents.length - 1] === file)
return;
changeEvents.push(file);
});
w.on("aggregated", function(changes) {
changes.should.be.eql([path.join(fixtures, "dir", "sub")]);
changeEvents.should.be.eql([path.join(fixtures, "dir", "sub")]);
w.close();
done();
});
testHelper.dir("dir");
testHelper.tick(function() {
w.watch([], [path.join(fixtures, "dir", "sub")]);
testHelper.tick(function() {
testHelper.dir(path.join("dir", "sub"));
});
});
});

it("should watch a directory (add directory)", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
});
var changeEvents = [];
w.on("change", function(file) {
if(changeEvents[changeEvents.length - 1] === file)
return;
changeEvents.push(file);
});
w.on("aggregated", function(changes) {
changes.should.be.eql([path.join(fixtures, "dir")]);
changeEvents.should.be.eql([path.join(fixtures, "dir", "sub")]);
w.close();
done();
});
testHelper.dir("dir");
testHelper.tick(function() {
w.watch([], [path.join(fixtures, "dir")]);
testHelper.tick(function() {
testHelper.dir(path.join("dir", "sub"));
});
});
});

it("should watch a directory (delete directory)", function(done) {
var w = new Watchpack({
aggregateTimeout: 1000
Expand Down