Files
yetone f5308cdb0f publish: every version is packed onto the dist branch, so a new plugin merges without waiting for npm
A new package's first npm version can't go through trusted publishing
(a Trusted Publisher is set on a package that exists, with an owner's
2FA), so reviewed plugin PRs (#84 and others) sat unmerged until an owner
published by hand. Now publish.yml's dist job packs every package's
version (scripts/dist.mjs, npm pack --ignore-scripts) onto the dist
branch: tgz/<file> and index.json with each tarball's sha512. magpie
installs a @magpie-community package from there when npm doesn't have
the version. A new package without NPM_TOKEN is a notice, not a failed
run; a version npm refuses still fails it, now naming the stage-only
default of trusts made since 2026-09-03 and --allow-publish.

Run: node scripts/dist.mjs into an empty folder packed all 22 packages
(index.json 164K); the zed tarball's sha512 equals npm pack's integrity;
a second run packs nothing. The workflow itself is tried on main after
this push (workflow_dispatch).
2026-10-15 04:42:51 +08:00

56 lines
3.0 KiB
JavaScript

// Packs every package's version onto the `dist` branch, where magpie
// installs it from when npm doesn't have it: a new package (npm's trusted
// publishing can't create one, and its first version waits for an owner's
// 2FA), or a version npm refused. Merging is all a plugin needs to reach
// magpie.
// node scripts/dist.mjs <folder> (the dist branch checked out there)
// The branch holds tgz/<npm pack's file name> and index.json:
// {version: 1, packages: {<name>: {latest, readme,
// versions: {<version>: {file, integrity, time, manifest}}}}}
// integrity is the tarball's sha512, as npm writes it; magpie installs a
// tarball only when it matches. A version packed once is never packed
// again, so a file magpie checked stays what it checked; a package taken
// out of the repo stays in the index for those who installed it.
import { execFileSync } from "node:child_process"
import { createHash } from "node:crypto"
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
import { join, resolve } from "node:path"
const out = resolve(process.argv[2] ?? "")
if (!process.argv[2] || !existsSync(out)) {
console.error("usage: node scripts/dist.mjs <folder with the dist branch checked out>")
process.exit(2)
}
const root = resolve(import.meta.dirname, "..")
const tgz = join(out, "tgz")
mkdirSync(tgz, { recursive: true })
const indexFile = join(out, "index.json")
const index = existsSync(indexFile) ? JSON.parse(readFileSync(indexFile, "utf8")) : { version: 1, packages: {} }
const README_MOST = 200 << 10
const added = []
for (const dir of readdirSync(join(root, "packages")).sort()) {
const pj = join(root, "packages", dir, "package.json")
if (!existsSync(pj)) continue
const manifest = JSON.parse(readFileSync(pj, "utf8"))
if (manifest.private) continue
const { name, version } = manifest
const entry = (index.packages[name] ??= { versions: {} })
const had = entry.versions[version]
if (!had || !existsSync(join(out, had.file))) {
// a package's own scripts never run here: this job can push
const [packed] = JSON.parse(execFileSync("npm", ["pack", "--json", "--ignore-scripts", "--pack-destination", tgz], { cwd: join(root, "packages", dir), encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] }))
const integrity = "sha512-" + createHash("sha512").update(readFileSync(join(tgz, packed.filename))).digest("base64")
entry.versions[version] = { file: "tgz/" + packed.filename, integrity, time: new Date().toISOString(), manifest }
added.push(`${name}@${version}`)
}
entry.latest = version
const readme = join(root, "packages", dir, "README.md")
entry.readme = existsSync(readme) ? readFileSync(readme, "utf8").slice(0, README_MOST) : ""
}
const sorted = { version: 1, packages: Object.fromEntries(Object.entries(index.packages).sort(([a], [b]) => a.localeCompare(b))) }
writeFileSync(indexFile, JSON.stringify(sorted, null, 2) + "\n")
console.log(added.length ? "packed: " + added.join(" ") : "every version is packed already")