Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[api] Complete file watching with .foreverignore
  • Loading branch information
mmalecki committed Sep 30, 2011
1 parent 28a7c16 commit b9d9703
Showing 1 changed file with 44 additions and 6 deletions.
50 changes: 44 additions & 6 deletions lib/forever/monitor.js
Expand Up @@ -13,6 +13,7 @@ var util = require('util'),
spawn = require('child_process').spawn,
winston = require('winston'),
watch = require('watch'),
minimatch = require('minimatch'),
forever = require('../forever');

//
Expand All @@ -37,6 +38,13 @@ var Monitor = exports.Monitor = function (script, options) {
this.childExists = false;
this.times = 0;


//
// Setup watch configuration options
//
this.watchIgnoreDotFiles = options.watchIgnoreDotFiles || true;
this.watchIgnorePatterns = options.watchIgnorePatterns || [];

//
// Setup restart timing. These options control how quickly forever restarts
// a child process as well as when to kill a "spinning" process
Expand Down Expand Up @@ -372,13 +380,43 @@ Monitor.prototype.kill = function (forceStop) {
Monitor.prototype.watch = function () {
var self = this;

watch.createMonitor(process.cwd(), {ignoreDotFiles: true}, function (monitor) {
monitor.on('changed', function (f, curr, prev) {
self.info('restaring script because ' + f + ' changed');
self.restart();
});
fs.readFile('.foreverignore', 'utf8', function (err, data) {
if (err) {
return self.warn('Could not read .foreverignore file: ' + err.message);
}
Array.prototype.push.apply(self.watchIgnorePatterns, data.split('\n'));
});
}

watch.createMonitor(
process.cwd(),
function (monitor) {
monitor.on('changed', function (f, curr, prev) {
if (self._watchFilter(f)) {
self.info('restaring script because ' + f + ' changed');
self.restart();
}
});
}
);
};

//
// ### @private function _watchFilter
// #### @file {string} File name
// Determines whether we should restart if `file` change (@mikeal's filtering
// is pretty messed up).
//
Monitor.prototype._watchFilter = function (fileName) {
if (this.watchIgnoreDotFiles && path.basename(fileName)[0] == '.') {
return false;
}
for (var key in this.watchIgnorePatterns) {
if (minimatch(fileName, this.watchIgnorePatterns[key], {matchBase: process.cwd()})) {
return false;
}
}
return true;
};

//
// ### @private function _getEnv ()
Expand Down

0 comments on commit b9d9703

Please sign in to comment.