Added target specific event emissions. Closes GH-116.

This commit is contained in:
Jurie-Jan Botha 2013-05-24 10:16:59 +02:00 committed by Kyle Robinson Young
parent 81636c6fea
commit ede6a107b3
7 changed files with 37 additions and 8 deletions

View File

@ -192,8 +192,8 @@ grunt.initConfig({
}, },
}, },
}); });
grunt.event.on('watch', function(action, filepath) { grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(filepath + ' has ' + action); grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
}); });
``` ```

View File

@ -41,8 +41,8 @@ grunt.initConfig({
}, },
}, },
}); });
grunt.event.on('watch', function(action, filepath) { grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(filepath + ' has ' + action); grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
}); });
``` ```

View File

@ -127,7 +127,7 @@ module.exports = function(grunt) {
// Emit watch events if anyone is listening // Emit watch events if anyone is listening
if (grunt.event.listeners('watch').length > 0) { if (grunt.event.listeners('watch').length > 0) {
grunt.event.emit('watch', status, filepath); grunt.event.emit('watch', status, filepath, target.name);
} }
// Group changed files only for display // Group changed files only for display

View File

@ -29,20 +29,27 @@ module.exports = function(grunt) {
event: ['added', 'deleted'], event: ['added', 'deleted'],
}, },
files: ['lib/*.js'], files: ['lib/*.js'],
} },
targetOne: { files: ['lib/one/*.js'] },
targetTwo: { files: ['lib/two/*.js'] },
}, },
}); });
// Load this watch task // Load this watch task
grunt.loadTasks('../../../tasks'); grunt.loadTasks('../../../tasks');
// trigger on watch events
var timeout; var timeout;
grunt.event.on('watch', function(action, filepath) {
// trigger on watch events
grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(filepath + ' was indeed ' + action); grunt.log.writeln(filepath + ' was indeed ' + action);
if (target !== undefined) {
grunt.log.writeln(target + ' specifc event was fired')
}
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(function() { timeout = setTimeout(function() {
grunt.util.exit(0); grunt.util.exit(0);
}, 2000); }, 2000);
}); });
}; };

1
test/fixtures/events/lib/one/test.js vendored Normal file
View File

@ -0,0 +1 @@
var test = false;

1
test/fixtures/events/lib/two/test.js vendored Normal file
View File

@ -0,0 +1 @@
var test = false;

View File

@ -110,4 +110,24 @@ exports.events = {
test.done(); test.done();
}); });
}, },
targetSpecific: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'events');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch([function() {
var write = 'var test = false;';
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib/one', 'test.js'), write);
}, 300);
setTimeout(function() {
grunt.file.write(path.join(cwd, 'lib/two', 'test.js'), write);
}, 300);
}], function(result) {
result = helper.unixify(result);
helper.verboseLog(result);
test.ok(result.indexOf('lib/one/test.js was indeed changed\ntargetOne specifc event was fired') !== -1, 'event should have been emitted with targetOne specified');
test.ok(result.indexOf('lib/two/test.js was indeed changed\ntargetTwo specifc event was fired') !== -1, 'event should have been emitted with targetTwo specified');
test.done();
});
}
}; };