@ -7,8 +7,6 @@ const fs = require('fs');
const os = require ( 'os' ) ;
const util = require ( 'util' ) ;
const debug = util . debuglog ( 'repl' ) ;
const errors = require ( 'internal/errors' ) ;
module . exports = Object . create ( REPL ) ;
module . exports . createInternalRepl = createRepl ;
@ -16,6 +14,11 @@ module.exports.createInternalRepl = createRepl;
// The debounce is to guard against code pasted into the REPL.
const kDebounceHistoryMS = 15 ;
function _ writeToOutput ( repl , message ) {
repl . _ writeToOutput ( message ) ;
repl . _ refreshLine ( ) ;
}
function createRepl ( env , opts , cb ) {
if ( typeof opts === 'function' ) {
cb = opts ;
@ -81,9 +84,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
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 ( ) ;
_ writeToOutput ( repl , '\nError: Could not get the home directory.\n' +
'REPL session history will not be persisted.\n' ) ;
debug ( err . stack ) ;
repl . _ historyPrev = _ replHistoryMessage ;
@ -104,9 +106,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
if ( err ) {
// Cannot open history file.
// Don't crash, just don't persist history.
repl . _ writeToOutput ( '\nError: Could not open history file.\n' +
'REPL session history will not be persisted.\n' ) ;
repl . _ refreshLine ( ) ;
_ writeToOutput ( repl , '\nError: Could not open history file.\n' +
'REPL session history will not be persisted.\n' ) ;
debug ( err . stack ) ;
repl . _ historyPrev = _ replHistoryMessage ;
@ -133,18 +134,13 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
} else if ( oldHistoryPath === historyPath ) {
// If pre-v3.0, the user had set NODE_REPL_HISTORY_FILE to
// ~/.node_repl_history, warn the user about it and proceed.
repl . _ writeToOutput (
'\nThe old repl history file has the same name and location as ' +
_ writeToOutput (
repl ,
'\nThe old repl history file has the same name and location as ' +
` the new one i.e., ${ historyPath } and is empty. \n Using it as is. \n ` ) ;
repl . _ refreshLine ( ) ;
} 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 ( ) ;
let threw = false ;
try {
// Pre-v3.0, repl history was stored as JSON.
// Try and convert it to line separated history.
@ -153,16 +149,31 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
// Only attempt to use the history if there was any.
if ( oldReplJSONHistory ) repl . history = JSON . parse ( oldReplJSONHistory ) ;
if ( ! Array . isArray ( repl . history ) ) {
throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
typeof repl . history , 'Array' ) ;
if ( Array . isArray ( repl . history ) ) {
repl . history = repl . history . slice ( 0 , repl . historySize ) ;
} else {
threw = true ;
_ writeToOutput (
repl ,
'\nError: The old history file data has to be an Array.\n' +
'REPL session history will not be persisted.\n' ) ;
}
repl . history = repl . history . slice ( 0 , repl . historySize ) ;
} catch ( err ) {
if ( err . code !== 'ENOENT' ) {
return ready (
new errors . Error ( 'ERR_PARSE_HISTORY_DATA' , oldHistoryPath ) ) ;
}
// Cannot open or parse history file.
// Don't crash, just don't persist history.
threw = true ;
const type = err instanceof SyntaxError ? 'parse' : 'open' ;
_ writeToOutput ( repl , ` \n Error: Could not ${ type } old history file. \n ` +
'REPL session history will not be persisted.\n' ) ;
}
if ( ! threw ) {
// Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
_ writeToOutput (
repl ,
'\nConverted old JSON repl history to line-separated history.\n' +
` The new repl history file can be found at ${ historyPath } . \n ` ) ;
} else {
repl . history = [ ] ;
}
}
@ -225,12 +236,12 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
function _ replHistoryMessage ( ) {
if ( this . history . length === 0 ) {
this . _ writeToOutput (
'\nPersistent history support disabled. ' +
_ writeToOutput (
this ,
'\nPersistent history support disabled. ' +
'Set the NODE_REPL_HISTORY environment\nvariable to ' +
'a valid, user-writable path to enable.\n'
) ;
this . _ refreshLine ( ) ;
}
this . _ historyPrev = Interface . prototype . _ historyPrev ;
return this . _ historyPrev ( ) ;