}
const repl = REPL.start(opts);
- if (opts.terminal && env.NODE_REPL_HISTORY_FILE) {
- return setupHistory(repl, env.NODE_REPL_HISTORY_FILE, cb);
+ if (opts.terminal && env.NODE_REPL_HISTORY !== '') {
+ return setupHistory(repl, env.NODE_REPL_HISTORY,
+ env.NODE_REPL_HISTORY_FILE, cb);
}
repl._historyPrev = _replHistoryMessage;
cb(null, repl);
}
-function setupHistory(repl, historyPath, ready) {
+function setupHistory(repl, historyPath, oldHistoryPath, ready) {
if (!historyPath) {
try {
historyPath = path.join(os.homedir(), '.node_repl_history');
}
if (data) {
+ repl.history = data.split(/[\n\r]+/).slice(-repl.historySize);
+ } else if (oldHistoryPath) {
+ // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
+ repl._writeToOutput(
+ '\nConverting old JSON repl history to line-separated history.\n' +
+ `The new repl history file can be found at ${historyPath}.\n`);
+ repl._refreshLine();
+
try {
- repl.history = JSON.parse(data);
+ repl.history = JSON.parse(fs.readFileSync(oldHistoryPath, 'utf8'));
if (!Array.isArray(repl.history)) {
throw new Error('Expected array, got ' + typeof repl.history);
}
- repl.history.slice(-repl.historySize);
+ repl.history = repl.history.slice(-repl.historySize);
} catch (err) {
return ready(
- new Error(`Could not parse history data in ${historyPath}.`));
+ new Error(`Could not parse history data in ${oldHistoryPath}.`));
}
}
return;
}
writing = true;
- const historyData = JSON.stringify(repl.history, null, 2);
+ const historyData = repl.history.join(os.EOL);
fs.write(repl._historyHandle, historyData, 0, 'utf8', onwritten);
}
if (this.history.length === 0) {
this._writeToOutput(
'\nPersistent history support disabled. ' +
- 'Set the NODE_REPL_HISTORY_FILE environment variable to ' +
+ 'Set the NODE_REPL_HISTORY environment\nvariable to ' +
'a valid, user-writable path to enable.\n'
);
this._refreshLine();