repl.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Generated by CoffeeScript 1.10.0
  2. (function() {
  3. var CoffeeScript, addHistory, addMultilineHandler, fs, getCommandId, merge, nodeREPL, path, ref, replDefaults, runInContext, updateSyntaxError, vm;
  4. fs = require('fs');
  5. path = require('path');
  6. vm = require('vm');
  7. nodeREPL = require('repl');
  8. CoffeeScript = require('./coffee-script');
  9. ref = require('./helpers'), merge = ref.merge, updateSyntaxError = ref.updateSyntaxError;
  10. replDefaults = {
  11. prompt: 'coffee> ',
  12. historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,
  13. historyMaxInputSize: 10240,
  14. "eval": function(input, context, filename, cb) {
  15. var Assign, Block, Literal, Value, ast, err, error, js, ref1, referencedVars, token, tokens;
  16. input = input.replace(/\uFF00/g, '\n');
  17. input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
  18. ref1 = require('./nodes'), Block = ref1.Block, Assign = ref1.Assign, Value = ref1.Value, Literal = ref1.Literal;
  19. try {
  20. tokens = CoffeeScript.tokens(input);
  21. referencedVars = (function() {
  22. var i, len, results;
  23. results = [];
  24. for (i = 0, len = tokens.length; i < len; i++) {
  25. token = tokens[i];
  26. if (token.variable) {
  27. results.push(token[1]);
  28. }
  29. }
  30. return results;
  31. })();
  32. ast = CoffeeScript.nodes(tokens);
  33. ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);
  34. js = ast.compile({
  35. bare: true,
  36. locals: Object.keys(context),
  37. referencedVars: referencedVars
  38. });
  39. return cb(null, runInContext(js, context, filename));
  40. } catch (error) {
  41. err = error;
  42. updateSyntaxError(err, input);
  43. return cb(err);
  44. }
  45. }
  46. };
  47. runInContext = function(js, context, filename) {
  48. if (context === global) {
  49. return vm.runInThisContext(js, filename);
  50. } else {
  51. return vm.runInContext(js, context, filename);
  52. }
  53. };
  54. addMultilineHandler = function(repl) {
  55. var inputStream, multiline, nodeLineListener, origPrompt, outputStream, ref1, rli;
  56. rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;
  57. origPrompt = (ref1 = repl._prompt) != null ? ref1 : repl.prompt;
  58. multiline = {
  59. enabled: false,
  60. initialPrompt: origPrompt.replace(/^[^> ]*/, function(x) {
  61. return x.replace(/./g, '-');
  62. }),
  63. prompt: origPrompt.replace(/^[^> ]*>?/, function(x) {
  64. return x.replace(/./g, '.');
  65. }),
  66. buffer: ''
  67. };
  68. nodeLineListener = rli.listeners('line')[0];
  69. rli.removeListener('line', nodeLineListener);
  70. rli.on('line', function(cmd) {
  71. if (multiline.enabled) {
  72. multiline.buffer += cmd + "\n";
  73. rli.setPrompt(multiline.prompt);
  74. rli.prompt(true);
  75. } else {
  76. rli.setPrompt(origPrompt);
  77. nodeLineListener(cmd);
  78. }
  79. });
  80. return inputStream.on('keypress', function(char, key) {
  81. if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {
  82. return;
  83. }
  84. if (multiline.enabled) {
  85. if (!multiline.buffer.match(/\n/)) {
  86. multiline.enabled = !multiline.enabled;
  87. rli.setPrompt(origPrompt);
  88. rli.prompt(true);
  89. return;
  90. }
  91. if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
  92. return;
  93. }
  94. multiline.enabled = !multiline.enabled;
  95. rli.line = '';
  96. rli.cursor = 0;
  97. rli.output.cursorTo(0);
  98. rli.output.clearLine(1);
  99. multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');
  100. rli.emit('line', multiline.buffer);
  101. multiline.buffer = '';
  102. } else {
  103. multiline.enabled = !multiline.enabled;
  104. rli.setPrompt(multiline.initialPrompt);
  105. rli.prompt(true);
  106. }
  107. });
  108. };
  109. addHistory = function(repl, filename, maxSize) {
  110. var buffer, fd, lastLine, readFd, size, stat;
  111. lastLine = null;
  112. try {
  113. stat = fs.statSync(filename);
  114. size = Math.min(maxSize, stat.size);
  115. readFd = fs.openSync(filename, 'r');
  116. buffer = new Buffer(size);
  117. fs.readSync(readFd, buffer, 0, size, stat.size - size);
  118. fs.close(readFd);
  119. repl.rli.history = buffer.toString().split('\n').reverse();
  120. if (stat.size > maxSize) {
  121. repl.rli.history.pop();
  122. }
  123. if (repl.rli.history[0] === '') {
  124. repl.rli.history.shift();
  125. }
  126. repl.rli.historyIndex = -1;
  127. lastLine = repl.rli.history[0];
  128. } catch (undefined) {}
  129. fd = fs.openSync(filename, 'a');
  130. repl.rli.addListener('line', function(code) {
  131. if (code && code.length && code !== '.history' && lastLine !== code) {
  132. fs.write(fd, code + "\n");
  133. return lastLine = code;
  134. }
  135. });
  136. repl.on('exit', function() {
  137. return fs.close(fd);
  138. });
  139. return repl.commands[getCommandId(repl, 'history')] = {
  140. help: 'Show command history',
  141. action: function() {
  142. repl.outputStream.write((repl.rli.history.slice(0).reverse().join('\n')) + "\n");
  143. return repl.displayPrompt();
  144. }
  145. };
  146. };
  147. getCommandId = function(repl, commandName) {
  148. var commandsHaveLeadingDot;
  149. commandsHaveLeadingDot = repl.commands['.help'] != null;
  150. if (commandsHaveLeadingDot) {
  151. return "." + commandName;
  152. } else {
  153. return commandName;
  154. }
  155. };
  156. module.exports = {
  157. start: function(opts) {
  158. var build, major, minor, ref1, repl;
  159. if (opts == null) {
  160. opts = {};
  161. }
  162. ref1 = process.versions.node.split('.').map(function(n) {
  163. return parseInt(n);
  164. }), major = ref1[0], minor = ref1[1], build = ref1[2];
  165. if (major === 0 && minor < 8) {
  166. console.warn("Node 0.8.0+ required for CoffeeScript REPL");
  167. process.exit(1);
  168. }
  169. CoffeeScript.register();
  170. process.argv = ['coffee'].concat(process.argv.slice(2));
  171. opts = merge(replDefaults, opts);
  172. repl = nodeREPL.start(opts);
  173. if (opts.prelude) {
  174. runInContext(opts.prelude, repl.context, 'prelude');
  175. }
  176. repl.on('exit', function() {
  177. if (!repl.rli.closed) {
  178. return repl.outputStream.write('\n');
  179. }
  180. });
  181. addMultilineHandler(repl);
  182. if (opts.historyFile) {
  183. addHistory(repl, opts.historyFile, opts.historyMaxInputSize);
  184. }
  185. repl.commands[getCommandId(repl, 'load')].help = 'Load code from a file into this REPL session';
  186. return repl;
  187. }
  188. };
  189. }).call(this);