diff --git a/configs/cli.js b/configs/cli.js index 80a8d3f5..e3fd3411 100644 --- a/configs/cli.js +++ b/configs/cli.js @@ -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 diff --git a/plugins/c9.cli.bridge/bridge_commands.js b/plugins/c9.cli.bridge/bridge_commands.js index 5e697a0a..f078cece 100644 --- a/plugins/c9.cli.bridge/bridge_commands.js +++ b/plugins/c9.cli.bridge/bridge_commands.js @@ -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 *****/ diff --git a/plugins/c9.cli.exec/exec.js b/plugins/c9.cli.exec/exec.js new file mode 100644 index 00000000..e8a48f4b --- /dev/null +++ b/plugins/c9.cli.exec/exec.js @@ -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: " [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 + }); + } +}); \ No newline at end of file