From 80baa275706d3f6ac1e607e1aa33bcfbc293f173 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 18:07:43 +0800 Subject: [PATCH] scripts/owner.sh: an npm owner's first publishes and Trusted Publishers in one command Finds from the registry, for origin/main, each package npm doesn't have (first publish, then publish.yml as its Trusted Publisher with --allow-publish) and each whose version npm lacks (its trust revoked and made again with --allow-publish, then publish.yml dispatched). Runs npm 11 through npx, since npm trust needs 11.5. Run: sh -n; --dry-run against the real registry lists opendesign 0.1.4 and type4me 0.1.0 as new, zed 0.1.7 and zen-free 0.1.8 as behind, and leaves no worktree behind. The trust-id parser was tried on sample JSON. The publish and trust steps themselves need an owner's 2FA and weren't run. --- README.md | 17 +++++-- scripts/owner.sh | 114 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 3 deletions(-) create mode 100755 scripts/owner.sh diff --git a/README.md b/README.md index 729b5d6..dbbd087 100644 --- a/README.md +++ b/README.md @@ -207,9 +207,20 @@ A version bump pushed to `main` ships itself, two ways A package's first version can't go through trusted publishing: a Trusted Publisher is set on a package that exists, and setting one takes an owner's 2FA. Until an owner of the magpie-community npm org -publishes it (`scripts/publish.sh `) and sets its Trusted Publisher as -above, magpie serves it from `dist`, and the publish run notes it without -failing. The repository's `NPM_TOKEN` secret, a granular token with 2FA +publishes it and sets its Trusted Publisher as above, magpie serves it +from `dist`, and the publish run notes it without failing. + +An owner does all of it with one command, from any checkout: + +```sh +sh scripts/owner.sh # --dry-run says what it would do +``` + +It first-publishes every package npm doesn't have and makes its Trusted +Publisher, makes again (with npm publish allowed) the Trusted Publisher of +every package whose version npm lacks, and runs `publish.yml` for those. +It works from origin/main, skips what is up to date, and asks for 2FA in +the browser. The repository's `NPM_TOKEN` secret, a granular token with 2FA bypassed, still publishes a first version while npm lets such tokens publish (npm plans to end that in January 2027). diff --git a/scripts/owner.sh b/scripts/owner.sh new file mode 100755 index 0000000..f5a999d --- /dev/null +++ b/scripts/owner.sh @@ -0,0 +1,114 @@ +#!/bin/sh +# Everything only an owner of the magpie-community npm org can do, in one +# run (README, Publishing): +# - a package npm doesn't have yet: its first version is published from +# here (trusted publishing can't create a package), then publish.yml is +# made its Trusted Publisher with npm publish allowed; +# - a package whose version on main npm doesn't have: its Trusted Publisher +# is made again with npm publish allowed (one made since 2026-09-03 +# allows stage publish only), and publish.yml is run to publish it. +# It works from a checkout of origin/main of its own, whatever the checkout +# it is run from holds, and does nothing for a package that is up to date, +# so running it again is safe. npm asks for 2FA in the browser; a sign-in +# there can skip it for the next 5 minutes. +# +# sh scripts/owner.sh do it +# sh scripts/owner.sh --dry-run only say what it would do +set -eu + +dry="" +[ "${1:-}" = "--dry-run" ] && dry=1 +repo=magpie-community/plugins +reg=https://registry.npmjs.org/ +export npm_config_registry="$reg" +# npm trust needs npm 11.5 or later +npm11() { npx -y npm@11 "$@"; } + +root=$(git -C "$(dirname "$0")" rev-parse --show-toplevel) +git -C "$root" fetch -q https://github.com/$repo.git main +work=$(mktemp -d) +trap 'git -C "$root" worktree remove --force "$work/main" >/dev/null 2>&1 || true; rm -rf "$work"' EXIT +git -C "$root" worktree add -q --detach "$work/main" FETCH_HEAD + +# each package as " ", state new, behind or ok +node - "$work/main/packages" "$reg" > "$work/state" <<'EOF' +const fs = require("fs"), path = require("path") +const [dir, reg] = process.argv.slice(2) +;(async () => { + for (const d of fs.readdirSync(dir).sort()) { + let pj + try { pj = JSON.parse(fs.readFileSync(path.join(dir, d, "package.json"), "utf8")) } catch { continue } + if (pj.private) continue + const r = await fetch(reg + pj.name.replace("/", "%2f"), { headers: { accept: "application/vnd.npm.install-v1+json" } }) + let state + if (r.status === 404) state = "new" + else if (!r.ok) throw new Error(`${pj.name}: npm answered ${r.status}`) + else state = (await r.json()).versions?.[pj.version] ? "ok" : "behind" + console.log(d, pj.name, pj.version, state) + } +})().catch(e => { console.error(e.message); process.exit(1) }) +EOF + +todo=$(grep -v ' ok$' "$work/state" || true) +if [ -z "$todo" ]; then + echo "npm has every package's version on main: nothing to do." + exit 0 +fi +echo "To do:" +echo "$todo" | while read -r d name version state; do + case $state in + new) echo " $name $version: first publish, then its Trusted Publisher" ;; + behind) echo " $name $version: its Trusted Publisher again, with npm publish allowed" ;; + esac +done +[ -n "$dry" ] && exit 0 + +if ! npm11 whoami >/dev/null 2>&1; then + echo "Signing in to npm (in the browser)..." + npm11 login --auth-type=web +fi +echo "npm user: $(npm11 whoami)" + +trust() { + # a trust can't be changed, only revoked and made again + ids=$(npm11 trust list "$1" --json 2>/dev/null | node -e ' + let s = ""; process.stdin.on("data", c => s += c).on("end", () => { + const ids = new Set(), walk = v => { + if (Array.isArray(v)) v.forEach(walk) + else if (v && typeof v === "object") { if (typeof v.id === "string") ids.add(v.id); Object.values(v).forEach(walk) } + } + try { walk(JSON.parse(s)) } catch {} + console.log([...ids].join(" ")) + })') || ids="" + for id in $ids; do + npm11 trust revoke "$1" --id="$id" -y + done + npm11 trust github "$1" --file publish.yml --repo "$repo" --allow-publish -y +} + +failed="" dispatch="" +while read -r d name version state; do + echo + echo "== $name $version" + case $state in + new) + if (cd "$work/main/packages/$d" && npm11 publish --access public) && trust "$name"; then :; else failed="$failed $name"; fi + ;; + behind) + if trust "$name"; then dispatch=1; else failed="$failed $name"; fi + ;; + esac +done <