1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Handling all ENOENTs potentially thrown by fs.watch (hopefully)

This commit is contained in:
Trevor Burnham 2011-12-18 14:01:13 -05:00
parent ed1ddbce50
commit 9fde794858
2 changed files with 107 additions and 69 deletions

View file

@ -188,7 +188,7 @@
};
watch = function(source, base) {
var callback, compile, prevStats, watcher;
var callback, compile, prevStats, watchErr, watcher;
prevStats = null;
compile = function() {
return fs.stat(source, function(err, stats) {
@ -203,41 +203,81 @@
});
});
};
return watcher = fs.watch(source, callback = function(event) {
if (event === 'change') {
return compile();
} else if (event === 'rename') {
watcher.close();
return setTimeout(function() {
return path.exists(source, function(exists) {
if (exists) {
compile();
return watcher = fs.watch(source, callback);
} else {
removeSource(source, base, true);
return compileJoin();
}
});
}, 250);
watchErr = function(e) {
if (e.code === 'ENOENT') {
removeSource(source, base, true);
return compileJoin();
} else {
throw e;
}
});
};
try {
return watcher = fs.watch(source, callback = function(event) {
if (event === 'change') {
return compile();
} else if (event === 'rename') {
watcher.close();
return setTimeout(function() {
compile();
try {
return watcher = fs.watch(source, callback);
} catch (e) {
return watchErr(e);
}
}, 250);
}
});
} catch (e) {
return watchErr(e);
}
};
watchDir = function(source, base) {
var watcher;
return watcher = fs.watch(source, function() {
return path.exists(source, function(exists) {
var file, toRemove, _i, _len;
if (exists) {
return fs.readdir(source, function(err, files) {
var file, _i, _len, _results;
if (err) throw err;
var watchErr, watcher;
watchErr = function(e) {
var file, toRemove, _i, _len;
toRemove = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
file = sources[_i];
if (file.indexOf(source) >= 0) _results.push(file);
}
return _results;
})();
for (_i = 0, _len = toRemove.length; _i < _len; _i++) {
file = toRemove[_i];
removeSource(file, base, true);
}
return compileJoin();
};
try {
return watcher = fs.watch(source, function() {
return fs.readdir(source, function(err, files) {
var file, toRemove, _i, _j, _len, _len2, _results;
if (err) {
if (err.code !== 'ENOENT') throw err;
toRemove = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
file = sources[_i];
if (file.indexOf(source) >= 0) _results.push(file);
}
return _results;
})();
for (_i = 0, _len = toRemove.length; _i < _len; _i++) {
file = toRemove[_i];
removeSource(file, base, true);
}
return compileJoin();
} else {
files = files.map(function(file) {
return path.join(source, file);
});
_results = [];
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
for (_j = 0, _len2 = files.length; _j < _len2; _j++) {
file = files[_j];
if (!(!notSources[file])) continue;
if (sources.some(function(s) {
return s.indexOf(file) >= 0;
@ -249,26 +289,12 @@
_results.push(compilePath(file, false, base));
}
return _results;
});
} else {
watcher.close();
toRemove = (function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = sources.length; _i < _len; _i++) {
file = sources[_i];
if (file.indexOf(source) >= 0) _results.push(file);
}
return _results;
})();
for (_i = 0, _len = toRemove.length; _i < _len; _i++) {
file = toRemove[_i];
removeSource(file, base, true);
}
return compileJoin();
}
});
});
});
} catch (e) {
if (e.code !== 'ENOENT') throw e;
}
};
removeSource = function(source, base, removeJs) {

View file

@ -179,39 +179,51 @@ watch = (source, base) ->
throw err if err
compileScript(source, code.toString(), base)
watcher = fs.watch source, callback = (event) ->
if event is 'change'
compile()
else if event is 'rename'
watcher.close()
setTimeout ->
path.exists source, (exists) ->
if exists
compile()
watchErr = (e) ->
if e.code is 'ENOENT'
removeSource source, base, yes
compileJoin()
else throw e
try
watcher = fs.watch source, callback = (event) ->
if event is 'change'
compile()
else if event is 'rename'
watcher.close()
setTimeout ->
compile()
try
watcher = fs.watch source, callback
else
removeSource source, base, yes
compileJoin()
, 250
catch e then watchErr e
, 250
catch e then watchErr e
# Watch a directory of files for new additions.
watchDir = (source, base) ->
watcher = fs.watch source, ->
path.exists source, (exists) ->
if exists
fs.readdir source, (err, files) ->
throw err if err
watchErr = (e) ->
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
compileJoin()
try
watcher = fs.watch source, ->
fs.readdir source, (err, files) ->
if err
throw err unless err.code is 'ENOENT'
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
compileJoin()
else
files = files.map (file) -> path.join source, file
for file in files when not notSources[file]
continue if sources.some (s) -> s.indexOf(file) >= 0
sources.push file
sourceCode.push null
compilePath file, no, base
else
watcher.close()
toRemove = (file for file in sources when file.indexOf(source) >= 0)
removeSource file, base, yes for file in toRemove
compileJoin()
catch e
throw e unless e.code is 'ENOENT'
# Remove a file from our source list, and source code cache. Optionally remove
# the compiled JS version as well.