Files
plugins/scripts/tagged-kind.mjs
T
平平 92de181056 tagged: a page with its own service module is a page (#1226)
A page plugin whose page talks to a local service it ships as its
main (magpie.page.service) was read as a provider by the tagged list.
A package with magpie.page.file and its own module is now a page when
its keywords include "magpie-page"; a provider with a settings page
says no such keyword and stays a provider. Matches magpie v0.1.4331
(50a38371d). READ is 3, so every tagged repository is read again.

By xiaopeng66.

Run at 630fbbf (mote, HOME=$(mktemp -d)): bun test scripts/: 40 pass,
0 fail. Broken by hand: the keyword test forced false, "a page stays a
page when it also has a module and service" fails.

Not tried: the tagged list's real reread (it reaches GitHub).
2026-11-16 22:51:06 +08:00

61 lines
2.4 KiB
JavaScript

// What a tagged repository is, and the file it loads, as magpie reads them
// (internal/plugin/github.go readTagged). Kept apart from tagged.mjs, which
// reaches GitHub when loaded, so they can be tested.
// READ names this way of reading: tagged.json carries it, and a file read
// another way is read again whole, not only the repositories pushed to.
// 2: an agent plugin with a module of its own is an agent, and hooks are
// read (#1200).
// 3: a page with its own module is a page only when its keywords include
// magpie-page; a page without its own module remains a page as before.
export const READ = 3
export const named = (e) => !!e && (typeof e !== "object" || Object.keys(e).length > 0)
export const pkgEntry = (exports, main) => {
const pick = (v) => {
if (typeof v === "string") return v
if (v && typeof v === "object") {
if ("." in v) return pick(v["."])
for (const k of ["bun", "import", "default", "node", "require"]) {
const s = pick(v[k])
if (s) return s
}
}
return ""
}
return pick(exports) || main || "index.js"
}
// runs is whether Bun loads a module of the package's own (main, exports)
export const runs = (pj) => !!pj.main || named(pj.exports)
const said = (v) => String(v ?? "").trim() !== ""
// kindOf is the kind magpie shows, or "" for a provider
export function kindOf(pj) {
const mg = pj.magpie ?? {}
const r = runs(pj)
if (!r && said(mg.middleware)) return "middleware"
// a package that adds an agent is an agent plugin, its module (main)
// only what keeps the agent working, not a provider (#737, #1200)
if (said(mg.agent)) return "agent"
if (said(mg.page?.file) && (!r || (Array.isArray(pj.keywords) && pj.keywords.some((k) => typeof k === "string" && k.trim().toLowerCase() === "magpie-page")))) return "page"
if (!r && said(mg.hooks)) return "hooks"
return ""
}
// entryOf is the file the installed plugin loads, which the repository must
// have: its own module when it has one, else the file its kind declares.
// git is whether it is installed from the repository rather than from npm.
export function entryOf(pj, kind, git) {
const mg = pj.magpie ?? {}
if (!runs(pj)) {
if (kind === "middleware") return mg.middleware
if (kind === "agent") return mg.agent
if (kind === "page") return mg.page?.file
if (kind === "hooks") return mg.hooks
}
return git ? pkgEntry(pj.exports, pj.main) : pkgEntry(null, "")
}