Created
September 2, 2015 09:43
-
-
Save robozevel/935a772d0a494bd35319 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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