-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathreload.js
125 lines (113 loc) · 2.99 KB
/
reload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*!
* Cluster - reload
* Copyright (c) 2011 LearnBoost <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var fs = require('fs')
, basename = require('path').basename
, extname = require('path').extname;
/**
* Restart the server the given js `files` have changed.
* `files` may be several directories, filenames, etc,
* defaulting to the server's root directory.
*
* Options:
*
* - `signal` Signal to send, defaults to __SIGTERM__
* - `interval` Watcher interval, defaulting to `100`
* - `extensions` File extensions to watch, defaults to ['.js']
*
* Examples:
*
* cluster(server)
* .use(cluster.reload())
* .listen(3000);
*
* cluster(server)
* .use(cluster.reload('lib'))
* .listen(3000);
*
* cluster(server)
* .use(cluster.reload(['lib', 'tests', 'index.js']))
* .listen(3000);
*
* cluster(server)
* .use(cluster.reload('lib', { signal: 'SIGQUIT', interval: 60000 }))
* .listen(3000);
*
* cluster(server)
* .use(cluster.reload('lib', { extensions: ['.js', '.coffee'] }))
* .listen(3000);
*
* Ignore Directories:
*
* By default `reload()` will ignore the following directories:
*
* - node_modules
* - support
* - examples
* - test
* - bin
*
* Alter with `reload.ignoreDirectories`
*
* cluster.reload.ignoreDirectories.push('src');
*
* @param {String|Array} files
* @param {Options} options
* @return {Function}
* @api public
*/
exports = module.exports = function(files, options){
options = options || {};
// defaults
var sig = options.sig || 'SIGTERM'
, interval = options.interval || 100
, extensions = options.extensions || ['.js'];
return function(master){
var restarting;
if (!files) files = master.dir;
if (!Array.isArray(files)) files = [files];
files.forEach(traverse);
// traverse file if it is a directory
// otherwise setup the watcher
function traverse(file) {
file = master.resolve(file);
fs.stat(file, function(err, stat){
if (!err) {
if (stat.isDirectory()) {
if (~exports.ignoreDirectories.indexOf(basename(file))) return;
fs.readdir(file, function(err, files){
files.map(function(f){
return file + '/' + f;
}).forEach(traverse);
});
} else {
watch(file);
}
}
});
}
// watch file for changes
function watch(file) {
if (!~extensions.indexOf(extname(file))) return;
fs.watchFile(file, { interval: interval }, function(curr, prev){
if (restarting) return;
if (curr.mtime > prev.mtime) {
console.log(' \033[36mchanged\033[0m \033[90m- %s\033[0m', file);
master.restart(sig);
}
});
}
master.on('restarting', function(){
restarting = true;
});
}
};
/**
* Directories to ignore.
*/
exports.ignoreDirectories = ['node_modules', 'support', 'test', 'bin'];