-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
fetch-stats.js
217 lines (185 loc) · 7.09 KB
/
fetch-stats.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
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
/**
* @fileoverview Script to fetch stats for the homepage
* @author Nicholas C. Zakas
*/
"use strict";
//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------
const semver = require("semver");
const cheerio = require("cheerio");
const { request } = require("undici");
const { graphql } = require("@octokit/graphql");
const fs = require("fs/promises");
const { DateTime } = require("luxon");
const util = require("util");
const downloadStats = require("download-stats");
const { upcomingVersionPrereleaseType } = require("./release-data");
//-----------------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------------
const statsFilePath = "./src/_data/stats.json";
const { ESLINT_GITHUB_TOKEN } = process.env;
if (!ESLINT_GITHUB_TOKEN) {
throw new Error("Missing ESLINT_GITHUB_TOKEN.");
}
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
const fetchWeeklyNpmDownloads = util.promisify(downloadStats.get.lastWeek);
async function fetchStatsFromGitHubAPI() {
const { repository } = await graphql(`query {
repository(owner:"eslint", name:"eslint") {
releases(first: 20, orderBy: { field: CREATED_AT, direction: DESC } ) {
nodes {
publishedAt
isPrerelease
tagName
}
}
stargazerCount
pushedAt
}
}`, {
headers: {
authorization: `token ${ESLINT_GITHUB_TOKEN}`,
Accept: "application/vnd.github.hawkgirl-preview+json"
}
});
/*
* We want to find:
*
* 1. `latestRelease` - This is the release tagged `latest` on the npm.
* For example, 8.56.0 or 9.0.0.
* In the terms of semver, this is the highest non-prerelease version.
* 2. `currentRelease` - This is the release with the newest features.
* This is normally the same as `latestRelease` (for example, 8.56.0 or 9.0.0),
* but if we are in the process of publishing prereleases of the upcoming
* major version, this is the most recent prerelease (for example, 9.0.0-alpha.2),
* the one tagged `next` on the npm.
* In the terms of semver, this is the highest version.
*
* So, we need to find the highest version and the highest non-prerelease version.
* We're fetching releases from GitHub, which returns them ordered by creation date.
* This order typically already matches the semver order. However, if we've recently released
* some backport versions (e.g., v8.57.0 after 9.0.0-alpha.2), the order in the fetched
* list will not follow semver:
*
* 8.57.0
* 9.0.0-alpha.2
* 9.0.0-alpha.1
* 9.0.0-alpha.0
* 8.56.0
* ...
*
* To account for this case, we'll first sort versions by semver, in descending order:
*
* 9.0.0-alpha.2
* 9.0.0-alpha.1
* 9.0.0-alpha.0
* 8.57.0
* 8.56.0
* ...
*
*/
const releases = repository.releases.nodes.toSorted(
(first, second) => semver.rcompare(first.tagName, second.tagName)
);
/*
* Now we know that the first version in the list is the highest version, and that
* the first non-prerelease version in the list is the highest non-prerelease version.
*/
const [currentRelease] = releases;
const latestRelease = releases.find(({ isPrerelease }) => !isPrerelease);
return {
latestVersion: latestRelease.tagName,
latestVersionDate: latestRelease.publishedAt,
currentVersion: currentRelease.tagName,
currentVersionDate: currentRelease.publishedAt,
currentVersionIsPrerelease: currentRelease.isPrerelease,
stars: repository.stargazerCount,
lastCommitDate: repository.pushedAt
};
}
async function fetchGitHubNetworkStats() {
const response = await request("https://github.com/eslint/eslint/network/dependents");
const html = await response.body.text();
const $ = cheerio.load(html);
const projectDependents = $("[href~='/eslint/eslint/network/dependents?dependent_type=REPOSITORY']").text();
return {
projectDependents: Number(projectDependents.trim().replace(/[^\d]/gu, ""))
};
}
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
(async () => {
const [
repoStats,
dependencyStats,
{ downloads: weeklyDownloads }
] = await Promise.all([
fetchStatsFromGitHubAPI(),
fetchGitHubNetworkStats(),
fetchWeeklyNpmDownloads("eslint")
]);
const stats = {
...repoStats,
...dependencyStats,
weeklyDownloads
};
const { currentVersion } = stats;
let nextVersion;
if (stats.currentVersionIsPrerelease) {
if (upcomingVersionPrereleaseType) {
/*
* prerelease -> prerelease
* Increments prerelease number or updates prerelease type.
* Examples:
* 9.0.0-alpha.0 -> 9.0.0-alpha.1
* 9.0.0-alpha.1 -> 9.0.0-beta.0
*/
nextVersion = semver.inc(currentVersion, "prerelease", upcomingVersionPrereleaseType);
} else {
/*
* prerelease -> major
* Example:
* 9.0.0-rc.1 -> 9.0.0
*/
nextVersion = semver.inc(currentVersion, "major");
}
} else {
if (upcomingVersionPrereleaseType) {
/*
* regular -> prerelease
* Example:
* 8.56.0 -> 9.0.0-alpha.0
*/
nextVersion = semver.inc(currentVersion, "premajor", upcomingVersionPrereleaseType);
} else {
/*
* regular -> regular
* Example:
* 8.56.0 -> 8.57.0
*/
nextVersion = semver.inc(currentVersion, "minor");
}
}
stats.nextVersion = `v${nextVersion}`;
/*
* Calculate next release date.
* We do scheduled releases every two weeks, on Fridays.
* One of the scheduled release dates was Friday, 2024-01-12. We'll use that date as the baseline.
* So, all planned releases are expected to be on 2024-01-12 + n * 14 days.
* Now we'll find the first such day after the current version date.
*/
const baseDate = DateTime.fromISO("2024-01-12");
const currentVersionDate = DateTime.fromISO(stats.currentVersionDate);
stats.nextVersionDate = currentVersionDate
.plus({
days: 14 - currentVersionDate.diff(baseDate, "days").days % 14
})
.toISODate();
await fs.writeFile(statsFilePath, JSON.stringify(stats, null, 4), { encoding: "utf8" });
console.log("Fetch Homepage Stats: Success");
})();