1

For example let's say i have this listener:

gulp.watch('../templates/**/*.tpl', gulp.series('template'));

and the associated task:

var template = gulp.task('template', function(done) {
    console.log(filename);
    done();
});

Is it possible to get the filename of the current .tpl file which has fired the watch-event and if yes, how?

FFirmenich
  • 5,611
  • 1
  • 19
  • 28

1 Answers1

3

From the gulp.watch docs.

const { watch } = require('gulp');

const watcher = watch(['input/*.js']);

watcher.on('all', function(path, stats) {
 console.log(`File ${path} was changed`);
});

You might use that 'path' info like this:

function scripts(myPath) {
  console.log(`in scripts, path = ${myPath}`);

  return gulp.src(myPath)
    .pipe(gulp.dest('pathTest'));
};

function watch(done) {

  const watcher = gulp.watch(["../templates/**/*.tpl"]);

  watcher.on('change', function (path, stats) {
    scripts(path);
  });

  done();
}

exports.default = gulp.series(watch);

So rearrange your code like the above example:

const watcher = gulp.watch('../templates/**/*.tpl');
watcher.on('change', function (path,stats) {
  template(path);
};

function template(filename) {
    console.log(filename);
    return gulp.src…...
});
Mark
  • 143,421
  • 24
  • 428
  • 436