Created
September 17, 2012 16:38
-
-
Save shama/3738386 to your computer and use it in GitHub Desktop.
Grunt Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A) Simple Watch Events | |
*/ | |
module.exports = function(grunt) { | |
grunt.initConfig({ | |
nodeunit: { | |
files: ['test/**/*.js'], | |
}, | |
watch: { | |
test: { | |
files: ['<%= nodeunit.files %>'], | |
tasks: ['nodeunit'] | |
} | |
} | |
}); | |
grunt.event.on(['watch', '*'], function(filepath) { | |
// Do something when file added/changed/deleted | |
// How helpful is this event? The watch.test.tasks above will still | |
// run regaurdless. | |
// What if I only wanted to run nodeunit on the corresponding test? | |
// I would need to create a watch target for every file. | |
// So simple events would be helpful for notifications but not useful | |
// for much else. | |
}); | |
grunt.registerTask('default', ['nodeunit']); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* B) Integrated Watch Events | |
*/ | |
module.exports = function(grunt) { | |
grunt.initConfig({ | |
nodeunit: { | |
files: ['test/**/*.js'], | |
}, | |
watch: { | |
test: { | |
files: ['<%= nodeunit.files %>'], | |
tasks: ['nodeunit'], | |
// events can be configured per target | |
on: { 'watch.*': onWatch } | |
} | |
} | |
}); | |
function onWatch(filepath) { | |
// filepath would be the file edited upon this event | |
// this.event === the event fired: changed, added, deleted | |
// do nothing here and it would run the tasks as normal | |
// and act as a notification | |
// only run the tasks for the edited file | |
// this.files() would set an array of files to run the task on | |
this.files([filepath]); | |
// or a set of tests setup for this src file | |
var testDir = require('path').basename(filepath, '.js'); | |
this.files(['test/' + testDir + '/*.js']); | |
// run the jshint task instead of nodeunit | |
// this.tasks() would set an array of tasks to run on the fiels | |
this.tasks(['jshint']); | |
} | |
grunt.registerTask('default', ['nodeunit']); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* C) External Watch Events (not integrated with config) | |
*/ | |
module.exports = function(grunt) { | |
grunt.initConfig({ | |
nodeunit: { | |
files: ['test/**/*.js'], | |
}, | |
watch: { | |
test: { | |
files: ['<%= nodeunit.files %>'], | |
tasks: ['nodeunit'] | |
} | |
} | |
}); | |
// One event listener for all watch targets | |
grunt.event.on('watch.*', function(filepath) { | |
// filepath would be the file edited upon this event | |
// this.event === the event fired: changed, added, deleted | |
// do nothing here and it would run the tasks as normal | |
// and act as a notification | |
// only run the tasks for the edited file | |
// this.files() would set an array of files to run the task on | |
this.files([filepath]); | |
// or a set of tests setup for this src file | |
var testDir = require('path').basename(filepath, '.js'); | |
this.files(['test/' + testDir + '/*.js']); | |
// run the jshint task instead of nodeunit | |
// this.tasks() would set an array of tasks to run on the fiels | |
this.tasks(['jshint']); | |
}); | |
grunt.registerTask('default', ['nodeunit']); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment