diff --git a/.github/workflows/tagged.yml b/.github/workflows/tagged.yml index c593fbc..1241d7e 100644 --- a/.github/workflows/tagged.yml +++ b/.github/workflows/tagged.yml @@ -1,7 +1,9 @@ # Builds tagged.json on the `dist` branch every half hour: the repositories # tagged magpie-plugin, which magpie's Plugins page lists under Discover. # magpie reads it through usemagpie.ai/api/plugins/tagged, so a desktop that -# can't reach GitHub, or is rate-limited there, still has the list. +# can't reach GitHub, or is rate-limited there, still has the list. Each +# listed plugin's README is kept beside it (readmes/), read through +# usemagpie.ai/api/plugins/readme, so npm's human check can't hold it back. name: tagged on: @@ -35,7 +37,7 @@ jobs: fi node scripts/tagged.mjs "$RUNNER_TEMP/dist" cd "$RUNNER_TEMP/dist" - git add tagged.json + git add -A tagged.json readmes if git diff --cached --quiet; then echo "tagged.json is current" else diff --git a/scripts/tagged.mjs b/scripts/tagged.mjs index e6ee9d2..2be351b 100644 --- a/scripts/tagged.mjs +++ b/scripts/tagged.mjs @@ -6,8 +6,8 @@ // tagged.json holds {version: 1, generated, plugins: [Tagged...]}, most // starred first. A repository whose package.json can't be read this time // stays as the last file had it; a search that fails writes nothing. -import { existsSync, readFileSync, writeFileSync } from "node:fs" -import { join, resolve, posix } from "node:path" +import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs" +import { dirname, join, resolve, posix } from "node:path" const TOPIC = "magpie-plugin" const pkgName = /^(@[a-z0-9][a-z0-9._~-]*\/)?[a-z0-9][a-z0-9._~-]*$/ @@ -177,6 +177,55 @@ if (!plugins.length) { console.error("no plugin read; keeping the last file") process.exit(1) } +// Each listed plugin's README, kept as readmes/.md beside tagged.json +// ( is the npm name, or github/owner/repo for one installed from the +// repository: internal/plugin/readme_cache.go's readmeKey), so the Discover +// page's README comes from usemagpie.ai and not from npm, whose human check +// stands in front of some networks (River_WLan on X). Read again when the +// repository was pushed to or the file is missing; a read that fails keeps +// the file there was. +const keyOf = (t) => (t.spec.startsWith("github:") ? "github/" + t.repo : t.spec) +const branches = new Map(items.map((it) => [it.full_name.toLowerCase(), it.default_branch || "HEAD"])) +const readmes = join(out, "readmes") +const wanted = new Set() +const MOST = 200 << 10 +async function keep(t) { + const key = keyOf(t) + if (key.includes("..") || key.split("/").some((p) => !p)) return + const f = join(readmes, key + ".md") + wanted.add(posix.normalize(key + ".md")) + const was = before.get(t.repo.toLowerCase()) + if (existsSync(f) && was && was.pushed === t.pushed) return + const branch = encodeURIComponent(branches.get(t.repo.toLowerCase()) ?? "HEAD") + for (const name of ["README.md", "readme.md", "Readme.md", "README"]) { + let r + try { + r = await get(`https://raw.githubusercontent.com/${t.repo}/${branch}/${name}`) + } catch (e) { + if (e instanceof NotFound) continue + console.error(`${t.repo} README: ${e.message}`) + return + } + const buf = Buffer.from(await r.arrayBuffer()).subarray(0, MOST) + mkdirSync(dirname(f), { recursive: true }) + writeFileSync(f, buf) + return + } + rmSync(f, { force: true }) // it has none now +} +const todo = [...plugins] +await Promise.all(Array.from({ length: 8 }, async () => { for (let t; (t = todo.shift()); ) await keep(t) })) +// a plugin no longer listed leaves no README behind +const sweep = (dir, rel) => { + if (!existsSync(dir)) return + for (const e of readdirSync(dir, { withFileTypes: true })) { + const r = rel ? rel + "/" + e.name : e.name + if (e.isDirectory()) sweep(join(dir, e.name), r) + else if (!wanted.has(r)) rmSync(join(dir, e.name), { force: true }) + } +} +sweep(readmes, "") + const same = JSON.stringify(last.map((t) => t.repo).sort()) === JSON.stringify(plugins.map((t) => t.repo).sort()) writeFileSync(file, JSON.stringify({ version: 1, generated: new Date().toISOString(), plugins })) console.log(`${plugins.length} plugins, ${failed} unread${same ? "" : ", list changed"}`)