I'm trying to set up a watch process in gulp that takes the file that was modified as a parameter.
I have tried using the ".on('change'...)" hook from gulp.watch(), as there doesn't appear to be any other way to get the full path of the file that triggered the watch event. This allows me access to the file that was changed, but I can't figure out how to execute a task at this point. I read elsewhere that gulp.start() used to work, but as of gulp 4.0, this functionality appears to have been removed.
I have also looked at gulp-run, and trying to execute a task from the command line but couldn't seem to get that working either.
This is a trimmed down version of what I am using:
gulp.task("watch", function() {
return gulp.watch("<path and filemask to watch>")
.on("change", function(file) {
runTaskHere(file);
})
}
I can run this watcher through "gulp watch" in the command line, and then modify a file that is part of the path & filemask. This all works fine. I can print out the file parameter and it is correct. I simply cannot get a task to execute from this point. If I switch the code around a bit so that instead of an anonymous function, it uses a gulp.series() pipeline, the task gets executed as expected but I do not have access to the path & filename of the file that was modified.
Basically what I'm trying to do is manually run a gulp task from within gulpfile.js based on the path information of the modified file, and have it run essentially from within a wrapper function (the .on(...) hook from watch). I understand that the designers of gulp have been trying to get away from this pattern, but there are still cases where I can see no alternative to it. If it's not possible to achieve what I'm doing here, are there any other options within gulp that can achieve what I'm trying to do here?
Is my only option to create a watch for each directory (based on my parsing rules) and forget about trying to read the path information?
As gulp tasks are just functions, why not :
function mytask() {
// do something here
}
gulp.task("sometask", mytask)
Then you can later start your task by just calling the function...
on("change", function(file) {
mytask(file);
}