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

tell me if i broke anything #1

Merged
10 commits merged into from
Oct 3, 2010
47 changes: 22 additions & 25 deletions nodemon
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ var fs = require('fs'),
node = null, // removes 'node' and this script
monitor = null,
ignoreFilePath = __dirname + '/ignore',
ignoreFileTime = null,
ignoreFiles = [flag], // ignore the monitor flag by default
timeout = 1000; // check every 1 second

Array.prototype.remove = function(e) {
for (var i = 0; i < this.length; i++) {
if (e == this[i]) {
this.splice(i, 1);
}
}
};
reIgnoreFiles = null,
timeout = 1000, // check every 1 second
// create once, reuse as needed
reComments = /#.*$/,
reTrim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g,
reEscapeChars = /[.|\-[\]()\\]/g,
reAsterisk = /\*/g;

function startNode() {
sys.log('[nodemon] starting node');
Expand Down Expand Up @@ -48,21 +47,19 @@ function startMonitor() {

exec(cmd, function (error, stdout, stderr) {
var files = stdout.split(/\n/);

files.pop(); // remove blank line ending and split
if (files.length) {
var finalFiles = [].slice.call(files, 0); // creates a clone
files.forEach(function (file) {
ignoreFiles.forEach(function (ignore) {
var re = new RegExp(ignore);
if (re.test(file)) {
finalFiles.remove(file);
}
// filter ignored files
if (ignoreFiles.length) {
files = files.filter(function(file) {
return !reIgnoreFiles.test(file);
});
});
}

fs.writeFileSync(flag, '');

if (finalFiles.length) {
if (files.length) {
sys.log('[nodemon] restarting due to changes...');
finalFiles.forEach(function (file) {
sys.log('[nodemon] ' + file);
Expand All @@ -79,15 +76,15 @@ function startMonitor() {
function readIgnoreFile() {
fs.unwatchFile(ignoreFilePath);
sys.log('[nodemon] reading ignore list');
var lines = fs.readFileSync(ignoreFilePath).toString().split(/\n/);
ignoreFiles = [flag];
lines.forEach(function (line) {
fs.readFileSync(ignoreFilePath).toString().split(/\n/).forEach(function (line) {
// remove comments and trim lines
line = line.replace(/#.*$/, '').replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, '');
if (line) {
ignoreFiles.push(line);
if (line = line.replace(reComments, '').replace(reTrim, '')) {
ignoreFiles.push(line.replace(reEscapeChars, '\\$&').replace(reAsterisk, '.*'));
}
});
});
reIgnoreFiles = new RegExp(ignoreFiles.join('|'));

fs.watchFile(ignoreFilePath, { persistent: false }, readIgnoreFile);
}

Expand All @@ -109,4 +106,4 @@ process.on('uncaughtException', function (err) {

sys.log('[nodemon] starting');
startNode();
setTimeout(startMonitor, timeout);
setTimeout(startMonitor, timeout);