Gruntjs Grunt watch-检测任务的成功与失败

Gruntjs Grunt watch-检测任务的成功与失败,gruntjs,watch,Gruntjs,Watch,更新 我目前正在使用一种类似于错误通知的解决方案,并使用下面的“当前解决方案”(不修改gruntforce选项)进行成功通知 原始问题 我无法确定由运行的子任务何时完成(成功或失败) 具体地说,我正在使用和grunt watch编译我的CoffeeScript文件。汇编工作进展顺利 我想做的是通知自己编译的状态。以下是我尝试过的内容(所有代码都在CS中): 当前解决方法 来自SO的问题() 我不喜欢的是:设置和恢复全局选项似乎很笨拙,特别是因为它发生在不同的任务/事件处理程序中。此外,我每次都必

更新

我目前正在使用一种类似于错误通知的解决方案,并使用下面的“当前解决方案”(不修改grunt
force
选项)进行成功通知

原始问题

我无法确定由运行的子任务何时完成(成功或失败)

具体地说,我正在使用和grunt watch编译我的CoffeeScript文件。汇编工作进展顺利

我想做的是通知自己编译的状态。以下是我尝试过的内容(所有代码都在CS中):

当前解决方法 来自SO的问题()

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

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

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
其他尝试 我试了很多东西

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



如果你能做到这一点,谢谢你的阅读:)。

我想我会满足你的要求。

我也遇到了同样的问题,试图找出手表子任务何时完成

部分问题似乎是Watch默认情况下会产生一个新的Grunt进程来运行子任务。因此,您的主要咕噜过程将不知道任务完成情况。您可以设置“nospawn”,但这并没有多大帮助,因为watch不会公开子任务本身

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

grunt.util.hooker.hook(grunt.fail, 'report', function(){});
但是,它不包含关于实际完成任务的信息,如果您希望根据监视任务中的特定子任务执行某些操作,这将非常有用

看看Grunt Watch github,似乎有一些动力来实现完整/失败事件:

Grunt Notify看起来很有希望,但我在使用一些Grunt插件时遇到了问题:具体来说是
Grunt contrib coffee
Grunt contrib sass
,以及
Grunt contrib haml
。如果我有任何进展,我会发布一个更新。我还想让grunt notify与grunt contrib coffee和其他人一起工作。如果编译失败,需要一个通知。谢谢你给我指出
grunt contrib watch
问题,@Matt。看起来它可能正在工作中。实际上,您可以通过检查回调中的
参数
对象来获取有关该任务的信息:
{'0':[Error:task“jshint:src”failed.],'1':3}
grunt.util.hooker.hook(grunt.fail, 'report', function(){});