-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
228 lines (190 loc) 路 3.9 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env node
let fs = require('fs');
let dir = require('node-dir');
let spawn = require('child_process').spawn;
let cluster = require('cluster');
//
// 1. Extract the name of the app/file name.
//
const cli_name = process.argv[1].split('/').pop();
//
// 2. Save the first provided argument as the server to run
//
const server_to_run = process.argv[2];
//
// 3. Check if the server file name was provided
//
if(!server_to_run)
{
//
// 1. Give the user an example how to use the app.
//
console.log("\n\tMissing argument! \n\n\tExample: ./%s FILE_NAME \n", cli_name);
//
// -> Exit the app if error.
//
process.exit(1);
}
//
// Depending which process is the master we do different thing
//
if(cluster.isMaster)
{
//
// 1. Let the user know we are starting.
//
console.log("Starting");
//
// 2. For a child.
//
cluster.fork();
//
// @. Listen for the exit message so we can restart ourself.
//
cluster.on('exit', function (worker) {
//
// 1. When the previous child exited, then we recreate it
// so we can restart the NodeJS server.
//
cluster.fork();
//
// 2. Let the user know what happened.
//
console.log("Restarting");
});
}
else
{
//
// 1. Run the server in a child process
//
let node = spawn("node", ['./' + server_to_run]);
//
// 2. Pass the server logs
//
node.stdout.on('data', function(data) {
console.log(`stdout: ${data}`);
});
//
// 3. Pass the error logs
//
node.stderr.on('data', function(data) {
console.log(`stdout: ${data}`);
});
//
// 4. The array that will store all the file to check
//
let files = []
//
// 5. Get all the file from the folder
//
dir.files(process.cwd(), function(err, paths) {
//
// 1. Make sure there was no error
//
if(err)
{
throw err;
}
//
// 2. Loop over each found file, and add its path and time stamp
// to the array for later monitoring
//
paths.forEach(function(file) {
//
// 1. dir to avoid
//
let patern = /node_modules/;
//
// 2. Skip unwanted dirs.
//
if(!patern.test(file))
{
//
// 1. Add each individual file to the array with the time
// when it was changed.
//
files.push({
file: file,
time: fs.statSync(file).mtime
});
}
});
//
// 3. Start the main loop
//
looper(node, files);
});
}
// _ _ ______ _ _____ ______ _____ _____
// | | | | ____| | | __ \| ____| __ \ / ____|
// | |__| | |__ | | | |__) | |__ | |__) | (___
// | __ | __| | | | ___/| __| | _ / \___ \
// | | | | |____| |____| | | |____| | \ \ ____) |
// |_| |_|______|______|_| |______|_| \_\_____/
//
//
// The main loop that will keep checking all the files
//
function looper(node, files)
{
//
// 1. The timer that acts as the loop
//
setTimeout(function() {
//
// 1. Loop over each file until we find a file that was changed.
//
for(let key in files)
{
//
// 1. Save the state of the check
//
let state = check(files[key]);
//
// 2. If the file was changed then we stop looping since
// we already found our suspect ;) and this is enough
// for us to restart the process.
//
if(state)
{
//
// 1. Kill the node process that we started
//
process.kill(node.pid);
//
// 2. Kill this very process so we can restart the server
//
process.exit();
}
}
//
// 2. Restart the loop if no file was changed
//
looper(node, files);
}, 4000);
}
//
// Function to get the edit time for an individual file
//
function check(object)
{
//
// 1. Get file statistic
//
let file_stats = fs.statSync(object.file);
//
// 2. Grate a data object out of the file time
//
let file_time = new Date(file_stats.mtime);
//
// 3. Compare the old time with the new one
//
if(object.time.getTime() !== file_time.getTime())
{
//
// -> Return true when the time doesn't match
//
return true;
}
}