Skip to content
Browse files

watch tests now pass

  • Loading branch information...
1 parent 657738c commit d47a09d9b68ca7c61b801ff1a0df9637dd04adf4 @substack committed Jul 9, 2011
Showing with 45 additions and 8 deletions.
  1. +42 −5 index.js
  2. +3 −3 test/watch.js
View
47 index.js
@@ -31,14 +31,31 @@ module.exports = function (target, dir, optsOrEx) {
}
};
- return function (bundle) {
+ var watches = {};
+ var self = function (bundle) {
var files = [];
- findit.sync(dir, function (file, stat) {
+ function finder (file, stat) {
if (stat.isDirectory()) {
if (opts.watch) {
+ watches[file] = true;
fs.watchFile(file, function (curr, prev) {
-
+ if (curr.nlink === 0) {
+ // deleted
+ }
+ else {
+ // modified
+ fs.readdir(file, function (err, xs) {
+ var rescan = false;
+ xs.forEach(function (x) {
+ var f = path.resolve(dir, x);
+ if (files.indexOf(f) < 0) {
+ files.push(f);
+ include(files);
+ }
+ });
+ });
+ }
});
}
}
@@ -47,12 +64,24 @@ module.exports = function (target, dir, optsOrEx) {
files.push(file);
if (opts.watch) {
+ watches[file] = true;
fs.watchFile(file, function (curr, prev) {
-
+ if (curr.nlink === 0) {
+ // deleted
+ var i = files.indexOf(file);
+ if (i >= 0) files.splice(i, 1);
+ include(files);
+ }
+ else {
+ include(files);
+ }
});
}
}
- });
+ }
+
+ finder(dir, { isDirectory : function () { return true }});
+ findit.sync(dir, finder);
var include = function (files) {
var dst = path.normalize('/node_modules/' + target);
@@ -80,5 +109,13 @@ module.exports = function (target, dir, optsOrEx) {
};
include(files);
};
+
+ self.end = function () {
+ Object.keys(watches).forEach(function (file) {
+ fs.unwatchFile(file);
+ });
+ };
+
+ return self;
};
View
6 test/watch.js
@@ -17,9 +17,8 @@ exports.watch = function () {
var x0 = Math.random();
write('x.txt', x0, function () {
- var b = browserify().use(
- fileify('files', __dirname + '/watch', { watch : true })
- );
+ var fn = fileify('files', __dirname + '/watch', { watch : true });
+ var b = browserify().use(fn);
var c0 = {};
vm.runInNewContext(b.bundle(), c0);
@@ -39,6 +38,7 @@ exports.watch = function () {
'x.txt' : x1,
'y.txt' : y0,
});
+ fn.end();
});
});
});

0 comments on commit d47a09d

Please sign in to comment.
Something went wrong with that request. Please try again.