Skip to content

Commit

Permalink
A main wrapper to start the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
jhs committed Sep 1, 2011
1 parent 0343e5a commit 889acea
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -32,7 +32,7 @@ task :export => ([:clean, "cli.js"] + LIBRARY + BROWSER_TOOLS) do |task|
'paths' => requirejs_paths,
}

main_js = "follow/cli.js"
main_js = "main.js"

boot.write([ 'require(',
'// Options',
Expand Down
90 changes: 90 additions & 0 deletions browser/main.js
@@ -0,0 +1,90 @@
window.process = { env: {} };

var BANNER = "";

if(!Object.keys)
Object.keys = function(o){
if(typeof o !== 'object')
throw new Error('Object.keys called on non-object: ' + JSON.stringify(o));
var ret=[], p;
for (p in o)
if(Object.prototype.hasOwnProperty.call(o,p))
ret.push(p);
return ret;
}

if(!Array.isArray)
Array.isArray = function(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}

if(!Array.prototype.forEach)
Array.prototype.forEach = function(callback) {
var i, len = this.length;
for(var i = 0; i < len; i++)
callback(this[i], i, this);
}

if(!Array.prototype.reduce)
Array.prototype.reduce = function(callback, state) {
var i, len = this.length;
for(i = 0; i < len; i++)
state = callback(state, this[i]);
return state;
}

if(!Array.prototype.filter)
Array.prototype.filter = function(pred) {
var i, len = this.length, result = [];
for(i = 0; i < len; i++)
if(!! pred(this[i]))
result.push(this[i]);
return result;
}

if(!Array.prototype.map)
Array.prototype.map = function(func) {
var i, len = this.length, result = [];
for(i = 0; i < len; i++)
result.push(func(this[i], i, this));
return result;
}


if(!window.console)
window.console = {};

; ['trace', 'debug', 'log', 'info', 'warn', 'error', 'fatal'].forEach(function(lev) {
window.console[lev] = window.console[lev] || function() {};
})

define(['events', 'querystring', 'follow/cli'], function(events, querystring, cli) {
jQuery('#boot').html('Starting' + (BANNER ? (' '+BANNER) : ""));
// Set up some faux Node stuff.
events.EventEmitter = events.EventEmitter2;
var process = window.process = new events.EventEmitter;

process.env = querystring.parse(window.location.search.slice(1));

process.stdout = {};
process.stdout.write = function(x) {
var con = jQuery('#results');
var html = con.html();
con.html(html + x);
}

process.exit = function(code) {
if(code === 0)
console.log("'EXIT' " + code);
else
console.error("'EXIT' " + code);
}


return function() { // main()
console.log('Main running');

try { cli.main() }
catch(er) { console.log("Error starting tests"); console.log(er) }
}
})
10 changes: 2 additions & 8 deletions cli.js
Expand Up @@ -26,13 +26,7 @@ function usage() {
}

function main() {
var opts;
if(require.isBrowser)
opts = require('querystring').parse(window.location.search);
else
opts = {'db': process.argv[2] };

var db = opts.db || "http://127.0.0.1:5984";
var db = require.isBrowser ? (process.env.db || '/_users') : process.argv[2];

if(! /^https?:\/\//.test(db))
db = 'http://' + db;
Expand Down Expand Up @@ -95,6 +89,6 @@ function main() {
feed.follow();
}

module.exports = main;
exports.main = main;
if(!require.isBrowser && process.argv[1] == module.filename)
main();

0 comments on commit 889acea

Please sign in to comment.