Search code examples
gruntjsgrowl

How to show a growl notification from within a grunt task


In this example grunt plugin, I want to send a growl notification every time the 'left' or 'right' task completes.

However, nothing is happening currently.

What am I missing?

module.exports = function (grunt) {

// Project configuration.
grunt.initConfig({

    notify: {
        left: {
            options: {
                title: 'Left Leg Down',
                message: 'go'
            }
        },
        right: {
            options: {
                title: 'Right Leg Down',
                message: 'go'
            }
        }
    }       
});



// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-notify');



// Tasks
grunt.registerTask('default', ['walk', 'notify']);

grunt.registerTask('left', function(){});
grunt.registerTask('right', function(){});

grunt.registerTask('walk', function(){
    //grunt.task.run();

    var done = this.async();
    var leg = 'left';
    var intervalID = setInterval(callSwitch, 1000);

    function callSwitch(){
        if (leg == 'left') {
            grunt.task.run('left');
            grunt.log.write('left...');
            leg = 'right';
        } else {
            grunt.task.run('right');
            grunt.log.write('right...');
            leg = 'left';
        }
    };

    grunt.log.write('walking...');

});


};

EDIT - posting the completed grunt file for reference

'use strict';

module.exports = function (grunt) {

var leg = 'left';

// Project configuration.
grunt.initConfig({

    notify: {
        left: {
            options: {
                title: 'Left Leg Down',
                message: 'go'
            }
        },
        right: {
            options: {
                title: 'Right Leg Down',
                message: 'go'
            }
        }
    }

});



// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-notify');



// Tasks
grunt.registerTask('default', ['walk']);

grunt.registerTask('left', function(target){return true;});
grunt.registerTask('right', function(target){return true;});

grunt.registerTask('walk', function(target){

    var done = this.async();

    if (leg == 'left') {
        grunt.task.run(['left', 'notify:left']);
        //grunt.log.write('left...');
        leg = 'right';
    } else {
        grunt.task.run(['right', 'notify:right']);
        //grunt.log.write('right...');
        leg = 'left';
    }

    // repeat task each minute
    setTimeout(function () {
        grunt.task.run('walk');             
        done();
    }, 1000 * 5);


});


};

Solution

  • Oh..., sorry that I was not aware that you are using this.async(). The notify:left and notify:right tasks were waiting for walk task to be finished by calling done().

    So here is a solution which I have verified if it runs as expected on my environment.

    var leg = 'left';
    
    grunt.registerTask('walk' , function(target){
        var done = this.async();
    
        if (leg == 'left') {
            grunt.task.run(['left', 'notify:left']);
            grunt.log.write('left...');
            leg = 'right';
        } else {
            grunt.task.run(['right', 'notify:right']);
            grunt.log.write('right...');
            leg = 'left';
        }
    
        setTimeout(function () {
            grunt.task.run('walk');             
            done();
        }, 1000);
    });