@@ -15,6 +15,11 @@ module.exports.createInternalRepl = createRepl;
15
15
// The debounce is to guard against code pasted into the REPL.
16
16
const kDebounceHistoryMS = 15 ;
17
17
18
+ function _writeToOutput ( repl , message ) {
19
+ repl . _writeToOutput ( message ) ;
20
+ repl . _refreshLine ( ) ;
21
+ }
22
+
18
23
function createRepl ( env , opts , cb ) {
19
24
if ( typeof opts === 'function' ) {
20
25
cb = opts ;
@@ -80,9 +85,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
80
85
try {
81
86
historyPath = path . join ( os . homedir ( ) , '.node_repl_history' ) ;
82
87
} catch ( err ) {
83
- repl . _writeToOutput ( '\nError: Could not get the home directory.\n' +
84
- 'REPL session history will not be persisted.\n' ) ;
85
- repl . _refreshLine ( ) ;
88
+ _writeToOutput ( repl , '\nError: Could not get the home directory.\n' +
89
+ 'REPL session history will not be persisted.\n' ) ;
86
90
87
91
debug ( err . stack ) ;
88
92
repl . _historyPrev = _replHistoryMessage ;
@@ -103,9 +107,8 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
103
107
if ( err ) {
104
108
// Cannot open history file.
105
109
// Don't crash, just don't persist history.
106
- repl . _writeToOutput ( '\nError: Could not open history file.\n' +
107
- 'REPL session history will not be persisted.\n' ) ;
108
- repl . _refreshLine ( ) ;
110
+ _writeToOutput ( repl , '\nError: Could not open history file.\n' +
111
+ 'REPL session history will not be persisted.\n' ) ;
109
112
debug ( err . stack ) ;
110
113
111
114
repl . _historyPrev = _replHistoryMessage ;
@@ -132,18 +135,13 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
132
135
} else if ( oldHistoryPath === historyPath ) {
133
136
// If pre-v3.0, the user had set NODE_REPL_HISTORY_FILE to
134
137
// ~/.node_repl_history, warn the user about it and proceed.
135
- repl . _writeToOutput (
136
- '\nThe old repl history file has the same name and location as ' +
138
+ _writeToOutput (
139
+ repl ,
140
+ '\nThe old repl history file has the same name and location as ' +
137
141
`the new one i.e., ${ historyPath } and is empty.\nUsing it as is.\n` ) ;
138
- repl . _refreshLine ( ) ;
139
142
140
143
} else if ( oldHistoryPath ) {
141
- // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
142
- repl . _writeToOutput (
143
- '\nConverting old JSON repl history to line-separated history.\n' +
144
- `The new repl history file can be found at ${ historyPath } .\n` ) ;
145
- repl . _refreshLine ( ) ;
146
-
144
+ let threw = false ;
147
145
try {
148
146
// Pre-v3.0, repl history was stored as JSON.
149
147
// Try and convert it to line separated history.
@@ -152,15 +150,31 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
152
150
// Only attempt to use the history if there was any.
153
151
if ( oldReplJSONHistory ) repl . history = JSON . parse ( oldReplJSONHistory ) ;
154
152
155
- if ( ! Array . isArray ( repl . history ) ) {
156
- throw new Error ( 'Expected array, got ' + typeof repl . history ) ;
153
+ if ( Array . isArray ( repl . history ) ) {
154
+ repl . history = repl . history . slice ( 0 , repl . historySize ) ;
155
+ } else {
156
+ threw = true ;
157
+ _writeToOutput (
158
+ repl ,
159
+ '\nError: The old history file data has to be an Array.\n' +
160
+ 'REPL session history will not be persisted.\n' ) ;
157
161
}
158
- repl . history = repl . history . slice ( 0 , repl . historySize ) ;
159
162
} catch ( err ) {
160
- if ( err . code !== 'ENOENT' ) {
161
- return ready (
162
- new Error ( `Could not parse history data in ${ oldHistoryPath } .` ) ) ;
163
- }
163
+ // Cannot open or parse history file.
164
+ // Don't crash, just don't persist history.
165
+ threw = true ;
166
+ const type = err instanceof SyntaxError ? 'parse' : 'open' ;
167
+ _writeToOutput ( repl , `\nError: Could not ${ type } old history file.\n` +
168
+ 'REPL session history will not be persisted.\n' ) ;
169
+ }
170
+ if ( ! threw ) {
171
+ // Grab data from the older pre-v3.0 JSON NODE_REPL_HISTORY_FILE format.
172
+ _writeToOutput (
173
+ repl ,
174
+ '\nConverted old JSON repl history to line-separated history.\n' +
175
+ `The new repl history file can be found at ${ historyPath } .\n` ) ;
176
+ } else {
177
+ repl . history = [ ] ;
164
178
}
165
179
}
166
180
@@ -223,12 +237,12 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
223
237
224
238
function _replHistoryMessage ( ) {
225
239
if ( this . history . length === 0 ) {
226
- this . _writeToOutput (
227
- '\nPersistent history support disabled. ' +
240
+ _writeToOutput (
241
+ this ,
242
+ '\nPersistent history support disabled. ' +
228
243
'Set the NODE_REPL_HISTORY environment\nvariable to ' +
229
244
'a valid, user-writable path to enable.\n'
230
245
) ;
231
- this . _refreshLine ( ) ;
232
246
}
233
247
this . _historyPrev = Interface . prototype . _historyPrev ;
234
248
return this . _historyPrev ( ) ;
0 commit comments