【发布时间】:2012-11-29 18:28:27
【问题描述】:
我想知道是否可以配置一个监视任务来监视两个不同的文件夹并在每个文件夹上执行不同的任务。例如,每当 /folder1 发生变化时,就应该执行 task1,每当 /folder2 发生变化时,就应该执行 task2。
文件夹结构如下: 根 |-文件夹1 |-文件夹2
【问题讨论】:
标签: javascript node.js watch gruntjs
我想知道是否可以配置一个监视任务来监视两个不同的文件夹并在每个文件夹上执行不同的任务。例如,每当 /folder1 发生变化时,就应该执行 task1,每当 /folder2 发生变化时,就应该执行 task2。
文件夹结构如下: 根 |-文件夹1 |-文件夹2
【问题讨论】:
标签: javascript node.js watch gruntjs
观看就像一个多任务,所以是的,你可以配置它来观看不同的文件集并执行不同的任务
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']);
尚未测试,但应该可以。
【讨论】:
grunt watchSet1And2,两个手表都会启动。
最好且唯一可行的解决方案是: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
}
}
}
然后您可以使用 focus:sources 或 focus:testu 来方便。
JM.
【讨论】:
如果您希望监视任务同时运行。 RobW 这里有一个很好的解决方案How to run two grunt watch tasks simultaneously
我花了一些时间找到解决方案,所以这是该解决方案的 sn-p。
在自定义任务中动态编写配置对象是可行的。
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');
});
【讨论】: