-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.js
More file actions
280 lines (238 loc) · 8.4 KB
/
shell.js
File metadata and controls
280 lines (238 loc) · 8.4 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* Shell Module for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
*/
"use strict";
var util = require("util");
var Path = require("path");
var Async = require("asyncjs");
var Plugin = require("../cloud9.core/plugin");
var c9util = require("../cloud9.core/util");
var name = "shell";
var ProcessManager;
var VFS;
module.exports = function setup(options, imports, register) {
ProcessManager = imports["process-manager"];
VFS = imports.vfs;
imports.ide.register(name, ShellPlugin, register);
};
var ShellPlugin = function(ide, workspace) {
Plugin.call(this, ide, workspace);
this.pm = ProcessManager;
this.vfs = VFS;
this.workspaceId = workspace.workspaceId;
this.workspaceDir = ide.workspaceDir;
this.hooks = ["command"];
this.name = name;
this.processCount = 0;
};
util.inherits(ShellPlugin, Plugin);
(function() {
this.metadata = {
"commands": {
"cd" : {
"hint": "change working directory",
"commands": {
"[PATH]": {"hint": "path pointing to a folder. Autocomplete with [TAB]"}
}
},
"ls" : {
"hint": "list directory contents",
"commands": {
"[PATH]": {"hint": "path pointing to a folder. Autocomplete with [TAB]"}
}
},
"rm" : {
"hint": "remove a file",
"commands": {
"[PATH]": {"hint": "path pointing to a folder. Autocomplete with [TAB]"}
}
},
"mv": {
"hint": "move or rename files or directories",
"commands": {
"[PATH]": {"hint": "path pointing to a folder. Autocomplete with [TAB]"}
}
},
"pwd": {"hint": "return working directory name"}
}
};
this.command = function(user, message, client) {
if (!this["command-" + message.command])
return false;
if (message.runner && message.runner !== "shell")
return false;
this["command-" + message.command.toLowerCase()](message);
return true;
};
this["command-internal-autocomplete"] = function(message) {
var argv = [].concat(message.argv);
var cmd = argv.shift();
var self = this;
this.getListing(argv.pop(), message.cwd || this.workspaceDir, (cmd == "cd" || cmd == "ls"), function(tail, matches) {
self.sendResult(0, "internal-autocomplete", {
matches: matches,
line : message.line,
base : tail,
cursor : message.cursor,
textbox: message.textbox,
argv : message.argv
});
});
};
this._isDir = function(stat) {
return (
stat.mime == "inode/directory" ||
(stat.linkStat && stat.linkStat.mime == "inode/directory")
);
};
this["command-internal-isfile"] = function(message) {
var file = message.argv.pop();
var path = message.cwd || this.workspaceDir;
var self = this;
path = Path.normalize(path + "/" + file.replace(/^\//g, ""));
if (path.indexOf(this.workspaceDir) === -1) {
this.sendResult();
return;
}
this.vfs.stat(path, {}, function(err, stat) {
if (err || stat.err) {
return self.sendResult(0, "error", {
errmsg: "Problem opening file; it does not exist or something else failed. More info: " +
err.toString().replace("Error: ENOENT, ", ""),
extra: message.extra
});
}
self.sendResult(0, "internal-isfile", {
cwd: path,
isfile: (stat && !self._isDir(stat)),
sender: message.sender || "shell",
extra: message.extra
});
});
};
this["command-commandhints"] = function(message) {
var commands = {},
_self = this;
Async.list(Object.keys(this.workspace.plugins))
.each(function(sName, next) {
var oExt = _self.workspace.getExt(sName);
if (oExt.$commandHints) {
oExt.$commandHints(commands, message, next);
}
else {
if (oExt.metadata && oExt.metadata.commands)
c9util.extend(commands, oExt.metadata.commands);
next();
}
})
.end(function() {
_self.sendResult(0, message.command, commands);
});
};
this["command-mv"] =
this["command-mkdir"] =
this["command-rm"] =
this["command-pwd"] =
this["command-ls"] = function(message) {
var self = this;
this.processCount += 1;
this.pm.exec("shell", {
command: message.command,
args: message.argv.slice(1),
cwd: message.cwd || this.workspaceDir,
extra: message.extra
}, function(code, out, err) {
self.processCount -= 1;
self.sendResult(0, message.command, {
code : code,
argv : message.argv,
err : err,
out : out,
extra : message.extra
});
});
};
this["command-cd"] = function(message) {
var to = message.argv.pop();
var path = message.cwd || this.workspaceDir;
var self = this;
path = Path.normalize(path + "/" + to.replace(/^\//g, ""));
if (path.indexOf(this.workspaceDir) === -1)
return this.sendResult();
this.vfs.stat(path, {}, function(err, stat) {
if (err || stat.err) {
return self.sendResult(0, "error", {
errmsg: "Problem changing directory; it does not exist or something else failed. More info: " +
err.toString().replace("Error: ENOENT, ", ""),
extra: message.extra
});
}
if (!self._isDir(stat)) {
return self.sendResult(0, "error", {
errmsg: "Not a directory.",
extra: message.extra
});
}
self.sendResult(0, message.command, {
cwd: path,
extra: message.extra
});
});
};
this["command-ps"] = function(message) {
var self = this;
this.pm.ps(function(err, procs) {
self.sendResult(0, message.command, {
argv : message.argv,
err : null,
out : procs,
extra : message.extra
});
});
};
this["command-kill"] = function(message) {
var self = this;
this.pm.kill(message.pid, function(err) {
if (!err)
return;
self.sendResult(0, message.command, {
argv : message.argv,
code : -1,
err : err || "There was a problem exiting the process",
extra : message.extra,
pid : message.pid
});
});
};
this.getListing = function(tail, path, dirmode, callback) {
var self = this;
var matches = [];
tail = (tail || "")
.trim()
.split(/\s+/g)
.pop();
if (tail.indexOf("/") > -1) {
path = path.replace(/[\/]+$/, "") + "/" + tail.substr(0, tail.lastIndexOf("/")).replace(/^[\/]+/, "");
tail = tail.substr(tail.lastIndexOf("/") + 1).replace(/^[\/]+/, "").replace(/[\/]+$/, "");
}
this.vfs.readdir(this.path, {encoding: null}, function(err, meta) {
if (err)
return callback(err);
var stream = meta.stream;
var nodes = [];
stream.on("data", function(stat) {
var isDir = self._isDir(stat);
if (stat.name.indexOf(tail) === 0 && (!dirmode || isDir))
matches.push(stat.name + (isDir ? "/" : ""));
});
stream.on("end", function() {
callback(tail, nodes);
});
});
};
this.canShutdown = function() {
return this.processCount === 0;
};
}).call(ShellPlugin.prototype);