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');
});
The best and only working solution is there : https://npmjs.org/package/grunt-focus Add this plugin and then :
focus: {
sources: {
include: ['js', 'html', 'css', 'grunt']
},
testu: {
include: ['js', 'html', 'css', 'testu', 'grunt']
},
testi: {
include: ['js', 'html', 'css', 'testu', 'testi', 'grunt']
}
},
watch: {
js: {
files: paths.js,
tasks: ['jshint'],
options: {
livereload: true
}
},
html: {
files: paths.html,
options: {
livereload: true
}
},
css: {
files: paths.css,
tasks: ['csslint'],
options: {
livereload: true
}
},
testu: {
files: ['test/**/*.js', 'test/**/*.css'],
tasks: ['mochaTest'],
options: {}
},
testi: {
files: ['test/**/*.js', 'test/**/*.css'],
tasks: ['exec:cleanTestDB', 'protractor_webdriver', 'protractor'],
options: {}
},
grunt: {
files: ['Gruntfile.js', 'server/config/env/*.js'],
options: {
reload: true
}
}
}
Then you use focus:sources or focus:testu as your convenience.
JM.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.