Files
oxc-project__oxc/.github/scripts/publish-if-needed.sh
Boshen 4da5da8eed fix(ci): skip already-published npm packages instead of erroring (#20173)
## Summary

- CI publish workflow fails when re-run after a partial publish because
npm rejects republishing an existing version
- Add version-published checks to `check-npm-packages.js` so
already-published packages are skipped in the dry-run step
- Extract a shared `publish-if-needed.sh` script that checks the npm
registry before publishing, used by all three trusted publish steps
- Consolidate the old `checkPackageExists` and new version check into a
single `npmViewVersion` helper to avoid redundant npm registry calls

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 13:20:59 +08:00

29 lines
960 B
Bash
Executable File

#!/usr/bin/env bash
# Publish an npm package only if its version is not already on the registry.
# Usage: bash .github/scripts/publish-if-needed.sh <pkg_dir> [publish_flags...]
# Example: bash .github/scripts/publish-if-needed.sh npm/oxlint --provenance --access public --no-git-checks
set -euo pipefail
pkg_dir="$1"
shift
read -r name version < <(node -e "const p=require('./${pkg_dir}/package.json'); console.log(p.name+' '+p.version)")
npm_stderr=$(mktemp)
if published=$(npm view "${name}@${version}" version 2>"$npm_stderr"); then
if [ "$published" = "$version" ]; then
rm -f "$npm_stderr"
echo "⏭ ${name}@${version} already published, skipping."
exit 0
fi
else
# npm view failed — log the error but continue to publish (which will
# produce a clear error if the registry is actually unreachable).
echo "⚠ npm view ${name}@${version} failed:"
cat "$npm_stderr" >&2
fi
rm -f "$npm_stderr"
pnpm publish "${pkg_dir}/" "$@"