我正在項目上運行一些grunt任務。其中一個設置一些grunt.options grunt.option(key, value)
,我需要在後續任務var option = grunt.option(key)
中訪問。當我嘗試在後面的任務中訪問這些選項時,這些選項將返回undefined
。由前一任務設置的Grunt任務grunt.option返回undefined
如果我在後面的任務的頭部記錄變量,則在該任務運行之前顯示該變量,並且我無法訪問任務配置中以前設置的值。
在設置grunt.option和在另一個任務中使用它以通知變化的咕嚕聲之間,我需要做些什麼?我在這裏做了一些固有的錯誤嗎?還是有更好的方式與各種各樣的全局變量來做到這一點(我的研究指出我使用grunt.option)
我Gruntfile.js
grunt.log.writeln('loading tasks');
grunt.loadTasks('grunttasks');
grunt.log.writeln('tasks loaded');
grunt.registerTask(
'jenkins',[
'clean', //clears out any existing build folders
'curl', //gets build config file from remote server
'set-env', //sets the grunt.options based on the build config file
'string-replace:config', //attempts to access the new grunt.options
....
....
....
....
]
);
在我的設置ENV任務,我根據curl任務中返回的文本文件的內容設置了一些環境變量。這工作正常,我可以在設置它們後立即記錄所有grunt.options,以便我知道它們被正確設置。
設置ENV任務
module.exports = function(grunt) {
grunt.registerTask('set-env', 'set-env', function() {
......
......
for (var i = 0; i < propFile.length; i++) {
if (propFile[i] !== '') {
......
......
keyValues[propName] = propValue;
grunt.option(propName, propValue);
console.log("FROM GRUNT.OPTION " + grunt.option(propName));
......
......
}
}
......
......
});
};
當我嘗試從我訪問上述任務設置的grunt.options字符串替換(或任何其他後續)任務undefined
返回。如果我在Gruntfile.js開始時設置的測試值這些grunt.options我可以沒有問題訪問它們:
module.exports = function(grunt) {
grunt.config('string-replace', {
..........
..........
config:{
files: configFiles,
options: {
replacements: [
..........
..........
{
pattern: /var _OPTION_KEY = \"(.*?)\"\;/ig,
replacement: 'var _OPTION_KEY = "'+grunt.option('_OPTION_KEY')+'";' //grunt.option('_OPTION_KEY') here is undefined
}
..........
..........
]
}
}
..........
..........
});
grunt.loadNpmTasks('grunt-string-replace');
}
(我有雙人間,三人間和四人間檢查,我使用正確的選項鍵)
模板做到了。非常感謝! – Fraser