Running a task from the .on() event of gulp-watch












0















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?










share|improve this question



























    0















    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?










    share|improve this question

























      0












      0








      0








      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?










      share|improve this question














      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?







      javascript ecmascript-6 gulp






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 19:30









      CodeMonkeyCodeMonkey

      21114




      21114
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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);
          }





          share|improve this answer



















          • 1





            The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

            – CodeMonkey
            Dec 28 '18 at 20:09





















          0














          Whatever task you want to run you can write in the on change event I think it will work. You can also use event emitter class that will help you to emit the event and on event triggered you can run the task.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963451%2frunning-a-task-from-the-on-event-of-gulp-watch%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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);
            }





            share|improve this answer



















            • 1





              The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

              – CodeMonkey
              Dec 28 '18 at 20:09


















            1














            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);
            }





            share|improve this answer



















            • 1





              The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

              – CodeMonkey
              Dec 28 '18 at 20:09
















            1












            1








            1







            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);
            }





            share|improve this answer













            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);
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 28 '18 at 19:45









            Holger WillHolger Will

            4,24011725




            4,24011725








            • 1





              The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

              – CodeMonkey
              Dec 28 '18 at 20:09
















            • 1





              The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

              – CodeMonkey
              Dec 28 '18 at 20:09










            1




            1





            The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

            – CodeMonkey
            Dec 28 '18 at 20:09







            The task I was trying to execute was browserify(...) and tsify(...) for transpiling typescript to javascript and minifying it. I just tried calling browserify and it appears to work, however, it doesn't do any of the typical logging that you see in the console when you run it as a gulp task. I can add my own logging so that's not a problem. I didn't realize that browserify and the other gulp plugins could be executed independently. Thank you.

            – CodeMonkey
            Dec 28 '18 at 20:09















            0














            Whatever task you want to run you can write in the on change event I think it will work. You can also use event emitter class that will help you to emit the event and on event triggered you can run the task.






            share|improve this answer




























              0














              Whatever task you want to run you can write in the on change event I think it will work. You can also use event emitter class that will help you to emit the event and on event triggered you can run the task.






              share|improve this answer


























                0












                0








                0







                Whatever task you want to run you can write in the on change event I think it will work. You can also use event emitter class that will help you to emit the event and on event triggered you can run the task.






                share|improve this answer













                Whatever task you want to run you can write in the on change event I think it will work. You can also use event emitter class that will help you to emit the event and on event triggered you can run the task.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 28 '18 at 19:41









                Amit ChowdhuryAmit Chowdhury

                112




                112






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963451%2frunning-a-task-from-the-on-event-of-gulp-watch%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas