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