repl: persist history in plain text
authorJeremiah Senkpiel <fishrock123@rocketmail.com>
Mon, 3 Aug 2015 20:10:19 +0000 (13:10 -0700)
committerRod Vagg <rod@vagg.org>
Tue, 4 Aug 2015 18:56:18 +0000 (11:56 -0700)
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>
lib/internal/repl.js

index 790ca15..75909f7 100644 (file)
@@ -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();