Skip to content

Commit ed6c249

Browse files
Fishrock123rvagg
authored andcommitted
repl: persist history in plain text
Persists the REPL history in plain text using the new NODE_REPL_HISTORY environment variable. Deprecates NODE_REPL_HISTORY_FILE. The REPL will notify the user and automatically convert the history to the new format if files are specified. PR-URL: #2224 Reviewed-By: Michaël Zasso <mic.besace@gmail.com> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent f7d5e4c commit ed6c249

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/internal/repl.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ function createRepl(env, opts, cb) {
5858
}
5959

6060
const repl = REPL.start(opts);
61-
if (opts.terminal && env.NODE_REPL_HISTORY_FILE) {
62-
return setupHistory(repl, env.NODE_REPL_HISTORY_FILE, cb);
61+
if (opts.terminal && env.NODE_REPL_HISTORY !== '') {
62+
return setupHistory(repl, env.NODE_REPL_HISTORY,
63+
env.NODE_REPL_HISTORY_FILE, cb);
6364
}
6465
repl._historyPrev = _replHistoryMessage;
6566
cb(null, repl);
6667
}
6768

68-
function setupHistory(repl, historyPath, ready) {
69+
function setupHistory(repl, historyPath, oldHistoryPath, ready) {
6970
if (!historyPath) {
7071
try {
7172
historyPath = path.join(os.homedir(), '.node_repl_history');
@@ -106,15 +107,23 @@ function setupHistory(repl, historyPath, ready) {
106107
}
107108

108109
if (data) {
110+
repl.history = data.split(/[\n\r]+/).slice(-repl.historySize);
111+
} else if (oldHistoryPath) {
112+
// Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
113+
repl._writeToOutput(
114+
'\nConverting old JSON repl history to line-separated history.\n' +
115+
`The new repl history file can be found at ${historyPath}.\n`);
116+
repl._refreshLine();
117+
109118
try {
110-
repl.history = JSON.parse(data);
119+
repl.history = JSON.parse(fs.readFileSync(oldHistoryPath, 'utf8'));
111120
if (!Array.isArray(repl.history)) {
112121
throw new Error('Expected array, got ' + typeof repl.history);
113122
}
114-
repl.history.slice(-repl.historySize);
123+
repl.history = repl.history.slice(-repl.historySize);
115124
} catch (err) {
116125
return ready(
117-
new Error(`Could not parse history data in ${historyPath}.`));
126+
new Error(`Could not parse history data in ${oldHistoryPath}.`));
118127
}
119128
}
120129

@@ -154,7 +163,7 @@ function setupHistory(repl, historyPath, ready) {
154163
return;
155164
}
156165
writing = true;
157-
const historyData = JSON.stringify(repl.history, null, 2);
166+
const historyData = repl.history.join(os.EOL);
158167
fs.write(repl._historyHandle, historyData, 0, 'utf8', onwritten);
159168
}
160169

@@ -177,7 +186,7 @@ function _replHistoryMessage() {
177186
if (this.history.length === 0) {
178187
this._writeToOutput(
179188
'\nPersistent history support disabled. ' +
180-
'Set the NODE_REPL_HISTORY_FILE environment variable to ' +
189+
'Set the NODE_REPL_HISTORY environment\nvariable to ' +
181190
'a valid, user-writable path to enable.\n'
182191
);
183192
this._refreshLine();

0 commit comments

Comments
 (0)