tagged: keep each listed plugin's README on dist (readmes/), so magpie's Discover page reads it from usemagpie.ai, not npm (River_WLan on X)

Run: node scripts/tagged.mjs on wsl against the real GitHub into an empty folder: 369 plugins, 369 README files (5.5MB); a second run into the same folder read none again and kept 369. Not tried: the workflow itself on Actions (dispatched after the push).
This commit is contained in:
yetone
2026-11-11 16:01:13 +08:00
parent fcde82d39f
commit 4ecf633125
2 changed files with 55 additions and 4 deletions
+4 -2
View File
@@ -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
+51 -2
View File
@@ -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/<key>.md beside tagged.json
// (<key> 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"}`)