Gulp watch callback task not firing

614 views Asked by At

I'm trying to set up a gulp-watch which triggers a gulp task when some files change. For this purpose I'm using the following code:

gulp.task('watchChanges', function(cb){
    console.log("Looking for file changes in " + watchPath+".");  

    return watch(watchPath, ['FilesChanged']);
});

gulp.task('FilesChanged', function (cb){
    FilesChanged();
    cb();
})

function FilesChanged()
{
    console.log("Files changed:")
}

Whenever the files change I would like it if the 'FilesChanged' task would fire and I'd see the "Files changed:" text in console. This doesn't happen.

When I change my watchChanges task to this:

gulp.task('watchChanges', function(cb){
    console.log("Looking for file changes in " + watchPath+".");  

    return watch(watchPath, FilesChanged);
});

Can anyone explain why the first piece of code does not execute correctly?

1

There are 1 answers

1
Mark On

Since you are using gulp-watch if you look at its function definition:

watch(glob, [options, callback])

you can see that it will take a callback function which is why your

return watch(watchPath, FilesChanged);

works and

return watch(watchPath, ['FilesChanged']);

does not. gulp-watch will not take an array of tasks like gulp.watch will:

gulp.watch(glob[, opts], tasks)

gulp-watch and gulp.watch are two entirely different things. What you probably want is

gulp.task('watchChanges', function(cb){
    console.log("Looking for file changes in " + watchPath+".");  

    gulp.watch(watchPath, ['FilesChanged']);
});

If you then to see which files are being processed, try one of the suggestions from get the current file name.