forked from OPSnet/Gazelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_styles_preview.js
70 lines (58 loc) · 2.04 KB
/
render_styles_preview.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
#!/usr/bin/env node
const fs = require('fs');
const { join } = require('path');
const puppeteer = require('puppeteer');
const sharp = require('sharp');
const rootPath = join(__dirname, '..');
const staticPath = join('public', 'static');
const stylesPath = join(rootPath, staticPath, 'styles');
const stylesPreviewPath = join(rootPath, staticPath, 'stylespreview');
if (!fs.existsSync(stylesPath)) {
console.error(`Could not find styles path: ${stylesPreviewPath}`);
}
if (!fs.existsSync(stylesPreviewPath)) {
fs.mkdirSync(stylesPreviewPath);
}
const styles = [];
const styleCandidates = fs.readdirSync(join(staticPath, 'styles'), {withFileTypes: true});
styleCandidates.forEach((entry) => {
if (!entry.isDirectory) {
return;
}
if (!fs.existsSync(join(stylesPath, entry.name, 'images'))) {
return;
}
if (entry.name === 'public') {
return;
}
styles.push(entry.name);
});
process.setMaxListeners(styles.length);
async function buildPreview(browser, style) {
console.log(` -> ${style}`);
var preview = join(stylesPath, style, 'preview.html');
var output = join(stylesPreviewPath, `full_${style}.png`);
fs.copyFileSync(join(__dirname, 'preview_base.html'), preview);
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
await page.setViewport({width: 1200, height: 1000});
await page.goto('file://' + preview);
await page.screenshot({path: output});
await context.close();
sharp(output).resize(480, 400).toFile(join(stylesPreviewPath, `thumb_${style}.png`));
fs.unlinkSync(preview);
}
console.log('Building styles:\n');
puppeteer.launch({args: ['--no-sandbox']}).then((browser) => {
Promise.all(styles.map((style) => buildPreview(browser, style))).then(() => {
console.log('\nAll style previews built');
}).catch((err) => {
console.error(err);
process.exitCode = 1;
}).finally(() => {
browser.close();
});
}).catch((err) => {
console.error(err);
process.exitCode = 1;
});