如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递

How to loop through an array and run a Grunt task passing each value in the array as a Grunt option(如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递)

本文介绍了如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下的数组:

I have an array similar to the following:

var themes = grunt.option('themes') || [
    'theme1',
    'theme2',
    'theme3'
];

还有一个变量:

var theme = grunt.option('theme') || 'theme1';

这个值在我的 grunt 文件中的不同地方使用,例如确定某些资产的路径等.

This value is used in various places in my grunt file for things such as determining the path to some assets etc.

长话短说,我运行以下命令来编译单个主题的资源:

To cut a long story short, I run the following command to compile the assets for a single theme:

grunt compile --theme=theme2

我正在寻找一种方法来循环遍历主题数组并使用适当的 grunt.option 运行 compile grunt 任务.从本质上讲,我希望实现的目标相当于:

I'm looking for a way to loop through the array of themes and run the compile grunt task with the appropriate grunt.option. Essentially, what I'm looking to achieve would be the equivalent of this:

grunt compile --theme=theme1 && grunt compile --theme=theme2 && grunt compile --theme=theme3

我尝试了以下方法:

grunt.registerTask('compile:all', function() {
    themes.forEach(function(currentTheme) {
        grunt.option('theme', currentTheme);
        grunt.task.run('compile');
    });
});

这会以适当的次数运行 compile 任务,但似乎没有设置 theme 选项.所以我的 Scss 文件生成了,但它们是空的.

This runs the compile task the appropriate number of times, but the theme option doesn't seem to get set. So my Scss files get generated, but they are empty.

我也试过这个:

grunt.registerTask('compile:all', function() {
    themes.forEach(function(currentTheme) {
        grunt.util.spawn({
            grunt : true,
            args  : ['compile', '--theme=' + currentTheme]
        });
    });
});

任务几乎立即完成并显示成功"消息,但它似乎没有做任何事情.

The task finishes almost instantly with a "success" message, but it doesn't appear to do anything.

我尝试的最后一件事与上面类似,除了我尝试使用异步:

The last thing I've tried is similar to the above, except I attempt to use async:

grunt.registerTask('compile:all', function() {
    themes.forEach(function(currentTheme) {
        var done = grunt.task.current.async();
        grunt.util.spawn({
            grunt : true,
            args  : ['compile', '--theme=' + currentTheme]
        }, done);
    });
});

但是这个任务失败了.我真的不确定我哪里出错了,

But this task fails. I'm not really sure where I'm going wrong,

感谢您的帮助

推荐答案

我认为你的问题是你的个人编译任务被 grunt.task.run('compile');,但是,当它们执行时,您的 themes.forEach 循环已经完成,并且您的 theme 选项设置为 themes 中的最后一个值.

I think your problem is that your individual compile tasks are getting queued-up by grunt.task.run('compile');, but, by the time they execute, your themes.forEach loop has completed and your theme option is set to the last value in themes.

我认为您需要注册一个单独的任务,该任务负责设置 theme 选项运行编译任务.

I think you will need to register a separate task that is responsible for setting the theme option and running the compile task.

grunt.registerTask('compile_theme', function (theme) {
    grunt.option('theme', theme);
    grunt.task.run('compile');
});

对于每个主题,您可以在 compile:all 任务中将此任务排入队列:

You would enqueue this task within your compile:all task for each of your themes:

themes.forEach(function(currentTheme) {
    grunt.task.run('compile_theme:' + currentTheme);
});

如果您希望能够在命令行中指定要编译的主题,则需要更新您的 compile:all 任务以读取所有 --theme= 参数并强制该值是一个数组:

If you want to be able to specify at the command-line which themes to compile, you would need to update your compile:all task to read all --theme= parameters and enforce that the value is an array:

grunt.registerTask('compile:all', function () {
    var compileThemes = grunt.option('theme') || 'theme1';

    if (grunt.util.kindOf(compileThemes) === 'string') {
        compileThemes = [compileThemes];
    }

    compileThemes.forEach(function(currentTheme) {
        grunt.task.run('compile_theme:' + currentTheme);
    });
});

您可以按如下方式调用该命令:

You would call the command as follows:

grunt compile:all // compiles 'theme1'
grunt compile:all --theme=theme2 // compiles 'theme2'
grunt compile:all --theme=theme2 --theme=theme3 // compiles 'theme2' and 'theme3'

注意:此时您可能想要重命名您的 compile:all 任务,因为它不再需要编译 all 主题.

Note: You would probably want to rename your compile:all task at this point because it no longer necessarily compiles all themes.

编辑

它不起作用,因为我们对 theme 选项的期望过高.我们正在尝试使用它来获取在命令行中输入的主题,以便在我们的配置中动态组合值(例如,dest: theme + '/app.js'.按照我构建答案的方式,theme 无法在配置中使用.

It is not working because we are expecting too much of the theme option. We are trying to use this to get the themes entered at the command-line and to compose values dynamically in our configuration (like, dest: theme + '/app.js'. With the way I have structured my answer, theme cannot be used in the configuration.

我会改为为 theme 使用配置变量,该变量将在配置中使用.这意味着更新 compile_theme 任务:

I would instead use a configuration variable for theme that will be used in the config. This would mean updating the compile_theme task:

grunt.registerTask('compile_theme', function (theme) {
    grunt.config('theme', theme);
    grunt.task.run('compile');
});

我们需要通过替换 theme 的模板字符串来更新我们的配置.例如:

We would need to update our configuration by substituting template strings for theme. For example:

dest: '<%= theme %>/app.js'

这篇关于如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何遍历数组并运行 Grunt 任务,将数组中的每个值作为 Grunt 选项传递

基础教程推荐