Grunt 任务 grunt.option 由上一个任务 returns 设置未定义
Grunt task grunt.option set by previous task returns undefined
我运行正在为一个项目执行多项 g运行t 任务。其中之一设置了一些 grunt.options grunt.option(key, value)
,我需要在后续任务 var option = grunt.option(key)
中访问它们。当我尝试在后一个任务中访问它们时,这些选项返回 undefined
。
如果我在后一个任务的开头记录变量,它会显示在该任务之前 运行 并且我无法访问任务配置中先前设置的值。
在设置 grunt.option 和在另一个任务中使用它以通知 g运行t 更改之间,我需要做些什么吗?我在这里做错了什么吗?或者有没有更好的方法来使用各种全局变量(我的研究指出我使用 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
....
....
....
....
]
);
在我的 set-env 任务中,我根据 curl 任务返回的文本文件的内容设置了一些环境变量。这工作正常,我可以在设置后立即记录所有 grunt.options,所以我知道它们设置正确。
设置环境任务
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');
}
(我已经双重、三次和四次检查我是否使用了正确的选项键)
问题是您在任务 "config stage" 期间访问 g运行t 选项设置的变量,运行s 一次,在您设置之前set-env
任务中的选项。在代码中评估自定义选项键确实应该产生 undefined
。 (请注意,这实际上等同于使用 initConfig 块)
你要做的不是从选项对象中读取选项值,而是直接修改任务的配置对象,使用 grunt.config.set
,这将使你能够做你一直在尝试的事情.
所以基本上,而不是
grunt.option(propName, propValue);
使用类似
的东西
grunt.config.set('mytask.replacements', options.replacements);
(当然,这将需要对您的代码进行大量修改,我不会深入讨论。)
编辑: 可能有一个使用 g运行t 模板功能的更简洁的解决方案,参见 this Whosebug answer, and the grunt api docs on templating:
Template strings can be processed manually using the provided template functions. In addition, the config.get method (used by many tasks) automatically expands <% %> style template strings specified as config data inside the Gruntfile.
重点是这些不是在解析配置块时进行评估,而是仅在任务使用 config.get
.
读取值时进行评估
如果在您的两个自定义任务之间使用选项对象在任务之间共享值的模式效果更好 - 您可以在一个任务中设置它,在另一个任务中读取它,而不是在配置中,而是作为运行完成任务的实际步骤。
总的来说,虽然这看起来可行,但我想说这不是 g运行t 考虑的工作流程 - 如果您知道您 运行ning g运行t in,当你运行一个g运行t任务时,直接通过options命令行标志传入环境参数更容易,这在你的任何任务配置中已经生效正在做。
我运行正在为一个项目执行多项 g运行t 任务。其中之一设置了一些 grunt.options grunt.option(key, value)
,我需要在后续任务 var option = grunt.option(key)
中访问它们。当我尝试在后一个任务中访问它们时,这些选项返回 undefined
。
如果我在后一个任务的开头记录变量,它会显示在该任务之前 运行 并且我无法访问任务配置中先前设置的值。
在设置 grunt.option 和在另一个任务中使用它以通知 g运行t 更改之间,我需要做些什么吗?我在这里做错了什么吗?或者有没有更好的方法来使用各种全局变量(我的研究指出我使用 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
....
....
....
....
]
);
在我的 set-env 任务中,我根据 curl 任务返回的文本文件的内容设置了一些环境变量。这工作正常,我可以在设置后立即记录所有 grunt.options,所以我知道它们设置正确。
设置环境任务
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');
}
(我已经双重、三次和四次检查我是否使用了正确的选项键)
问题是您在任务 "config stage" 期间访问 g运行t 选项设置的变量,运行s 一次,在您设置之前set-env
任务中的选项。在代码中评估自定义选项键确实应该产生 undefined
。 (请注意,这实际上等同于使用 initConfig 块)
你要做的不是从选项对象中读取选项值,而是直接修改任务的配置对象,使用 grunt.config.set
,这将使你能够做你一直在尝试的事情.
所以基本上,而不是
grunt.option(propName, propValue);
使用类似
的东西grunt.config.set('mytask.replacements', options.replacements);
(当然,这将需要对您的代码进行大量修改,我不会深入讨论。)
编辑: 可能有一个使用 g运行t 模板功能的更简洁的解决方案,参见 this Whosebug answer, and the grunt api docs on templating:
Template strings can be processed manually using the provided template functions. In addition, the config.get method (used by many tasks) automatically expands <% %> style template strings specified as config data inside the Gruntfile.
重点是这些不是在解析配置块时进行评估,而是仅在任务使用 config.get
.
如果在您的两个自定义任务之间使用选项对象在任务之间共享值的模式效果更好 - 您可以在一个任务中设置它,在另一个任务中读取它,而不是在配置中,而是作为运行完成任务的实际步骤。
总的来说,虽然这看起来可行,但我想说这不是 g运行t 考虑的工作流程 - 如果您知道您 运行ning g运行t in,当你运行一个g运行t任务时,直接通过options命令行标志传入环境参数更容易,这在你的任何任务配置中已经生效正在做。