【发布时间】:2016-09-13 22:49:02
【问题描述】:
我目前正在尝试在 grunt 中创建一个任务,发送参数以多次运行另一个任务。
这是我已经可以使用的单一构建任务:
var country = grunt.option('country') || 'X0';
var product = grunt.option('product') || 'Y0';
var subdomain = grunt.option('subdomain') || 'Z0';
grunt.registerTask('build_home', [
'sprite:publicProduct',
'sprite:home',
'uglify:home',
'sass:home',
'csscomb:home',
'cssmin:home'
]);
所以在我的日常生活中,我会运行以下命令:
grunt build_home --country=X0 --product=Y7 --subdomain=Z3
我现在想要的是一个可以运行我所有可能的预定义选项的任务:
grunt build_home_all
应该是这样的:
grunt.registerTask('build_home_all', function() {
var products = ['Y0','Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8','Y9'];
var subdomains = ['Z0','Z1','Z2'];
for (i = 0; i < products.length; i++) {
for (j = 0; j < subdomains.length; j++) {
grunt.task.run('build_home --product='+products[i]+' --subdomain='+subdomains[j]);
};
};
grunt.log.ok(['Finished.']);
});
我已经用 grunt.util.spawn 实现了这一点,但它有点异步运行,并且迫使我的 cpu 同时运行各种任务。
【问题讨论】:
-
您应该将任务推送到您在 for 循环中生成的数组中,然后运行该数组,而不是在 for 循环中运行任务。
-
@theaccordance 这实际上非常有用并且是解决方案的一部分,但我仍然无法运行像
grunt.task.run('build_home --country=X0 --product=Y7 --subdomain=Z3')这样的东西,因为它显示Warning: Task "build_home --country=X0 --product=Y7 --subdomain=Z3" not found. Use --force to continue.它只是无法识别参数。跨度> -
您将希望创建一个自定义任务作为每个构建过程的初始化程序,并使用
this.args传递选项。您的排队任务将看起来像build_home:X0:Y7:Z3。参考:gruntjs.com/api/inside-tasks#this.args -
好的,我写了一个答案,它可能需要一些调整,因为我做了几个假设。希望这会有所帮助
标签: javascript node.js for-loop gruntjs command-line-interface