Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to issue #510 Watch doesn't detect cascading changes when nospawn is true #512

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 68 additions & 64 deletions tasks/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ module.exports = function(grunt) {
var name = self.name || 'watch';

// Close any previously opened watchers
watchers.forEach(function(watcher) {
watcher.close();
});
watchers = [];
if (!taskrun.running) {
watchers.forEach(function(watcher) {
watcher.close();
});
watchers = [];
}

// Never gonna give you up, never gonna let you down
if (grunt.config([name, 'options', 'forever']) !== false) {
Expand Down Expand Up @@ -111,80 +113,82 @@ module.exports = function(grunt) {
}

// Create watcher per target
watchers.push(new Gaze(patterns, target.options, function(err) {
if (err) {
if (typeof err === 'string') {
err = new Error(err);
if (!taskrun.running) {
watchers.push(new Gaze(patterns, target.options, function(err) {
if (err) {
if (typeof err === 'string') {
err = new Error(err);
}
grunt.log.writeln('ERROR'.red);
grunt.fatal(err);
return taskrun.done();
}
grunt.log.writeln('ERROR'.red);
grunt.fatal(err);
return taskrun.done();
}

// Log all watched files with --verbose set
if (grunt.option('verbose')) {
var watched = this.watched();
Object.keys(watched).forEach(function(watchedDir) {
watched[watchedDir].forEach(function(watchedFile) {
grunt.log.writeln('Watching ' + path.relative(process.cwd(), watchedFile) + ' for changes.');

// Log all watched files with --verbose set
if (grunt.option('verbose')) {
var watched = this.watched();
Object.keys(watched).forEach(function(watchedDir) {
watched[watchedDir].forEach(function(watchedFile) {
grunt.log.writeln('Watching ' + path.relative(process.cwd(), watchedFile) + ' for changes.');
});
});
});
}
}

// On changed/added/deleted
this.on('all', function(status, filepath) {
// On changed/added/deleted
this.on('all', function(status, filepath) {

// Skip events not specified
if (!_.includes(target.options.event, 'all') &&
!_.includes(target.options.event, status)) {
return;
}
// Skip events not specified
if (!_.includes(target.options.event, 'all') &&
!_.includes(target.options.event, status)) {
return;
}

filepath = path.relative(eventCwd, filepath);
filepath = path.relative(eventCwd, filepath);

// Skip empty filepaths
if (filepath === '') {
return;
}
// Skip empty filepaths
if (filepath === '') {
return;
}

// If Gruntfile.js changed, reload self task
if (target.options.reload || /gruntfile\.(js|coffee)/i.test(filepath)) {
taskrun.reload = true;
}
// If Gruntfile.js changed, reload self task
if (target.options.reload || /gruntfile\.(js|coffee)/i.test(filepath)) {
taskrun.reload = true;
}

// Emit watch events if anyone is listening
if (grunt.event.listeners('watch').length > 0) {
grunt.event.emit('watch', status, filepath, target.name);
}
// Emit watch events if anyone is listening
if (grunt.event.listeners('watch').length > 0) {
grunt.event.emit('watch', status, filepath, target.name);
}

// Group changed files only for display
changedFiles[filepath] = status;
// Group changed files only for display
changedFiles[filepath] = status;

// Add changed files to the target
if (taskrun.targets[target.name]) {
if (!taskrun.targets[target.name].changedFiles) {
taskrun.targets[target.name].changedFiles = Object.create(null);
// Add changed files to the target
if (taskrun.targets[target.name]) {
if (!taskrun.targets[target.name].changedFiles) {
taskrun.targets[target.name].changedFiles = Object.create(null);
}
taskrun.targets[target.name].changedFiles[filepath] = status;
}
taskrun.targets[target.name].changedFiles[filepath] = status;
}

// Queue the target
if (taskrun.queue.indexOf(target.name) === -1) {
taskrun.queue.push(target.name);
}
// Queue the target
if (taskrun.queue.indexOf(target.name) === -1) {
taskrun.queue.push(target.name);
}

// Run the tasks
taskrun.run();
});
// Run the tasks
taskrun.run();
});

// On watcher error
this.on('error', function(err) {
if (typeof err === 'string') {
err = new Error(err);
}
grunt.log.error(err.message);
});
}));
// On watcher error
this.on('error', function(err) {
if (typeof err === 'string') {
err = new Error(err);
}
grunt.log.error(err.message);
});
}));
}
});

});
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/nospawn/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module.exports = function(grunt) {
var port = 1337;

grunt.initConfig({
simplecopy: {
cascade: {
src: 'lib/source.js',
dest: 'lib/destination.js',
},
},
watch: {
nospawn: {
files: ['lib/nospawn.js'],
Expand All @@ -17,6 +23,16 @@ module.exports = function(grunt) {
files: ['lib/spawn.js'],
tasks: ['server'],
},
cascading: {
files: ['lib/source.js'],
tasks: ['simplecopy:cascade'],
options: {
nospawn: true,
},
},
cascaded: {
files: ['lib/destination.js'],
},
interrupt: {
files: ['lib/interrupt.js'],
tasks: ['long', 'long', 'long'],
Expand All @@ -28,6 +44,9 @@ module.exports = function(grunt) {
},
});

// Load the simplecopy task
grunt.loadTasks('../tasks');

// Load this watch task
grunt.loadTasks('../../../tasks');

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/nospawn/lib/destination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var cascading = true;
1 change: 1 addition & 0 deletions test/fixtures/nospawn/lib/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var cascading = true;
31 changes: 31 additions & 0 deletions test/fixtures/tasks/simplecopy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var fs = require('fs');

module.exports = function(grunt) {
'use strict';
grunt.registerMultiTask('simplecopy', 'A task that simply copy a file.', function() {
var src = this.data.src;
var dest = this.data.dest;
var done = this.async();

var rd = fs.createReadStream(src);
rd.on("error", function(err) {
grunt.fail.fatal(err);
done();
});
var wr = fs.createWriteStream(dest);
wr.on("error", function(err) {
grunt.fail.fatal(err);
done();
});
wr.on("close", function(ex) {
if (ex) {
grunt.fail.fatal(ex);
} else {
grunt.log.writeln('Copied ' + src + ' to ' + dest + '.');
}
done();
});
rd.pipe(wr);
});
};

16 changes: 16 additions & 0 deletions test/tasks/nospawn_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ exports.nospawn = {
test.done();
});
},
cascading: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'nospawn');
var assertWatch = helper.assertTask('watch', {cwd: cwd});
assertWatch(function() {
var write = 'var cascading = true;';
grunt.file.write(path.join(cwd, 'lib', 'source.js'), write);
}, function(result) {
helper.verboseLog(result);
test.ok(/File "lib[\/\\]source\.js" changed/.test(result),
'Cascading file should have been detected.');
test.ok(/File "lib[\/\\]destination\.js" changed/.test(result),
'Cascaded file should have been detected.');
test.done();
});
},
};