-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
59 lines (56 loc) · 1.81 KB
/
update.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
const dataPath = './data/data.json'
const hashPath = './data/hash.json'
const gid = process.env.GIST_ID
const filename = process.env.GIST_FILENAME
const hash = require('object-hash')
const Gists = require('gists')
const low = require('lowdb')
const fileSync = require('lowdb/adapters/FileSync')
const dataAdapter = new fileSync(dataPath)
const hashAdapter = new fileSync(hashPath)
const dataDB = low(dataAdapter)
const hashDB = low(hashAdapter)
const gists = new Gists({
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD
})
function setHash() {
let c = dataDB.get('words').value()
let h = hash(c)
let date = Date()
hashDB.set('hash', h)
.set('last_update', date)
.write()
console.log(`Hash set with ${h}`)
}
// Init
module.exports = function () {
console.log('Reading DB...')
dataDB.read() // Get latest state
if (dataDB.get('words').value().length === 0) {
return gists.get(gid)
.then(res => {
dataDB.set('words', JSON.parse(res.body.files[filename].content)).write()
setHash()
console.log('Init Database Successfully!')
return true
})
.catch(console.error)
} else {
console.log('Update Started')
let c = dataDB.get('words').value()
if (hashDB.get('hash').value() !== hash(c)) {
let update = { files: {} }
update.files[filename] = {
content: JSON.stringify(c, null, 2)
}
return gists.edit(gid, update).then(res => {
setHash()
console.log(`Gist updated at ${hashDB.get('last_update').value()}`)
return true
}).catch(console.error)
} else {
console.log(`File Unchanged`)
}
}
}