mirror of
https://github.com/samsonjs/repl-edit.git
synced 2026-04-27 15:07:40 +00:00
add history file support to repl.js
This commit is contained in:
parent
d6d800a44b
commit
c83c0105b6
1 changed files with 41 additions and 1 deletions
42
repl.js
42
repl.js
|
|
@ -1,3 +1,43 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
require('./lib/index').startRepl()
|
var fs = require('fs')
|
||||||
|
, path = require('path')
|
||||||
|
, repl = require('./lib/index').startRepl()
|
||||||
|
, DefaultHistoryFile = path.join(process.env.HOME, '.node_history')
|
||||||
|
, historyFile
|
||||||
|
|
||||||
|
if ('NODE_HISTORY' in process.env)
|
||||||
|
historyFile = process.env.NODE_HISTORY
|
||||||
|
else
|
||||||
|
historyFile = DefaultHistoryFile
|
||||||
|
|
||||||
|
// restore history immediately
|
||||||
|
if (historyFile) {
|
||||||
|
try {
|
||||||
|
fs.statSync(historyFile)
|
||||||
|
var json = fs.readFileSync(historyFile)
|
||||||
|
repl.rli.history = JSON.parse(json)
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
if (e.code !== 'ENOENT') {
|
||||||
|
console.error('!!! Error reading history from ' + historyFile)
|
||||||
|
if (e.message === 'Unexpected token ILLEGAL') {
|
||||||
|
console.error('is this a JSON array of strings? -> ' + json)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.error(e.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// save history on exit
|
||||||
|
process.on('exit', function() {
|
||||||
|
try {
|
||||||
|
fs.writeFileSync(historyFile, JSON.stringify(repl.rli.history))
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error('Error writing history file to ' + historyFile)
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue