【问题标题】:Grunt watch - detecting success and failure of tasksGrunt watch - 检测任务的成功和失败
【发布时间】:2013-06-25 11:02:45
【问题描述】:

更新

我目前正在使用类似于here 描述的错误通知的解决方案,以及下面的“当前解决方法”(不修改 grunt force 选项)用于成功通知。

原始问题

我无法确定grunt-contrib-watch 运行的子任务何时完成(成功与否)。

具体来说,我正在使用 grunt-contrib-coffee 和 grunt watch 来编译我的 CoffeeScript 文件,因为它们发生了变化。编译工作正常。

我想做的是通知自己编译的状态。这是我尝试过的(CS中的所有代码):

当前的解决方法

来自 SO 问题 (How can I make a Grunt task fail if one of its sub tasks fail?)

我不喜欢的是:设置和恢复全局选项似乎很笨拙,尤其是因为它发生在不同的任务/事件处理程序中。另外,我每次都必须删除目标文件。

不设置全局选项,我可以通知编译成功,这很好,但我也想通知编译失败。

grunt.initConfig
  watch: 
    options: nospawn: true
    coffee: 
      files: '<%= coffee.dev.cwd %>/<%= coffee.dev.src %>'
      options: 
        events: ['changed', 'added']
  coffee:
    dev: 
      expand: true
      cwd: 'app'
      src: '**/*.coffee'
      dest: 'public'
      ext: '.js'

grunt.registerTask 'completedCompile', (srcFilePath, destFilePath) ->
  grunt.option 'force', false
  if grunt.file.exists( destFilePath )
    # notify success
  else 
    # notify failure

grunt.event.on 'watch', (action, filepath) ->
  if grunt.file.isMatch grunt.config('watch.coffee.files'), filepath
    filepath = # compose source filepath from config options (omitted)
    dest     = # compose destination filepath from config options (omitted)

    if grunt.file.exists( dest )
      grunt.file.delete dest   # delete the destination file so we can tell in 'completedCompile' whether or not 'coffee:dev' was successful

    grunt.option 'force', true   # needed so 'completedCompile' runs even if 'coffee:dev' fails
    grunt.config 'coffee.dev.src', filepath   # compile just the one file, not all watched files
    grunt.task.run 'coffee:dev'
    grunt.task.run 'completedCompile:'+filepath+':'+dest   # call 'completedCompile' task with args

另一种选择(太慢了)

根据另一个 SO Question (Gruntfile getting error codes from programs serially) 的建议,我使用了 grunt.util.spawn

这行得通,但速度很慢(每次保存 CS 文件都要几秒钟)。

grunt.event.on 'watch', (action, filepath) ->
  if grunt.file.isMatch grunt.config('watch.coffee.files'), filepath
    filepath = # compose source filepath from config options (omitted)
    dest     = # compose destination filepath from config options (omitted)

    if grunt.file.exists( dest )
      grunt.file.delete dest   # delete the destination file so we can tell in 'completedCompile' whether or not 'coffee:dev' was successful

    grunt.util.spawn {
      grunt: true # use grunt to spawn
      args: ['coffee:dev']
      options: { stdio: 'inherit' } # print to same stdout
    }, -> # coffee:dev finished
      if grunt.file.exists( dest )
        # notify success
      else 
        # notify error

其他尝试

我尝试了很多东西。

  • 如果以前的编译失败,grunt.fail.errorcount(在 'completedCompile' 任务中使用时)非零。 (手动将其重置为零是否安全?如果可以,我不必每次都删除 dest 文件。)即便如此,这需要将全局选项“强制”设置为 true。
  • 任何涉及在 grunt.initConfig 中指定 'watch.coffee.tasks' 选项的操作都不起作用,因为 'coffee:dev' 任务在 'watch' 事件处理程序完成后运行。
  • grunt.task.current 当然总是指“监视”任务



如果你已经做到了,感谢阅读:)。

【问题讨论】:

    标签: gruntjs watch


    【解决方案1】:

    我也遇到了同样的问题,想弄清楚 watch 子任务何时完成。

    部分问题似乎是 Watch 默认情况下会产生一个新的 Grunt 进程来运行子任务。所以你的主要 Grunt 进程不会知道任务的完成情况。您可以设置“nospawn”,但这无济于事,因为 watch 不会暴露子任务本身。

    我能做到的最接近的方法是使用 Grunt.util.hooker(受 Grunt Notify 启发)在调用 Grunt fail 'report' 方法时做出反应。

    grunt.util.hooker.hook(grunt.fail, 'report', function(){});
    

    但是,这不包含有关已完成的实际任务的信息,如果您想根据监视任务中的特定子任务执行某些操作,这将很有帮助。

    查看 Grunt Watch github 似乎有一些牵引力来实现完成/失败事件: https://github.com/gruntjs/grunt-contrib-watch/issues/131

    【讨论】:

    • 感谢您指出grunt-contrib-watch 问题,@Matt。看起来它可能正在开发中。
    • 实际上,您可以通过检查回调中的arguments 对象来获取有关任务的信息:{ '0': [Error: Task "jshint:src" failed.], '1': 3 }
    【解决方案2】:

    我认为Grunt-Notify 会满足您的需求。

    【讨论】:

    • Grunt-Notify 看起来很有希望,但我无法让它与一些 grunt 插件一起工作:特别是 grunt-contrib-coffeegrunt-contrib-sassgrunt-contrib-haml。如果我有任何进展,我会发布更新。
    • 也有兴趣让 grunt-notify 与 grunt-contrib-coffee 和其他人一起工作。如果编译失败等等,希望收到通知。
    猜你喜欢
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    相关资源
    最近更新 更多