Skip to content

Commit 7089ad3

Browse files
author
Boris Diakur
committed
persisting repl session history
For now the history is stored in a file named “.n_repl_history” in the user’s home directory. If some day in the future a sharable default emerges, this may be changed. References: tmpvar/repl.history#6 closes #16
1 parent 722674b commit 7089ad3

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 1.3.0 (2016-03-18)
2+
3+
* **n_** stores its session history under `~/.n_repl_history`
4+
15
### 1.2.0 (2016-03-16)
26

37
* you can now enable FP mode via `$ n_ --fp`

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ n_ >
3636

3737
### FP mode
3838

39-
It is possible to use lodash's functional programming variant `lodash/fp`:
39+
It is possible to use lodashs functional programming variant `lodash/fp`:
4040

4141
```shell
4242
$ n_ --fp
@@ -61,7 +61,9 @@ $ NODE_REPL_MODE=strict n_
6161
n_ >
6262
```
6363

64-
__Note:__
64+
## Notes
65+
66+
### Special character `_`
6567

6668
The `_` character is special in the Node REPL (see [nodejs.org/api/repl.html](http://nodejs.org/api/repl.html#repl_repl_features)).
6769
**n_** redirects this special variable to `$` per default, but you can set your own using the environment variable `SPECIAL_VAR` like this:
@@ -77,4 +79,8 @@ n_ >
7779

7880
Also note that using the command `.clear` you clear the context lodash is bound to.
7981

82+
### History persistence
83+
84+
**n_** stores its session history under `~/.n_repl_history`.
85+
8086
Enjoy!

lib/n_.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
var osHomedir = require('os-homedir'),
4+
path = require('path'),
5+
replHistory = require('repl.history');
6+
37
// used as the special var to assign the result of REPL expressions
48
var specialVar = process.env.SPECIAL_VAR || '$';
59

@@ -15,6 +19,9 @@ var repl = require('repl'),
1519
replMode: (process.env.NODE_REPL_MODE === 'strict' || process.argv.indexOf('--use_strict') !== -1) ? repl.REPL_MODE_STRICT : repl.REPL_MODE_MAGIC
1620
});
1721

22+
// save repl history
23+
replHistory(server, path.join(osHomedir(), '.n_repl_history'));
24+
1825
// create new pristine `lodash` instance
1926
var _ = require('lodash').runInContext(server.context);
2027

@@ -57,7 +64,8 @@ _.each(repl._builtinLibs, function (name) {
5764
var events = server.rli._events;
5865
events.line = _.wrap(events.line, function (line, cmd) {
5966
var context = server.context;
60-
line(cmd);
67+
line[0](cmd); // actual command execution
68+
line[1](cmd); // history persistance
6169
context[specialVar] = context._;
6270
currVal = prevVal;
6371
});

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
],
2323
"license": "MIT",
2424
"dependencies": {
25-
"lodash": "^4.0.0"
25+
"lodash": "^4.0.0",
26+
"os-homedir": "^1.0.1",
27+
"repl.history": "^0.1.3"
2628
},
2729
"devDependencies": {
2830
"coveralls": "^2.11.2",

test/test.js

+36-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
/* global describe, it */
33

44
var assert = require('assert'),
5+
fs = require('fs'),
56
_ = require('lodash'),
67
n_ = require('../lib/n_'),
78
line = n_.rli._events.line,
9+
osHomedir = require('os-homedir'),
10+
path = require('path'),
811
result = null;
912

1013
n_.writer = _.wrap(n_.writer, function (writer, obj) {
@@ -113,13 +116,13 @@ describe('n_', function () {
113116

114117
describe('enabling fp mode', function () {
115118
it('should use lodash/fp', function (done) {
116-
var previousArgv = process.argv;
117119
// enable fp mode
120+
var previousArgv = process.argv;
118121
process.argv = _.concat(previousArgv, ['--fp']);
119122

120123
// now require and setup n_ (it should now use lodash/fp)
121124
reset();
122-
// Reset argv to previous value
125+
// reset argv to previous value
123126
process.argv = previousArgv;
124127

125128
line('_.map(function(v) { return v * 2; }, [1, 2, 3]);');
@@ -137,29 +140,56 @@ describe('n_', function () {
137140
});
138141
it('should throw in strict mode set via environment variable', function (done) {
139142
// enable strict mode
143+
var previousReplMode = process.env.NODE_REPL_MODE;
140144
process.env.NODE_REPL_MODE = 'strict';
141145

142146
// now require and setup n_ (it should now run in strict mode)
143147
reset();
148+
// reset NODE_REPL_MODE to previous value
149+
process.env.NODE_REPL_MODE = previousReplMode;
144150

145151
line('var fixed = {}; Object.preventExtensions(fixed); fixed.newProp = 1;');
146152
assert.equal(result, null);
147153
done();
148154
});
149155
it('should throw in strict mode set via command line option', function (done) {
150-
// reset environment variab
151-
process.env.NODE_REPL_MODE = undefined;
152-
153156
// enable strict mode
154-
process.argv.push('--use_strict');
157+
var previousArgv = process.argv;
158+
process.argv = _.concat(previousArgv, ['--use_strict']);
155159

156160
// now require and setup n_ (it should now run with strict mode enabled)
157161
reset();
162+
// reset argv to previous value
163+
process.argv = previousArgv;
158164

159165
line('var fixed = {}; Object.preventExtensions(fixed); fixed.newProp = 1;');
160166
assert.equal(result, null);
161167
done();
162168
});
163169
});
164170
}
171+
172+
describe('repl history', function () {
173+
it('should save and load repl history across multiple sessions', function (done) {
174+
var historyPath = path.join(osHomedir(), '.n_repl_history');
175+
176+
// delete any previously created history file
177+
fs.unlinkSync(historyPath);
178+
179+
reset(); // new session
180+
line('1+2');
181+
reset(); // new session
182+
line('null');
183+
reset(); // new session
184+
line('"foobar"');
185+
reset(); // new session
186+
187+
// check history (as thoroughly as possible)
188+
var historyFileContent = fs.readFileSync(historyPath, 'utf-8');
189+
assert.equal(historyFileContent, ['1+2', 'null', '"foobar"', ''].join('\n'));
190+
line('.load ' + historyPath);
191+
assert.equal(result, 'foobar');
192+
done();
193+
});
194+
});
165195
});

0 commit comments

Comments
 (0)