From a85cd8582387df1ad0381775641f2f8d3f1be450 Mon Sep 17 00:00:00 2001 From: Stanislav Sysoev Date: Wed, 18 Oct 2017 13:19:24 +1100 Subject: [PATCH] bail out from watching eralier using watchOptions ignored --- lib/DirectoryWatcher.js | 9 ++++++++- package.json | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/DirectoryWatcher.js b/lib/DirectoryWatcher.js index 2aea0df..ad1a2f5 100644 --- a/lib/DirectoryWatcher.js +++ b/lib/DirectoryWatcher.js @@ -5,6 +5,7 @@ var EventEmitter = require("events").EventEmitter; var async = require("async"); var chokidar = require("chokidar"); +var anymatch = require('anymatch'); var fs = require("graceful-fs"); var path = require("path"); @@ -230,13 +231,14 @@ DirectoryWatcher.prototype.watch = function watch(filePath, startTime) { DirectoryWatcher.prototype.onFileAdded = function onFileAdded(filePath, stat) { if(filePath.indexOf(this.path) !== 0) return; if(/[\\\/]/.test(filePath.substr(this.path.length + 1))) return; - + if(this.isIgnored(filePath)) return; this.setFileTime(filePath, +stat.mtime, false, "add"); }; DirectoryWatcher.prototype.onDirectoryAdded = function onDirectoryAdded(directoryPath /*, stat */) { if(directoryPath.indexOf(this.path) !== 0) return; if(/[\\\/]/.test(directoryPath.substr(this.path.length + 1))) return; + if(this.isIgnored(directoryPath)) return; this.setDirectory(directoryPath, true, false, "add"); }; @@ -283,6 +285,7 @@ DirectoryWatcher.prototype.doInitialScan = function doInitialScan() { callback(); return; } + if(this.isIgnored(itemPath)) return; if(stat.isFile()) { if(!this.files[itemPath]) this.setFileTime(itemPath, +stat.mtime, true); @@ -341,6 +344,10 @@ DirectoryWatcher.prototype.close = function() { this.emit("closed"); }; +DirectoryWatcher.prototype.isIgnored = function(itemPath) { + return this.options.ignored && anymatch(this.options.ignored, itemPath); +} + function ensureFsAccuracy(mtime) { if(!mtime) return; if(FS_ACCURACY > 1 && mtime % 1 !== 0) diff --git a/package.json b/package.json index 72567df..51da2fe 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "should": "^8.3.1" }, "dependencies": { + "anymatch": "^1.3.2", "async": "^2.1.2", "chokidar": "^1.7.0", "graceful-fs": "^4.1.2"