-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-node.js
More file actions
163 lines (136 loc) · 5.09 KB
/
run-node.js
File metadata and controls
163 lines (136 loc) · 5.09 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
/**
* Node Runtime Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
var Plugin = require("../cloud9.core/plugin");
var util = require("util");
var name = "node-runtime";
var ProcessManager;
var EventBus;
module.exports = function setup(options, imports, register) {
ProcessManager = imports["process-manager"];
EventBus = imports.eventbus;
imports.ide.register(name, NodeRuntimePlugin, register);
};
var NodeRuntimePlugin = function(ide, workspace) {
this.ide = ide;
this.pm = ProcessManager;
this.eventbus = EventBus;
this.workspace = workspace;
this.workspaceId = workspace.workspaceId;
this.channel = this.workspaceId + "::node-runtime";
this.hooks = ["command"];
this.name = name;
this.processCount = 0;
};
util.inherits(NodeRuntimePlugin, Plugin);
(function() {
this.debugInitialized = {};
this.init = function() {
var self = this;
this.eventbus.on(this.channel, function(msg) {
msg.type = msg.type.replace(/^node-debug-(start|data|exit)$/, "node-$1");
var type = msg.type;
if (type == "node-start" || type == "node-exit") {
self.workspace.getExt("state").publishState();
}
if (msg.type == "node-start") {
self.processCount += 1;
}
if (msg.type == "node-exit") {
delete self.debugInitialized[msg.pid];
self.processCount -= 1;
}
if (msg.type == "node-debug-ready") {
self.debugInitialized[msg.pid] = true;
}
self.ide.broadcast(JSON.stringify(msg), self.name);
});
};
this.command = function(user, message, client) {
var cmd = (message.command || "").toLowerCase();
if (!(/node/.test(message.runner)))
return false;
var res = true;
switch (cmd) {
case "run":
this.$run(message.file, message.args || [], message.env || {}, message.version, message, client);
break;
case "rundebug":
this.$debug(message.file, message.args || [], message.env || {}, false, message.version, message, client);
break;
case "rundebugbrk":
this.$debug(message.file, message.args || [], message.env || {}, true, message.version, message, client);
break;
case "kill":
this.$kill(message.pid, message, client);
break;
case "debugnode":
this.pm.debug(message.pid, message.body, function(err) {
if (err) {
// uncommenting this helps with debugging, but we keep it
// commented out for the moment, because it throws at the moment
// that you stop the debugger, so we have to fix that some day
// console.error("debugnode threw error", err)
}
});
break;
default:
res = false;
}
return res;
};
this.$run = function(file, args, env, version, message, client) {
var self = this;
this.workspace.getExt("state").getState(function(err, state) {
if (err)
return self.error(err, 1, message, client);
if (state.processRunning)
return self.error("Child process already running!", 1, message);
self.pm.spawn("node", {
file: file,
args: args,
env: env,
nodeVersion: version,
extra: message.extra,
encoding: "ascii"
}, self.channel, function(err, pid, child) {
if (err)
self.error(err, 1, message, client);
});
});
};
this.$debug = function(file, args, env, breakOnStart, version, message, client) {
var self = this;
this.workspace.getExt("state").getState(function(err, state) {
if (err)
return self.error(err, 1, message, client);
if (state.processRunning)
return self.error("Child process already running!", 1, message);
self.pm.spawn("node-debug", {
file: file,
args: args,
env: env,
breakOnStart: breakOnStart,
nodeVersion: version,
extra: message.extra,
encoding: "ascii"
}, self.channel, function(err, pid) {
if (err)
self.error(err, 1, message, client);
});
});
};
this.$kill = function(pid, message, client) {
var self = this;
this.pm.kill(pid, function(err) {
if (err)
return self.error(err, 1, message, client);
});
};
this.canShutdown = function() {
return this.processCount === 0;
};
}).call(NodeRuntimePlugin.prototype);