grunt-contrib-watch icon indicating copy to clipboard operation
grunt-contrib-watch copied to clipboard

A way to get notified when watch build fails?

Open abierbaum opened this issue 12 years ago • 11 comments

Is there a way to be notified when a watch build fails?

I have watch monitoring our code base to run jshint, etc at all times. When any of the tools fail, I would like to use grunt-play to play a sound that let's me know something is broken.

abierbaum avatar Jun 20 '13 21:06 abierbaum

We could add an event for fail and complete.

shama avatar Jun 20 '13 22:06 shama

@shama That would be great. Then if I can just find a grunt extension to play an audio file cross platform (win32, linux) I would be set. :)

abierbaum avatar Jun 21 '13 13:06 abierbaum

+1

But is it actually possible to know whether a task has passed or failed? Grunt doesn't have an events API, and I'm not seeing any other way to do it, except maybe based on the Grunt process return code

sfrdmn avatar Jul 26 '13 23:07 sfrdmn

Yes, Grunt has grunt.fail.errorcount and grunt.fail.warncount which increment each time a task has erred or warned. Maybe using those would handle this use case sufficiently:

grunt.initConfig({
  watch: {
    files: ['lib/*.js'],
    tasks: ['jshint', 'hasfailed', 'build'],
  },
});
grunt.registerTask('hasfailed', function() {
  if (grunt.fail.errorcount > 0) {
    grunt.log.write('\x07'); // beep!
    return false; // stops the task run
  }
  // otherwise continue the task run as normal
});

shama avatar Jul 27 '13 02:07 shama

Works for me! I guess it'd be a tad bit more convenient to just attach some event listeners and forgo the errorcount checking logic, but not all that much more. Also, this pattern works across plugins and the functionality might really be better suited as its own plugin rather than an idiosyncrasy of contrib-watch.

sfrdmn avatar Jul 29 '13 04:07 sfrdmn

I just went ahead and wrote a little plugin based on that code snippet. Realize now that it's kinda cumbersome since it relies on setting --force, but meh

https://github.com/sfrdmn/grunt-passfail

sfrdmn avatar Jul 29 '13 19:07 sfrdmn

Nice @sfrdmn!

shama avatar Jul 30 '13 00:07 shama

+1

It seems that a simple approach would be to emit another event within Watch's own callback (watch.js:32):

 // When task runner has ended
  taskrun.on('end', function(time) {
    if (time > 0) {
      grunt.log.writeln(String(
        'Completed in ' +
        time.toFixed(3) +
        's at ' +
        (new Date()).toString()
      ).cyan + ' - ' + waiting);
    }
  });

It would be really nice to know what task was completed as well so you could add an event listener to handle task completion and act based on the actual task completed.

Does that sound like a good approach?

k0nG avatar Aug 08 '13 15:08 k0nG

I'm hesitant because events when tasks start/complete/fail/warn is something I feel should be handled in Grunt itself (and likely will in the next version of Grunt). So for now I think the best way is through a task like grunt-passfail or your own custom one.

shama avatar Aug 08 '13 15:08 shama

Ah I didn't know that. Yes it does seem like it would be better if Grunt could handle this centrally.

Let's hope it makes the next version.

k0nG avatar Aug 08 '13 15:08 k0nG

I was trying to do something along this line: http://stackoverflow.com/questions/17351717/grunt-watch-detecting-success-and-failure-of-tasks but with grunt watch and TypeScript. I have the same issue, there doesn't seem to be an easy way to get the feedback from a task when it's done.

jdecaron avatar Jun 02 '15 20:06 jdecaron