gruntjs / grunt-contrib-watch

Run tasks whenever watched files change.
http://gruntjs.com/
MIT License
1.98k stars 356 forks source link

A way to get notified when watch build fails? #131

Open abierbaum opened 11 years ago

abierbaum commented 11 years ago

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.

shama commented 11 years ago

We could add an event for fail and complete.

abierbaum commented 11 years ago

@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. :)

sfrdmn commented 11 years ago

+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

shama commented 11 years ago

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
});
sfrdmn commented 11 years ago

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 commented 11 years ago

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

shama commented 11 years ago

Nice @sfrdmn!

k0nG commented 11 years ago

+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?

shama commented 11 years ago

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.

k0nG commented 11 years ago

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.

jdecaron commented 9 years ago

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.