From ed6c24910469dd366770a307e56cd20ba5631f8f Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Mon, 3 Aug 2015 13:10:19 -0700 Subject: [PATCH] repl: persist history in plain text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Chris Dickinson Reviewed-By: Roman Reiss --- lib/internal/repl.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/internal/repl.js b/lib/internal/repl.js index 790ca15d622b13..75909f72e2415e 100644 --- a/lib/internal/repl.js +++ b/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();