Parris
Parris

Reputation: 18408

Grunt task output then calling grunt-notify

Grunt notify: https://github.com/dylang/grunt-notify is great. However, it seems a bit limited. As far as I can tell all my messages need to be pre-generated. So the first question is how can I generate notifications?

Next, It seems that grunt notify triggers based on error or success of some task. I guess based on std in/out/err? The problem where this breaks down is if some task doesn't use these. grunt compass doesn't use stderr if there are compile errors. So how can I run grunt notify when it has an error? This then leads to the next question. How can I grab the console output or stderr from a grunt task?

Upvotes: 4

Views: 3038

Answers (1)

asgoth
asgoth

Reputation: 35829

First of all, use growl. It is easy and flexible to use. To install growl:

npm install growl --save-dev

Then you need to hook into the stderr/out stream of the process. This way you can create a notification each time a new message arrives at the stderr/out stream.

This is what I've created. I've made a CommonJs module which adds hooks to:

  • grunt.fail.warn(), grunt.fail.fatal()
  • grunt.log.warn(), grunt.log.error()
  • grunt.warn()
  • process.stderr.write()
  • process.stdout.write() (error lines)
  • (child) process.stderr.write()
  • (child) process.stdout.write() (error lines)

It works more or less, but it may need some tweaking.

tasks/lib/notify.js

(function (module) {
    var grunt = require('grunt'),
        growl = require('growl'),
        Buffer = require('buffer').Buffer;

    function notify(obj, title) {
        if (obj) {
            var message = Buffer.isBuffer(obj) ? obj.toString() : (obj.message || obj);
            var msg = grunt.log.uncolor(message);

            if (msg.length > 0) {
                growl(msg, {
                    title: title,
                    image: 'Console'
                });
            }
        }
    }

// add a hook to grunt.fail.warn(), grunt.fail.fatal()
    ['warn', 'fatal'].forEach(function (level) {
        grunt.util.hooker.hook(grunt.fail, level, function(obj) {
            notify(obj);
        });
    });

// add a hook to grunt.log.warn(), grunt.log.error()
    ['warn', 'error'].forEach(function (level) {
        grunt.util.hooker.hook(grunt.log, level, function(obj) {
            notify(obj, level);
        });
    });

// add a hook to grunt.warn()
    grunt.util.hooker.hook(grunt, 'warn', function(obj) {
        notify(obj, 'warn');
    });

// add a hook to process.stderr.write()
    grunt.util.hooker.hook(process.stderr, 'write', function(obj) {
        var messages = grunt.log.uncolor((Buffer.isBuffer(obj) ? obj.toString() : (obj.message || obj))).split('\n');
        messages.forEach(function (message) {
            notify(message, 'stderr');
        });
    });

// add a hook to process.stdout.write() (only error lines)
    grunt.util.hooker.hook(process.stdout, 'write', function(obj) {
        var messages = grunt.log.uncolor((Buffer.isBuffer(obj) ? obj.toString() : (obj.message || obj))).split('\n');
        messages.forEach(function (message) {
            if (message && message.indexOf('error ') > -1) {
                notify(message, 'stdout');
            }
        });
    });

// add a hook to child process stdout/stderr write() (only error lines)
    grunt.util.hooker.hook(grunt.util, 'spawn', {
        post: function(child) {
            child.stderr.on('data', function (data) {
                var messages = grunt.log.uncolor(data.toString()).split('\n');
                messages.forEach(function (message) {
                    notify(message, 'stderr');
                });
            });
            child.stdout.on('data', function (data) {
                var messages = grunt.log.uncolor(data.toString()).split('\n');
                messages.forEach(function (message) {
                    if (message && message.indexOf('error ') > -1) {
                        notify(message, 'stdout');
                    }
                });
            });
        }
    });
}) (module);

Then you need to include it in your Gruntfile.js with a require statement:

module.exports = function (grunt) {

    grunt.initConfig({
       ...
    });
    require('./tasks/lib/notify');

    ...
};

PS I've placed the file in tasks/lib/notify.js, but feel free to place it somewhere else.

Upvotes: 7

Related Questions