/
index.js
102 lines (81 loc) · 1.89 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
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
var cp = require('child_process'),
fs = require('fs');
// SimpleR prototype constructor
simpler = function (path, args, callback) {
Object.defineProperties(this, {
args: {
value: [],
writable: true
},
callback: {
value: null,
writable: true
},
child: {
value: null,
writable: true
},
path: {
value: path
}
});
// Start the script reloader
this.start(args, callback);
};
// Start or restart the script reloading
simpler.prototype.start = function (args, callback) {
var that = this;
// Get the provided parameters
if (typeof args === 'function') {
callback = args;
args = [];
} else {
args = args || [];
}
this.args = args || this.args;
this.callback = callback || this.callback;
// If the child process exists then kill it
if (this.child) {
this.child.kill();
}
// Create the child process
this.child = cp.fork(this.path, this.args);
// Watch file changes and reload script
fs.watchFile(this.path, {
persistent: true
}, function (current, previous) {
// Kill the child process
that.child.kill();
// If the file is removed stop watching it
if (!current.nlink) {
fs.unwatchFile(that.path);
} else {
// Create a new child process
that.child = cp.fork(that.path, that.args);
// Use callback if it is defined
if (typeof that.callback === 'function') {
that.callback(that.child);
}
}
});
// Call the first time the callback function
if (typeof this.callback === 'function') {
this.callback(this.child);
}
return this;
};
// Stop the script reloading
simpler.prototype.stop = function () {
// Kill the child process only if it exists
if (this.child) {
this.child.kill();
this.child = null;
}
// Unwatch the file on the provided path
fs.unwatchFile(this.path);
return this;
};
// Export a new simpleR instance
module.exports = function (path, args, callback) {
return new simpler(path, args, callback);
};