|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +function toOwnerRepo(repo) { |
| 5 | + if (!repo) return null; |
| 6 | + |
| 7 | + let s = String(repo).trim().replace(/\/+$/, ""); |
| 8 | + |
| 9 | + // strip github prefixes |
| 10 | + s = s.replace(/^https?:\/\/(www\.)?github\.com\//i, ""); |
| 11 | + s = s.replace(/^(www\.)?github\.com\//i, ""); |
| 12 | + |
| 13 | + // remove /tree/... or /blob/... if pasted |
| 14 | + const parts = s.split("/").filter(Boolean); |
| 15 | + if (parts.length < 2) return null; |
| 16 | + |
| 17 | + return `${parts[0]}/${parts[1]}`; |
| 18 | +} |
| 19 | + |
| 20 | +async function getStars(ownerRepo) { |
| 21 | + const res = await fetch(`https://api.github.com/repos/${ownerRepo}`, { |
| 22 | + headers: { |
| 23 | + Accept: "application/vnd.github+json", |
| 24 | + "User-Agent": "openchoreo-stars-generator", |
| 25 | + ...(process.env.GITHUB_TOKEN |
| 26 | + ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } |
| 27 | + : {}), |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + if (!res.ok) { |
| 32 | + let body = ""; |
| 33 | + try { |
| 34 | + body = await res.text(); |
| 35 | + } catch { |
| 36 | + // ignore |
| 37 | + } |
| 38 | + return { ok: false, status: res.status, body }; |
| 39 | + } |
| 40 | + |
| 41 | + const data = await res.json(); |
| 42 | + const count = data?.stargazers_count; |
| 43 | + return { ok: true, stars: typeof count === "number" ? count : 0 }; |
| 44 | +} |
| 45 | + |
| 46 | +async function main() { |
| 47 | + const inputPath = path.join("src", "data", "marketplace-plugins.source.json"); |
| 48 | + const outputPath = path.join("src", "data", "marketplace-plugins.json"); |
| 49 | + |
| 50 | + const raw = await fs.readFile(inputPath, "utf8"); |
| 51 | + const plugins = JSON.parse(raw); |
| 52 | + |
| 53 | + const out = []; |
| 54 | + for (const p of plugins) { |
| 55 | + const ownerRepo = toOwnerRepo(p.repo); |
| 56 | + |
| 57 | + if (!ownerRepo) { |
| 58 | + console.warn(` Invalid repo for "${p.name}": ${p.repo}`); |
| 59 | + out.push({ ...p, stars: 0 }); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + const result = await getStars(ownerRepo); |
| 64 | + |
| 65 | + if (!result.ok) { |
| 66 | + console.warn( |
| 67 | + ` Stars fetch failed for ${ownerRepo} (${result.status}). Setting stars=0.` |
| 68 | + ); |
| 69 | + // uncomment next line if you want full error text |
| 70 | + // console.warn(result.body); |
| 71 | + out.push({ ...p, stars: 0 }); |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + out.push({ ...p, stars: result.stars }); |
| 76 | + } |
| 77 | + |
| 78 | + await fs.writeFile(outputPath, JSON.stringify(out, null, 2) + "\n", "utf8"); |
| 79 | + console.log(`Generated: ${outputPath}`); |
| 80 | +} |
| 81 | + |
| 82 | +main().catch((e) => { |
| 83 | + console.error(" Failed generating plugin JSON:", e); |
| 84 | + process.exit(1); |
| 85 | +}); |
0 commit comments