javascript - Grunt Shell 输出到其他任务

标签 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 的一部分。

最佳答案

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

你的意思是你想在你的其他任务中使用你的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();
              }
            }
        }
    }
});

关于javascript - Grunt Shell 输出到其他任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167750/

相关文章:

javascript - Grunt 读取模板中的替换文件

javascript - 使用 Canvas 在 JS 中模拟重力

javascript - jQuery 如何从字符串中删除多个空格

node.js - Heroku - 错误 : ENOENT: no such file or directory, 统计 '/app/dist/index.html'

node.js - 在 Gruntfile 中使用 JavaScript 库

javascript - Grunt 可以单独缩小多个文件,而不将它们连接起来吗?

gruntjs - Grunt-命令行参数,不起作用

javascript - 如何使用 jQuery 追加一个包含多个元素的元素(div)?

javascript - 查找多维数组中元素的单个索引(该元素之前存在的元素数量)

node.js - ImageMagick:不使用文件的 PDF 到 PNG nodejs?