繁体   English   中英

Grunt-shell将命令输出作为变量

[英]Grunt-shell save command output as variable

我正在使用Grunt和Grunt-shell来构建/部署我的Javascript项目。

我想得到最新的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%>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM