forked from phaserjs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-screenshots.js
114 lines (101 loc) · 3.33 KB
/
generate-screenshots.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const puppeteer = require('puppeteer');
const httpServer = require('http-server');
const fs = require('fs-extra');
const p = require('path');
var server = httpServer.createServer();
server.listen(8080, 'localhost', () => {});
let examples = [];
const getExamples = (path, depth = 0) => {
const files = fs.readdirSync(p.resolve(path));
if (depth > maxDepth || path.includes('archived') || path.includes('3.24')) {
return;
}
if (files.includes('boot.json')){
examples.push({
url: path + '/' + 'boot.json',
path: p.resolve(path, 'boot.json'),
});
} else {
for(let file of files) {
const fileInfo = fs.statSync(p.resolve(path, file));
if (fileInfo.isDirectory() && file[0] !== '_') {
getExamples(path + '/' + file, depth + 1);
} else if (file[0] !== '_' && file[0] !== '.') {
examples.push({
url: path + '/' + file,
path: p.resolve(path, file),
});
}
}
}
}
let screenshots = [];
const maxDepth = 25;
const getScreenshots = (path, depth = 0) => {
const files = fs.readdirSync(path);
if (depth > maxDepth) {
return;
}
for(let file of files) {
const fileInfo = fs.statSync(p.resolve(path, file));
if (fileInfo.isDirectory()) {
getScreenshots(p.resolve(path, file), depth + 1);
}
screenshots.push(p.resolve(path, file));
}
}
getExamples('./public/src');
examples = examples
.filter(e => !e.url.includes('rbush'))
.filter(e => e.url.match(/[^\.]+$/)[0].slice(0,2) === 'js')
.map(e => ({ url: e.url = 'http://localhost:8080/' + (e.url.includes('boot.json') ? 'boot.html?src=' : 'view.html?src=') + e.url.slice(9).replace(/\//g, '\\'), path: e.path.toLowerCase().replace(/src/, 'screenshots').replace(/\.json/, '').replace(/\.js/, '') + '.png' }));
getScreenshots('./public/screenshots');
screenshots = screenshots
.map(s => s.toLowerCase());
const saveCanvas = async (page, example) => {
return new Promise(async (resolve, reject) => {
const path = example.path;
console.log('- ' + example.url);
console.log(' Screenshot to path:', path);
const filename = path.match(/[^\/\\]+$/)[0];
fs.mkdirp(path.slice(0, -(filename.length + 1)));
await page.goto(example.url);
try {
await page.waitForSelector('canvas', {
timeout: 5000,
});
} catch (e) {
fs.copyFileSync(p.resolve('public/images/doc.png'), p.resolve(path));
resolve();
return;
}
await page.evaluate(() => {
const c = document.querySelector('canvas');
c.style.width = 800 + 'px';
c.style.height = 600 + 'px';
});
page.mouse.click(200, 150);
await page.waitFor(1000);
const canvas = await page.$('canvas');
if (canvas) {
await canvas.screenshot({
path: p.resolve(path),
});
}
else {
fs.copyFileSync(p.resolve('public/images/doc.png'), p.resolve(path));
}
resolve();
});
};
async function run() {
const browser = await puppeteer.launch({ headless: true });
const [page] = await browser.pages();
await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1');
for (let example of examples.filter(e => !screenshots.includes(e.path))) {
await saveCanvas(page, example);
}
await browser.close();
server.close();
}
run();