← all scripts

.github/scripts/use-pinned-npm.sh

18 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.
310set -euo pipefail
4
510MANIFEST="${1:-package.json}"
610if [ ! -f "$MANIFEST" ]; then
71 echo "npm manifest not found: $MANIFEST" >&2
81 exit 1
9fi
10
1118PACKAGE_MANAGER=$(node -e '
12const fs = require("node:fs");
13const manifest = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
14process.stdout.write(String(manifest.packageManager || ""));
15' "$MANIFEST")
16
179if ! [[ "$PACKAGE_MANAGER" =~ ^npm@([0-9]+\.){2}[0-9]+$ ]]; then
184 echo "packageManager must declare an exact npm version: $MANIFEST" >&2
194 exit 1
20fi
215REQUIRED_VERSION="${PACKAGE_MANAGER#npm@}"
2211CURRENT_VERSION=$(npm --version 2>/dev/null || true)
23
245if [ "$CURRENT_VERSION" != "$REQUIRED_VERSION" ]; then
253 npm install --global "npm@${REQUIRED_VERSION}" \
26 --ignore-scripts --no-audit --no-fund
27fi
28
2910CURRENT_VERSION=$(npm --version)
305if [ "$CURRENT_VERSION" != "$REQUIRED_VERSION" ]; then
311 echo "npm version mismatch: expected $REQUIRED_VERSION, found $CURRENT_VERSION" >&2
321 exit 1
33fi
344printf 'Using npm %s declared by %s\n' "$CURRENT_VERSION" "$MANIFEST"