Browse Source

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: https://github.com/nodejs/io.js/pull/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>
v4.0.0-rc
Jeremiah Senkpiel 9 years ago
committed by Rod Vagg
parent
commit
ed6c249104
  1. 25
      lib/internal/repl.js

25
lib/internal/repl.js

@ -58,14 +58,15 @@ function createRepl(env, opts, cb) {
}
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');
@ -106,15 +107,23 @@ function setupHistory(repl, historyPath, ready) {
}
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}.`));
}
}
@ -154,7 +163,7 @@ function setupHistory(repl, historyPath, ready) {
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);
}
@ -177,7 +186,7 @@ function _replHistoryMessage() {
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();

Loading…
Cancel
Save