Skip to content

Commit

Permalink
repl: persist history in plain text
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Fishrock123 authored and rvagg committed Aug 4, 2015
1 parent f7d5e4c commit ed6c249
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/internal/repl.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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}.`));
}
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down

0 comments on commit ed6c249

Please sign in to comment.