最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to loop through an array and run a Grunt task passing each value in the array as a Grunt option - Stack Overflo

programmeradmin3浏览0评论

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 mand to pile the assets for a single theme:

grunt pile --theme=theme2

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

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

I have tried the following:

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

This runs the pile 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('pile:all', function() {
    themes.forEach(function(currentTheme) {
        grunt.util.spawn({
            grunt : true,
            args  : ['pile', '--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('pile:all', function() {
    themes.forEach(function(currentTheme) {
        var done = grunt.task.current.async();
        grunt.util.spawn({
            grunt : true,
            args  : ['pile', '--theme=' + currentTheme]
        }, done);
    });
});

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

Thanks for any help

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 mand to pile the assets for a single theme:

grunt pile --theme=theme2

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

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

I have tried the following:

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

This runs the pile 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('pile:all', function() {
    themes.forEach(function(currentTheme) {
        grunt.util.spawn({
            grunt : true,
            args  : ['pile', '--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('pile:all', function() {
    themes.forEach(function(currentTheme) {
        var done = grunt.task.current.async();
        grunt.util.spawn({
            grunt : true,
            args  : ['pile', '--theme=' + currentTheme]
        }, done);
    });
});

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

Thanks for any help

Share Improve this question asked Jun 9, 2016 at 0:15 ESRESR 1,6891 gold badge18 silver badges23 bronze badges 2
  • Could you share an example of how the theme option is used? – 76484 Commented Jun 9, 2016 at 2:17
  • For example in the JS concat task: dest: theme + '/app.js' Really just used to determine directory paths. – ESR Commented Jun 9, 2016 at 9:07
Add a ment  | 

3 Answers 3

Reset to default 5

I think your problem is that your individual pile tasks are getting queued-up by grunt.task.run('pile');, but, by the time they execute, your themes.forEach loop has pleted 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 pile task.

grunt.registerTask('pile_theme', function (theme) {
    grunt.option('theme', theme);
    grunt.task.run('pile');
});

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

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

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

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

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

    pileThemes.forEach(function(currentTheme) {
        grunt.task.run('pile_theme:' + currentTheme);
    });
});

You would call the mand as follows:

grunt pile:all // piles 'theme1'
grunt pile:all --theme=theme2 // piles 'theme2'
grunt pile:all --theme=theme2 --theme=theme3 // piles 'theme2' and 'theme3'

Note: You would probably want to rename your pile:all task at this point because it no longer necessarily piles 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 mand-line and to pose 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 pile_theme task:

grunt.registerTask('pile_theme', function (theme) {
    grunt.config('theme', theme);
    grunt.task.run('pile');
});

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

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

Using for each never worked for me, but this did based off of this help doc from grunt: https://gruntjs./creating-tasks#multi-tasks

I added the list to my initConfig, ie

grunt.initConfig({
    run_themes: {
        theme1: 'sdi',
        theme2: 'syd',
        theme3: 'phl'
    }});

Then:

//register task to loop through the themes
grunt.task.registerMultiTask('run_themes', 'Compile themes.', function() {
    //grunt.themes.writeln(this.target + ': ' + this.data);
    grunt.log.writeln(this.target + ': ' + this.data);
    grunt.option('theme', this.data);
    grunt.task.run('sass');
});

And my sass definition makes use of grunt.option('theme') like this to put a copy of the same piled css in each theme directory like sdi/default/... syd/default... phl/default...

// pile our sass to css
sass: {
    // sass pilation for "dev", unminified files
    dev: {
        options: {
            style: 'expanded'
        },
        files: [{
            expand: true,
            cwd:'shared-resources/css/sass',
            src: ['*.scss', 'standalone/*.scss'],
            dest: "../<%= grunt.option('theme') %>/default/template-resources/css/dev",
            ext: '.css'
        }]
    }},

If you want to use the forEach method to build out a series of tasks, push the tasks to a task array instead of running within the forEach block:

grunt.registerTask('buildAll', function() {
    var tasks = [];
    themes.forEach(function(currentTheme) {
        tasks.push('pile:' + currentTheme);
    });

    grunt.tasks.run(tasks);
});

Within your pile task, you can pass currentTheme to the task using this.args[0] for normal tasks, or this.target for multitasks:

grunt.registerTask('pile', function() {
    var theme = this.args[0];  // sets local variable for use within task.
    grunt.option('theme', this.args[0]);  //sets option that can be referenced within this instance of `pile` 
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论