From ca219b00d163254f866e3287f690845d437b993a Mon Sep 17 00:00:00 2001 From: Chris Dickinson Date: Mon, 4 May 2015 08:56:14 -0700 Subject: [PATCH] repl: fix for a+ fd clearing the file on read The second step of augmenting the internal REPL with persistent history was to re-open the history file with a 'w' handle. This truncated the file. If a user did not enter a new line before closing the REPL, their history would be deleted. PR-URL: https://github.com/iojs/io.js/pull/1605 Reviewed-By: Roman Reiss --- lib/internal/repl.js | 9 +++++++-- src/node.js | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/internal/repl.js b/lib/internal/repl.js index 65c3f77ed6abb9..902e81f495573c 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -107,8 +107,13 @@ function setupHistory(repl, historyPath, ready) { } repl._historyHandle = hnd; repl.on('line', online); - repl.resume(); - return ready(null, repl); + + // reading the file data out erases it + repl.once('flushHistory', function() { + repl.resume(); + ready(null, repl); + }); + flushHistory(); } // ------ history listeners ------ diff --git a/src/node.js b/src/node.js index 2d6ce45a928d29..bd8ef5b04ef8a2 100644 --- a/src/node.js +++ b/src/node.js @@ -130,7 +130,8 @@ // If -i or --interactive were passed, or stdin is a TTY. if (process._forceRepl || NativeModule.require('tty').isatty(0)) { // REPL - Module.requireRepl().createInternalRepl(process.env, function(err, repl) { + var cliRepl = Module.requireRepl(); + cliRepl.createInternalRepl(process.env, function(err, repl) { if (err) { throw err; }