Gulp 在查看多个文件集时从Gaze获取重复的更改通知

Gulp 在查看多个文件集时从Gaze获取重复的更改通知,gulp,Gulp,我们使用gulp with Gaze来监视文件/目录的更改。我们需要观察两组独立的文件,如果有变化,我们需要为每一组运行不同的任务 问题是一个文件集中的更改会触发两个手表 这是一个最小的gulpfile,它展示了这种行为 'use strict'; var gulp = require('gulp'), Gaze = require('gaze').Gaze; gulp.task('watch', function () { function watch(paths, taskTo

我们使用gulp with Gaze来监视文件/目录的更改。我们需要观察两组独立的文件,如果有变化,我们需要为每一组运行不同的任务

问题是一个文件集中的更改会触发两个手表

这是一个最小的gulpfile,它展示了这种行为

'use strict';

var gulp = require('gulp'),
  Gaze = require('gaze').Gaze;

gulp.task('watch', function () {

  function watch(paths, taskToRun) {
    new Gaze(paths, {mode: 'poll'}).on('all', function(event, filename) {
      console['log']('Change detected - in '+filename+' for event '+event+' for task watcher '+taskToRun);
    });
  }

  watch(['foo/**/*.js'], 'jstask1');
  watch(['bar/**/*.js'], 'jstask2');

});
我通过跑步来设置它

npm install gulp gaze 
touch foo/a.js
touch bar/b.js
gulp watch
我在另一个候机楼跑

touch foo/a.js
我得到

[12:51:33] Using gulpfile ~/tmp/gaze/gulpfile.js
[12:51:33] Starting 'watch'...
[12:51:33] Finished 'watch' after 13 ms
Change detected - in /home/foo/tmp/gaze/foo/a.js for event changed for task watcher jstask1
Change detected - in /home/foo/tmp/gaze/foo/a.js for event changed for task watcher jstask2
jstask1和jstask2都被触发。由于jstask2的路径不匹配,我认为应该只触发jstask1

我们正在使用Gaze,因为我们正在多个平台(OSX和Linux)上运行vagrant内部开发,在VirtualBox上使用源目录的VirtualBox文件共享。我们在使用其他方法正确检测文件的添加和删除时遇到问题。这个特殊的测试是直接在我的linux机器上完成的(不涉及vagrant),但我们在vagrant中也看到了同样的问题


这是一个bug还是我设置的凝视错误?

找到了解决问题的方法。看起来凝视的行为仍然是不正确的,但是如果我直接在事件回调中使用minimatch库,我可以检查文件名是否与我真正想要的特定任务的路径匹配。如果匹配,则可以运行所需的任务,否则将忽略该事件

下面是上面更新的示例

'use strict';

var gulp = require('gulp'),
  Gaze = require('gaze').Gaze,
  Minimatch = require("minimatch").Minimatch;

gulp.task('watch', function () {

  function watch(paths, task) {
    //check for bare strings instead of arrays
    if(typeof paths == 'string') {
      paths=[paths];
    }

    var cwd=process.cwd();
    var len=paths.length;
    var minimatchPaths=[];
    for(var i=0; i < len; i++) {
      var path = paths[i];
      minimatchPaths.push(new Minimatch(cwd + '/' + path));
    }

    function checkPaths(filename) {
      var len=minimatchPaths.length;
      for(var i=0; i < len; i++) {
        var minimatchPath = minimatchPaths[i];
        if(minimatchPath.match(filename)) {
          return true;
        }
      }
      return false;
    }

    new Gaze(paths, {mode: 'poll'}).on('all', function(event, filename) {
      if(checkPaths(filename)) {
        console.log(event+' detected on '+filename+' for '+ task);
      }
    });
  }

  watch(['foo/**/*.js'], 'jstask1');
  watch(['bar/**/*.js'], 'jstask2');

});
“严格使用”;
var gulp=需要('gulp'),
凝视=需要(“凝视”)。凝视,
最小匹配=要求(“最小匹配”)。最小匹配;
吞咽任务('watch',函数(){
功能监视(路径、任务){
//检查是否有空字符串而不是数组
如果(路径类型=='string'){
路径=[路径];
}
var cwd=process.cwd();
var len=路径长度;
var minimatchPaths=[];
对于(变量i=0;i