forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (54 loc) · 1.65 KB
/
index.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
const fs = require('fs');
const puppeteer = require('puppeteer');
const Jimp = require('jimp');
const c = require('kleur');
const slugs = [];
fs.readdirSync(`content/examples`).forEach(group_dir => {
fs.readdirSync(`content/examples/${group_dir}`).filter(file => file !== 'meta.json').map(example_dir => {
const slug = example_dir.replace(/^\d+-/, '');
slugs.push(slug);
});
});
async function main() {
const browser = await puppeteer.launch({
defaultViewport: {
width: 400 * 10 / 4,
height: 400 + 42,
deviceScaleFactor: 2
}
});
const page = await browser.newPage();
for (const slug of slugs) {
try {
const output_file = `static/examples/thumbnails/${slug}.jpg`;
if (fs.existsSync(output_file)) {
console.log(c.gray(`skipping ${slug}`));
continue;
}
console.log(slug);
await page.goto(`http://localhost:3000/repl/embed?example=${slug}`);
await page.waitForSelector('iframe.inited[title=Result]');
await page.waitFor(1500);
const iframe = await page.$('iframe.inited[title=Result]');
const buffer = await iframe.screenshot();
const image = await Jimp.read(buffer);
console.log(image.bitmap.width, image.bitmap.height);
image.crop(3, 3, image.bitmap.width - 6, image.bitmap.height - 6);
image.autocrop();
// image.scale(0.25);
if (image.bitmap.width > 200 || image.bitmap.width > 200) {
const scale = Math.min(
200 / image.bitmap.width,
200 / image.bitmap.height
);
image.scale(scale);
}
await image.quality(75).write(output_file);
} catch (err) {
console.log(c.bold().red(`failed to screenshot ${slug}`));
console.log(err);
}
}
await browser.close();
}
main();