cocalc/src / smc-project / node_modules / forever-monitor / lib / forever-monitor / plugins / watch.js
50655 views/*1* logger.js: Plugin for `Monitor` instances which adds file watching.2*3* (C) 2010 Nodejitsu Inc.4* MIT LICENCE5*6*/78var fs = require('fs'),9path = require('path'),10minimatch = require('minimatch'),11watch = require('watch');1213exports.name = 'watch';1415//16// ### @private function _watchFilter17// #### @file {string} File name18// Determines whether we should restart if `file` change (@mikeal's filtering19// is pretty messed up).20//21function watchFilter(fileName) {22if (this.watchIgnoreDotFiles && path.basename(fileName)[0] === '.') {23return false;24}2526for (var key in this.watchIgnorePatterns) {27if (minimatch(fileName, this.watchIgnorePatterns[key], { matchBase: this.watchDirectory })) {28return false;29}30}3132return true;33};3435//36// ### function attach (options)37// #### @options {Object} Options for attaching to `Monitor`38//39// Attaches functionality for logging stdout and stderr to `Monitor` instances.40//41exports.attach = function () {42var monitor = this;4344fs.readFile(path.join(this.watchDirectory, '.foreverignore'), 'utf8', function (err, data) {45if (err) {46return monitor.emit('watch:error', {47message: 'Could not read .foreverignore file.',48error: err.message49});50}5152Array.prototype.push.apply(monitor.watchIgnorePatterns, data.split('\n'));53});5455watch.watchTree(this.watchDirectory, function (f, curr, prev) {56if (!(curr === null && prev === null && typeof f === 'object')) {57//58// `curr` == null && `prev` == null && typeof f == "object" when watch59// finishes walking the tree to add listeners. We don't need to know60// about it, so we simply ignore it (anything different means that61// some file changed/was removed/created - that's what we want to know).62//63if (watchFilter.call(monitor, f)) {64monitor.emit('watch:restart', { file: f, stat: curr });65monitor.restart();66}67}68});69};7071