forked from c9/cloud9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
220 lines (185 loc) · 7.18 KB
/
Copy pathindex.js
File metadata and controls
220 lines (185 loc) · 7.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
/**
* Debugger Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
var Path = require("path"),
Spawn = require("child_process").spawn,
NodeDebugProxy = require("./nodedebugproxy"),
ChromeDebugProxy = require("./chromedebugproxy"),
Plugin = require("cloud9/plugin"),
sys = require("sys"),
netutil = require("cloud9/netutil");
var DebuggerPlugin = module.exports = function(ide) {
this.ide = ide;
this.hooks = ["command"];
this.name = "debugger";
};
sys.inherits(DebuggerPlugin, Plugin);
(function() {
this.init = function() {
var _self = this;
this.ide.getExt("state").on("statechange", function(state) {
state.debugClient = !!_self.debugClient;
state.processRunning = !!_self.child;
});
};
this.NODE_DEBUG_PORT = 5858;
this.CHROME_DEBUG_PORT = 9222;
this.command = function(user, message, client) {
if (!(/js/.test(message.runner)))
return false;
var _self = this;
var cmd = (message.command || "").toLowerCase(),
res = true;
switch (cmd) {
case "run":
this.$run(message, client);
break;
case "rundebug":
netutil.findFreePort(this.NODE_DEBUG_PORT, "localhost", function(port) {
_self.NODE_DEBUG_PORT = port;
message.preArgs = ["--debug=" + _self.NODE_DEBUG_PORT];
message.debug = true;
_self.$run(message, client);
setTimeout(function() {
_self.$startDebug();
}, 100);
});
break;
case "rundebugbrk":
netutil.findFreePort(this.NODE_DEBUG_PORT, "localhost", function(port) {
_self.NODE_DEBUG_PORT = port;
message.preArgs = ["--debug-brk=" + _self.NODE_DEBUG_PORT];
message.debug = true;
_self.$run(message, client);
setTimeout(function() {
_self.$startDebug();
}, 100);
});
break;
case "rundebugchrome":
if (this.chromeDebugProxy) {
this.ide.error("Chrome debugger already running!", 7, message);
break;
}
this.chromeDebugProxy = new ChromeDebugProxy(this.CHROME_DEBUG_PORT);
this.chromeDebugProxy.connect();
this.chromeDebugProxy.addEventListener("connection", function() {
_self.ide.broadcast('{"type": "chrome-debug-ready"}', _self.name);
});
break;
case "debugnode":
if (!this.nodeDebugProxy)
this.ide.error("No debug session running!", 6, message);
else
this.nodeDebugProxy.send(message.body);
break;
case "debugattachnode":
if (this.nodeDebugProxy)
this.ide.broadcast('{"type": "node-debug-ready"}', _self.name);
break;
case "kill":
this.$kill();
break;
default:
res = false;
break;
}
return res;
};
this.$kill = function() {
var child = this.child;
if (!child)
return;
try {
child.kill();
// check after 2sec if the process is really dead
// If not kill it harder
setTimeout(function() {
if (child.pid > 0)
child.kill("SIGKILL");
}, 2000)
}
catch(e) {}
};
this.$run = function(message, client) {
var _self = this;
if (this.child)
return _self.ide.error("Child process already running!", 1, message);
var file = _self.ide.workspaceDir + "/" + message.file;
Path.exists(file, function(exists) {
if (!exists)
return _self.ide.error("File does not exist: " + message.file, 2, message);
var cwd = _self.ide.workspaceDir + "/" + (message.cwd || "");
Path.exists(cwd, function(exists) {
if (!exists)
return _self.ide.error("cwd does not exist: " + message.cwd, 3, message);
// lets check what we need to run
var args = (message.preArgs || []).concat(file).concat(message.args || []);
_self.$runProc(_self.ide.nodeCmd, args, cwd, message.env || {}, message.debug || false);
});
});
};
this.$runProc = function(proc, args, cwd, env, debug) {
var _self = this;
// mixin process env
for (var key in process.env) {
if (!(key in env))
env[key] = process.env[key];
}
console.log("Executing node "+proc+" "+args.join(" ")+" "+cwd);
var child = _self.child = Spawn(proc, args, {cwd: cwd, env: env});
_self.debugClient = args.join(" ").search(/(?:^|\b)\-\-debug\b/) != -1;
_self.ide.getExt("state").publishState();
_self.ide.broadcast(JSON.stringify({"type": "node-start"}), _self.name);
child.stdout.on("data", sender("stdout"));
child.stderr.on("data", sender("stderr"));
function sender(stream) {
return function(data) {
var message = {
"type": "node-data",
"stream": stream,
"data": data.toString("utf8")
};
_self.ide.broadcast(JSON.stringify(message), _self.name);
};
}
child.on("exit", function(code) {
_self.ide.broadcast(JSON.stringify({"type": "node-exit"}), _self.name);
_self.debugClient = false;
delete _self.child;
delete _self.nodeDebugProxy;
});
return child;
};
this.$startDebug = function(message) {
var _self = this;
if (!this.debugClient)
return this.ide.error("No debuggable application running", 4, message);
if (this.nodeDebugProxy)
return this.ide.error("Debug session already running", 5, message);
this.nodeDebugProxy = new NodeDebugProxy(this.NODE_DEBUG_PORT);
this.nodeDebugProxy.on("message", function(body) {
var msg = {
"type": "node-debug",
"body": body
};
_self.ide.broadcast(JSON.stringify(msg), _self.name);
});
this.nodeDebugProxy.on("connection", function() {
_self.ide.broadcast('{"type": "node-debug-ready"}', _self.name);
});
this.nodeDebugProxy.on("end", function() {
if (_self.nodeDebugProxy == this) {
delete _self.nodeDebugProxy;
}
});
this.nodeDebugProxy.connect();
};
this.dispose = function(callback) {
this.$kill();
callback();
};
}).call(DebuggerPlugin.prototype);