asynchronous - 将 grunt 参数从一项任务传递到另一项任务

标签 asynchronous configuration dependencies gruntjs

我正在尝试将从服务器(zookeeper)返回的配置值传递到指南针(cdnHost、环境等)中,并且似乎很难使用正确的方法。

作为起点,我研究了在此页面上将 args 从一个任务传递到另一个任务的方法

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

我也尝试过直接调用指南针任务,没有任何区别。
grunt.task.run('compass');

任何见解将不胜感激。 (例如,使用 initConfig 并使值可用的方法)。

谢谢

最佳答案

当你写:

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会让你得到结果。

关于asynchronous - 将 grunt 参数从一项任务传递到另一项任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161145/

相关文章:

java - 将非开源依赖项与 Maven 捆绑在一起

java - 如何提取java方法调用?

javascript - Node.Js API 等待 dynamodb 调用完成以发送到浏览器

configuration - ActionMailer 设置在开发和生产之间存在错误差异

android - 为什么线程在加载所有数据之前终止?

Gradle:从插件更改任务的属性

php - 在 CGI 模式下运行时如何覆盖 PHP 配置

maven - gradle - 无法从 maven repo 中找到依赖项

node.js - 异步等待然后在同一个函数上

javascript - 如何从 for 语句中的异步回调获取结果