forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththumbnails.js
More file actions
71 lines (64 loc) · 2.53 KB
/
thumbnails.js
File metadata and controls
71 lines (64 loc) · 2.53 KB
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
const fs = require('fs');
const puppeteer = require('puppeteer');
const sharp = require('sharp');
const pathData = [];
let categoriesCounter = 0;
let examplesCounter = 0;
fs.readdir(`${__dirname}/src/examples/`, function (err, categories) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
categoriesCounter = categories.length;
categories.forEach(function (category) {
fs.readdir(`${__dirname}/src/examples/${category}`, (err, examples) => {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
examplesCounter += examples.length;
examples.forEach((e) => {
pathData.push({
category,
example: e.replace('.tsx', '')
});
examplesCounter--;
if (examplesCounter === 0) {
categoriesCounter--;
if (categoriesCounter === 0) {
takeScreenshots();
}
}
});
});
});
});
async function takeScreenshots() {
for (let i = 0; i < pathData.length; i++) {
const { example, category } = pathData[i];
if (fs.existsSync(`dist/thumbnails/${category}_${example}.png`)) {
console.log('skipped: ', `http://localhost:5000/#/iframe/${category}/${example}?fullscreen=true`)
continue;
}
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(`http://localhost:5000/#/iframe/${category}/${example}?fullscreen=true`);
const promise = new Promise((resolve, reject) => {
setTimeout(async () => {
if (!fs.existsSync(`dist/thumbnails`)) {
fs.mkdirSync(`dist/thumbnails`);
}
if (!fs.existsSync(`dist/temp`)) {
fs.mkdirSync(`dist/temp`);
}
await page.screenshot({ path: `dist/temp/${category}_${example}.png` });
await sharp(`dist/temp/${category}_${example}.png`)
.resize(320, 240)
.toFile(`dist/thumbnails/${category}_${example}.png`);
console.log('screenshot taken for: ', `http://localhost:5000/#/iframe/${category}/${example}?fullscreen=true`);
await browser.close();
resolve();
}, 5000);
});
await promise;
}
fs.rmdirSync('dist/temp', { recursive: true });
}