2014-03-04 37 views
4

我试图将服务器(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的方式,并且可以使用该值)。

感谢

+0

我添加了一个'测试'任务,看看是否有任何值被拾取,并且他们都正确读取。 'grunt.registerTask( '试验',函数(){' '的console.log( 'TEST1',grunt.option( '富'));' '的console.log( 'TEST2', global.bar);' '的console.log( 'TEST3',grunt.config.get( '巴兹'));' '});' 它必须以不同的方式来传递该值作为一个arg进入指南针。 –

回答

4

当你写:

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

... grunt.config被称为右走,因为它是现在返回baz值。在另一项任务中改变它(稍后)将不会被拾取。

如何解决?

#1:更新compass.dist.options,而不是更新巴兹

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将让你的结果。

+0

结束使用#1。谢谢! –