-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrex.js
54 lines (42 loc) · 1.15 KB
/
drex.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
var fs = require('fs');
var path = require('path');
var mc = path.join(__dirname, 'module_cache');
var watchers = {};
if (fs.existsSync(mc) == false)
{
fs.mkdirSync(mc);
}
exports.require = function(modname, cb)
{
if (!watchers[modname])
{
var opts = {};
opts.persistent = true;
opts.interval = 2190;
fs.watchFile(modname, opts, function (curr, prev)
{
console.log('the current mtime is: ' + curr.mtime);
console.log('the previous mtime was: ' + prev.mtime);
createModule(modname, function(mnm)
{
watchers[modname] = mnm;
cb(require(mnm), mnm);
})
});
watchers[modname] = modname;
cb(require(watchers[modname]), watchers[modname]);
}
else
{
cb(require(watchers[modname]), watchers[modname]);
}
}
var createModule = function(modname, cb)
{
var dt = new Date();
var mnm = path.join(mc,'_mod' + '_' + dt.getTime() + '.js');
console.log('copying %s to %s', modname, mnm);
var content = fs.readFileSync(modname);
fs.writeFileSync(mnm, content);
cb(mnm);
}