Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some command line args

  • Loading branch information...
commit aac7c9b7e59f76cc94cd8d8d2d276d461f5f9927 1 parent 1b0022c
@davglass authored
Showing with 311 additions and 155 deletions.
  1. +49 −153 bin/yui-repl.js
  2. +259 −0 lib/repl.js
  3. +3 −2 package.json
View
202 bin/yui-repl.js
@@ -2,170 +2,66 @@
var net = require('net'),
util = require('util'),
- replServer = require('repl');
+ cli = require('cli'),
+ replServer = require('repl'),
+ yrepl = require('../lib/repl');
require('colors');
-var Y = require('yui3').silent().useSync('yui-base');
-
-var prompt = 'YUI@' + Y.version + '> ';
-var len = prompt.length;
-prompt = 'YUI'.magenta + '@'.white + Y.version.yellow + '> '.white;
-
-replServer.REPLServer.prototype.displayPrompt = function() {
- var l = this.bufferedCommand.length ? 4 : len;
- this.rli.setPrompt(this.bufferedCommand.length ? '... ' : this.prompt, l);
- this.rli.prompt();
-};
-
-var repl = replServer.start(prompt);
-
-
-
-var debug = false;
-
-var ctx = repl.context;
-ctx.Y = Y;
-
-repl.defineCommand('clear', {
- help: 'Clear the current Y context',
- action: function() {
- this.outputStream.write('Resetting Y to the default state\n'.magenta);
- this.bufferedCommand = '';
- this.context.Y = require('yui3').silent().useSync('yui-base');
- this.displayPrompt();
- }
+cli.enable('status', 'help');
+cli.parse({
+ url: ['u', 'Import document from the url passed', 'url'],
+ get: ['g', 'Add this script before starting the repl', 'path'],
+ html: ['H', 'Import HTML string', 'string'],
+ silent: ['s', 'Silent mode: on or off', 'string', 'on']
});
-var load = function(url) {
- var self = this;
- self.outputStream.write('Fetching URL: '.magenta + url.yellow);
- this.context.Y.load(url, function() {
- self.outputStream.write(' [done]\n'.white);
- self.displayPrompt();
- });
-};
-
-repl.defineCommand('debug', {
- help: 'Toggle the YUI debug config option',
- action: function() {
- var d = !debug+'';
- debug = !debug;
- this.outputStream.write('Setting debug on the Y instance to: '.magenta + d.white + '\n');
- this.context.Y.config.debug = debug;
- this.displayPrompt();
- }
-});
-repl.defineCommand('import', {
- help: 'Import a document into this context',
- action: load
-});
-
-repl.defineCommand('load', {
- help: 'Alias for import',
- action: load
-});
+cli.main(function() {
+ var opts = this.options;
-repl.defineCommand('fetch', {
- help: 'Alias for import',
- action: load
-});
-
-repl.defineCommand('version', {
- help: 'Show the YUI version',
- action: function() {
- this.outputStream.write('Current YUI Version: '.magenta + this.context.Y.version.yellow + '\n');
- this.displayPrompt();
+ var Y = require('yui3');
+ if (opts.silent == 'on') {
+ Y = Y.silent();
}
-});
-
-repl.defineCommand('use', {
- help: 'Comma seperated list of modules to use inside this Y context',
- action: function(l) {
- var args = l.split(',');
- this.outputStream.write('Using modules: '.magenta + l);
- this.context.Y.useSync.apply(this.context.Y, args);
- this.outputStream.write(' [done]\n'.white);
- this.displayPrompt();
- }
-});
-
-repl.defineCommand('io', {
- help: 'Make an IO request to the passed URL',
- action: function(url) {
- var self = this,
- Y = this.context.Y;
- self.outputStream.write('Making IO Request: '.magenta + url.yellow);
- Y.use('io', function() {
- Y.io(url, {
- on: {
- complete: function(id, e) {
- self.outputStream.write(' [done]\n'.white);
- self.outputStream.write(' (' + (e.status + '').green + ' ' + e.statusText + '): Content-Type: "' + e.headers['content-type'].green + '"\n\n'.white);
- var str;
- try {
- str = util.inspect(JSON.parse(e.responseText), false, Infinity, true);
- } catch (e) {
- str = e.responseText;
- }
- self.outputStream.write(str);
- self.outputStream.write('\n\n');
- self.displayPrompt();
- }
- }
- });
- });
+ Y = Y.useSync('yui-base');
+
+ var start = function() {
+ if (opts.get) {
+ util.print('Fetching script:'.magenta + opts.get.yellow);
+ var r = yrepl.startPrompt(Y);
+ r.commands['.get'].action.call(r, opts.get);
+ } else {
+ yrepl.startPrompt(Y);
+ }
}
-});
-
-repl.defineCommand('headers', {
- help: 'Make an IO request to the passed URL and only return the headers',
- action: function(url) {
- var self = this,
- Y = this.context.Y;
- self.outputStream.write('Making IO Request: '.magenta + url.yellow);
- Y.use('io', function() {
- Y.io(url, {
- on: {
- complete: function(id, e) {
- self.outputStream.write(' [done]\n'.white);
- self.outputStream.write(' (' + (e.status + '').green + ' ' + e.statusText + '): Content-Type: "' + e.headers['content-type'].green + '"\n\n'.white);
- var str = util.inspect(e.headers, false, Infinity, true);
- self.outputStream.write(str);
- self.outputStream.write('\n\n');
- self.displayPrompt();
- }
- }
- });
+
+ if (opts.html) {
+ var html = opts.html;
+ util.print('Importing HTML into document'.magenta);
+ Y.useSync('node');
+ if (html.indexOf('<body') === -1) {
+ //No Body, append to body
+ Y.one('body').append(html);
+ } else if (html.indexOf('<html') > -1) {
+ //This is an HTML doc
+ Y.one('doc').set('innerHTML', html);
+ } else if (html.indexOf('<body') > -1) {
+ //Body without HTML
+ Y.one('body').set('outerHTML', html);
+ }
+ util.print(' [done]\n'.white);
+ start();
+ } else if (opts.url) {
+ util.print('Fetching URL: '.magenta, opts.url.yellow);
+ Y.useSync('node');
+ Y.fetch(opts.url, function() {
+ util.print(' [done]\n'.white);
+ start();
});
+ } else {
+ start();
}
-});
-repl.defineCommand('yql', {
- help: 'Make an YQL request with the passed SQL statement',
- action: function(sql) {
- var self = this,
- Y = this.context.Y;
- self.outputStream.write('Making YQL Request: '.magenta + sql.yellow);
- Y.use('yql', function() {
- Y.YQL(sql, function(r) {
- self.outputStream.write(' [done]\n'.white);
- var str;
- try {
- str = util.inspect(r.query.results, false, Infinity, true);
- } catch (e) {
- str = util.inspect(r, false, Infinity, true);
- }
- self.outputStream.write(str);
- self.outputStream.write('\n\n');
- self.displayPrompt();
- });
- });
- }
});
-
-for (var i in repl.commands) {
- repl.commands[i].help = repl.commands[i].help.white;
-}
View
259 lib/repl.js
@@ -0,0 +1,259 @@
+var replServer = require('repl');
+var util = require('util');
+var len;
+replServer.REPLServer.prototype.displayPrompt = function() {
+ var l = this.bufferedCommand.length ? 11 : len;
+ this.rli.setPrompt(this.bufferedCommand.length ? '.......... '.yellow : this.prompt, l);
+ this.rli.prompt();
+};
+
+require('colors');
+
+
+exports.startPrompt = function(Y) {
+
+ var prompt = 'YUI@' + Y.version + '> ';
+ len = prompt.length;
+ prompt = 'YUI'.magenta + '@'.white + Y.version.yellow + '> '.white;
+ var debug = Y.config.debug;
+
+
+ var repl = replServer.start(prompt);
+
+
+ var ctx = repl.context;
+ ctx.Y = Y;
+
+ var clear = function(write) {
+ this.outputStream.write('Resetting Y to the default state'.magenta);
+ this.bufferedCommand = '';
+ this.context.Y = require('yui3').silent().useSync('yui-base');
+ this.outputStream.write(' [done]\n'.white);
+ if (write !== false) {
+ this.displayPrompt();
+ }
+ };
+
+ repl.defineCommand('help', {
+ help: 'Show help',
+ action: function() {
+ var self = this,
+ max = 0,
+ aliases = {};
+
+ Object.keys(this.commands).forEach(function(name) {
+ if (name.length > max) {
+ max = name.length;
+ }
+ var cmd = self.commands[name];
+ if (cmd.help && cmd.help.indexOf('Alias') === 0) {
+ var a = cmd.help.replace('Alias for ', '.');
+ if (!aliases[a]) {
+ aliases[a] = [];
+ }
+ aliases[a].push(name);
+ }
+ });
+ Object.keys(this.commands).sort().forEach(function(name) {
+ var cmd = self.commands[name],
+ rawName = name;
+ if (cmd.help && cmd.help.indexOf('Alias') === 0) {
+ return;
+ }
+ if (name.length < max) {
+ for (var i = name.length; i < max; i++) {
+ name += ' ';
+ }
+ }
+ self.outputStream.write(name.bold.yellow + ' ' + (cmd.help || '').white);
+ if (aliases[rawName]) {
+ self.outputStream.write((' (aliases: ' + aliases[rawName].join(',') + ')').yellow);
+ }
+ self.outputStream.write('\n');
+ });
+ this.displayPrompt();
+ }
+ });
+
+
+ repl.defineCommand('clear', {
+ help: 'Clear the current Y context',
+ action: clear
+ });
+
+ var load = function(url) {
+ var self = this;
+ clear.call(this, false);
+ self.outputStream.write('Fetching URL: '.magenta + url.yellow);
+ this.context.Y.load(url, function() {
+ self.outputStream.write(' [done]\n'.white);
+ self.displayPrompt();
+ });
+ };
+
+ repl.defineCommand('debug', {
+ help: 'Toggle the YUI debug config option',
+ action: function() {
+ var d = !debug+'';
+ debug = !debug;
+ this.outputStream.write('Setting debug on the Y instance to: '.magenta + d.white + '\n');
+ this.context.Y.config.debug = debug;
+ this.displayPrompt();
+ }
+ });
+
+ repl.defineCommand('d', {
+ help: 'Alias for debug',
+ action: function() {
+ this.commands['.debug'].action.apply(this, arguments);
+ }
+ });
+
+ repl.defineCommand('import', {
+ help: 'Import a document into this context',
+ action: load
+ });
+
+ repl.defineCommand('load', {
+ help: 'Alias for import',
+ action: load
+ });
+
+ repl.defineCommand('fetch', {
+ help: 'Alias for import',
+ action: load
+ });
+
+ repl.defineCommand('version', {
+ help: 'Show the YUI version',
+ action: function() {
+ this.outputStream.write('Current YUI Version: '.magenta + this.context.Y.version.yellow + '\n');
+ this.displayPrompt();
+ }
+ });
+
+ var use = function(l) {
+ var args = l.split(',');
+ this.outputStream.write('Using modules: '.magenta + l);
+ this.context.Y.useSync.apply(this.context.Y, args);
+ this.outputStream.write(' [done]\n'.white);
+ this.displayPrompt();
+ };
+
+ repl.defineCommand('use', {
+ help: 'Comma seperated list of modules to use inside this Y context',
+ action: use
+ });
+
+ repl.defineCommand('u', {
+ help: 'Alias for use',
+ action: use
+ });
+
+ repl.defineCommand('IO', {
+ help: 'Alias for io',
+ action: function() {
+ this.commands['.io'].action.apply(this, arguments);
+ }
+ });
+ repl.defineCommand('io', {
+ help: 'Make an IO request to the passed URL',
+ action: function(url) {
+ var self = this,
+ Y = this.context.Y;
+ self.outputStream.write('Making IO Request: '.magenta + url.yellow);
+ Y.use('io', function() {
+ Y.io(url, {
+ on: {
+ complete: function(id, e) {
+ self.outputStream.write(' [done]\n'.white);
+ self.outputStream.write(' (' + (e.status + '').green + ' ' + e.statusText + '): Content-Type: "' + e.headers['content-type'].green + '"\n\n'.white);
+ var str;
+ try {
+ var data = JSON.parse(e.responseText);
+ str = util.inspect(data, false, Infinity, true);
+ } catch (e) {
+ str = e.responseText;
+ }
+ if (str) {
+ self.outputStream.write(str);
+ }
+ self.outputStream.write('\n\n');
+ self.displayPrompt();
+ }
+ }
+ });
+ });
+ }
+ });
+
+ repl.defineCommand('headers', {
+ help: 'Make an IO request to the passed URL and only return the headers',
+ action: function(url) {
+ var self = this,
+ Y = this.context.Y;
+ self.outputStream.write('Making IO Request: '.magenta + url.yellow);
+ Y.use('io', function() {
+ Y.io(url, {
+ on: {
+ complete: function(id, e) {
+ self.outputStream.write(' [done]\n'.white);
+ self.outputStream.write(' (' + (e.status + '').green + ' ' + e.statusText + '): Content-Type: "' + e.headers['content-type'].green + '"\n\n'.white);
+ var str = util.inspect(e.headers, false, Infinity, true);
+ self.outputStream.write(str);
+ self.outputStream.write('\n\n');
+ self.displayPrompt();
+ }
+ }
+ });
+ });
+ }
+ });
+
+ repl.defineCommand('YQL', {
+ help: 'Alias for yql',
+ action: function() {
+ this.commands['.yql'].action.apply(this, arguments);
+ }
+ });
+
+ repl.defineCommand('yql', {
+ help: 'Make an YQL request with the passed SQL statement',
+ action: function(sql) {
+ var self = this,
+ Y = this.context.Y;
+ self.outputStream.write('Making YQL Request: '.magenta + sql.yellow);
+ Y.use('yql', function() {
+ Y.YQL(sql, function(r) {
+ self.outputStream.write(' [done]\n'.white);
+ var str;
+ try {
+ str = util.inspect(r.query.results, false, Infinity, true);
+ } catch (e) {
+ str = util.inspect(r, false, Infinity, true);
+ }
+ self.outputStream.write(str);
+ self.outputStream.write('\n\n');
+ self.displayPrompt();
+ });
+ });
+ }
+ });
+
+ repl.defineCommand('get', {
+ help: 'Get a remote/local script and eval it into this context',
+ action: function(url) {
+ var self = this;
+ self.outputStream.write('Fetching Script: '.magenta + url.yellow);
+ Y.Get.script(url, {
+ onSuccess: function() {
+ self.outputStream.write(' [done]\n'.white);
+ self.displayPrompt();
+ }
+ });
+ }
+ });
+
+ return repl;
+};
+
View
5 package.json
@@ -1,7 +1,7 @@
{
"name": "yui-repl",
"description": "YUI 3 Powered REPL",
- "version": "0.0.4",
+ "version": "0.0.5",
"author": "Dav Glass <davglass@gmail.com>",
"bugs": { "web": "http://github.com/davglass/yui-repl/issues" },
"os": [ "darwin", "linux" ],
@@ -18,6 +18,7 @@
"dependencies": {
"yui3": ">=0.5.26",
"yui3-core": "3.3.0",
- "colors": "0.3.0"
+ "colors": "0.3.0",
+ "cli": "0.2.6"
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.