Skip to content

Commit edbcf1d

Browse files
committed
Fix dependents count
1 parent ec7f2ec commit edbcf1d

2 files changed

Lines changed: 63 additions & 30 deletions

File tree

scripts/release/steps/update-dependents-count.js

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import styleText from "node-style-text";
22
import { fetchText, logPromise, processFile, runGit } from "../utils.js";
33

4-
async function update({ repo }) {
4+
async function getNpmDependentsCount() {
55
const npmPage = await logPromise(
66
"Fetching npm dependents count",
77
fetchText("https://www.npmjs.com/package/prettier"),
88
);
99
const dependentsCountNpm = Number(
10-
npmPage.match(/"dependentsCount":(\d+),/u)[1],
10+
npmPage.match(/"dependentsCount":"(?<dependentsCount>\d+)",/u).groups
11+
.dependentsCount,
1112
);
1213
if (Number.isNaN(dependentsCountNpm)) {
1314
throw new TypeError(
1415
"Invalid data from https://www.npmjs.com/package/prettier",
1516
);
1617
}
1718

19+
return dependentsCountNpm;
20+
}
21+
22+
async function getGithubDependentsCount() {
1823
const githubPage = await logPromise(
1924
"Fetching github dependents count",
2025
fetchText("https://github.com/prettier/prettier/network/dependents"),
@@ -23,41 +28,69 @@ async function update({ repo }) {
2328
githubPage
2429
.replaceAll("\n", "")
2530
.match(
26-
/<svg.*?octicon-code-square.*?>.*?<\/svg>\s*([\d,]+)\s*Repositories\s*<\/a>/u,
27-
)[1]
28-
.replaceAll(",", ""),
31+
/<svg.*?octicon-code-square.*?>.*?<\/svg>\s*(?<dependentsCount>[\d,]+)\s*Repositories\s*<\/a>/u,
32+
)
33+
.groups.dependentsCount.replaceAll(",", ""),
2934
);
30-
if (Number.isNaN(dependentsCountNpm)) {
35+
if (Number.isNaN(dependentsCountGithub)) {
3136
throw new TypeError(
3237
"Invalid data from https://github.com/prettier/prettier/network/dependents",
3338
);
3439
}
3540

36-
processFile("website/src/pages/index.jsx", (content) =>
37-
content
38-
.replace(
39-
/(<strong data-placeholder="dependent-npm">)(.*?)(<\/strong>)/u,
40-
`$1${formatNumber(dependentsCountNpm)}$3`,
41-
)
42-
.replace(
43-
/(<strong data-placeholder="dependent-github">)(.*?)(<\/strong>)/u,
44-
`$1${formatNumber(dependentsCountGithub)}$3`,
45-
),
46-
);
41+
return dependentsCountGithub;
42+
}
4743

48-
const isUpdated = await logPromise(
49-
"Checking if dependents count has been updated",
50-
async () =>
51-
(await runGit(["diff", "--name-only"])).stdout ===
52-
"website/src/pages/index.jsx",
53-
);
44+
async function update({ repo }) {
45+
const [
46+
{ value: dependentsCountNpm, reason: dependentsNpmError },
47+
{ value: dependentsCountGithub, reason: dependentsGithubError },
48+
] = await Promise.allSettled([
49+
getNpmDependentsCount(),
50+
getGithubDependentsCount(),
51+
]);
5452

55-
if (isUpdated) {
56-
await logPromise("Committing and pushing to remote", async () => {
57-
await runGit(["add", "."]);
58-
await runGit(["commit", "-m", "Update dependents count"]);
59-
await runGit(["push", "--repo", repo]);
53+
if (dependentsCountNpm || dependentsCountGithub) {
54+
processFile("website/src/pages/index.jsx", (content) => {
55+
if (dependentsCountNpm) {
56+
content = content.replace(
57+
/(<strong data-placeholder="dependent-npm">)(.*?)(<\/strong>)/u,
58+
`$1${formatNumber(dependentsCountNpm)}$3`,
59+
);
60+
}
61+
62+
if (dependentsCountGithub) {
63+
content = content.replace(
64+
/(<strong data-placeholder="dependent-github">)(.*?)(<\/strong>)/u,
65+
`$1${formatNumber(dependentsCountGithub)}$3`,
66+
);
67+
}
68+
69+
return content;
6070
});
71+
72+
const isUpdated = await logPromise(
73+
"Checking if dependents count has been updated",
74+
async () =>
75+
(await runGit(["diff", "--name-only"])).stdout ===
76+
"website/src/pages/index.jsx",
77+
);
78+
79+
if (isUpdated) {
80+
await logPromise("Committing and pushing to remote", async () => {
81+
await runGit(["add", "."]);
82+
await runGit(["commit", "-m", "Update dependents count"]);
83+
await runGit(["push", "--repo", repo]);
84+
});
85+
}
86+
}
87+
88+
if (dependentsNpmError) {
89+
throw dependentsNpmError;
90+
}
91+
92+
if (dependentsGithubError) {
93+
throw dependentsGithubError;
6194
}
6295
}
6396

website/src/pages/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function UsersSection() {
331331
<div>
332332
<p>
333333
More than{" "}
334-
<strong data-placeholder="dependent-github">9.8 million</strong>{" "}
334+
<strong data-placeholder="dependent-github">9.9 million</strong>{" "}
335335
dependent repositories on GitHub
336336
</p>
337337
<Link
@@ -353,7 +353,7 @@ function UsersSection() {
353353
<div>
354354
<p>
355355
More than{" "}
356-
<strong data-placeholder="dependent-npm">19.8k</strong>{" "}
356+
<strong data-placeholder="dependent-npm">19.4k</strong>{" "}
357357
dependent packages on npm
358358
</p>
359359
<Link

0 commit comments

Comments
 (0)