Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  54] [ 4]  / answers: 1 / hits: 5795  / 11 Years ago, fri, november 29, 2013, 12:00:00

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.


More From » node.js

 Answers
11

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



[#49995] Monday, July 18, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margob

Total Points: 302
Total Questions: 89
Total Answers: 100

Location: Guadeloupe
Member since Sat, Jul 25, 2020
4 Years ago
margob questions
Mon, Jun 29, 20, 00:00, 4 Years ago
Wed, Sep 26, 18, 00:00, 6 Years ago
;