c9-core/plugins/c9.cli.bridge/bridge_commands.js

209 wiersze
7.3 KiB
JavaScript
Czysty Zwykły widok Historia

2015-02-10 19:41:24 +00:00
define(function(require, exports, module) {
main.consumes = [
2015-05-01 01:53:12 +00:00
"Plugin", "bridge", "tabManager", "panels", "tree.favorites", "tree",
2016-03-16 21:16:01 +00:00
"fs", "preferences", "settings", "c9", "commands"
2015-02-10 19:41:24 +00:00
];
2015-04-30 22:30:29 +00:00
main.provides = ["bridge.commands"];
2015-02-10 19:41:24 +00:00
return main;
function main(options, imports, register) {
var Plugin = imports.Plugin;
var bridge = imports.bridge;
2015-04-30 22:30:29 +00:00
var tabManager = imports.tabManager;
2015-02-10 19:41:24 +00:00
var panels = imports.panels;
var tree = imports.tree;
2015-05-01 01:53:12 +00:00
var settings = imports.settings;
2015-02-10 19:41:24 +00:00
var favs = imports["tree.favorites"];
var fs = imports.fs;
2015-05-01 08:13:47 +00:00
var c9 = imports.c9;
2015-05-01 01:53:12 +00:00
var prefs = imports.preferences;
2016-03-16 21:16:01 +00:00
var commands = imports.commands;
2015-02-10 19:41:24 +00:00
2015-04-30 22:30:29 +00:00
var async = require("async");
2015-02-10 19:41:24 +00:00
/***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes);
// var emit = plugin.getEmitter();
var BASEPATH = options.basePath;
function load(){
bridge.on("message", function(e) {
var message = e.message;
switch (message.type) {
case "open":
2015-04-30 22:30:29 +00:00
open(message, e.respond);
break;
2016-03-16 21:16:01 +00:00
case "exec":
exec(message, e.respond);
break;
2016-02-12 19:09:59 +00:00
case "pipe":
createPipe(message, e.respond);
break;
case "pipeData":
updatePipe(message, e.respond);
break;
2015-02-10 19:41:24 +00:00
case "ping":
2015-04-30 22:30:29 +00:00
e.respond(null, true);
break;
2015-04-30 04:01:13 +00:00
default:
2015-11-25 10:56:50 +00:00
if (message.type)
console.error("Unknown Bridge Command: ", message.type);
2015-04-30 22:30:29 +00:00
break;
2015-02-10 19:41:24 +00:00
}
2015-04-30 04:01:13 +00:00
}, plugin);
2015-05-01 01:53:12 +00:00
settings.on("read", function(e) {
settings.setDefaults("user/terminal", [
["defaultEnvEditor", "false"]
2015-05-01 01:53:12 +00:00
]);
}, plugin);
prefs.add({
"Editors" : {
"Terminal" : {
"Use Cloud9 as the Default Editor" : {
type: "checkbox",
path: "user/terminal/@defaultEnvEditor",
2015-05-01 01:53:12 +00:00
position: 14000
}
}
}
}, plugin);
2015-02-10 19:41:24 +00:00
}
/***** Methods *****/
2016-02-12 19:09:59 +00:00
function createPipe(message, callback) {
tabManager.once("ready", function(){
2016-03-13 17:10:51 +00:00
tabManager.open({
focus: true,
editorType: "ace",
path: message.path && c9.toInternalPath(message.path),
document: { meta : { newfile: true } }
}, function(err, tab) {
if (err)
return callback(err);
2016-03-13 17:10:51 +00:00
callback(null, tab.path || tab.name);
});
});
2016-02-12 19:09:59 +00:00
}
function updatePipe(message, callback) {
tabManager.once("ready", function() {
2016-03-13 17:10:51 +00:00
var tab = tabManager.findTab(message.tab);
var c9Session = tab && tab.document.getSession();
if (c9Session && c9Session.session)
c9Session.session.insert({row: Number.MAX_VALUE, column: Number.MAX_VALUE} , message.data);
callback(null, true);
});
2016-02-12 19:09:59 +00:00
}
2015-04-30 22:30:29 +00:00
function open(message, callback) {
var i = -1;
var tabs = [];
2015-05-01 08:13:47 +00:00
BASEPATH = c9.toInternalPath(BASEPATH);
2015-04-30 22:30:29 +00:00
async.each(message.paths, function(info, next) {
2015-02-10 19:41:24 +00:00
var path = info.path;
2015-04-30 22:30:29 +00:00
i++;
2015-02-10 19:41:24 +00:00
2015-05-01 08:13:47 +00:00
path = c9.toInternalPath(path);
2015-02-10 19:41:24 +00:00
// Make sure file is inside workspace
2015-04-30 22:30:29 +00:00
if (path.charAt(0) !== "~") {
if (path.substr(0, BASEPATH.length) !== BASEPATH)
return; // Dont' call callback. Perhaps another client will pick this up.
// Remove base path
path = path.substr(BASEPATH.length);
}
2015-02-10 19:41:24 +00:00
if (info.type == "directory") {
path = path.replace(/\/$/, "");
panels.activate("tree");
var node = favs.addFavorite(path);
2015-04-30 04:01:13 +00:00
tree.expand(path, function() {
2015-02-10 19:41:24 +00:00
tree.select(node); //path || "/");
tree.scrollToSelection();
2015-04-30 22:30:29 +00:00
next();
2015-02-10 19:41:24 +00:00
});
tree.focus();
}
else {
2015-04-30 22:30:29 +00:00
tabManager.once("ready", function(){
2016-03-13 17:10:51 +00:00
var m = /:(\d*)(?::(\d*))?$/.exec(path);
var jump = {};
if (m) {
if (m[1])
jump.row = parseInt(m[1], 10) - 1;
if (m[2])
jump.column = parseInt(m[2], 10);
path = path.slice(0, m.index);
}
2015-02-10 19:41:24 +00:00
fs.exists(path, function(existing) {
2015-04-30 22:30:29 +00:00
var tab = tabManager.open({
2015-02-10 19:41:24 +00:00
path: path,
2015-06-09 12:15:19 +00:00
focus: i === 0,
document: existing
2016-03-13 17:10:51 +00:00
? { ace: { jump: jump } }
2015-06-09 12:15:19 +00:00
: { meta : { newfile: true } }
2015-04-30 22:30:29 +00:00
}, function(){
next();
});
if (message.wait) {
tab.on("close", function(){
tabs.splice(tabs.indexOf(tab), 1);
if (!tabs.length)
callback(null, true);
});
}
tabs.push(tab);
2015-02-10 19:41:24 +00:00
});
});
}
2015-04-30 22:30:29 +00:00
}, function(err){
if (err)
return callback(err);
if (!message.wait || !tabs.length)
callback(null, true);
2015-02-10 19:41:24 +00:00
});
}
2016-03-16 21:16:01 +00:00
function exec(message, callback) {
var result = commands.exec(message.command, message.args);
var err = result ? null : "command failed";
callback(err, result);
}
2015-02-10 19:41:24 +00:00
/***** Lifecycle *****/
plugin.on("load", function(){
load();
});
plugin.on("unload", function(){
});
/***** Register and define API *****/
/**
* Bridge To Communicate from CLI to IDE
**/
plugin.freezePublicAPI({});
register(null, {
2015-04-30 22:30:29 +00:00
"bridge.commands": plugin
2015-02-10 19:41:24 +00:00
});
}
});