forked from PrismJS/prism-themes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
120 lines (100 loc) · 3.19 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
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
115
116
117
118
119
120
const fs = require('fs').promises;
const { src, dest, parallel } = require('gulp');
const cleanCSS = require('gulp-clean-css')
const rename = require('gulp-rename')
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')
]
});
}
/**
* Checks that all themes have a screenshot.
*/
async function checkScreenshots() {
for (const theme of await getThemes()) {
const file = `${screenshotDir}/${theme}.png`;
if (!await fs.stat(file).then(s => s.isFile()).catch(() => false)) {
throw new Error(`The theme "${theme}" doesn't have a screenshot.`);
}
}
}
/**
* Checks that all themes are in the list of available themes.
*/
async function checkAvailableThemes() {
const readme = await fs.readFile(path.join(__dirname, 'README.md'), 'utf-8');
for (const theme of await getThemes()) {
if (!readme.includes(theme + ".css")) {
throw new Error(`The theme "${theme}" is not included in the list of available themes.`);
}
if (!readme.includes(theme + ".png")) {
throw new Error(`The screenshot of "${theme}" is not included in the list of available themes.`);
}
}
}
const ISSUE_RE = /#(\d+)(?![\d\]])/g;
const ISSUE_SUB = '[#$1](https://github.com/PrismJS/prism-themes/issues/$1)';
const CHANGELOG = 'CHANGELOG.md';
async function linkify() {
let changelog = await fs.readFile(CHANGELOG, 'utf-8');
changelog = changelog.replace(ISSUE_RE, ISSUE_SUB);
await fs.writeFile(CHANGELOG, changelog, 'utf-8');
}
function minify() {
return src(['themes/*.css', '!themes/*.min.css'])
.pipe(cleanCSS())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('themes/'));
}
exports.screenshot = screenshotMissingThemes;
exports['screenshot-all'] = screenshotAllThemes;
exports.check = parallel(checkScreenshots, checkAvailableThemes);
exports.linkify = linkify;
exports.minify = minify;