Gruntjs观看不同的文件夹并执行任务

我想知道是否可以configuration一个监视任务来观看两个不同的文件夹,并在每个文件夹上执行不同的任务。 例如,每当有变化的是/ folder1,那么task1应该被执行,只要/ folder2中的东西被改变,那么task2就应该被执行。

文件夹结构具有以下forms:root | -folder1 | -folder2

手表的行为像一个多任务,所以是的,你可以configuration它来观看不同的文件集,并执行不同的任务

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

那么你可以运行一个观察任务或两个

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

没有testing,但它应该工作。

如果你想让监视任务同时运行。 RobW在这里有一个很好的解决scheme如何同时运行两个grunt监视任务

我花了一些时间去解决scheme,所以这里是该解决scheme的片段。

在自定义任务中dynamic编写configuration对象。

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

最好的和唯一的工作解决scheme是: https : //npmjs.org/package/grunt-focus添加这个插件,然后:

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

然后你使用焦点:来源或重点:testu作为您的方便。

JM。