Browse Source

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.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
ffded5ac86
  1. 43
      bin/node-repl
  2. 37
      lib/repl.js

43
bin/node-repl

@ -1,43 +1,10 @@
#!/usr/bin/env node #!/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 = ''; include("/repl.js");
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();
});
// vim:ft=javascript // vim:ft=javascript

37
lib/repl.js

@ -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();
});
Loading…
Cancel
Save