Grunt-shell保存命令输出作为变量
问题描述:
我正在使用Grunt和Grunt-shell来构建/部署我的Javascript项目。Grunt-shell保存命令输出作为变量
我想获得最新的git-commit号码并将它作为一个变量存储,但不能想象如何。 我试过了回调并设置了一个全局变量。这个变量是在函数中使用,但不包括从其它块中似乎
grunt.initConfig({
...
shell: {
getGitCommitNo: {
command: 'git rev-parse --short HEAD',
options: {
callback: function (err, stdout, stderr, cb) {
global['gitCommitNo'] = stdout;
grunt.log.ok(global.gitCommitNo);
cb();
}
}
},
philTest: {
command: 'echo Git Commit No: ' + global.gitCommitNo
},
...
}
输出:
>> Starting deployment process for version 1.1 in dev environment
Running "shell:getGitCommitNo" (shell) task
bfc82a9
>> bfc82a9
Running "shell:printTest" (shell) task
Git Commit No: undefined
Done, without errors.
任何人都可以建议我怎么可能挽救一个命令行变量的输出,可用请?
答
发现我实际上可以在回调中使用配置变量(而不是全局)来做到这一点。 (注意下面的行也删除换行符)。
grunt.config.set('gitCommitNo', stdout.replace('\n', ''));
那么这可以通过访问:
<%=gitCommitNo%>
+0
这应该被标记为正确的答案。非常感谢。 – 2015-02-11 10:19:34
+0
注意:我必须在grunt文件的顶部放置'grunt.loadNpmTasks('grunt-shell')'**,在**'grunt.initConfig({...')的上方为此工作 – 2016-05-12 19:12:52
不知道你可以用'shell'插件做到这一点,但你可以写一个简单的自定义模块,如果有必要去做。 – jakerella 2015-02-10 18:38:48