2025-01-11 10:20:49 +08:00
|
|
|
import styleText from "node-style-text";
|
2025-12-01 07:25:25 +08:00
|
|
|
import { fetchText, logPromise, processFile, runGit } from "../utilities.js";
|
2020-05-20 17:51:08 +08:00
|
|
|
|
2025-11-28 22:58:44 +08:00
|
|
|
async function getNpmDependentsCount() {
|
2020-05-20 17:51:08 +08:00
|
|
|
const npmPage = await logPromise(
|
|
|
|
|
"Fetching npm dependents count",
|
2021-04-21 22:15:12 +08:00
|
|
|
fetchText("https://www.npmjs.com/package/prettier"),
|
2020-05-20 17:51:08 +08:00
|
|
|
);
|
|
|
|
|
const dependentsCountNpm = Number(
|
2025-12-21 23:44:08 +08:00
|
|
|
npmPage.match(/"dependentsCount":"(?<dependentsCount>\d+)",/).groups
|
2025-11-28 22:58:44 +08:00
|
|
|
.dependentsCount,
|
2020-05-20 17:51:08 +08:00
|
|
|
);
|
2020-12-21 17:09:20 +08:00
|
|
|
if (Number.isNaN(dependentsCountNpm)) {
|
2020-05-20 17:51:08 +08:00
|
|
|
throw new TypeError(
|
|
|
|
|
"Invalid data from https://www.npmjs.com/package/prettier",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-28 22:58:44 +08:00
|
|
|
return dependentsCountNpm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getGithubDependentsCount() {
|
2020-05-20 17:51:08 +08:00
|
|
|
const githubPage = await logPromise(
|
|
|
|
|
"Fetching github dependents count",
|
2021-04-21 22:15:12 +08:00
|
|
|
fetchText("https://github.com/prettier/prettier/network/dependents"),
|
2020-05-20 17:51:08 +08:00
|
|
|
);
|
|
|
|
|
const dependentsCountGithub = Number(
|
|
|
|
|
githubPage
|
2022-11-21 23:41:27 +08:00
|
|
|
.replaceAll("\n", "")
|
2020-05-20 17:51:08 +08:00
|
|
|
.match(
|
2025-12-21 23:44:08 +08:00
|
|
|
/<svg.*?octicon-code-square.*?>.*?<\/svg>\s*(?<dependentsCount>[\d,]+)\s*Repositories\s*<\/a>/,
|
2025-11-28 22:58:44 +08:00
|
|
|
)
|
|
|
|
|
.groups.dependentsCount.replaceAll(",", ""),
|
2020-05-20 17:51:08 +08:00
|
|
|
);
|
2025-11-28 22:58:44 +08:00
|
|
|
if (Number.isNaN(dependentsCountGithub)) {
|
2020-05-20 17:51:08 +08:00
|
|
|
throw new TypeError(
|
|
|
|
|
"Invalid data from https://github.com/prettier/prettier/network/dependents",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-28 22:58:44 +08:00
|
|
|
return dependentsCountGithub;
|
|
|
|
|
}
|
2020-09-12 03:49:44 +09:00
|
|
|
|
2025-11-28 22:58:44 +08:00
|
|
|
async function update({ repo }) {
|
|
|
|
|
const [
|
|
|
|
|
{ value: dependentsCountNpm, reason: dependentsNpmError },
|
|
|
|
|
{ value: dependentsCountGithub, reason: dependentsGithubError },
|
|
|
|
|
] = await Promise.allSettled([
|
|
|
|
|
getNpmDependentsCount(),
|
|
|
|
|
getGithubDependentsCount(),
|
|
|
|
|
]);
|
2020-09-12 03:49:44 +09:00
|
|
|
|
2025-11-28 22:58:44 +08:00
|
|
|
if (dependentsCountNpm || dependentsCountGithub) {
|
|
|
|
|
processFile("website/src/pages/index.jsx", (content) => {
|
|
|
|
|
if (dependentsCountNpm) {
|
|
|
|
|
content = content.replace(
|
2025-12-21 23:44:08 +08:00
|
|
|
/(<strong data-placeholder="dependent-npm">)(.*?)(<\/strong>)/,
|
2025-11-28 22:58:44 +08:00
|
|
|
`$1${formatNumber(dependentsCountNpm)}$3`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dependentsCountGithub) {
|
|
|
|
|
content = content.replace(
|
2025-12-21 23:44:08 +08:00
|
|
|
/(<strong data-placeholder="dependent-github">)(.*?)(<\/strong>)/,
|
2025-11-28 22:58:44 +08:00
|
|
|
`$1${formatNumber(dependentsCountGithub)}$3`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content;
|
2021-04-21 22:15:12 +08:00
|
|
|
});
|
2025-11-28 22:58:44 +08:00
|
|
|
|
|
|
|
|
const isUpdated = await logPromise(
|
|
|
|
|
"Checking if dependents count has been updated",
|
|
|
|
|
async () =>
|
|
|
|
|
(await runGit(["diff", "--name-only"])).stdout ===
|
|
|
|
|
"website/src/pages/index.jsx",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (isUpdated) {
|
|
|
|
|
await logPromise("Committing and pushing to remote", async () => {
|
|
|
|
|
await runGit(["add", "."]);
|
|
|
|
|
await runGit(["commit", "-m", "Update dependents count"]);
|
|
|
|
|
await runGit(["push", "--repo", repo]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dependentsNpmError) {
|
|
|
|
|
throw dependentsNpmError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dependentsGithubError) {
|
|
|
|
|
throw dependentsGithubError;
|
2020-09-12 03:49:44 +09:00
|
|
|
}
|
2020-05-20 17:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatNumber(value) {
|
|
|
|
|
if (value < 1e4) {
|
|
|
|
|
return String(value).slice(0, 1) + "0".repeat(String(value).length - 1);
|
|
|
|
|
}
|
|
|
|
|
if (value < 1e6) {
|
|
|
|
|
return Math.floor(value / 1e2) / 10 + "k";
|
|
|
|
|
}
|
2020-07-31 19:36:12 +08:00
|
|
|
return Math.floor(value / 1e5) / 10 + " million";
|
2020-05-20 17:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-03 09:29:13 +08:00
|
|
|
export default async function updateDependentsCount({ dry, next, repo }) {
|
2023-12-17 22:51:37 +09:00
|
|
|
if (dry || next) {
|
2022-10-14 17:17:47 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 17:51:08 +08:00
|
|
|
try {
|
2025-03-03 09:29:13 +08:00
|
|
|
await update({ repo });
|
2020-05-20 17:51:08 +08:00
|
|
|
} catch (error) {
|
2025-01-11 10:20:49 +08:00
|
|
|
console.log(styleText.red.bold(error.message));
|
2020-05-20 17:51:08 +08:00
|
|
|
}
|
2021-07-26 09:24:44 +08:00
|
|
|
}
|