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 };