1 // Generated by CoffeeScript 1.8.0
3 var CoffeeScript, addHistory, addMultilineHandler, fs, getCommandId, merge, nodeREPL, path, replDefaults, updateSyntaxError, vm, _ref;
7 path = require('path');
11 nodeREPL = require('repl');
13 CoffeeScript = require('./coffee-script');
15 _ref = require('./helpers'), merge = _ref.merge, updateSyntaxError = _ref.updateSyntaxError;
19 historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,
20 historyMaxInputSize: 10240,
21 "eval": function(input, context, filename, cb) {
22 var Assign, Block, Literal, Value, ast, err, js, result, _ref1;
23 input = input.replace(/\uFF00/g, '\n');
24 input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
25 _ref1 = require('./nodes'), Block = _ref1.Block, Assign = _ref1.Assign, Value = _ref1.Value, Literal = _ref1.Literal;
27 ast = CoffeeScript.nodes(input);
28 ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);
31 locals: Object.keys(context)
33 result = context === global ? vm.runInThisContext(js, filename) : vm.runInContext(js, context, filename);
34 return cb(null, result);
37 updateSyntaxError(err, input);
43 addMultilineHandler = function(repl) {
44 var inputStream, multiline, nodeLineListener, origPrompt, outputStream, rli, _ref1;
45 rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
46 origPrompt = (_ref1 = repl._prompt) != null ? _ref1 : repl.prompt;
49 initialPrompt: origPrompt.replace(/^[^> ]*/, function(x) {
50 return x.replace(/./g, '-');
52 prompt: origPrompt.replace(/^[^> ]*>?/, function(x) {
53 return x.replace(/./g, '.');
57 nodeLineListener = rli.listeners('line')[0];
58 rli.removeListener('line', nodeLineListener);
59 rli.on('line', function(cmd) {
60 if (multiline.enabled) {
61 multiline.buffer += "" + cmd + "\n";
62 rli.setPrompt(multiline.prompt);
65 rli.setPrompt(origPrompt);
66 nodeLineListener(cmd);
69 return inputStream.on('keypress', function(char, key) {
70 if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
73 if (multiline.enabled) {
74 if (!multiline.buffer.match(/\n/)) {
75 multiline.enabled = !multiline.enabled;
76 rli.setPrompt(origPrompt);
80 if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
83 multiline.enabled = !multiline.enabled;
86 rli.output.cursorTo(0);
87 rli.output.clearLine(1);
88 multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
89 rli.emit('line', multiline.buffer);
90 multiline.buffer = '';
92 multiline.enabled = !multiline.enabled;
93 rli.setPrompt(multiline.initialPrompt);
99 addHistory = function(repl, filename, maxSize) {
100 var buffer, fd, lastLine, readFd, size, stat;
103 stat = fs.statSync(filename);
104 size = Math.min(maxSize, stat.size);
105 readFd = fs.openSync(filename, 'r');
106 buffer = new Buffer(size);
107 fs.readSync(readFd, buffer, 0, size, stat.size - size);
108 repl.rli.history = buffer.toString().split('\n').reverse();
109 if (stat.size > maxSize) {
110 repl.rli.history.pop();
112 if (repl.rli.history[0] === '') {
113 repl.rli.history.shift();
115 repl.rli.historyIndex = -1;
116 lastLine = repl.rli.history[0];
118 fd = fs.openSync(filename, 'a');
119 repl.rli.addListener('line', function(code) {
120 if (code && code.length && code !== '.history' && lastLine !== code) {
121 fs.write(fd, "" + code + "\n");
122 return lastLine = code;
125 repl.rli.on('exit', function() {
128 return repl.commands[getCommandId(repl, 'history')] = {
129 help: 'Show command history',
131 repl.outputStream.write("" + (repl.rli.history.slice(0).reverse().join('\n')) + "\n");
132 return repl.displayPrompt();
137 getCommandId = function(repl, commandName) {
138 var commandsHaveLeadingDot;
139 commandsHaveLeadingDot = repl.commands['.help'] != null;
140 if (commandsHaveLeadingDot) {
141 return "." + commandName;
148 start: function(opts) {
149 var build, major, minor, repl, _ref1;
153 _ref1 = process.versions.node.split('.').map(function(n) {
155 }), major = _ref1[0], minor = _ref1[1], build = _ref1[2];
156 if (major === 0 && minor < 8) {
157 console.warn("Node 0.8.0+ required for CoffeeScript REPL");
160 CoffeeScript.register();
161 process.argv = ['coffee'].concat(process.argv.slice(2));
162 opts = merge(replDefaults, opts);
163 repl = nodeREPL.start(opts);
164 repl.on('exit', function() {
165 return repl.outputStream.write('\n');
167 addMultilineHandler(repl);
168 if (opts.historyFile) {
169 addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
171 repl.commands[getCommandId(repl, 'load')].help = 'Load code from a file into this REPL session';