-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
237 lines (201 loc) · 6.61 KB
/
index.ts
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
232
233
234
235
236
237
import { compare, parse } from "@std/semver";
const GITHUB_TOKEN = Deno.env.get("GITHUB_TOKEN");
if (!GITHUB_TOKEN) {
console.error("No GITHUB_TOKEN value provided");
Deno.exit(1);
}
const noCache = Deno.args.includes("--no-cache");
interface Release {
created_at: string;
name: string;
tag_name: string;
}
interface VsCode {
created_at: string;
electron: string;
name: string;
node: string;
chromium: string;
version: string;
}
/**
* Parses the link header value in order to retrieve the next link value for pagination
*
* @param {string} header - The link header from the response
* @returns {string|undefined} - The next link value if one exists
*/
function parseLinkHeader(header: string): string | undefined {
let nextLink: string | undefined;
for (const part of header.split(/,\s*</)) {
const [link, rel] = part.split(/;\s*/);
if (!rel.includes("next")) {
continue;
}
nextLink = link.match(/<(.*)>/)?.[1];
}
return nextLink;
}
/**
* Performs a GitHub API request handling pagination and authentication
*
* @template T
* @param {string} uri - The URI to request
* @returns {T[]} The response from the GitHub API
*/
async function githubApiRequest<T extends Array<unknown>>(uri: string): Promise<T> {
const response = await fetch(uri, {
headers: new Headers({
"User-Agent": "vscode-versions",
"Authorization": `token ${GITHUB_TOKEN}`,
}),
});
const data = await response.json() as T;
const linkHeader = response.headers.get("link");
if (linkHeader) {
const nextLink = parseLinkHeader(linkHeader);
if (nextLink) {
data.push(...(await githubApiRequest<T>(nextLink)));
}
}
return data;
}
/**
* Request a file on GitHub
*
* @param {string} uri - The URI to request
* @returns {Promise<string>} The file contents
*/
async function githubFileRequest(uri: string): Promise<string> {
const response = await fetch(uri, {
headers: new Headers({
"User-Agent": "vscode-versions",
"Authorization": `token ${GITHUB_TOKEN}`,
}),
});
if (response.status >= 400) {
console.error(`Failed to get text for ${uri}`);
throw new Error(await response.text());
}
return response.text();
}
/**
* Looks up the Electron version included in the VS Code version
*
* @param {string} version - The VS Code version
* @returns {Promise<string>} The Electron version
*/
async function getElectronVersion(version: string): Promise<string> {
let electronVersion = "Unknown";
let rcFile;
try {
// newer versions moved to .npmrc
rcFile = await githubFileRequest(`https://raw.githubusercontent.com/Microsoft/vscode/${version}/.npmrc`);
} catch (_) {
// older versions used .yarnrc
rcFile = await githubFileRequest(`https://raw.githubusercontent.com/Microsoft/vscode/${version}/.yarns`);
}
const target = rcFile.match(/target[ =]"(\d.*)"/);
if (target && target[1]) {
electronVersion = target[1];
}
return electronVersion;
}
/**
* Looks up the Chromium version included in the Electron version used in VS Code
*
* @param electronVersion - The Electron version used in VS Code
* @returns {Promise<string>} The Chromium version
*/
async function getChromiumVersion(electronVersion: string): Promise<string> {
let chromiumVersion = "Unknown";
const file = await githubFileRequest(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/DEPS`);
const version = file.match(/'chromium_version':\s+'(\d.*)'/);
if (version && version.length > 1) {
chromiumVersion = version[1];
}
return chromiumVersion;
}
/**
* Looks up the Node.js version included in the Electron version in VS Code
*
* @param {string} electronVersion - The Electron version used in VS Code
* @returns {Promise<string>} The Node.js version
*/
async function getNodeVersion(electronVersion: string): Promise<string> {
let nodeVersion = "Unknown";
const file = await githubFileRequest(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/DEPS`);
const version = file.match(/'node_version':\s+'(v\d.*)'/);
const versionOrSha = version?.[1];
if (versionOrSha?.startsWith("v")) {
nodeVersion = versionOrSha.substring(1);
}
return nodeVersion;
}
/**
* Read the cached versions
*
* @returns {Promise<VsCode[]>} A promise which will resolve with an array of cached versions or
* an empty array if --no-cache was provided
*/
async function getCachedVersions(): Promise<VsCode[]> {
if (noCache) {
return [];
}
return JSON.parse(await Deno.readTextFile("./versions.json"));
}
async function getVsCodeVersions() {
const versions = await getCachedVersions();
const cachedVersions = versions.map((vscode) => vscode.version);
const releases = await githubApiRequest<Release[]>("https://api.github.com/repos/Microsoft/vscode/releases");
for (const release of releases) {
const { name, tag_name, created_at } = release;
if (!noCache && cachedVersions.includes(tag_name)) {
console.log(`Already have data for ${tag_name}`);
continue;
}
console.log(`Get versions for ${tag_name}`);
const electron = await getElectronVersion(tag_name);
const [chromium, node] = await Promise.all([
getChromiumVersion(electron),
getNodeVersion(electron),
]);
versions.push({
version: tag_name,
chromium,
electron,
node,
name,
created_at,
});
}
// reverse sort to ensure we have latest versions at the top
return versions.sort((a, b) => compare(parse(b.version), parse(a.version)));
}
const versions = await getVsCodeVersions();
await Deno.writeTextFile("./versions.json", JSON.stringify(versions, undefined, " "));
await Deno.writeTextFile(
"./README.md",
`# VS Code Versions
An overview of the Electron, Node.js, and Chromium version in each VS Code release.
Last updated: ${new Date().toISOString()}
|VS Code|Codename|Electron|Node|Chromium|
|:-------:|:--------:|:--------:|:----:|:------:|
${
versions.map((version) => (
`|[${version.version}](https://github.com/microsoft/vscode/releases/tag/${version.version})|${version.name}|${version.electron}|${version.node}|${version.chromium}|`
)).join("\n")
}
## How it works
The scripts works by doing the following:
1. Retrieve all the releases in the microsoft/vscode repo
2. For each release
- Retrieve the Electron version in VS Code
- Retrieve the Chromium and Node.js versions in Electron
3. Update the README file
## Running locally
1. [Install Deno](https://deno.land/#installation)
2. Create a .env file based on the .env.example file
3. Run using \`deno task run\`
\`:bulb: If you need to update the cache provide the --no-cache flag after index.ts\`
`,
);