.github/scripts/use-pinned-npm.sh18 of 18 statements covered (100.00%).
coveredmissednever traced by Bash (not counted)A line ending in … continues the statement above it and shares its fate.
| 1 | #!/usr/bin/env bash | |
| 2 | # Install and verify the exact npm release declared by packageManager. | |
| 3 | 10 | set -euo pipefail |
| 4 | ||
| 5 | 10 | MANIFEST="${1:-package.json}" |
| 6 | 10 | if [ ! -f "$MANIFEST" ]; then |
| 7 | 1 | echo "npm manifest not found: $MANIFEST" >&2 |
| 8 | 1 | exit 1 |
| 9 | fi | |
| 10 | ||
| 11 | 18 | PACKAGE_MANAGER=$(node -e ' |
| 12 | const fs = require("node:fs"); | |
| 13 | const manifest = JSON.parse(fs.readFileSync(process.argv[1], "utf8")); | |
| 14 | process.stdout.write(String(manifest.packageManager || "")); | |
| 15 | ' "$MANIFEST") | |
| 16 | ||
| 17 | 9 | if ! [[ "$PACKAGE_MANAGER" =~ ^npm@([0-9]+\.){2}[0-9]+$ ]]; then |
| 18 | 4 | echo "packageManager must declare an exact npm version: $MANIFEST" >&2 |
| 19 | 4 | exit 1 |
| 20 | fi | |
| 21 | 5 | REQUIRED_VERSION="${PACKAGE_MANAGER#npm@}" |
| 22 | 11 | CURRENT_VERSION=$(npm --version 2>/dev/null || true) |
| 23 | ||
| 24 | 5 | if [ "$CURRENT_VERSION" != "$REQUIRED_VERSION" ]; then |
| 25 | 3 | npm install --global "npm@${REQUIRED_VERSION}" \ |
| 26 | --ignore-scripts --no-audit --no-fund | |
| 27 | fi | |
| 28 | ||
| 29 | 10 | CURRENT_VERSION=$(npm --version) |
| 30 | 5 | if [ "$CURRENT_VERSION" != "$REQUIRED_VERSION" ]; then |
| 31 | 1 | echo "npm version mismatch: expected $REQUIRED_VERSION, found $CURRENT_VERSION" >&2 |
| 32 | 1 | exit 1 |
| 33 | fi | |
| 34 | 4 | printf 'Using npm %s declared by %s\n' "$CURRENT_VERSION" "$MANIFEST" |