generated from HeraldStudio/herald-webservice-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplHistory.js
44 lines (39 loc) · 999 Bytes
/
replHistory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
let fs = require('fs')
module.exports = function (repl, file) {
try {
fs.statSync(file)
repl.history = fs.readFileSync(file, 'utf-8').split('\n').reverse()
repl.history.shift()
repl.historyIndex = -1 // will be incremented before pop
// eslint-disable-next-line no-empty
} catch (e) { }
let fd = fs.openSync(file, 'a')
let wstream = fs.createWriteStream(file, {
fd: fd
})
wstream.on('error', function (err) {
throw err
})
repl.addListener('line', function (code) {
if (code && code !== '.history') {
wstream.write(code + '\n')
} else {
repl.historyIndex++
repl.history.pop()
}
})
process.on('exit', function () {
fs.closeSync(fd)
})
repl.commands['history'] = {
help: 'Show the history',
action: function () {
let out = []
repl.history.forEach(function (v) {
out.push(v)
})
repl.outputStream.write(out.reverse().join('\n') + '\n')
repl.displayPrompt()
}
}
}