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
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.
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');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With