FSWatcher.prototype._watch = function(item, callback) { var basename, directory, options, parent, watcher; if (callback == null) callback = Function.prototype; // empty function directory = sysPath.dirname(item); basename = sysPath.basename(item); parent = this._getWatchedDir(directory); if (parent.indexOf(basename) !== -1) return; this._addToWatchedDir(directory, basename); options = {persistent: this.options.persistent}; if (this.options.usePolling) { options.interval = this.enableBinaryInterval && isBinaryPath(basename) ? this.options.binaryInterval : this.options.interval; fs.watchFile(item, options, function(curr, prev) { if (curr.mtime.getTime() > prev.mtime.getTime()) { callback(item, curr); } }); } else { watcher = fs.watch(item, options, function(event, path) { callback(item); }); this.watchers.push(watcher); } };
createWatcher() { try { // TODO options.ignored if(this.polledWatching) { const listener = this.onWatchFileEvent.bind(this); fs.watchFile(this.path, { persistent: true, interval: this.polledWatching }, listener); // Fallback to caputure too late attached watchers const timeout = setTimeout(() => { fs.stat(this.path, (err, stats) => { if(!err) { listener(stats); } }) }, 1000); this.watcher = { close: () => { clearTimeout(timeout); fs.unwatchFile(this.path, listener); } }; } else { if(IS_OSX) { this.watchInParentDirectory(); } this.watcher = fs.watch(this.path); this.watcher.on("change", this.onWatchEvent.bind(this)); this.watcher.on("error", this.onWatcherError.bind(this)); } } catch(err) { this.onWatcherError(err); } }
Array.forEach( watchFiles, function( watchFile ) { // Verify file is already beign watched if ( !this.watchList.contains( watchFile ) ) { fs.watchFile( this.getAbsolutePath( watchFile ), { interval: this.options.interval }, function() { this.update( watchFile ); }.bind(this) ); } this.watchList.include( watchFile ); }, this );
_ensureFilePolling(path) { if(!this.filesWatchListener.has(path)) { const listener = this.onWatchFileChildEvent.bind(this, path); this.filesWatchListener.set(path, listener); fs.watchFile(path, { persistent: true, interval: this.polledWatching }, listener); // It's possible that we miss the first event // so we have a fallback after 1s setTimeout(() => { fs.stat(path, (err, stats) => { if(!err) { listener(stats); } }) }, 1000); } }
fs.writeFileSync('./config/config.js', fs.readFileSync('./config/config-example.js') ); } /********************************************************* * Load configuration *********************************************************/ global.Config = require('./config/config.js'); if (Config.watchconfig) { fs.watchFile('./config/config.js', function (curr, prev) { if (curr.mtime <= prev.mtime) return; try { delete require.cache[require.resolve('./config/config.js')]; global.Config = require('./config/config.js'); console.log('Reloaded config/config.js'); } catch (e) {} }); } // Autoconfigure the app when running in cloud hosting environments: var cloudenv = require('cloud-env'); Config.bindaddress = cloudenv.get('IP', Config.bindaddress || ''); Config.port = cloudenv.get('PORT', Config.port); if (process.argv[2] && parseInt(process.argv[2])) { Config.port = parseInt(process.argv[2]); } global.ResourceMonitor = {