-
Notifications
You must be signed in to change notification settings - Fork 0
/
JournalWatcher.js
81 lines (68 loc) · 2.57 KB
/
JournalWatcher.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fs = require("fs")
const Tail = require('tail').Tail;
const os = require("os");
const events = require("events");
var EventEmitter = new events.EventEmitter();
var options= {separator: /[\r]{0,1}\n/, fromBeginning: true, fsWatchOptions: {}, follow: true, logger: console, useWatchFile: true}
const journalDir = `C:/Users/${os.userInfo().username}/Saved Games/Frontier Developments/Elite Dangerous/`
var dirContent = fs.readdirSync(journalDir)
var files = []
dirContent.forEach(name => {
if (name.match(/Journal..+.log/g)) {
files.push([name.replace(/Journal.|\.\d{2}\.|log/gm,""),name])
}
});
files.sort(function(a,b) {return Number(a[0])>Number(b[0])})
//Read file and check for updates
function startRead(event, dir) {
dir = dir || `C:/Users/${os.userInfo().username}/Saved Games/Frontier Developments/Elite Dangerous/`
if(event==="Journal") {
JournalTail.watch()
return `Started watching ${event}`
}
fs.readFile(dir+event+".json", (err,data) => {
EventEmitter.emit(event, JSON.parse(Buffer.from(data, "utf-8").toString()))
EventEmitter.emit("allData", JSON.parse(Buffer.from(data, "utf-8").toString()))
return `Started watching ${event}`
})
fs.watchFile(dir+event+".json",(curr,prev) => {
fs.readFile(dir+event+".json", (err,data) => {
EventEmitter.emit(event, JSON.parse(Buffer.from(data, "utf-8").toString()))
EventEmitter.emit("allData", JSON.parse(Buffer.from(data, "utf-8").toString()))
})
});
}
function stopRead(event, dir) {
dir = dir || `C:/Users/${os.userInfo().username}/Saved Games/Frontier Developments/Elite Dangerous/`
if(event==="Journal") {
JournalTail.unwatch()
return `Stopped watching ${event}`
} else {
fs.unwatchFile(dir+event+".json")
return `Stopped watching ${dir+event}.json`
}
}
function startAll() {
startRead("Cargo")
startRead("Market")
startRead("ModulesInfo")
startRead("NavRoute")
startRead("Outfitting")
startRead("Shipyard")
startRead("Status")
}
// Journal events
var JournalTail = new Tail(journalDir+files[files.length-1][1], options)
JournalTail.on("error", error => console.error(error));
JournalTail.on('line',data=>{
data = JSON.parse(data);
EventEmitter.emit("Journal", data);
EventEmitter.emit("allData", data)
EventEmitter
});
JournalTail.unwatch()
module.exports.file = journalDir+files[files.length-1][1]
module.exports = EventEmitter
module.exports.startRead = startRead
module.exports.stopRead = stopRead
module.exports.startAll = startAll