react / wstein / node_modules / jest-cli / node_modules / jasmine-only / node_modules / coffee-script / lib / coffee-script / repl.js
81146 views// Generated by CoffeeScript 1.6.31(function() {2var CoffeeScript, addHistory, addMultilineHandler, fs, merge, nodeREPL, path, prettyErrorMessage, replDefaults, vm, _ref;34fs = require('fs');56path = require('path');78vm = require('vm');910nodeREPL = require('repl');1112CoffeeScript = require('./coffee-script');1314_ref = require('./helpers'), merge = _ref.merge, prettyErrorMessage = _ref.prettyErrorMessage;1516replDefaults = {17prompt: 'coffee> ',18historyFile: process.env.HOME ? path.join(process.env.HOME, '.coffee_history') : void 0,19historyMaxInputSize: 10240,20"eval": function(input, context, filename, cb) {21var Assign, Block, Literal, Value, ast, err, js, _ref1;22input = input.replace(/\uFF00/g, '\n');23input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');24_ref1 = require('./nodes'), Block = _ref1.Block, Assign = _ref1.Assign, Value = _ref1.Value, Literal = _ref1.Literal;25try {26ast = CoffeeScript.nodes(input);27ast = new Block([new Assign(new Value(new Literal('_')), ast, '=')]);28js = ast.compile({29bare: true,30locals: Object.keys(context)31});32return cb(null, vm.runInContext(js, context, filename));33} catch (_error) {34err = _error;35return cb(prettyErrorMessage(err, filename, input, true));36}37}38};3940addMultilineHandler = function(repl) {41var inputStream, multiline, nodeLineListener, outputStream, rli;42rli = repl.rli, inputStream = repl.inputStream, outputStream = repl.outputStream;43multiline = {44enabled: false,45initialPrompt: repl.prompt.replace(/^[^> ]*/, function(x) {46return x.replace(/./g, '-');47}),48prompt: repl.prompt.replace(/^[^> ]*>?/, function(x) {49return x.replace(/./g, '.');50}),51buffer: ''52};53nodeLineListener = rli.listeners('line')[0];54rli.removeListener('line', nodeLineListener);55rli.on('line', function(cmd) {56if (multiline.enabled) {57multiline.buffer += "" + cmd + "\n";58rli.setPrompt(multiline.prompt);59rli.prompt(true);60} else {61nodeLineListener(cmd);62}63});64return inputStream.on('keypress', function(char, key) {65if (!(key && key.ctrl && !key.meta && !key.shift && key.name === 'v')) {66return;67}68if (multiline.enabled) {69if (!multiline.buffer.match(/\n/)) {70multiline.enabled = !multiline.enabled;71rli.setPrompt(repl.prompt);72rli.prompt(true);73return;74}75if ((rli.line != null) && !rli.line.match(/^\s*$/)) {76return;77}78multiline.enabled = !multiline.enabled;79rli.line = '';80rli.cursor = 0;81rli.output.cursorTo(0);82rli.output.clearLine(1);83multiline.buffer = multiline.buffer.replace(/\n/g, '\uFF00');84rli.emit('line', multiline.buffer);85multiline.buffer = '';86} else {87multiline.enabled = !multiline.enabled;88rli.setPrompt(multiline.initialPrompt);89rli.prompt(true);90}91});92};9394addHistory = function(repl, filename, maxSize) {95var buffer, fd, lastLine, readFd, size, stat;96lastLine = null;97try {98stat = fs.statSync(filename);99size = Math.min(maxSize, stat.size);100readFd = fs.openSync(filename, 'r');101buffer = new Buffer(size);102fs.readSync(readFd, buffer, 0, size, stat.size - size);103repl.rli.history = buffer.toString().split('\n').reverse();104if (stat.size > maxSize) {105repl.rli.history.pop();106}107if (repl.rli.history[0] === '') {108repl.rli.history.shift();109}110repl.rli.historyIndex = -1;111lastLine = repl.rli.history[0];112} catch (_error) {}113fd = fs.openSync(filename, 'a');114repl.rli.addListener('line', function(code) {115if (code && code.length && code !== '.history' && lastLine !== code) {116fs.write(fd, "" + code + "\n");117return lastLine = code;118}119});120repl.rli.on('exit', function() {121return fs.close(fd);122});123return repl.commands['.history'] = {124help: 'Show command history',125action: function() {126repl.outputStream.write("" + (repl.rli.history.slice(0).reverse().join('\n')) + "\n");127return repl.displayPrompt();128}129};130};131132module.exports = {133start: function(opts) {134var build, major, minor, repl, _ref1;135if (opts == null) {136opts = {};137}138_ref1 = process.versions.node.split('.').map(function(n) {139return parseInt(n);140}), major = _ref1[0], minor = _ref1[1], build = _ref1[2];141if (major === 0 && minor < 8) {142console.warn("Node 0.8.0+ required for CoffeeScript REPL");143process.exit(1);144}145opts = merge(replDefaults, opts);146repl = nodeREPL.start(opts);147repl.on('exit', function() {148return repl.outputStream.write('\n');149});150addMultilineHandler(repl);151if (opts.historyFile) {152addHistory(repl, opts.historyFile, opts.historyMaxInputSize);153}154return repl;155}156};157158}).call(this);159160161