-
Notifications
You must be signed in to change notification settings - Fork 91
/
generate-html.mjs
154 lines (146 loc) · 4.74 KB
/
generate-html.mjs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'node:fs/promises';
import {predatesChromeDriverAvailability, predatesChromeHeadlessShellAvailability} from './is-older-version.mjs';
import {readJsonFile} from './json-utils.mjs';
import {escapeHtml, minifyHtml} from './html-utils.mjs';
const OK = '\u2705';
const NOT_OK = '\u274C';
const renderDownloads = (downloads, version, forceOk = false) => {
const list = [];
for (const [binary, downloadsPerBinary] of Object.entries(downloads)) {
if (binary === 'chromedriver' && predatesChromeDriverAvailability(version)) {
continue;
}
if (binary === 'chrome-headless-shell' && predatesChromeHeadlessShellAvailability(version)) {
continue;
}
if (binary === 'mojojs') {
// Exclude mojojs from the dashboard + API.
continue;
}
for (const download of downloadsPerBinary) {
list.push(
`<tr class="status-${
(forceOk || download.status === 200) ? 'ok' : 'not-ok'
}"><th><code>${escapeHtml(
binary
)}</code><th><code>${escapeHtml(
download.platform
)}</code><td><code>${escapeHtml(
download.url
)}</code><td><code>${forceOk ? '200' : escapeHtml(download.status)}</code>`
);
}
}
return `
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>Binary
<th>Platform
<th>URL
<th>HTTP status
<tbody>
${list.join('')}
</table>
</div>
`;
};
const render = (data) => {
const summary = [];
const main = [];
for (const [channel, channelData] of Object.entries(data.channels)) {
const { version, revision, downloads } = channelData;
const isOk = channelData.ok;
if (isOk) {
summary.push(`
<tr class="status-ok">
<th><a href="#${escapeHtml(channel.toLowerCase())}">${escapeHtml(channel)}</a>
<td><code>${escapeHtml(version)}</code>
<td><code>r${escapeHtml(revision)}</code>
<td>${ OK }
`);
} else {
const fallbackData = lastKnownGoodVersions.channels[channel];
const fallbackVersion = fallbackData.version;
const fallbackRevision = fallbackData.revision;
summary.push(`
<tr class="status-ok">
<th><a href="#${escapeHtml(channel.toLowerCase())}">${escapeHtml(channel)}</a>
<td><code>${escapeHtml(fallbackVersion)}</code>
<td><code>r${escapeHtml(fallbackRevision)}</code>
<td>${ OK }
<tr class="status-upcoming">
<th><a href="#${escapeHtml(channel.toLowerCase())}">${escapeHtml(channel)} (upcoming)</a>
<td><code>${escapeHtml(version)}</code>
<td><code>r${escapeHtml(revision)}</code>
<td>${ NOT_OK }
`);
}
main.push(`
<section id="${escapeHtml(channel.toLowerCase())}" class="status-${
isOk ? 'ok' : 'not-ok'
}">
`);
if (isOk) {
main.push(`
<h2>${escapeHtml(channel)}</h2>
<p>Version: <code>${escapeHtml(version)}</code> (<code>r${escapeHtml(revision)}</code>)</p>
${renderDownloads(downloads, version)}
`);
} else {
const fallbackChannelData = lastKnownGoodVersions.channels[channel];
const fallbackVersion = fallbackChannelData.version;
const fallbackRevision = fallbackChannelData.revision;
const fallbackDownloads = fallbackChannelData.downloads;
main.push(`
<h2>${escapeHtml(channel)}</h2>
<p>Version: <code>${escapeHtml(fallbackVersion)}</code> (<code>r${escapeHtml(fallbackRevision)}</code>)</p>
${renderDownloads(fallbackDownloads, fallbackVersion, true)}
<p>Upcoming version: <code>${escapeHtml(version)}</code> (<code>r${escapeHtml(revision)}</code>)</p>
${renderDownloads(downloads, version)}
`);
}
main.push(`
</section>
`);
}
return `
<div class="table-wrapper summary">
<table>
<thead>
<tr>
<th>Channel
<th>Version
<th>Revision
<th>Status
<tbody>
${summary.join('')}
</table>
</div>
${main.join('')}
`;
};
const data = await readJsonFile('./data/dashboard.json');
const lastKnownGoodVersions = await readJsonFile('data/last-known-good-versions-with-downloads.json');
const htmlTemplate = await fs.readFile('./_tpl/template.html', 'utf8');
const html = htmlTemplate.toString()
.replace('%%%DATA%%%', render(data))
.replace('%%%TIMESTAMP%%%', data.timestamp);
const minifiedHtml = await minifyHtml(html);
await fs.writeFile('./dist/index.html', minifiedHtml);