Skip to content

Instantly share code, notes, and snippets.

@robozevel
Created September 2, 2015 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robozevel/935a772d0a494bd35319 to your computer and use it in GitHub Desktop.
Save robozevel/935a772d0a494bd35319 to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
var fs = require('fs');
var noop = function(){};
var DEFAULT_TIMEOUT = 60 * 2 * 1000;
module.exports = function watch(fileName, until, timeout) {
var watcher;
if (typeof until !== 'function') until = noop;
timeout = Number(timeout) || DEFAULT_TIMEOUT;
return new Promise(function(resolve, reject) {
if (until()) return resolve();
console.log('watching %s for changes...', fileName);
watcher = fs.watch(fileName, function(event) {
if (event !== 'change') return;
console.log(fileName, 'changed');
if (until()) resolve();
});
}).timeout(timeout).finally(function(e) {
if (watcher) watcher.close();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment