How to print out a text once grunt task completes?

gruntjs, javascript, node.js

Solution

There is a much better way to do it, without creating an extra task, and modifying anything else.

Grunt is a node process, so you can:

- use the process `stdout` to write what you need

- subscribe to the process `exit` event to do it when a task is finishing its execution

This is a simple example which prints out the time when the tasks has finished their execution:

module.exports = function (grunt) {
    // Creates a write function bound to process.stdout:
    var write = process.stdout.write.bind(process.stdout);

    // Subscribes to the process exit event...
    process.on("exit", function () {
        // ... to write the information in the process stdout
        write('\nFinished at ' + new Date().toLocaleTimeString()+ '\n');
    });

    // From here, your usual gruntfile configuration, without changes
    grunt.initConfig({

When you run any task, you'll see a message at the bottom like:

Finished at 18:26:45

Problem

Once a Grunt task completes, I want to print out some information. See the Grunt snippet below. Is there a way to achieve this? I noticed that `grunt.task.run()` does not support callbacks. This causes my message to be printed out prior to coverage report output. ``` grunt.registerTask('coverage', 'Runs all unit tests available via Mocha and generates code coverage report', function() { grunt.task.run('env:unitTest', 'mochaTest'); grunt.log.writeln('Code coverage report was generated into "build/coverage.html"'); }); ``` I also want to avoid "hacks" such as creating a grunt task only for printing the information out and adding it to the `grunt.task.run()` chain of tasks.

Original source