-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f1d82df
Showing
6 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('./src/repl'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |