Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
/*
2
* logger.js: Plugin for `Monitor` instances which adds file watching.
3
*
4
* (C) 2010 Nodejitsu Inc.
5
* MIT LICENCE
6
*
7
*/
8
9
var fs = require('fs'),
10
path = require('path'),
11
minimatch = require('minimatch'),
12
watch = require('watch');
13
14
exports.name = 'watch';
15
16
//
17
// ### @private function _watchFilter
18
// #### @file {string} File name
19
// Determines whether we should restart if `file` change (@mikeal's filtering
20
// is pretty messed up).
21
//
22
function watchFilter(fileName) {
23
if (this.watchIgnoreDotFiles && path.basename(fileName)[0] === '.') {
24
return false;
25
}
26
27
for (var key in this.watchIgnorePatterns) {
28
if (minimatch(fileName, this.watchIgnorePatterns[key], { matchBase: this.watchDirectory })) {
29
return false;
30
}
31
}
32
33
return true;
34
};
35
36
//
37
// ### function attach (options)
38
// #### @options {Object} Options for attaching to `Monitor`
39
//
40
// Attaches functionality for logging stdout and stderr to `Monitor` instances.
41
//
42
exports.attach = function () {
43
var monitor = this;
44
45
fs.readFile(path.join(this.watchDirectory, '.foreverignore'), 'utf8', function (err, data) {
46
if (err) {
47
return monitor.emit('watch:error', {
48
message: 'Could not read .foreverignore file.',
49
error: err.message
50
});
51
}
52
53
Array.prototype.push.apply(monitor.watchIgnorePatterns, data.split('\n'));
54
});
55
56
watch.watchTree(this.watchDirectory, function (f, curr, prev) {
57
if (!(curr === null && prev === null && typeof f === 'object')) {
58
//
59
// `curr` == null && `prev` == null && typeof f == "object" when watch
60
// finishes walking the tree to add listeners. We don't need to know
61
// about it, so we simply ignore it (anything different means that
62
// some file changed/was removed/created - that's what we want to know).
63
//
64
if (watchFilter.call(monitor, f)) {
65
monitor.emit('watch:restart', { file: f, stat: curr });
66
monitor.restart();
67
}
68
}
69
});
70
};
71