forked from carbonplan/research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-cards.js
34 lines (29 loc) · 1.02 KB
/
build-cards.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
const fs = require('fs')
const glob = require('glob')
const puppeteer = require('puppeteer')
const contents = require('./utils/mdx.js')
glob('./cards/**.png', async (err, filePaths) => {
if (!fs.existsSync('./cards')) {
fs.mkdirSync('./cards')
}
for (const commentary of contents.commentaryMetadata) {
if (!filePaths.find((p) => p.includes(commentary.id))) {
const file = await getScreenshot(commentary.id)
fs.writeFileSync(`./cards/${commentary.id}.png`, file)
}
}
process.exit()
})
const baseUrl = process.env.CARDS_BASE_URL || 'http://localhost:4000'
async function getScreenshot(id) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setViewport({ width: 1200, height: 630 })
await page.goto(baseUrl + '/cards/' + id)
await page.waitForSelector('#final-authors')
// Wait for 0.5 seconds to ensure that fonts load
await new Promise((resolve) => setTimeout(resolve, 500))
const file = await page.screenshot()
await page.close()
return file
}