├── .gitignore ├── LICENSE.txt ├── README.md ├── bin └── repl.history ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | gmon.out 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright © 2014 Elijah Insua 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the “Software”), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # repl.history 2 | 3 | Persist a node repl's history to a file. 4 | 5 | ## from node 6 | 7 | install: `npm install repl.history` 8 | 9 | ```javascript 10 | var os = require('os'); 11 | var path = require('path'); 12 | 13 | var historyFile = path.join(os.homedir(), '.node_history'); 14 | 15 | var repl = require('repl').start('> '); 16 | require('repl.history')(repl, historyFile); 17 | ``` 18 | 19 | this will drop a `.node_history` file in your home directory. 20 | 21 | ## from the command line 22 | 23 | install: `npm install -g repl.history` 24 | 25 | run `repl.history` on the command line 26 | 27 | A file `~/.node_history` will be created. 28 | 29 | I like to alias it to `nr` for node repl 30 | -------------------------------------------------------------------------------- /bin/repl.history: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var repl = require('repl').start('> '); 3 | require('../')(repl, process.env.HOME + '/.node_history'); 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | module.exports = function (repl, file) { 3 | 4 | try { 5 | var stat = fs.statSync(file); 6 | repl.rli.history = fs.readFileSync(file, 'utf-8').split('\n').reverse(); 7 | repl.rli.history.shift(); 8 | repl.rli.historyIndex = -1; // will be incremented before pop 9 | } catch (e) {} 10 | 11 | var fd = fs.openSync(file, 'a'), reval = repl.eval; 12 | var wstream = fs.createWriteStream(file, { 13 | fd: fd 14 | }); 15 | wstream.on('error', function(err) { 16 | throw err; 17 | }); 18 | 19 | repl.rli.addListener('line', function(code) { 20 | if (code && code !== '.history') { 21 | wstream.write(code + '\n'); 22 | } else { 23 | repl.rli.historyIndex++; 24 | repl.rli.history.pop(); 25 | } 26 | }); 27 | 28 | process.on('exit', function() { 29 | fs.closeSync(fd); 30 | }); 31 | 32 | repl.commands['history'] = { 33 | help : 'Show the history', 34 | action : function() { 35 | var out = []; 36 | repl.rli.history.forEach(function(v, k) { 37 | out.push(v); 38 | }); 39 | repl.outputStream.write(out.reverse().join('\n') + '\n'); 40 | repl.displayPrompt(); 41 | } 42 | }; 43 | }; 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Elijah Insua (http://tmpvar.com)", 3 | "contributors": [ 4 | { 5 | "name": "Scott Nelson", 6 | "url": "https://github.com/scttnlsn" 7 | } 8 | ], 9 | "name": "repl.history", 10 | "description": "add history to node's repl", 11 | "license": "MIT", 12 | "version": "0.1.4", 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/tmpvar/repl.history" 16 | }, 17 | "dependencies": {}, 18 | "devDependencies": {}, 19 | "bin": { 20 | "repl.history": "./bin/repl.history" 21 | } 22 | } 23 | --------------------------------------------------------------------------------