-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwatchFile.js
More file actions
executable file
·51 lines (40 loc) · 1.22 KB
/
watchFile.js
File metadata and controls
executable file
·51 lines (40 loc) · 1.22 KB
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
var fs = require('fs') ,
path = require("path");
var isWindows = process.platform === 'win32';
function listenToChange (file ){
file = path.resolve(file)
function onChg(prev,now) {
if (prev.mtime == now.mtime ) return;
delete require.cache[file] ;
}
if (isWindows)
fs.watch(file ,{ persistent: true, interval: 10 } , onChg);
else
fs.watchFile(file ,{ persistent: true, interval: 10 } , onChg);
}
function mapDir (dir ,ext) {
fs.readdir (dir , function(err , files){
if (err) return;
if (ext && !files.indexOf(ext) ) return;
files.forEach(function(file){
file = dir + '/' + file;
fs.lstat ( file , function (err, stats) {
if (err) return;
if (stats.isDirectory() ) {
mapDir(file , ext);
}else if (stats.isFile() ){
listenToChange(file);
}
} );
});
});
}
/*
'somedir' | ['somedir' , 'another dir']
*/
exports.takeCare = function( dir , ext){
if ('string' == typeof dir) dir = [dir];
dir.forEach(function(dirItem){
mapDir (dirItem , ext);
})
}