Merge pull request +12936 from c9/pull/core/272

Added CLI exec command
pull/284/head
Harutyun Amirjanyan 2016-03-22 02:12:04 +04:00
commit 3e8757b251
3 zmienionych plików z 107 dodań i 1 usunięć

Wyświetl plik

@ -80,6 +80,10 @@ return [
packagePath: "./c9.cli.open/open",
platform: process.platform
},
{
packagePath: "./c9.cli.exec/exec",
platform: process.platform
},
{
packagePath: "./c9.cli.open/restart",
platform: process.platform

Wyświetl plik

@ -2,7 +2,7 @@
define(function(require, exports, module) {
main.consumes = [
"Plugin", "bridge", "tabManager", "panels", "tree.favorites", "tree",
"fs", "preferences", "settings", "c9"
"fs", "preferences", "settings", "c9", "commands"
];
main.provides = ["bridge.commands"];
return main;
@ -18,6 +18,7 @@ define(function(require, exports, module) {
var fs = imports.fs;
var c9 = imports.c9;
var prefs = imports.preferences;
var commands = imports.commands;
var async = require("async");
@ -36,6 +37,9 @@ define(function(require, exports, module) {
case "open":
open(message, e.respond);
break;
case "exec":
exec(message, e.respond);
break;
case "pipe":
createPipe(message, e.respond);
break;
@ -173,6 +177,12 @@ define(function(require, exports, module) {
callback(null, true);
});
}
function exec(message, callback) {
var result = commands.exec(message.command, message.args);
var err = result ? null : "command failed";
callback(err, result);
}
/***** Lifecycle *****/

Wyświetl plik

@ -0,0 +1,92 @@
define(function(require, exports, module) {
main.consumes = ["Plugin", "cli_commands", "bridge.client"];
main.provides = ["exec"];
return main;
function main(options, imports, register) {
var Plugin = imports.Plugin;
var cmd = imports.cli_commands;
var bridge = imports["bridge.client"];
/***** Initialization *****/
var plugin = new Plugin("Ajax.org", main.consumes);
// var emit = plugin.getEmitter();
var loaded;
function load(){
if (loaded) return;
loaded = true;
cmd.addCommand({
name: "exec",
info: " Executes remote c9 commands.",
usage: "<command> [argument 1] [argument 2] ... [argument n]",
check: function(argv) {
if (argv._.length < 2)
throw new Error("Missing command");
},
options: {},
exec: function(argv) {
exec(
argv._[1],
argv._.slice(2),
function(){});
}
});
}
/***** Methods *****/
function exec(command, args, callback) {
args.unshift(process.cwd());
var message = {
type: "exec",
command: command,
args: args
};
bridge.send(message, function cb(err, response) {
if (err) {
console.log(err.message);
}
if (response !== true)
console.log("Could not execute", command);
process.exit(); // I don't get why this is needed
});
}
/***** Lifecycle *****/
plugin.on("load", function(){
load();
});
plugin.on("enable", function(){
});
plugin.on("disable", function(){
});
plugin.on("unload", function(){
loaded = false;
});
/***** Register and define API *****/
/**
* Finds or lists files and/or lines based on their filename or contents
**/
plugin.freezePublicAPI({
/**
*
*/
exec: exec
});
register(null, {
exec: plugin
});
}
});