UNPKG

nesh

Version:

An enhanced, extensible shell for Node.js

89 lines (74 loc) 2.67 kB
// Generated by CoffeeScript 1.11.1 /* History Plugin - adds persistent history to the interpreter so long as no `.history` command has already been defined. This plugin adds two new options: * historyFile: the filename of where to store history lines * historyMaxInputSize: Maximum number of bytes to load from the history file History support can be disabled by setting `historyFile` to null or false in the interpreter options on startup: nesh.start({historyFile: null}, function (err) { ... }); */ (function() { var fs, path; fs = require('fs'); path = require('path'); exports.name = 'history'; exports.description = 'Provides persistent history between sessions'; exports.setup = function(context) { var config, defaults, ref; ref = context.nesh, defaults = ref.defaults, config = ref.config; if (defaults.historyFile == null) { defaults.historyFile = path.join(config.home, '.node_history'); } return defaults.historyMaxInputSize != null ? defaults.historyMaxInputSize : defaults.historyMaxInputSize = 10240; }; exports.postStart = function(context) { var buffer, cmd, fd, lastLine, maxSize, readFd, repl, size, stat; repl = context.repl; if (!repl.opts.historyFile) { return; } if (repl.commands['.history']) { return; } maxSize = repl.opts.historyMaxInputSize; lastLine = null; try { stat = fs.statSync(repl.opts.historyFile); size = Math.min(maxSize, stat.size); readFd = fs.openSync(repl.opts.historyFile, 'r'); buffer = new Buffer(size); fs.readSync(readFd, buffer, 0, size, stat.size - size); repl.rli.history = buffer.toString().split('\n').reverse(); if (stat.size > maxSize) { repl.rli.history.pop(); } if (repl.rli.history[0] === '') { repl.rli.history.shift(); } repl.rli.historyIndex = -1; lastLine = repl.rli.history[0]; } catch (error) {} fd = fs.openSync(repl.opts.historyFile, 'a'); repl.rli.addListener('line', function(code) { if (code && code.length && code !== '.history' && lastLine !== code) { fs.write(fd, code + "\n", function() {}); return lastLine = code; } }); repl.rli.on('exit', function() { return fs.close(fd, function() {}); }); cmd = { help: 'Show command history', action: function() { repl.outputStream.write((repl.rli.history.slice(0).reverse().join('\n')) + "\n"); return repl.displayPrompt(); } }; return repl.defineCommand('history', cmd); }; }).call(this); //# sourceMappingURL=history.js.map