我正在尝试将从服务器(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/