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

54 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
# Publishes every package whose version isn't on npm yet.
# scripts/publish.sh [otp] (by hand: the npm account's one-time password)
# GitHub Actions runs it with no password on each version bump pushed to
# main (.github/workflows/publish.yml, npm's trusted publishing).
# One password covers the lot: it is sent with each publish while it lasts.
# Trusted publishing can't make a package npm has never seen, and npm
# refuses (404) one whose Trusted Publisher isn't set. Actions publishes
# both with the NPM_TOKEN secret, which npm falls back to when OIDC fails.
# Without the secret Actions passes a new package by and says so: magpie
# installs it from the dist branch (scripts/dist.mjs) until an owner
# publishes its first version, by hand `scripts/publish.sh <otp>` from an
# npm account of the magpie-community org. A version npm refuses fails the
# run: its Trusted Publisher needs mending.
set -e
cd "$(dirname "$0")/.."
# npm's own registry, whatever ~/.npmrc names (a mirror such as npmmirror
# takes no publish)
export npm_config_registry=https://registry.npmjs.org
# setup-node's .npmrc reads the token from NODE_AUTH_TOKEN
if [ -n "$NPM_TOKEN" ]; then
export NODE_AUTH_TOKEN="$NPM_TOKEN"
fi
bun scripts/check.mjs
otp="$1"
for dir in packages/*/; do
name=$(node -p "require('./${dir}package.json').name")
version=$(node -p "require('./${dir}package.json').version")
if [ "$(npm view "$name@$version" version 2>/dev/null)" = "$version" ]; then
echo "= $name@$version is on npm already"
continue
fi
if [ -n "$GITHUB_ACTIONS" ] && [ -z "$otp$NPM_TOKEN" ] && ! npm view "$name" name >/dev/null 2>&1; then
echo "::notice::$name is not on npm yet: trusted publishing can't create it, and the repository has no NPM_TOKEN secret. magpie installs it from the dist branch meanwhile; an owner publishes its first version (README, Publishing)."
new="$new $name"
continue
fi
if ! (cd "$dir" && npm publish --access public ${otp:+--otp="$otp"}); then
if [ -n "$GITHUB_ACTIONS" ] && [ -n "$NPM_TOKEN" ]; then
echo "::error::$name@$version wasn't published with OIDC or NPM_TOKEN: the token may have expired (npm's write tokens last 90 days at most) or lost access to @magpie-community. Replace the NPM_TOKEN secret (README, Publishing)."
elif [ -n "$GITHUB_ACTIONS" ]; then
echo "::error::$name@$version wasn't published: this workflow isn't its Trusted Publisher on npmjs.com, or the trust allows npm stage publish only (the default since 2026-09-03): npm trust github $name --file publish.yml --repo magpie-community/plugins --allow-publish (README, Publishing)."
fi
refused="$refused $name"
fi
done
if [ -n "$new" ]; then
echo "not published, new to npm (served from dist):$new"
fi
if [ -n "$refused" ]; then
echo "not published, refused by npm:$refused"
exit 1
fi