-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (46 loc) · 1.45 KB
/
index.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
//hotreloader.js
'use strict';
const debug = require('debug')('hotreloader');
const chokidar=require('chokidar');
const path=require('path');
const watch = new chokidar.FSWatcher({ ignored: /[\/\\]\./, persistent: true,awaitWriteFinish:{stabilityThreshold:2000,pollInterval:100} });
var methods = {};
function reload(required) {
var modulePath = path.resolve(required);
watch.add(modulePath);
load(modulePath);
return methods[modulePath];
}
function swap(modulePath) {
var moduleCache = require.cache[modulePath];
var exportKeys = Object.keys(moduleCache.exports);
for (var i = 0; i < exportKeys.length; i++) {
var exportKey = exportKeys[i];
moduleCache.exports[exportKey] = null
delete moduleCache.exports[exportKey];
}
var tmpModulePath = modulePath + '.tmp';
require.cache[tmpModulePath] = require.cache[modulePath];
delete require.cache[modulePath];
load(modulePath);
require.cache[modulePath] = require.cache[tmpModulePath];
delete require.cache[tmpModulePath];
var methodKeys = Object.keys(methods[modulePath]);
for (var i = 0; i < methodKeys.length; i++) {
var methodKey = methodKeys[i];
moduleCache.exports[methodKey] = methods[modulePath][methodKey];
}
}
function load(modulePath) {
methods[modulePath] = require(modulePath);
}
watch.on('change', swap);
module.exports = reload;
watch.on('add', p => {
debug('On added: ', p)
})
process.on('SIGINT',() => {
watch.close();
debug('Closing watch process.');
process.exit(0)
})