Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gruntjs watch different folders and execute tasks

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

like image 249
markovuksanovic Avatar asked Dec 12 '12 05:12

markovuksanovic


2 Answers

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.

like image 118
jaime Avatar answered Nov 10 '22 01:11

jaime


If you want the watch tasks to run simultaneously. There is a great solution by RobW here How to run two grunt watch tasks simultaneously

I spent some time getting to the solution, so here's the snippet from that solution.

Dynamically writing a config object in a custom task works.

grunt.registerTask('watch:test', function() {
  // Configuration for watch:test tasks.
  var config = {
    options: {
      interrupt: true
    },
    unit: {
      files: [
        'test/unit/**/*.spec.coffee'
      ],
      tasks: ['karma:unit']
    },
    integration: {
      files: [
        'test/integration/**/*.rb',
        '.tmp/scripts/**/*.js'
      ],
      tasks: ['exec:rspec']
    }
  };

  grunt.config('watch', config);
  grunt.task.run('watch');
});
like image 2
Vishak Partha Avatar answered Nov 10 '22 01:11

Vishak Partha