-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.js
95 lines (75 loc) · 3.16 KB
/
run.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require('fs');
const index = require('./index.js').run
const bookkeepingPath = './store/bookkeeping.json';
const bookkeeping = JSON.parse(fs.readFileSync(bookkeepingPath));
const utils = require('./utils.js');
const today = new Date();
function isTodayMonday() {
return today.getDay() === 1;
}
console.log("checking...", today)
JSDOM
.fromURL("https://github.com/mdn/browser-compat-data/releases", {})
.then(async dom => {
// inside the github page is a link to the latest release
let el = dom.window.document.querySelector('[href^="/mdn/browser-compat-data/releases/"]');
let curReleaseURL = el.getAttribute('href')
// the last part of the url is the version number
let curReleasePathParts = curReleaseURL.split("/")
let version = curReleasePathParts.pop()
// there is always a .json if we swap in the version
let jsonURL = `https://github.com/mdn/browser-compat-data/releases/download/${version}/data.json`
console.log(`url: `, jsonURL)
let resp = await fetch(jsonURL)
let data = await resp.json()
console.log(`we have the new one:`)
// we have the new one!document.querySelectorAll('[datetime]').forEach(item => console.log(item.getAttribute('datetime')) )
let updatedDate = new Date(data.__meta.timestamp || Date.now())
let latestReleaseDate = new Date(bookkeeping.latest.timestamp)
let deltaInDays = Math.round((updatedDate - latestReleaseDate) / 86400000)
let calendarDeltaInDays = Math.round((updatedDate - new Date()) / 86400000)
console.log("meta:", JSON.stringify(data.__meta))
console.log("updated %s, latest %s", updatedDate, latestReleaseDate)
console.log(`It's been ${calendarDeltaInDays} days since then.`)
if (
(updatedDate > latestReleaseDate)
) {
console.log("A new release is available...")
let dateStr = utils.toISODateString(updatedDate)
//write the store to the file
let filename = `${dateStr}.json`
fs.writeFileSync(`./store/${filename}`, JSON.stringify(data), 'utf8')
if (isTodayMonday() && !utils.areSameDate(latestReleaseDate, updatedDate)) {
//update bookkeeping values
bookkeeping.previous = bookkeeping.latest
bookkeeping.latest = {
"file": filename,
"timestamp": data.__meta.timestamp,
"version": data.__meta.version
}
//rerun index stuff with those
index(
bookkeeping.previous.file,
bookkeeping.latest.file,
utils.toISODateString(today)
)
//only write the update has happened, whne it has happened
fs.writeFileSync(bookkeepingPath, JSON.stringify(bookkeeping, null, 2), 'utf8')
console.log("********** UPDATED *************")
}
console.log(JSON.stringify(data.__meta))
} else {
//rerun index stuff with those
if (isTodayMonday()) {
console.log("No updates to the store, but we'll write a new report...")
index(
bookkeeping.latest.file,
bookkeeping.latest.file,
utils.toISODateString(today)
)
}
}
console.log("I'll check again later...")
});