Skip to content

Commit

Permalink
Fix windows (yuanchuan#86)
Browse files Browse the repository at this point in the history
* Detect temporary editor files more wisely.
Fix yuanchuan#85

* Avoid side effects whenever possible.
Fix yuanchuan#79
  • Loading branch information
yuanchuan authored Apr 18, 2019
1 parent a7c597a commit 965c676
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ test/utils/__TREE__
.nyc_output/
coverage/
package-lock.json
.DS_Store
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ watcher.isClosed()

## Known issues

**Windows**

* Will output its parent directory when a new file/directory is created in a deep directory.

**Windows, node < v4.2.5**

* Failed to detect `remove` event
Expand Down
39 changes: 32 additions & 7 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,42 @@ function getMessages(cache) {
// Saving file from an editor? If so, assuming the
// non-existed files in the cache are temporary files
// generated by an editor and thus be filtered.
var dup = hasDup(cache.map(function(c) {
return c.replace(/^[~#]+|[~#]+$/, '');
}));
var reg = /~$|^\.#|^##$/g;
var hasSpecialChar = cache.some(function(c) {
return reg.test(c);
});

if (dup) {
filtered = filtered.filter(function(m) {
return is.exists(m)
if (hasSpecialChar) {
var dup = hasDup(cache.map(function(c) {
return c.replace(reg, '');
}));
if (dup) {
filtered = filtered.filter(function(m) {
return is.exists(m);
});
}
}

// Prevent redundant event for its parent when creating file/directory.
// The operation is kinda expensive so only be triggered under Windows.
// https://github.com/yuanchuan/node-watch/issues/79
if (is.windows()) {
var parents = filtered.map(function(n) {
return path.parse(n).dir;
});
filtered = filtered.filter(function(n) {
// Skip on removal
if (!is.exists(n)) {
return true;
}
// Ignore the parent directory
return !parents.some(function(m) {
return is.samePath(n, m);
});
});
}

return composeMessage(filtered)
return composeMessage(filtered);
}

function debounce(info, fn) {
Expand Down
16 changes: 14 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ describe('file events', function() {
});
});

it('should identify `remove` event on directory', function(done) {
var dir = 'home/a';
var home = tree.getPath('home');
var fpath = tree.getPath(dir);
watcher = watch(home, function(evt, name) {
if (evt === 'remove' && name === fpath) done();
});
watcher.on('ready', function() {
tree.remove(dir);
});
});

it('should identify `update` event', function(done) {
var file = 'home/a/file1';
var fpath = tree.getPath(file);
Expand Down Expand Up @@ -390,7 +402,7 @@ describe('options', function() {
delay: 0,
recursive: true,
filter: function(name) {
return /bb\/file2/.test(name);
return /file2/.test(name);
}
}

Expand All @@ -407,7 +419,7 @@ describe('options', function() {
tree.modify(file2);

setTimeout(function() {
assert.equal(times, 1, 'report file2');
assert.equal(times, 1, 'should only report /home/bb/file2 once');
assert.equal(matchIgnoredFile, false, 'home/bb/file1 should be ignored');
done();
}, 100);
Expand Down

0 comments on commit 965c676

Please sign in to comment.