"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/coffee-script/lib/coffee-script/repl.js" (8 Mar 2017, 6321 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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