-
Notifications
You must be signed in to change notification settings - Fork 90
/
generate-extra-json.mjs
231 lines (210 loc) · 6.74 KB
/
generate-extra-json.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* 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 {binaries, platforms, makeDownloadUrl} from './url-utils.mjs';
import {
isOlderVersion,
predatesChromeDriverAvailability,
predatesChromeHeadlessShellAvailability,
} from './is-older-version.mjs';
import {
readJsonFile,
writeJsonFile,
writeMinifiedJsonFile,
} from './json-utils.mjs';
const createTimestamp = () => {
return new Date().toISOString();
};
const prepareLastKnownGoodVersionsData = async (data) => {
const lastKnownGoodVersions = await readJsonFile('./data/last-known-good-versions.json');
for (const channelData of Object.values(data.channels)) {
if (!channelData.ok) continue;
const channelName = channelData.channel;
const knownData = lastKnownGoodVersions.channels[channelName];
if (
knownData.version === channelData.version &&
knownData.revision === channelData.revision
) {
continue;
}
lastKnownGoodVersions.timestamp = createTimestamp();
knownData.version = channelData.version;
knownData.revision = channelData.revision;
}
return lastKnownGoodVersions;
};
const updateKnownGoodVersions = async (filePath, lastKnownGoodVersions) => {
const knownGoodVersions = await readJsonFile(filePath);
const set = new Set();
const versions = knownGoodVersions.versions;
for (const entry of versions) {
set.add(entry.version);
}
for (const entry of Object.values(lastKnownGoodVersions.channels)) {
const {version, revision} = entry;
if (set.has(version)) {
continue;
}
set.add(version);
versions.push({
version,
revision,
});
knownGoodVersions.timestamp = createTimestamp();
}
knownGoodVersions.versions.sort((a, b) => {
if (isOlderVersion(a.version, b.version)) return -1;
if (a.version === b.version) return 0; // This cannot happen.
return 1;
});
await writeJsonFile(filePath, knownGoodVersions);
return knownGoodVersions;
};
const addDownloads = (data, key) => {
const copy = structuredClone(data);
for (const channelData of Object.values(copy[key])) {
const downloads = channelData.downloads = {};
for (const binary of binaries) {
const version = channelData.version;
if (binary === 'chromedriver' && predatesChromeDriverAvailability(version)) {
continue;
}
if (binary === 'chrome-headless-shell' && predatesChromeHeadlessShellAvailability(version)) {
continue;
}
if (binary === 'mojojs') {
// Exclude mojojs from the dashboard + API.
continue;
}
const downloadsForThisBinary = downloads[binary] = [];
// `mojojs.zip` is platform-agnostic. (This is dead code right now,
// but it’s useful in case we ever decide to include mojojs in the
// dashboard + API.)
if (binary === 'mojojs') {
const url = makeDownloadUrl({
version: version,
binary: binary,
});
downloadsForThisBinary.push({
url: url,
});
continue;
}
for (const platform of platforms) {
const url = makeDownloadUrl({
version: version,
platform: platform,
binary: binary,
});
downloadsForThisBinary.push({
platform: platform,
url: url,
});
}
}
}
return copy;
};
const updateLatestVersionsPerMilestone = async (filePath, lastKnownGoodVersionsData) => {
const latestVersionsPerMilestoneData = await readJsonFile(filePath);
const milestones = latestVersionsPerMilestoneData.milestones;
let needsUpdate = false;
for (const channelData of Object.values(lastKnownGoodVersionsData.channels)) {
const {version, revision} = channelData;
const milestone = version.split('.')[0];
if (Object.hasOwn(milestones, milestone)) {
const current = milestones[milestone];
if (isOlderVersion(current.version, version)) {
needsUpdate = true;
current.version = version;
current.revision = revision;
}
} else {
needsUpdate = true;
milestones[milestone] = {
milestone,
version,
revision,
};
}
}
if (needsUpdate) {
latestVersionsPerMilestoneData.timestamp = createTimestamp();
}
await writeJsonFile(filePath, latestVersionsPerMilestoneData);
return latestVersionsPerMilestoneData;
};
const prepareLatestPatchVersionsPerBuild = (knownGoodVersions) => {
const map = new Map(); // partialVersion → versionInfo
const re = /(?<build>.*)\.(?<patch>\d+)$/;
for (const entry of knownGoodVersions.versions) {
const version = entry.version;
const match = re.exec(version);
const {build, patch} = match.groups;
if (map.has(build)) {
const knownEntry = map.get(build);
if (isOlderVersion(knownEntry.version, version)) {
map.set(build, entry);
}
} else {
map.set(build, entry);
}
}
const result = {
timestamp: knownGoodVersions.timestamp,
builds: {},
};
const builds = result.builds;
for (const [partialVersion, entry] of map) {
builds[partialVersion] = entry;
}
return result;
};
const DASHBOARD_DATA = await readJsonFile('./data/dashboard.json');
const lastKnownGoodVersionsData = await prepareLastKnownGoodVersionsData(DASHBOARD_DATA);
await writeJsonFile(
'./data/last-known-good-versions.json',
lastKnownGoodVersionsData
);
await writeJsonFile(
'./data/last-known-good-versions-with-downloads.json',
addDownloads(lastKnownGoodVersionsData, 'channels')
);
const latestVersionsPerMilestone = await updateLatestVersionsPerMilestone('./data/latest-versions-per-milestone.json', lastKnownGoodVersionsData);
await writeJsonFile(
'./data/latest-versions-per-milestone-with-downloads.json',
addDownloads(latestVersionsPerMilestone, 'milestones')
);
const knownGoodVersions = await updateKnownGoodVersions('./data/known-good-versions.json', lastKnownGoodVersionsData);
await writeJsonFile(
'./data/known-good-versions-with-downloads.json',
addDownloads(knownGoodVersions, 'versions')
);
const latestPatchVersionsPerBuild = prepareLatestPatchVersionsPerBuild(knownGoodVersions);
await writeJsonFile(
'./data/latest-patch-versions-per-build.json',
latestPatchVersionsPerBuild
);
await writeJsonFile(
'./data/latest-patch-versions-per-build-with-downloads.json',
addDownloads(latestPatchVersionsPerBuild, 'builds')
);
const writePerVersionFiles = async () => {
await Promise.all(addDownloads(knownGoodVersions, 'versions').versions.map((release) => {
const fileName = `./dist/${release.version}.json`;
return writeMinifiedJsonFile(fileName, release);
}));
};
await writePerVersionFiles();