How to print text after grunt job completion?

As soon as the Grunt task completes, I want to print some information. See the Grunt snippet below.

Is there any way to achieve this? I noticed that grunt.task.run() does not support callbacks. This causes my message to be printed before the coverage report.

 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 to print out information and add it to the grunt.task.run() task chain.

+7
javascript gruntjs
source share
2 answers

There is a much better way to do this without creating an additional task and changing nothing.

Grunt is a node process, so you can:

  • use the stdout process to write what you need
  • subscribe to the exit process to do this when the task completes execution

This is a simple example that prints the completion time of tasks:

 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 will see a message below, like:

Finished at 18:26:45

0
source share

Create a task that will be executed when everything is completed, and then add it to the 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']); 
+17
source share

All Articles