-
Notifications
You must be signed in to change notification settings - Fork 140
/
debug.js
204 lines (174 loc) · 7.65 KB
/
debug.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
var fs = require('fs'),
options = {};
options.ip = process.env.IP || '127.0.0.1';
options.port = process.env.PORT || 8000;
// 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;
function debug() {
var framework = require("total.js"),
port = parseInt(process.argv[process.argv.length - 1]);
return isNaN(port) || (options || (options = {}), options.port = port), port > 0 && !options.port && (options.port = port || 8e3), options.https ? framework.https("debug", options) : (framework.http("debug", options), void(first ? framework.emit("debug-start") : framework.emit("debug-restart")))
}
function app() {
function onFilter(path, isDirectory) {
return !isDirectory && path.match(/\/themes\//i) ? path.match(/themes(\/|\\)?[a-z0-9_.-]+(\/|\\)?index\.js/gi) ? !0 : !1 : isDirectory ? !0 : null !== path.match(/\.(js|resource|package)/i)
}
function onIncrease(clear) {
clear && (clearTimeout(pidIncrease), speed = TIME), pidIncrease = setTimeout(function () {
speed += TIME, speed > 4e3 && (speed = 4e3), onIncrease()
}, 12e4)
}
function onComplete(f) {
fs.readdir(directory, function (err, arr) {
for (var length = arr.length, i = 0; length > i; i++) {
var name = arr[i];
"debug.js" !== name && name.match(/config\-debug|config\-release|config|versions|sitemap|dependencies|\.js|\.resource/i) && f.push(name)
}
length = f.length;
for (var i = 0; length > i; i++) {
var name = f[i];
files[name] || (files[name] = isLoaded ? 0 : null)
}
refresh()
})
}
function refresh() {
for (var filenames = Object.keys(files), length = filenames.length, i = 0; length > i; i++) {
var filename = filenames[i];
!function (filename) {
async.await(function (next) {
fs.stat(filename, function (err, stat) {
if (err)
delete files[filename], changes.push(prefix + filename.replace(directory, "") + " (removed)"), force = !0;
else {
var ticks = stat.mtime.getTime();
null !== files[filename] && files[filename] !== ticks && (changes.push(prefix + filename.replace(directory, "") + (0 === files[filename] ? " (added)" : " (modified)")), force = !0), files[filename] = ticks
}
next()
})
})
}(filename)
}
async.complete(function () {
if (isLoaded = !0, setTimeout(refresh_directory, speed), onIncrease(), 1 === status && force) {
onIncrease(!0), restart();
for (var length = changes.length, i = 0; length > i; i++)
console.log(changes[i]);
changes = [], force = !1
}
})
}
function refresh_directory() {
utils.ls(directories, onComplete, onFilter)
}
function restart() {
if (null !== app) {
try {
isSkip = !0, process.kill(app.pid)
}
catch (err) {
}
app = null
}
var arr = process.argv,
port = arr.pop();
if (-1 !== process.execArgv.indexOf("--debug")) {
var key = "--debug=" + (options["debugger"] || 40894);
-
1 === process.execArgv.indexOf(key) && process.execArgv.push(key)
}
first ? first = !1 : arr.push("restart"), arr.push("debugging"), arr.push(port), app = fork(path.join(directory, "debug.js"), arr), app.on("message", function (msg) {
"eaddrinuse" === msg && process.exit(1)
}), app.on("exit", function () {
return isSkip === !1 ? (app = null, void process.exit()) : (isSkip = !1, void(255 === status && (app = null)))
}), 0 === status && app.send("debugging"), status = 1
}
function end() {
if (!arguments.callee.isEnd) {
if (arguments.callee.isEnd = !0, fs.unlink(pid, noop), null === app)
return void process.exit(0);
isSkip = !0, process.kill(app.pid), app = null, process.exit(0)
}
}
function noop() {
}
var pidIncrease, fork = require("child_process").fork,
utils = require("total.js/utils"),
directories = [directory + "/controllers", directory + "/definitions", directory + "/isomorphic", directory + "/modules", directory + "/resources", directory + "/models", directory + "/source", directory + "/workers", directory + "/packages", directory + "/themes"],
files = {},
force = !1,
changes = [],
app = null,
status = 0,
async = new utils.Async,
pid = "",
pidInterval = null,
prefix = "----------------------------------------------------> ",
isLoaded = !1,
isSkip = !1,
speed = TIME;
process.on("SIGTERM", end), process.on("SIGINT", end), process.on("exit", end), 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 (exist) {
exist || (fs.unlink(pid, noop), null !== app && (isSkip = !0, process.kill(app.pid)), process.exit(0))
})
}, 2e3)), restart(), refresh_directory()
}
function run() {
if (isDebugging)
return void debug(); //install(debug) if needed
var filename = path.join(directory, "debug.pid");
return fs.existsSync(filename) ? (fs.unlinkSync(filename), void setTimeout(function () {
app()
}, 3e3)) : void app()
}
/*function install(done) {
console.log("Install new TM modules");
var dir = __dirname + '/install';
fs.stat(dir, function (err, stats) {
if (err)
return console.log(err);
fs.readdirSync(dir).forEach(function (file) {
//Test directory !
if (!fs.statSync(dir + '/' + file).isDirectory())
return false;
// Test exist file package.json on directory
try {
if (!fs.statSync(dir + '/' + file + '/package.json'))
return false;
}
catch (e) {
return false;
}
// Test exist gulp file installer
try {
if (!fs.statSync(dir + '/' + file + '/gulpfile.js'))
return false;
}
catch (e) {
return false;
}
//var gulp = require('gulp').parse(dir + '/' + file + '/gulpfile.js');
var gulp = require('gulp');
require(dir + '/' + file + '/gulpfile');
if (gulp.tasks.install) {
console.log(dir + '/gulpfile.js contains install task!');
gulp.start('install');
}
//var package = require(path + '/' + file + '/package.json');
//return console.log(package);
});
done();
});
}
*/
var isDebugging = -1 !== process.argv.indexOf("debugging"),
directory = process.cwd(),
path = require("path"),
first = -1 === process.argv.indexOf("restart"),
VERSION = "3.0",
TIME = 2e3;
process.on("uncaughtException", function (e) {
-1 === e.toString().indexOf("ESRCH") && console.log(e)
}), run();