0

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?

  • Do you have your own watch function that you are calling or do intend to use gulp.watch? If you have a watch function what does it look like? Are you using gulp-watch there? We need more information. Gulp-watch can be passed a function as you have it in the working example but not a list of task names. – Mark Jul 27 '17 at 00:51
  • @Mark I am using gulp-watch. I forgot to include a part of my code in the example: `var gulp = require('gulp'); var watch = require('gulp-watch');` – Jasper Callens Jul 27 '17 at 07:36

1 Answers1

0

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Thanks for your response. I had indeed looked over the function defintions for watch and gulp-watch. I have correct my code to your last example but I still do not get the required result. So this works: `watch(watchPath, FilesChanged) .pipe(debug());` But this does not: `gulp.watch(watchPath, ['FilesChanged']) .pipe(debug());` – Jasper Callens Jul 28 '17 at 08:28