mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
- add monochrome GitHub and Gitee assets with light/dark CSS backgrounds - replace the GitHub text link with icon links for both repositories - fetch and cache repository star counts at build time and render them beside the icons when available - abbreviate counts of 1000 or more as `Nk` and separate the navbar actions with vertical dividers - move the version number from the navbar to a badge below the Home title - harden the star script for cold checkouts: root-relative paths, request timeouts, cache fallbacks and CI coverage
145 lines
3.9 KiB
JavaScript
145 lines
3.9 KiB
JavaScript
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const HOUR = 60 * 60 * 1000;
|
|
const DAY = 24 * HOUR;
|
|
|
|
const SOURCES = {
|
|
github: "https://api.github.com/repos/lc-soft/LCUI",
|
|
gitee: "https://gitee.com/api/v5/repos/lc-soft/LCUI",
|
|
};
|
|
|
|
const ROOT = path.resolve(__dirname, "..");
|
|
|
|
const DEFAULT_CACHE = path.join(
|
|
ROOT,
|
|
"examples/doc-viewer/.cache/repo-stars.json"
|
|
);
|
|
const DEFAULT_OUT = path.join(
|
|
ROOT,
|
|
"examples/doc-viewer/app/components/repo-stars.generated.ts"
|
|
);
|
|
const REQUEST_TIMEOUT_MS = 10000;
|
|
|
|
function readCache(cachePath) {
|
|
try {
|
|
const data = JSON.parse(fs.readFileSync(cachePath, "utf8"));
|
|
return data && typeof data === "object" ? data : {};
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
function isFresh(entry, now, maxAgeMs) {
|
|
return (
|
|
entry &&
|
|
typeof entry.count === "number" &&
|
|
typeof entry.fetchedAt === "number" &&
|
|
now - entry.fetchedAt < maxAgeMs
|
|
);
|
|
}
|
|
|
|
async function requestCount(key, fetchImpl) {
|
|
const response = await fetchImpl(SOURCES[key], {
|
|
headers: { "User-Agent": "lcui-doc-viewer" },
|
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(`request failed with status ${response.status}`);
|
|
}
|
|
const data = await response.json();
|
|
if (typeof data.stargazers_count !== "number") {
|
|
throw new Error("response has no stargazers_count");
|
|
}
|
|
return data.stargazers_count;
|
|
}
|
|
|
|
function writeGeneratedFile(outPath, result) {
|
|
const github = result.github ? result.github.count : null;
|
|
const gitee = result.gitee ? result.gitee.count : null;
|
|
const values = `{ github: ${github === null ? "null" : github}, gitee: ${gitee === null ? "null" : gitee} }`;
|
|
|
|
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
fs.writeFileSync(
|
|
outPath,
|
|
[
|
|
"// Generated by scripts/fetch-repo-stars.js",
|
|
"export const repoStars: { github: number | null; gitee: number | null } = " +
|
|
values +
|
|
";",
|
|
"",
|
|
].join("\n")
|
|
);
|
|
}
|
|
|
|
async function fetchRepoStars(options = {}) {
|
|
const cachePath = options.cachePath || DEFAULT_CACHE;
|
|
const outPath = options.outPath || DEFAULT_OUT;
|
|
const maxAgeMs = options.maxAgeMs ?? DAY;
|
|
const now = options.now ? options.now() : Date.now();
|
|
const fetchImpl = options.fetchImpl || fetch;
|
|
const cache = readCache(cachePath);
|
|
const result = {};
|
|
|
|
for (const key of Object.keys(SOURCES)) {
|
|
const entry = cache[key];
|
|
if (isFresh(entry, now, maxAgeMs)) {
|
|
result[key] = entry;
|
|
continue;
|
|
}
|
|
try {
|
|
result[key] = { count: await requestCount(key, fetchImpl), fetchedAt: now };
|
|
} catch (err) {
|
|
if (entry && typeof entry.count === "number") {
|
|
result[key] = { count: entry.count, fetchedAt: entry.fetchedAt };
|
|
} else {
|
|
result[key] = null;
|
|
}
|
|
console.warn(`[stars] ${key}: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
writeGeneratedFile(outPath, result);
|
|
try {
|
|
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
fs.writeFileSync(cachePath, JSON.stringify(result, null, 2) + "\n");
|
|
} catch (err) {
|
|
console.warn(`[stars] cannot write cache: ${err.message}`);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function parseArgs(argv) {
|
|
const options = {};
|
|
for (let i = 0; i < argv.length; ++i) {
|
|
switch (argv[i]) {
|
|
case "--max-age": {
|
|
const hours = Number(argv[++i]);
|
|
if (Number.isFinite(hours) && hours > 0) {
|
|
options.maxAgeMs = hours * HOUR;
|
|
} else {
|
|
console.warn(`[stars] ignoring invalid --max-age value`);
|
|
}
|
|
break;
|
|
}
|
|
case "--cache":
|
|
options.cachePath = argv[++i];
|
|
break;
|
|
case "--out":
|
|
options.outPath = argv[++i];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return options;
|
|
}
|
|
|
|
if (require.main === module) {
|
|
fetchRepoStars(parseArgs(process.argv.slice(2))).catch((err) => {
|
|
console.warn(`[stars] ${err.message}`);
|
|
});
|
|
}
|
|
|
|
module.exports = { fetchRepoStars };
|