Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing grunt parameters from one task to another

I'm trying to pass the configuration values returned from the server(zookeeper) into compass (cdnHost, environment, etc) and seem to be having a hard time using the right approach.

I looked at ways to pass around args from one task to another on this page as a starting point

http://gruntjs.com/frequently-asked-questions#how-can-i-share-parameters-across-multiple-tasks

module.exports = function(grunt) {
    grunt.initConfig({
        compass: {
            dist: {
                //options: grunt.option('foo')
                //options: global.bar
                options: grunt.config.get('baz')
            }
        },

    ...

    grunt.registerTask('compassWithConfig', 'Run compass with external async config loaded first', function () {
        var done = this.async();
        someZookeeperConfig( function () {
            // some global.CONFIG object from zookeeper
            var config = CONFIG;
            // try grunt.option
            grunt.option('foo', config);
            // try config setting
            grunt.config.set('bar', config);
            // try global
            global['baz'] = config;
            done(true);
        });
    });

    ...

    grunt.registerTask('default', ['clean', 'compassWithConfig', 'compass']);

I also tried calling the compass task directly, and it made no difference.

grunt.task.run('compass');

Any insights would be greatly appreciated. (e.g. way to use initConfig and have the value be available).

Thanks

like image 671
Trial Offer Avatar asked Oct 01 '22 10:10

Trial Offer


1 Answers

When you write:

grunt.initConfig({
    compass: {
        dist: {
            options: grunt.config.get('baz')
        }
    }

... grunt.config is called right away, and returns the value of baz as it is right now. Altering it (later) in another task simply won't get picked-up.

How to solve that?

#1: update compass.dist.options instead of updating baz

grunt.registerTask('compassWithConfig', 'Run compass with external async config loaded first', function () {
    var done = this.async();
    someZookeeperConfig( function () {
        // some global.CONFIG object from zookeeper
        var config = CONFIG;
        grunt.config.set('compass.dist.options', config);
        done();
    });
});

Now, running task compassWithConfig first, then task compass will get the result you expect.

#2: wrap-up compass task execution in order to abstract away config mapping

grunt.registerTask('wrappedCompass', '', function () {
    grunt.config.set('compass.dist.options', grunt.config.get('baz'));
    grunt.task.run('compass');
});

// Then, you can manipulate 'baz' without knowing how it needs to be mapped for compass

grunt.registerTask('globalConfigurator', '', function () {
    var done = this.async();
    someZookeeperConfig( function () {
        // some global.CONFIG object from zookeeper
        var config = CONFIG;
        grunt.config.set('baz', config);
        done();
    });
});

Finally, running task globalConfigurator then wrappedCompass will get you to the result.

like image 62
Mangled Deutz Avatar answered Oct 05 '22 10:10

Mangled Deutz