#!/usr/bin/env bash set -euo pipefail real_pnpm="${TEVM_REAL_PNPM:-}" if [ -z "${real_pnpm}" ]; then # -ef compares device and inode after following symlinks, including directory aliases. IFS=: read -r -a dirs <<< "$PATH" for dir in "${dirs[@]}"; do candidate="${dir:-.}/pnpm" if [[ -f "$candidate" && -x "$candidate" && ! "$candidate" -ef "$0" ]]; then real_pnpm="$candidate" break fi done elif [[ "$real_pnpm" != */* ]]; then real_pnpm="$(type -P -- "$real_pnpm" || true)" fi if [[ ! -f "$real_pnpm" || ! -x "$real_pnpm" || "$real_pnpm" -ef "$0" ]]; then echo "pnpm publish wrapper requires a distinct pnpm executable on PATH or in TEVM_REAL_PNPM" >&2 exit 1 fi if [ "${1:-}" != "publish" ]; then exec "${real_pnpm}" "$@" fi shift tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/tevm-publish.XXXXXXXX")" cleanup() { rm -rf "${tmpdir}" } trap cleanup EXIT "${real_pnpm}" pack --pack-destination "${tmpdir}" >/dev/null tarball="$(find "${tmpdir}" -maxdepth 1 -name '*.tgz' -print -quit)" if [ -z "${tarball}" ]; then echo "pnpm publish wrapper could not find a packed tarball" >&2 exit 1 fi # Defense-in-depth guard: refuse to publish any packed tarball whose manifest # still contains unresolved workspace:, link:, file:, or portal: specifiers. tar -xOf "${tarball}" package/package.json | node -e ' const fs = require("node:fs"); const pkg = JSON.parse(fs.readFileSync(0, "utf8")); const bad = []; for (const field of ["dependencies", "peerDependencies", "optionalDependencies"]) { for (const [dep, spec] of Object.entries(pkg[field] || {})) { if (typeof spec === "string" && /^(?:workspace:|link:|file:|portal:)/.test(spec.trim())) { bad.push(`${field}.${dep}=${spec}`); } } } if (bad.length > 0) { console.error(`Refusing to publish ${pkg.name}@${pkg.version}: leaked specifier(s): ${bad.join(", ")}`); process.exit(1); } ' npm_args=("publish" "${tarball}") provenance=false while [ "$#" -gt 0 ]; do case "$1" in --no-git-checks) shift ;; --provenance) provenance=true npm_args+=("$1") shift ;; --access | --tag | --otp) if [ "$#" -lt 2 ]; then echo "Missing value for $1" >&2 exit 1 fi npm_args+=("$1" "$2") shift 2 ;; *) npm_args+=("$1") shift ;; esac done if [ "${provenance}" = false ] && [ "${NPM_CONFIG_PROVENANCE:-}" = "true" ]; then npm_args+=("--provenance") fi # Prefer GitHub Actions OIDC Trusted Publishing over any stale NPM_TOKEN when an # OIDC token endpoint is available. Clearing NPM_CONFIG_USERCONFIG and auth env # vars prevents a blank or expired _authToken in .npmrc from shadowing OIDC. auth_token="${NODE_AUTH_TOKEN:-${NPM_TOKEN:-}}" if [ -n "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ]; then if NPM_CONFIG_USERCONFIG=/dev/null env -u NODE_AUTH_TOKEN -u NPM_TOKEN npm "${npm_args[@]}"; then exit 0 fi if [ -n "${auth_token}" ]; then echo "OIDC publish failed; falling back to explicit npm auth token" >&2 exec npm "${npm_args[@]}" fi exit 1 elif [ -z "${auth_token}" ]; then NPM_CONFIG_USERCONFIG=/dev/null env -u NODE_AUTH_TOKEN -u NPM_TOKEN npm "${npm_args[@]}" else npm "${npm_args[@]}" fi