-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
44 lines (38 loc) · 1 KB
/
cli.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const extract = require('./extract')
//load configuration
const config = require('./config')
// load previous results
const storeFile = path.join(__dirname, 'store.json')
let store
if (fs.existsSync(storeFile)) {
store = JSON.parse(fs.readFileSync(storeFile, 'utf8'))
} else {
store = []
}
// retrieve and extract current state of pages
extract(config.pages, store).then(([err, update, delta]) => {
if (err.length) {
err.forEach(console.warn)
}
// write current state to storage
fs.writeFileSync(storeFile, JSON.stringify(update), {encoding: 'utf8'})
if (!delta.length) {
console.log('No new changes found')
} else {
console.log(`Changes found on ${delta.length} pages`)
// send new findings via mail
if (config.mail !== undefined) {
const mailer = require('./mailer')
mailer(config.mail, delta, (err) => {
if (err) {
throw err
}
console.log('Mail with results sent!')
})
}
}
})
.catch(console.error)