forked from totaljs/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-raw.js
More file actions
299 lines (229 loc) · 6.18 KB
/
debug-raw.js
File metadata and controls
299 lines (229 loc) · 6.18 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// ===================================================
// IMPORTANT: only for development
// Total.js - framework for Node.js
// https://www.totaljs.com
// ===================================================
const fs = require('fs');
const options = {};
// options.ip = '127.0.0.1';
// options.port = parseInt(process.argv[2]);
// options.config = { name: 'Total.js' };
// options.https = { key: fs.readFileSync('keys/agent2-key.pem'), cert: fs.readFileSync('keys/agent2-cert.pem')};
// options.sleep = 3000;
// options.debugger = 40894;
const isDebugging = process.argv.indexOf('debugging') !== -1;
const directory = process.cwd();
const path = require('path');
const VERSION = '6.0';
const TIME = 2000;
const REG_FILES = /config\-debug|config\-release|config|versions|sitemap|dependencies|\.js|\.resource/i;
const REG_THEMES = /\/themes\//i;
const REG_THEMES_INDEX = /themes(\/|\\)?[a-z0-9_.-]+(\/|\\)?index\.js/i;
const REG_EXTENSION = /\.(js|resource|package)/i;
var first = process.argv.indexOf('restart') === -1;
process.on('uncaughtException', function(e) {
e.toString().indexOf('ESRCH') == -1 && console.log(e);
});
function debug() {
require('total.js');
var port = parseInt(process.argv[process.argv.length - 1]);
if (!isNaN(port)) {
if (!options)
options = {};
options.port = port;
}
if (port > 0 && !options.port)
options.port = port || 8000;
if (options.https)
return F.https('debug', options);
F.http('debug', options);
if (first)
F.emit('debug-start');
else
F.emit('debug-restart');
}
function app() {
const fork = require('child_process').fork;
const utils = require('total.js/utils');
const directories = [directory + '/controllers', directory + '/definitions', directory + '/isomorphic', directory + '/modules', directory + '/resources', directory + '/models', directory + '/source', directory + '/workers', directory + '/packages', directory + '/themes'];
const async = new utils.Async();
const prefix = '---------------------------------> ';
var files = {};
var force = false;
var changes = [];
var app = null;
var status = 0;
var pid = '';
var pidInterval = null;
var isLoaded = false;
var isSkip = false;
var pidIncrease;
var speed = TIME;
function onFilter(path, isDirectory) {
if (!isDirectory && REG_THEMES.test(path))
return REG_THEMES_INDEX.test(path);
return isDirectory ? true : REG_EXTENSION.test(path);
}
function onIncrease(clear) {
if (clear) {
clearTimeout(pidIncrease);
speed = TIME;
}
pidIncrease = setTimeout(function() {
speed += TIME;
if (speed > 4000)
speed = 4000;
onIncrease();
}, 120000);
}
function onComplete(f) {
fs.readdir(directory, function(err, arr) {
var length = arr.length;
for (var i = 0; i < length; i++) {
var name = arr[i];
name !== 'debug.js' && REG_FILES.test(name) && f.push(name);
}
length = f.length;
for (var i = 0; i < length; i++) {
var name = f[i];
if (files[name] === undefined)
files[name] = isLoaded ? 0 : null;
}
refresh();
});
}
function refresh() {
var filenames = Object.keys(files);
var length = filenames.length;
for (var i = 0; i < length; i++) {
var filename = filenames[i];
(function(filename) {
async.await(function(next) {
fs.stat(filename, function(err, stat) {
var stamp = '--- # --- [ ' + new Date().format('yyyy-MM-dd HH:mm:ss') + ' ] ';
if (err) {
delete files[filename];
changes.push(stamp.replace('#', 'REM') + prefix + filename.replace(directory, ''));
force = true;
} else {
var ticks = stat.mtime.getTime();
if (files[filename] != null && files[filename] !== ticks) {
changes.push(stamp.replace('#', files[filename] === 0 ? 'ADD' : 'UPD') + prefix + filename.replace(directory, ''));
force = true;
}
files[filename] = ticks;
}
next();
});
});
})(filename);
}
async.complete(function() {
isLoaded = true;
setTimeout(refresh_directory, speed);
onIncrease();
if (status !== 1 || !force)
return;
onIncrease(true);
restart();
var length = changes.length;
for (var i = 0; i < length; i++)
console.log(changes[i]);
changes = [];
force = false;
});
}
function refresh_directory() {
utils.ls(directories, onComplete, onFilter);
}
function restart() {
if (app !== null) {
try
{
isSkip = true;
process.kill(app.pid);
} catch (err) {}
app = null;
}
var arr = process.argv;
var port = arr.pop();
if (process.execArgv.indexOf('--debug') !== -1) {
var key = '--debug=' + (options.debugger || 40894);
process.execArgv.indexOf(key) === -1 && process.execArgv.push(key);
}
if (first)
first = false;
else
arr.push('restart');
arr.push('debugging');
arr.push(port);
app = fork(path.join(directory, 'debug.js'), arr);
app.on('message', function(msg) {
msg === 'eaddrinuse' && process.exit(1);
});
app.on('exit', function() {
// checks unexpected exit
if (isSkip === false) {
app = null;
process.exit();
return;
}
isSkip = false;
if (status === 255)
app = null;
});
status === 0 && app.send('debugging');
status = 1;
}
process.on('SIGTERM', end);
process.on('SIGINT', end);
process.on('exit', end);
function end() {
if (arguments.callee.isEnd)
return;
arguments.callee.isEnd = true;
fs.unlink(pid, noop);
if (app === null) {
process.exit(0);
return;
}
isSkip = true;
process.kill(app.pid);
app = null;
process.exit(0);
}
function noop() {}
if (process.pid > 0) {
console.log(prefix + 'PID: ' + process.pid + ' (v' + VERSION + ')');
pid = path.join(directory, 'debug.pid');
fs.writeFileSync(pid, process.pid);
pidInterval = setInterval(function() {
fs.exists(pid, function(e) {
if (e)
return;
fs.unlink(pid, noop);
if (app !== null) {
isSkip = true;
process.kill(app.pid);
}
process.exit(0);
});
}, 2000);
}
restart();
refresh_directory();
}
function run() {
if (isDebugging) {
debug();
return;
}
var filename = path.join(directory, 'debug.pid');
if (!fs.existsSync(filename)) {
app();
return;
}
fs.unlinkSync(filename);
setTimeout(function() { app() }, 3000);
}
run();