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

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

问题描述

我有一个类似如下的数组:

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

另一个变量:
$ b $

  var theme = grunt.option('theme')|| THEME1’ ; 

这个值在我的grunt文件的不同位置用于确定某些资产的路径等等。



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

  grunt compile --theme = theme2 



m寻找一种方式来遍历主题数组,并使用适当的grunt.option运行 compile grunt任务。从本质上讲,我期望达到的效果与此相当:

  grunt compile --theme = theme1&& amp ; 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 的时间,但主题选项似乎没有设置。所以我的Scss文件得到了生成,但它们是空的。



我也试过这个:

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

任务几乎立即以成功消息结束,但它似乎没有做任何事情。

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

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

但是这项任务失败。我不确定我要出错的地方,

感谢您的帮助

解决方案

我认为你的问题在于你的单个编译任务正在被 grunt.task.run('compile'); 排队,但是,在执行时,您的 themes.forEach 循环已完成,您的主题选项设置为最后一个值 themes



我认为您需要注册一个独立的任务,负责设置主题选项运行编译任务。

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

您可以在 compile:all 任务:

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

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



pre $ g code $ grunt.registerTask('compile:all',function(){
var compileThemes = grunt.option('主题')||'theme1';

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

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

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

  grunt compile:all //编译'theme1'
grunt编译:全部--theme = theme2 //编译'theme2'
grunt编译:全部 - theme = theme2 --theme = theme3 //编译'theme2'和'theme3'

注意:可能会想在这里重命名你的 compile:all 任务,因为它不再需要编译所有的主题。



编辑

它不起作用,因为我们期望太多的主题选项。我们试图用这个命令来获取在命令行输入的主题,以便在我们的配置中动态地组合值(例如, dest:theme +'/app.js' 。通过我的回答结构, theme 不能用于配置。



相反,我会使用配置变量来配置主题,这将意味着更新 compile_theme task:

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

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

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


I have an array similar to the following:

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

And another variable:

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

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

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

I have tried the following:

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

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.

I've also tried this:

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,

Thanks for any help

解决方案

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.

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

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

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'

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

EDIT

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.

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

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆