Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathdebug.js
More file actions
Latest commit
executable file
·203 lines (184 loc) · 6.24 KB
/
Copy pathdebug.js
File metadata and controls
executable file
·203 lines (184 loc) · 6.24 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
varpackages=[
'nodee-total',
'nodee-admin',
'nodee-cms'
];
/*
* Debug configs
*/
varchildProcessDebuggerPort=5859,
watchDirectories=['/controllers','/definitions','/modules','/resources','/components','/models','/source'],
watchExtensions=['.js','.resource','.html'],
watchFiles=['config','config-debug','config-release','versions'];
/*
* DEBUG setup:
*/
varisDebugging=process.argv[process.argv.length-1]==='debugging';
vardirectory=process.cwd();
varpath=require('path');
varfs=require('fs');
// download all packages and start app
if(!isDebugging)require('./packages.js').downloadPackages(packages,function(){
require('total.js');// init total.js
run();// run app in debug mode with file watchers
console.warn('DOWNLOADING');
});
else{
require('total.js');// init total.js
run();// run app in debug mode with file watchers
}
functiondebug(){
varoptions={};
varframework=require('total.js');
varport=parseInt(process.argv[2]);
if(options.https)returnframework.https('debug',options);
framework.http('debug',options);
}
functionapp(){
varfork=require('child_process').fork;
vardirectories=[];
varfiles={};
varforce=false;
varchanges=[];
varapp=null;
varstatus=0;
varasync=newutils.Async;
varpid='';
varpidInterval=null;
varprefix='------------> ';
varisLoaded=false;
for(vari=0;i<watchDirectories.length;i++){
directories.push(directory+watchDirectories[i]);
}
functiononFilter(path,isDirectory){
returnisDirectory||isWatchedFile(path);
}
functionisWatchedFile(path){
for(vari=0;i<watchExtensions.length;i++){
if(path.indexOf(watchExtensions[i])!==-1)returntrue;
}
returnfalse;
}
functiononComplete(){
varself=this;
fs.readdir(directory,function(err,arr){
varlength=arr.length;
for(vari=0;i<length;i++){
varname=arr[i];
if(watchFiles.indexOf(name)!==-1||isWatchedFile(name))self.file.push(name)
}
length=self.file.length;
for(vari=0;i<length;i++){
varname=self.file[i];
if(!files[name])files[name]=isLoaded ? 0 : null
}
refresh()
})
}
functionrefresh(){
varfilenames=Object.keys(files);
varlength=filenames.length;
for(vari=0;i<length;i++){
varfilename=filenames[i];
(function(filename){
async.await(function(next){
fs.stat(filename,function(err,stat){
if(!err){
varticks=stat.mtime.getTime();
if(files[filename]!==null&&files[filename]!==ticks){
changes.push(prefix+filename.replace(directory,"")+(files[filename]===0 ? " (added)" : " (modified)"));
force=true
}
files[filename]=ticks
}else{
deletefiles[filename];
changes.push(prefix+filename.replace(directory,"")+" (removed)");
force=true
}
next()
})
})
})(filename)
}
async.complete(function(){
isLoaded=true;
setTimeout(refresh_directory,2e3);
if(status!==1)return;
if(!force)return;
restart();
varlength=changes.length;
for(vari=0;i<length;i++)console.log(changes[i]);
changes=[];
force=false
})
}
functionrefresh_directory(){
utils.ls(directories,onComplete,onFilter)
}
functionrestart(){
if(!app)returnstart();
try{
app.on('exit',start);
process.kill(app.pid);
}
catch(err){}
}
functionstart(){
vararr=process.argv;
arr.pop();
arr.push('debugging');
if(childProcessDebuggerPort)app=fork(path.join(directory,'debug.js'),arr,{execArgv: ['--debug='+childProcessDebuggerPort]});
elseapp=fork(path.join(directory,'debug.js'),arr);
app.on('message',function(msg){
if(msg.substring(0,5)==='name:'){
process.title='debug: '+msg.substring(6);
return;
}
if(msg==='eaddrinuse')process.exit(1);
});
app.on('exit',function(){
if(status!==255)return;
app=null;
});
if(status===0)app.send('debugging');
status=1;
}
process.on('SIGTERM',end);
process.on('SIGINT',end);
process.on('exit',end);
functionend(){
if(arguments.callee.isEnd)return;
arguments.callee.isEnd=true;
fs.unlink(pid,noop);
if(app===null){
process.exit(0);
return
}
process.kill(app.pid);
app=null;
process.exit(0)
}
functionnoop(){}
if(process.pid>0){
console.log(prefix+'PID: '+process.pid);
pid=path.join(directory,'debug.pid');
fs.writeFileSync(pid,process.pid);
pidInterval=setInterval(function(){
fs.exists(pid,function(exist){
if(exist)return;
fs.unlink(pid,noop);
if(app!==null)process.kill(app.pid);
process.exit(0);
})
},2e3);
}
restart();
refresh_directory();
}
functionrun(){
if(isDebugging)returndebug();
varfilename=path.join(directory,'debug.pid');
if(!fs.existsSync(filename))returnapp();
fs.unlinkSync(filename);
setTimeout(app,3e3);
}