mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
Extract the good parts of node-repl into standalone library.
Now you can require("/repl.js") in your server to be able to examine it
while it's running.
This commit is contained in:
@@ -1,43 +1,10 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
node.stdio.open();
|
||||
puts("Welcome to the Node.js REPL.");
|
||||
puts("Enter ECMAScript at the prompt.");
|
||||
puts("Tip 1: Use 'rlwrap node-repl' for a better interface");
|
||||
puts("Tip 2: Type Control-D to exit.");
|
||||
|
||||
var buffered_cmd = '';
|
||||
|
||||
function prompt () {
|
||||
node.stdio.write(buffered_cmd.length ? '... ' : "\nnode> ");
|
||||
}
|
||||
|
||||
node.stdio.write("Welcome to the Node.js REPL.\n");
|
||||
node.stdio.write("Enter ECMAScript at the prompt.\n");
|
||||
node.stdio.write("Type Control-D to exit.\n");
|
||||
node.stdio.write("For more information, see http://tinyclouds.org/node\n");
|
||||
|
||||
prompt();
|
||||
|
||||
var trimmer = /^\s*(.+)\s*$/m;
|
||||
|
||||
node.stdio.addListener("data", function (cmd) {
|
||||
var matches = trimmer.exec(cmd);
|
||||
|
||||
if (matches && matches.length == 2) {
|
||||
cmd = matches[1];
|
||||
try {
|
||||
buffered_cmd += cmd;
|
||||
try {
|
||||
node.stdio.write(JSON.stringify(eval(buffered_cmd)) + "\n");
|
||||
buffered_cmd = '';
|
||||
} catch (e) {
|
||||
if (!(e instanceof SyntaxError))
|
||||
throw e;
|
||||
}
|
||||
} catch (e) {
|
||||
node.stdio.writeError('caught an exception: ' + e + '\n');
|
||||
buffered_cmd = '';
|
||||
}
|
||||
}
|
||||
|
||||
prompt();
|
||||
});
|
||||
include("/repl.js");
|
||||
|
||||
// vim:ft=javascript
|
||||
|
||||
37
lib/repl.js
Normal file
37
lib/repl.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// A repl library that you can include in your own code to get a runtime
|
||||
// interface to your program. Just require("/repl.js").
|
||||
|
||||
node.stdio.open();
|
||||
|
||||
var buffered_cmd = '';
|
||||
var trimmer = /^\s*(.+)\s*$/m;
|
||||
|
||||
exports.prompt = "node> ";
|
||||
|
||||
function displayPrompt () {
|
||||
print(buffered_cmd.length ? '... ' : exports.prompt);
|
||||
}
|
||||
|
||||
displayPrompt();
|
||||
|
||||
node.stdio.addListener("data", function (cmd) {
|
||||
var matches = trimmer.exec(cmd);
|
||||
|
||||
if (matches && matches.length == 2) {
|
||||
cmd = matches[1];
|
||||
try {
|
||||
buffered_cmd += cmd;
|
||||
try {
|
||||
puts(JSON.stringify(eval(buffered_cmd)));
|
||||
buffered_cmd = '';
|
||||
} catch (e) {
|
||||
if (!(e instanceof SyntaxError))
|
||||
throw e;
|
||||
}
|
||||
} catch (e) {
|
||||
puts('caught an exception: ' + e);
|
||||
buffered_cmd = '';
|
||||
}
|
||||
}
|
||||
displayPrompt();
|
||||
});
|
||||
Reference in New Issue
Block a user