Permalink
Browse files

Merge pull request #1 from nearinfinity/master

 - Removed your shell and replaced it with the built-in readline module (this is what the node REPL uses).
 - Now that it uses readline I added support for auto completion of commands.
 - Made it possible to create multiple RemoteExecution with different configurations.
 - Fixed the results call on the client side to allow it to return complex objects. So if you type "window" you get the properties on window.
 - Code refactoring

Thank you @joeferner
  • Loading branch information...
2 parents ccf67c4 + 6e5a455 commit 4b895cee08778815826947e5f0fbb3476f0468da @ernesto-jimenez committed Feb 24, 2012
Showing with 325 additions and 425 deletions.
  1. +2 −1 .gitignore
  2. +210 −50 bin/remote_js.js
  3. +23 −2 client/client.js
  4. +3 −2 package.json
  5. +87 −167 server/remote_execution.js
  6. +0 −203 server/shell.js
View
@@ -1 +1,2 @@
-node_modules
+node_modules
+.idea
View
@@ -1,52 +1,212 @@
#!/usr/bin/env node
-;(function () { // wrapper in case we're in module_context mode
-var RemoteExecution = require('../server/remote_execution.js').RemoteExecution,
- Shell = require('../server/shell.js').Shell;
-
-RemoteExecution.init();
-RemoteExecution.printInstructions();
-
-Shell.init(RemoteExecution.sendCmd,{
- ls: {
- desc: 'list connected clients',
- fn: function () {
- var conn_id;
- for (conn_id in RemoteExecution.clients) {
- if (conn_id === RemoteExecution.client) {
- console.log(" * " + conn_id);
- } else {
- console.log(" " + conn_id);
- }
- }
- if (conn_id) {
- console.log("Select your client with the select command");
- } else {
- console.log("No clients connected");
- RemoteExecution.printInstructions();
- }
- }
- },
- select: {
- desc: 'select a connected client',
- fn: function (client) {
- RemoteExecution.selectClient(client[0]);
- }
- },
- selected: {
- desc: 'shows selected client',
- fn: function () {
- if (RemoteExecution.client === undefined) {
- console.log("No client selected");
- } else {
- console.log("Selected client: " + RemoteExecution.client);
- }
- }
- },
- disconnect: {
- desc: 'disconnect current client',
- fn: function () {
- RemoteExecution.disconnect();
- }
- }
-});
+
+(function () { // wrapper in case we're in module_context mode
+ var RemoteExecution = require('../server/remote_execution.js').RemoteExecution;
+ var readline = require('readline');
+ var args = require('commander');
+ var colorize = require('colorize');
+ var os = require('os');
+
+ args
+ .option('-v, --verbose', 'Verbose output')
+ .option('-p, --port <n>', 'Port (default: 3400)', parseInt);
+
+ args.parse(process.argv)
+ args.port = args.port || 3400;
+
+ var selectedClient = undefined;
+ var remote = new RemoteExecution(args);
+
+ function log (msg) {
+ console.log(colorize.ansify('#gray[' + msg + ']'));
+ }
+
+ function error (msg) {
+ console.log(colorize.ansify('#red[' + msg + ']'));
+ }
+
+ function clientLog (msg) {
+ console.log(colorize.ansify('#blue[' + msg + ']'));
+ }
+
+ function clientOutput (msg) {
+ process.stdout.write(colorize.ansify('=> #green['));
+ console.log(msg);
+ process.stdout.write(colorize.ansify(']'));
+ }
+
+ function printInstructions (arg) {
+ var url = 'http://' + os.hostname() + ':' + args.port + '/client.js';
+ log('Add this to your HTML and open the webpage <script src="' + url + '"></script>');
+ }
+
+ function selectClient (client) {
+ for (var conn_id in remote.clients) {
+ if (client == undefined || (conn_id + '').match("^" + client)) {
+ selectedClient = conn_id;
+ log("Selected client " + conn_id);
+ return;
+ }
+ }
+
+ log('No client selected');
+ }
+
+ remote.on('error', function(err) {
+ error(err);
+ });
+
+ remote.on('clientConnected', function (conn) {
+ log("Connected " + conn.id);
+ if (selectedClient === undefined) {
+ selectClient(conn.id);
+ }
+ });
+
+ remote.on('clientDisconnected', function (conn) {
+ log("Disconnected " + conn.id);
+ if (selectedClient === conn.id) {
+ selectedClient = undefined;
+ selectClient();
+ }
+ });
+
+ remote.on('message', function(conn, message) {
+ try {
+ switch(message.msg) {
+ case 'cmdresult':
+ clientOutput(message.data);
+ break;
+ case 'exception':
+ error("Remote Error: " + message.data.message);
+ if (message.data.sourceURL) error(" " + message.data.sourceURL + ':' + message.data.line);
+ break;
+ case 'log':
+ clientLog(message.data);
+ break;
+ default:
+ log("Unknown message");
+ log(client_message);
+ break;
+ }
+ } catch (exception) {
+ log(exception.stack);
+ }
+ log('');
+ });
+
+ var commands = {
+ ls: {
+ desc: 'list connected clients',
+ fn: function () {
+ var conn_id;
+ for (conn_id in remote.clients) {
+ if (conn_id === remote.client) {
+ console.log(" * " + conn_id);
+ } else {
+ console.log(" " + conn_id);
+ }
+ }
+ if (conn_id) {
+ console.log("Select your client with the select command");
+ } else {
+ console.log("No clients connected");
+ printInstructions(args);
+ }
+ }
+ },
+ select: {
+ desc: 'select a connected client',
+ fn: function (client) {
+ selectClient(client[0]);
+ }
+ },
+ selected: {
+ desc: 'shows selected client',
+ fn: function () {
+ if (remote.client === undefined) {
+ console.log("No client selected");
+ } else {
+ console.log("Selected client: " + remote.client);
+ }
+ }
+ },
+ disconnect: {
+ desc: 'disconnect current client',
+ fn: function () {
+ if (selectedClient) {
+ selectedClient = undefined;
+ selectClient();
+ }
+ }
+ },
+ help: {
+ desc: 'help',
+ fn: function () {
+ console.log("This is the list of reserved commands:\n");
+ for (var cmd in commands) {
+ var description = commands[cmd].desc;
+ if (description) {
+ console.log(" " + cmd + ": " + description);
+ }
+ }
+ console.log("Any other input is executed in the remote client\n");
+ }
+ },
+ exit: {
+ desc: 'exit',
+ fn: function () {
+ process.exit(0);
+ }
+ }
+ };
+
+ function shellCompleter(linePartial, callback) {
+ var items = commands;
+
+ if (linePartial.match(/^select /)) {
+ items = remote.clients;
+ linePartial = linePartial.substr('select '.length);
+ }
+
+ var matches = [];
+ for (var k in items) {
+ if (linePartial.length <= k.length
+ && k.substr(0, linePartial.length) == linePartial) {
+ matches.push(k);
+ }
+ }
+ callback(null, [matches, linePartial]);
+ }
+
+ var shell = readline.createInterface(process.stdin, process.stdout, shellCompleter);
+
+ shell.on('line', function (line) {
+ line = line.trim();
+ if (line === '') {
+ shell.prompt();
+ return;
+ }
+
+ var cmdOpts = line.split(' ');
+ var cmd = cmdOpts.shift();
+ if (cmd in commands) {
+ commands[cmd].fn(cmdOpts);
+ shell.prompt();
+ return;
+ }
+
+ if (selectedClient) {
+ remote.sendCmd(selectedClient, line);
+ } else {
+ error("ERROR: no client connected, type 'help'");
+ }
+ shell.prompt();
+ });
+
+ shell.setPrompt('> ');
+
+ printInstructions(args);
+ shell.prompt();
+
})();
View
@@ -64,11 +64,19 @@ RemoteJSDebugger.prototype.socketSend = function (data) {
};
RemoteJSDebugger.prototype.serviceUrl = function () {
+ if(window.remoteJsServiceUrl) {
+ return window.remoteJsServiceUrl;
+ }
var script, scripts = document.getElementsByTagName('script');
for (var i in scripts) {
script = scripts[i].src;
- if (script && script.match(':3400')) break;
+ if (script && script.match('/client.js')) break;
}
+ if(!script) {
+ var msg = "Could not find window.remoteJsServiceUrl try setting it explicitly";
+ alert(msg);
+ throw new Error(msg);
+ }
return script.replace('/client.js', '/');
};
@@ -90,7 +98,20 @@ RemoteJSDebugger.prototype.send = function (msg, object) {
};
RemoteJSDebugger.prototype.sendResult = function (result) {
- this.send('cmdresult', result);
+ try {
+ this.send('cmdresult', result);
+ } catch(ex) {
+ // can't send it the easy way send a simplified version.
+ var data = {};
+ for(var k in result) {
+ if(result[k]) {
+ data[k] = result[k].toString();
+ } else {
+ data[k] = result[k];
+ }
+ }
+ this.send('cmdresult', data);
+ }
};
RemoteJSDebugger.prototype.sendException = function (exception) {
View
@@ -14,8 +14,9 @@
"url": "https://ernesto-jimenez@github.com/ernesto-jimenez/remote-js.git"
},
"dependencies": {
- "socket.io": "0.7.9",
- "colorize": "0.1.0"
+ "socket.io": "0.8.7",
+ "colorize": "0.1.0",
+ "commander": "0.5.2"
},
"files": ["remote_js", "README.md", "package.json", "server", "client", "bin"],
"directories": {
Oops, something went wrong.

0 comments on commit 4b895ce

Please sign in to comment.