-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhot-reload.js
60 lines (51 loc) · 1.45 KB
/
hot-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
const fs = require('fs');
const {spawn} = require('child_process');
const ignore = {
directory: ['node_modules', 'public', '.git', '.idea', 'scripts', 'tests', 'upload'],
extensions: [],
files: ['hot-reload.js']
};
const watch = ['.js'];
let node = start();
function recurse(path) {
fs.readdir(path, function (err, files) {
for (let f in files) {
const file = files[f];
if (fs.lstatSync(path + file).isDirectory()) {
if (!ignore.directory.includes(file)) recurse(path + file + '/');
} else {
const extension = file.substring(file.indexOf('.'));
if (watch.includes(extension) && !ignore.files.includes(file)) {
fs.watchFile(path + file, {interval: 1000}, () => {
reload();
});
}
}
}
});
}
recurse('./');
function reload() {
if (node) {
// noinspection JSCheckFunctionSignatures
node.kill('SIGKILL');
}
node = null;
setTimeout(function () {
if (!node) node = start();
}, 1000);
}
function start() {
const process = spawn('node', ['index.js']);
process.stdout.on('data', (data) => {
console.log(data.toString())
});
process.stderr.on('data', (data) => {
console.log(data.toString())
});
process.on('error', () => {
});
process.on('close', () => {
});
return process;
}