UNPKG

skypager-repl

Version:
49 lines (39 loc) 1.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _fs = require('fs'); exports.default = function (repl, file) { console.log('Setting up history', file); try { var stat = (0, _fs.statSync)(file); // eslint-disable-line repl.rli.history = (0, _fs.readFileSync)(file, 'utf-8').split('\n').reverse(); repl.rli.history.shift(); repl.rli.historyIndex = 0; } catch (e) {} var fd = (0, _fs.openSync)(file, 'a'); repl.rli.addListener('line', function (code) { if (code && code !== '.history') { (0, _fs.write)(fd, code + '\n', function () {}); } else { repl.rli.historyIndex++; repl.rli.history.pop(); } }); process.on('exit', function () { (0, _fs.closeSync)(fd); }); repl.commands['.history'] = { help: 'Show the history', action: function action() { var out = []; repl.rli.history.forEach(function (v) { out.push(v); }); repl.outputStream.write(out.reverse().join('\n') + '\n', function (err) {}); repl.displayPrompt(); } }; return repl; }; //# sourceMappingURL=history.js.map