-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwatcher.js
More file actions
107 lines (82 loc) · 2.64 KB
/
Copy pathwatcher.js
File metadata and controls
107 lines (82 loc) · 2.64 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
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
var fs = require('fs'),
config = require('./config'),
configFolderPath = process.cwd(),
vm = require('vm'),
path = require('path');
function reverseFile (dirname) {
var dirname = dirname || configFolderPath;
fs.readdir(dirname, function (err, files) {
files.forEach(function (file, idx) {
var fullpath = path.join(dirname, file),
executeFlag = true;
fs.stat(fullpath, function (err, stats) {
if (stats.isDirectory()) {
reverseFile(fullpath);
}
else {
config.excludeFile.forEach(function (fp, idx) {
if (fullpath.match(fp)) {
executeFlag = false;
return;
}
});
if (!executeFlag) {
return;
}
config.includeFile.forEach(function (fp, idx) {
if (fullpath.match(fp)) {
watchFile(fullpath);
return;
}
});
}
});
});
});
}
function watchFile(fullpath) {
fs.watchFile(fullpath, {presistent: true,interval:0}, function (curr, prev) {
if (+curr.mtime !== +prev.mtime) {
console.log(fullpath + ': File modified');
module.exports.fire('update', {
fullpath: fullpath,
curr: curr,
prev: prev
});
}
});
}
/**
* Set export instances, and set setter.
*/
module.exports.callbacks = [];
module.exports.on = function (event, cb) {
if (this.callbacks[event]) {
console.log('this event is register');
return;
}
this.callbacks[event] = cb;
}
module.exports.fire = function (event, obj) {
if (this.callbacks[event]) {
this.callbacks[event].call(this, obj);
}
};
module.exports.setConfig = function (path) {
configFolderPath = path || configFolderPath;
console.log('Setup => Loading Path :' + configFolderPath);
var configFile = 'config.js',
encode = 'utf8',
src;
try {
src = fs.readFileSync(configFolderPath + '/' + configFile, encode);
vm.runInNewContext(config, src, configFile);
} catch (e) {
}
};
module.exports.run = function () {
console.log('Start Watching path is :' + configFolderPath);
reverseFile(configFolderPath);
};
module.exports.config = config;
module.exports.folderPath = configFolderPath;