-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoal-table.es
57 lines (47 loc) · 1.43 KB
/
goal-table.es
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
import { ensureDirSync, readJsonSync, writeJsonSync } from 'fs-extra'
import { join } from 'path-extra'
const { APPDATA_PATH } = window
const latestVersion = '2.0.0'
// ensures goal table path exists, and returns goal table file name
const getGoalTablePath = admiralId => {
const goalTablePath = join(APPDATA_PATH,'leveling')
ensureDirSync(goalTablePath)
return join(goalTablePath, `goal-table-${admiralId}.json`)
}
const saveGoalTable = (admiralId,gt) => {
try {
const filePath = getGoalTablePath(admiralId)
const gtWithVer = {
...gt,
$version: latestVersion,
}
writeJsonSync(filePath,gtWithVer)
} catch (err) {
console.error('Error while writing to goal table file', err)
}
}
const updateGoalTable = oldGT => {
// eslint-disable-next-line prefer-const
let curGT = oldGT
if (curGT.$version === latestVersion) {
const {$version: _ignored, ...actualGT} = curGT
return actualGT
}
throw new Error(`error while updating p-state`)
}
const loadGoalTable = admiralId => {
try {
return updateGoalTable(readJsonSync(getGoalTablePath(admiralId)))
} catch (err) {
// ignore error when it's about not finding the file, which is fine,
// otherwise this could be a problem and we print it in this case.
if (err.syscall !== 'open' || err.code !== 'ENOENT') {
console.error('Error while loading goal table', err)
}
}
return {}
}
export {
saveGoalTable,
loadGoalTable,
}