-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_manager.js
More file actions
76 lines (66 loc) · 2.27 KB
/
Copy pathcommand_manager.js
File metadata and controls
76 lines (66 loc) · 2.27 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
module.exports = (function() {
var util = require("util");
var EventEmitter = require('events').EventEmitter;
var executors = require("./executors");
var commandline = require("./commandline");
var commands = require("./plugins/commands");
var classifiers = require("./plugins/classifiers");
var visualizers = require("./plugins/visualizers");
function ExecutionContext(frame) {
this.addVisualizer = function(type) {
if (visualizers.hasOwnProperty(type)) {
// create a new tab for the visualizer
var tab = frame.createTab();
visualizers[type](this, tab);
}
}
}
util.inherits(ExecutionContext, EventEmitter);
ExecutionContext.prototype.write = function(data) {
this.emit("out", data);
}
ExecutionContext.prototype.writeln = function(data) {
this.write(data + "\n");
}
ExecutionContext.prototype.error = function(data) {
this.emit("error", data);
}
ExecutionContext.prototype.close = function() {
this.emit("close");
}
function getExecutor(command) {
if (command.args.length > 0) {
if (commands.hasOwnProperty(command.args[0])) {
// first try internal commands
return commands[command.args[0]];
} else {
// for others using cmd to execute
return executors.cmd;
}
}
}
function initializeClassifier(command, context) {
if (classifiers.hasOwnProperty(command.args[0])) {
var classifier = classifiers[command.args[0]];
classifier(command, context);
}
}
return {
exec: function(commandText, env, frame, callback) {
var command = commandline.parse(commandText);
var executor = getExecutor(command);
var context = new ExecutionContext(frame);
context.on("close", callback);
var returns;
if (executor !== undefined) {
// always add a default visualizer to display raw output
context.addVisualizer("text");
initializeClassifier(command, context);
returns = executor(command, env, context);
}
// if func returns nothing, context will automatically be closed after it returns.
// to avoid context from being closed (e.g. in async mode), return something from func (usually a false).
if (returns === undefined) context.close();
}
};
})();