repl: default persistence to ~/.node_repl_history
authorJeremiah Senkpiel <fishrock123@rocketmail.com>
Tue, 4 Aug 2015 06:24:03 +0000 (23:24 -0700)
committerRod Vagg <rod@vagg.org>
Tue, 4 Aug 2015 18:56:18 +0000 (11:56 -0700)
Makes the REPL persistently save history by default to
~/.node_repl_history when in terminal mode.
This can be disabled by setting NODE_REPL_HISTORY="".

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 278035f..790ca15 100644 (file)
@@ -3,6 +3,9 @@
 const Interface = require('readline').Interface;
 const REPL = require('repl');
 const path = require('path');
+const fs = require('fs');
+const os = require('os');
+const debug = require('util').debuglog('repl');
 
 module.exports = Object.create(REPL);
 module.exports.createInternalRepl = createRepl;
@@ -63,7 +66,20 @@ function createRepl(env, opts, cb) {
 }
 
 function setupHistory(repl, historyPath, ready) {
-  const fs = require('fs');
+  if (!historyPath) {
+    try {
+      historyPath = path.join(os.homedir(), '.node_repl_history');
+    } catch (err) {
+      repl._writeToOutput('\nError: Could not get the home directory.\n' +
+                          'REPL session history will not be persisted.\n');
+      repl._refreshLine();
+
+      debug(err.stack);
+      repl._historyPrev = _replHistoryMessage;
+      return ready(null, repl);
+    }
+  }
+
   var timer = null;
   var writing = false;
   var pending = false;