-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoderunner.js
More file actions
205 lines (173 loc) · 6.43 KB
/
noderunner.js
File metadata and controls
205 lines (173 loc) · 6.43 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
/**
* Node Runner Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
var ide = require("core/ide");
var ext = require("core/ext");
var settings = require("core/settings");
var markup = require("text!ext/noderunner/noderunner.xml");
var c9console = require("ext/console/console");
var _debugger = require("ext/debugger/debugger");
/*global stProcessRunning*/
module.exports = ext.register("ext/noderunner/noderunner", {
name : "Node Runner",
dev : "Ajax.org",
type : ext.GENERAL,
alone : true,
offline : false,
autodisable : ext.ONLINE | ext.LOCAL,
markup : markup,
NODE_VERSION: "auto",
init : function() {
var _self = this;
if (ide.connected) {
this.queryServerState();
ide.addEventListener("socketDisconnect", function() {
ide.dispatchEvent("dbg.exit");
});
} else {
ide.addEventListener("socketConnect", function() {
_self.queryServerState();
});
}
ide.addEventListener("socketMessage", this.onMessage.bind(this));
ide.addEventListener("consolecommand.run", function(e) {
ide.send({
command: "internal-isfile",
argv: e.data.argv,
cwd: e.data.cwd,
sender: "noderunner"
});
return false;
});
this.nodePid = null;
ide.addEventListener("settings.load", function(e){
_self.NODE_VERSION = e.model.queryValue("auto/node-version/@version") || "auto";
});
},
onMessage : function(e) {
var message = e.message;
//if (message.type != "shell-data")
// console.log("MSG", message)
var runners = window.cloud9config.runners;
var lang;
if ((lang = /^(\w+)-debug-ready$/.exec(message.type)) && runners.indexOf(lang[1]) >= 0) {
ide.dispatchEvent("dbg.ready", message);
return;
}
else if ((lang = /^(\w+)-exit$/.exec(message.type)) && runners.indexOf(lang[1]) >= 0) {
ide.dispatchEvent("dbg.exit", message);
if (message.pid == this.nodePid) {
stProcessRunning.deactivate();
this.nodePid = 0;
}
return;
}
switch(message.type) {
case "state":
this.nodePid = message.processRunning || 0;
stProcessRunning.setProperty("active", !!message.processRunning);
ide.dispatchEvent("dbg.state", message);
break;
case "error":
// child process already running
if (message.code == 1) {
stProcessRunning.setProperty("active", true);
}
// debug process already running
else if (message.code == 5) {
stProcessRunning.setProperty("active", true);
}
/*
6:
401: Authorization Required
*/
// Command error
else if (message.code === 9) {
c9console.log("<div class='item console_log' style='font-weight:bold;color:yellow'>"
+ apf.escapeXML(message.message) + "</div>");
}
else if (message.code !== 6 && message.code != 401 && message.code != 455 && message.code != 456) {
c9console.log("<div class='item console_log' style='font-weight:bold;color:#ff0000'>[C9 Server Exception "
+ apf.escapeXML(message.code || "") + "] " + apf.escapeXML(message.message) + "</div>");
apf.ajax("/api/debug", {
method : "POST",
contentType : "application/json",
data : JSON.stringify({
agent : navigator.userAgent,
type : "C9 SERVER EXCEPTION",
code : e.code,
message : e.message
})
});
}
this.queryServerState();
break;
}
},
queryServerState : function() {
// load the state, which is quite a weird name actually, but it contains
// info about the debugger. The response is handled by 'noderunner.js'
// who publishes info for the UI of the debugging controls based on this.
ide.send({command: "state", action: "publish" });
},
debug : function() {
},
run : function(path, args, debug, nodeVersion) {
var runner;
if (stProcessRunning.active || typeof path != "string")
return false;
// TODO there should be a way to set satate to waiting
stProcessRunning.activate();
path = path.trim();
if (nodeVersion == 'default' || !nodeVersion) {
runner = this.detectRunner(path);
nodeVersion = runner == 'node' ? settings.model.queryValue("auto/node-version/@version") || this.NODE_VERSION : 'auto';
}
else {
runner = nodeVersion.split(" ")[0];
nodeVersion = nodeVersion.split(" ")[1] || 'auto';
}
var page = ide.getActivePageModel();
var command = {
"command" : apf.isTrue(debug) ? "RunDebugBrk" : "Run",
"file" : path.replace(/^\/+/, ""),
"runner" : runner,
"args" : args || [],
"version" : nodeVersion,
"env" : {
"C9_SELECTED_FILE": page ? page.getAttribute("path").slice(ide.davPrefix.length) : ""
}
};
ide.send(command);
},
stop : function() {
if (!stProcessRunning.active)
return;
ide.send({
"command": "kill",
"runner" : "node",
"pid" : this.nodePid
});
ide.send({"command": "state", "action": "publish"});
},
enable : function(){
},
disable : function(){
},
destroy : function(){
},
detectRunner: function(path) {
if (path.match(/\.(php|phtml)$/))
return "apache";
if (path.match(/\.py$/))
return "python";
if (path.match(/\.rb$/))
return "ruby";
return "node";
}
});
});