Gruntjs watch different folders and execute tasks

markovuksanovic picture markovuksanovic · Dec 12, 2012 · Viewed 9k times · Source

I was wondering if it's possible to configure a watch task to watch two different folders and execute a different task on each on folder. For example, whenever something changes is /folder1 then task1 should be executed, whenever something is changed in /folder2 then task2 should be executed.

The folder structure is of the following form: root |-folder1 |-folder2

Answer

jaime picture jaime · Dec 12, 2012

Watch behaves like a multi-task, so yes you can configure it to watch different sets of files and perform different tasks

watch:{
  set1: {
    files: [ 'folder1/**/*' ],  //<- this watch all files (even sub-folders)
    tasks: ['task1']
  },
  set2: {
    files: ['folder2/**/*'],
    tasks: ['task2']
  }
},

Then you can run one watch task or both

grunt.registerTask('watchSet1', ['watch:set1']);
grunt.registerTask('watchSet1And2', ['watch:set1', 'watch:set2']);      

Haven't tested but it should work.