Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out a text once grunt task completes?

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.

like image 525
Pavel Lobodinský Avatar asked Nov 29 '13 06:11

Pavel Lobodinský


2 Answers

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

like image 118
JotaBe Avatar answered Oct 18 '22 19:10

JotaBe


Create a task that will run when everything is all done and then add it to your task chain:

grunt.registerTask('alldone', function() {
  grunt.log.writeln('Code coverage report was generated into "build/coverage.html"');
});
grunt.registerTask('default', ['env:unitTest', 'mochaTest', 'alldone']);
like image 19
Kyle Robinson Young Avatar answered Oct 18 '22 21:10

Kyle Robinson Young