Grunt Shell输出到其他任务

时间:2014-01-16 16:45:38

标签: javascript node.js gruntjs

我正在使用grunt-shell并且想知道我是否可以将我的配置用于shell并添加到我的另一个任务中?这就是我所拥有的:

shell: {
  gitlog: {
    command: 'git log -1 --pretty=format:%h',
      options: {
        stdout: true
      }
  }
}

然后我的任务:

grunt.registerTask('build-version', 'Set the information about the version', function() {
    grunt.file.write('version.json', JSON.stringify({
            version: pkg['version'],
            metaRevision: shell.gitlog,
            date: grunt.template.today()
    }));
});

感谢您在尝试弄清楚我需要做什么的任何帮助,以便我的git sha-1将成为我的metaRevision的一部分。

1 个答案:

答案 0 :(得分:3)

你的问题有点难以理解: - )

您是否想要在其他任务中使用执行shell命令的结果?

如果是这样,在你描述的情况下,我会使用命令执行中的回调,并将文件保存在那里,而不需要额外的第二个任务(参见https://github.com/sindresorhus/grunt-shell):

grunt.initConfig({
    shell: {
        gitlog: {
            command: 'git log -1 --pretty=format:%h',
            options: {
              callback: function log(err, stdout, stderr, cb) {
                grunt.file.write('version.json', JSON.stringify({
                        version: pkg['version'],
                        metaRevision: stdout,
                        date: grunt.template.today()
                }));
                cb();
              }
            }
        }
    }
});