2014-03-04 54 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。謝謝! –

相關問題