【问题标题】:Grunt for-loop on grunt.task.run with variables在 grunt.task.run 上使用变量进行 Grunt for 循环
【发布时间】: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


【解决方案1】:

当使用grunt.task.run 方法调用任务时,您将无法像通常使用 CLI 那样传递参数,但您仍然可以使用冒号分隔的符号传递参数:
build_home --country=X0 --product=Y7 --subdomain=Z3 => build_home:X0:Y7:Z3

将您的 build_home_all 任务更新为以下内容:

function buildHomeAll() = {
    var products = ['Y0','Y1','Y2','Y3','Y4','Y5','Y6','Y7','Y8','Y9'],
        subdomains = ['Z0','Z1','Z2'],
        tasks = [],
        country = grunt.option('country') || 'X0';


    for (i = 0; i < products.length; i++) {
        for (j = 0; j < subdomains.length; j++) {
            tasks.push(['build_home',country, products[i],'subdomains[j]'].join(':'));
        };
    };
    grunt.task.run(tasks);

    grunt.log.ok(['Finished.']);
}

您将使用 grunt build_home_all --country=X0 调用此函数

接下来,您需要修改您的 build_home 任务以考虑可以传入参数的新方式:

function buildHome() = {
    if (!grunt.option('country') {
        grunt.option('country', this.args[0] || 'X0');
    })
    if (!grunt.option('product') {
        grunt.option('product', this.args[1] || 'Y0');
    })
    if (!grunt.option('subdomain') {
        grunt.option('subdomain', this.args[2] || 'Z0');
    })

    grunt.task.run([
        'sprite:publicProduct',
        'sprite:home',
        'uglify:home',
        'sass:home',
        'csscomb:home',
        'cssmin:home'
    ]);

最后,公开gruntfile.js中的函数:

grunt.registerTask('build_home', buildHome);
grunt.registerTask('build_home_all', buildHomeAll);

【讨论】:

  • 我已经成功地创建了这个任务,并且几乎和你一样。不同之处在于我已将选项设置为 grunt.initConfig 并使用 grunt.config.set 对其进行了更改。会有明显的性能差异吗?非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
相关资源
最近更新 更多