This repository has been archived by the owner on Jan 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
52 lines (43 loc) · 1.51 KB
/
build.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
const handlebars = require('handlebars');
const fs = require("fs");
const path = require('path');
const puppeteer = require('puppeteer');
handlebars.registerHelper('date', function (date) {
const options = { weekday: 'short', month: 'short', day: 'numeric' };
return new Date(date).toLocaleDateString('en-us', options);
});
const template = handlebars.compile(fs.readFileSync(path.join("site", "index.hbs")).toString('utf8'));
const data = JSON.parse(fs.readFileSync(path.join("data", "ny_state.json")));
const events = [];
const sites = [];
for (const site of data.sites) {
for (const event of site.events) {
event.site = site;
delete event.site.events;
events.push(event);
}
delete site.events;
sites.push(site);
}
fs.writeFileSync(path.join("site", "index.html"), template({ sites: sites.sort(sortByProperty("name")), events: events.sort(sortByProperty("date")) }));
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({
width: 1200,
height: 628,
deviceScaleFactor: 1
});
await page.goto(`file://${path.join(__dirname, "site", "index.html")}`);
await page.screenshot({path: path.join("site", "images", "social-share.png")});
await browser.close();
})();
function sortByProperty(property) {
return function (a, b) {
if (a[property] > b[property])
return 1;
else if (a[property] < b[property])
return -1;
return 0;
}
}