Skip to content

Instantly share code, notes, and snippets.

@mdasberg
Last active March 8, 2019 15:13
Show Gist options
  • Save mdasberg/92455614afbe6987cc8f to your computer and use it in GitHub Desktop.
Save mdasberg/92455614afbe6987cc8f to your computer and use it in GitHub Desktop.
Pre-execution and post-execution "hooks" for Grunt
var grunt = require('grunt');
var hooker = require('hooker');
module.exports = preAndPostHook = function () {
var currentTask = undefined;
/**
* Do something when the task has sta.
* @param taskName The task name.
*/
function taskStartCallback(taskName) {
// do something
currentTask = taskName;
}
/**
* Do something when the task has finished.
* @param taskResult <code>true</code> if successful, else <code>false</code>.
*/
function taskEndCallback(taskResult) {
currentTask = undefined;
// do something
}
/** Hook into the grunt task runner. */
hooker.hook(grunt.task, 'runTaskFn', {
pre: function (context) {
var taskName = context.nameArgs;
if (currentTask !== undefined) {
taskEndCallback(true); // true indicates the task has finished successfully.
}
taskStartCallback(taskName);
}
});
/** Hook into the success / fail writer. */
hooker.hook(grunt.log.writeln(), ['success', 'fail'], function (res) { // check done or aborted
var done = res === 'Done, without errors.';
var warning = res === 'Done, but with warnings.';
var aborted = res === 'Aborted due to warnings.';
var error = warning || aborted;
if (done || error) {
if (currentTask !== undefined) {
taskEndCallback(error ? false : true);
}
}
});
/** Hook into the fatal writer. */
hooker.hook(grunt.fail, 'fatal', function () { // in case of a real failure
if (currentTask !== undefined) {
taskEndCallback(false);
}
});
}
@edsilv
Copy link

edsilv commented Feb 18, 2015

Hi, is it possible to get an example of this wrapping a grunt.task.run('taskname')? Having trouble getting the syntax right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment