forked from PrismJS/prism-themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
64 lines (55 loc) · 1.62 KB
/
gulpfile.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
const fs = require('fs').promises;
const captureWebsite = require('capture-website');
const path = require('path');
const themesDir = path.join(__dirname, 'themes');
const screenshotDir = path.join(__dirname, 'screenshots');
/**
* Returns the names of all themes. This includes the `prism-` prefix.
*/
async function getThemes() {
return (await fs.readdir(themesDir)).map(f => (/^.+(?=\.css$)/.exec(f) || [''])[0]).filter(f => f);
}
/**
* Takes a screenshot of all themes overwriting the old ones.
*/
async function screenshotAllThemes() {
for (const theme of await getThemes()) {
await screenshotTheme(theme, true);
}
}
/**
* Takes a screenshot of themes which don't have one already.
*/
async function screenshotMissingThemes() {
for (const theme of await getThemes()) {
await screenshotTheme(theme, false);
}
}
/**
* Takes a screenshot of the given themes and saves the image file in the screenshot directory.
*
* __IMPORTANT:__ Screenshots have to be taken sequentially, one after an other, to prevent a memory leak.
*
* @param {string} theme
* @param {boolean} overwrite
*/
async function screenshotTheme(theme, overwrite) {
const file = `${screenshotDir}/${theme}.png`;
if (await fs.stat(file).then(s => s.isFile()).catch(() => false)) {
if (overwrite) {
await fs.unlink(file);
} else {
return;
}
}
await captureWebsite.file(screenshotDir + '/code.html', file, {
defaultBackground: false,
scaleFactor: 1,
element: 'pre',
styles: [
await fs.readFile(`${themesDir}/${theme}.css`, 'utf-8')
]
});
}
exports.screenshot = screenshotMissingThemes;
exports['screenshot-all'] = screenshotAllThemes;