Passing grunt parameters from one task to another

I am trying to transfer the configuration values ​​returned from the server (zookeeper) to the compass (cdnHost, environment, etc.) and it seems to be difficult to use the correct approach.

I looked at ways to transfer arguments 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 to directly call the compass task, and that didn't matter.

grunt.task.run('compass');

Any ideas would be greatly appreciated. (for example, the method of using initConfig and the available value).

thank

+4
source share
1 answer

When you write:

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

... grunt.config baz, . () .

?

# 1: compass.dist.options 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();
    });
});

, compassWithConfig, compass .

# 2: -

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

, globalConfigurator, wrappedCompass .

+4

Source: https://habr.com/ru/post/1529913/


All Articles