Skip to content

Commit

Permalink
repl.js v1.0.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
StreetStrider committed Dec 9, 2013
0 parents commit f1d82df
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

node_modules/
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name" : "repl.js",
"version": "1.0.0",
"author" : "StreetStrider",
"license": "MIT",
"private": true,

"main": "repl.js",

"dependencies":
{
"cli-color": ">=0.2.3",
"lodash": ">=2.4.1"
}
}
3 changes: 3 additions & 0 deletions repl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('./src/repl');
61 changes: 61 additions & 0 deletions src/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@



var
_ = require('lodash'),
clc = require('cli-color'),

console_error = console.error,
console_warn = console.warn,
console_info = console.info;

module.exports = console;

console.color = clc;

function red (x)
{
return clc.red(x);
}

console.error = function error ()
{
arguments = _.map(arguments, red);
console_error.apply(console, arguments);
};


function yellow (x)
{
return clc.xterm(214)(x);
}

console.warn = function warn ()
{
arguments = _.map(arguments, yellow);
console_warn.apply(console, arguments);
};


function blue (x)
{
return clc.blue(x);
}

console.info = function info ()
{
arguments = _.map(arguments, blue);
console_info.apply(console, arguments);
};


console.dir = function dir (object, options)
{
options || (options = 0);
if (typeof Object(options).valueOf() === 'number')
{
options = { depth: options };
}
_.extend(options, { colors: true });
console.log(util.inspect(object, options));
};
71 changes: 71 additions & 0 deletions src/parseArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@



var
cwd = process.cwd(),
path = require('path');

module.exports = function parseArgs (context, modules)
{
modules.forEach(function (module)
{
if (module.match(/^[a-zA-Z_][a-zA-Z01-9_-]*$/))
{
var moduleVar = varName(module);
tryModule(context, moduleVar, module);
}
else
{
var match = module.match(/^([a-zA-Z_][a-zA-Z01-9_]*)=(.+)$/);
if (match)
{
var
moduleVar = match[1],
modulePath = path.join(cwd, match[2]);

tryModule(context, moduleVar, modulePath);
}
else
{
var match = module.match(/([a-zA-Z_][a-zA-Z01-9_-]*)(\.[^\/]*)?$/);
if (match)
{
var
moduleVar = varName(match[1]),
modulePath = path.join(cwd, module);

tryModule(context, moduleVar, modulePath);
}
else
{
console.warn('Cannot recognize module `'+ module +'`.');
}
}
}
});
};

function tryModule (context, moduleVar, modulePath)
{
try
{
context[moduleVar] = require(modulePath);
if (moduleVar === modulePath)
{
console.info('Loaded `'+ moduleVar +'`.');
}
else
{
console.info('Loaded `'+ modulePath +'` as `'+ moduleVar +'`.');
}
}
catch (e)
{
console.error('Module `'+ modulePath +'` cannot be found.');
}
}

function varName (moduleName)
{
return moduleName.replace(/-/g, '_').replace(/[^a-zA-Z01-9_]/g, '');
}
28 changes: 28 additions & 0 deletions src/repl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@



var
modules = process.argv.slice(2),

repl = require('repl'),

parseArgs = require('./parseArgs'),
console = require('./console'),

context = {};

parseArgs(context, modules);

instance = repl.start({
prompt: 'js > ',
ignoreUndefined: true,
useGlobal: true
});

for (var key in context) if (context.hasOwnProperty(key))
{
global[key] = context[key];
}

global.dir = console.dir;
global.L = require('lodash');

0 comments on commit f1d82df

Please sign in to comment.