Skip to content

Commit

Permalink
able to watch new files now
Browse files Browse the repository at this point in the history
  • Loading branch information
jrsacks committed May 18, 2011
1 parent 1b35489 commit 35ab95a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
50 changes: 30 additions & 20 deletions lib/jezebel/watcher.js
@@ -1,43 +1,53 @@
// Basically stolen from supervisor.js, so there are no tests
// Initially stolen from supervisor.js
// https://github.com/isaacs/node-supervisor

var fs = require('fs');
var utils = require('jezebel/utils');
var watched = [];

function watchGivenFile (watch, callback) {
fs.watchFile(watch, {persistent: true, interval: 500}, callback);
function reset() {
watched = [];
}

function watchFiles(path, callback) {
var curriedCallback = function(curr, prev) {
callback(path, curr, prev);
};
function watchDirectory(path, callback) {
fs.readdir(path, function(err, fileNames) {
if(err) {
sys.puts('Error reading path: ' + path);
}
else {
fileNames.forEach(function (fileName) {
if (utils.isInProject(fileName)) {
watchFiles(path + '/' + fileName, callback);
}
});
}
});
}

function watchFileOnce(path, callback) {
if(watched.indexOf(path) == -1){
watched.push(path)
fs.watchFile(path, {persistent: true, interval: 500}, callback);
}
}

function watchFiles(path, callback) {
fs.stat(path, function(err, stats){
if (err) {
sys.error('Error retrieving stats for file: ' + path);
}
else {
if (stats.isDirectory()) {
fs.readdir(path, function(err, fileNames) {
if(err) {
sys.puts('Error reading path: ' + path);
}
else {
fileNames.forEach(function (fileName) {
if (utils.isInProject(fileName)) {
watchFiles(path + '/' + fileName, callback);
}
});
}
});
watchDirectory(path, callback);
watchFileOnce(path, function() { watchDirectory(path, callback); });
}
else {
watchGivenFile(path, curriedCallback);
watchFileOnce(path, function(curr, prev) { callback(path, curr, prev); });
}
}
});
}

exports.watchFiles = watchFiles;
exports.reset = reset;

31 changes: 26 additions & 5 deletions spec/watcher_spec.js
Expand Up @@ -3,8 +3,10 @@ describe('watcher', function() {

beforeEach(function() {
watcher = require('jezebel/watcher');
watcher.reset();
fs = require('fs');
spyOn(fs, 'watchFile');
spyOn(fs, 'unwatchFile');
spyOn(fs, 'stat');
});

Expand All @@ -25,21 +27,40 @@ describe('watcher', function() {
fs.watchFile.invokeCallback('curr', 'prev');
expect(callback).toHaveBeenCalledWith('filename', 'curr' , 'prev');
});

it('only watches a file once', function() {
watcher.watchFiles('filename', callback);
watcher.watchFiles('filename', callback);
expect(fs.watchFile.callCount).toEqual(1);
});
});

describe('watching a directory', function() {
it('calls watchFile on all files in the directory', function() {
beforeEach(function() {
spyOn(fs, 'readdir');
watcher.watchFiles('dirName', {});
watcher.watchFiles('dirName', function() {});
fs.stat.invokeCallback('', {isDirectory : function(){return true;}});
});

it('calls watchFile on dir and all files in the dir', function() {
fs.readdir.invokeCallback('', ['file0', 'file1']);
fs.stat.invokeCallback('', {isDirectory : function(){return false;}});

expect(fs.watchFile.callCount).toEqual(2);
expect(fs.watchFile.callCount).toEqual(3);
expect(fs.watchFile.argsForCall[0][0]).toEqual('dirName');
expect(fs.watchFile.argsForCall[1][0]).toEqual('dirName/file0');
expect(fs.watchFile.argsForCall[2][0]).toEqual('dirName/file1');
});

it('detects new files and watches those', function() {
fs.readdir.reset();
fs.watchFile.invokeCallback('');

fs.readdir.invokeCallback('', ['file0']);
fs.stat.invokeCallback('', {isDirectory : function(){return false;}});

expect(fs.watchFile.callCount).toEqual(1);
expect(fs.watchFile.argsForCall[0][0]).toEqual('dirName/file0');
expect(fs.watchFile.argsForCall[1][0]).toEqual('dirName/file1');
});

});
});

0 comments on commit 35ab95a

Please sign in to comment.