|
| 1 | +#!/bin/bash |
| 2 | +# Script to fix npm dist-tag for packages |
| 3 | +# Usage: ./scripts/fix-npm-dist-tag.sh <package-name> <target-version> |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +PACKAGE_NAME="$1" |
| 8 | +TARGET_VERSION="$2" |
| 9 | + |
| 10 | +if [ -z "$PACKAGE_NAME" ] || [ -z "$TARGET_VERSION" ]; then |
| 11 | + echo "Usage: $0 <package-name> <target-version>" |
| 12 | + echo "Example: $0 @wataruoguchi/emmett-event-store-kysely 2.1.0" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +echo "Checking current dist-tag for $PACKAGE_NAME..." |
| 17 | +CURRENT_LATEST=$(npm dist-tag ls "$PACKAGE_NAME" 2>/dev/null | grep "latest:" | awk '{print $2}' || echo "") |
| 18 | + |
| 19 | +if [ -z "$CURRENT_LATEST" ]; then |
| 20 | + echo "Error: Could not fetch current dist-tag for $PACKAGE_NAME" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +echo "Current 'latest' dist-tag: $CURRENT_LATEST" |
| 25 | +echo "Target version: $TARGET_VERSION" |
| 26 | + |
| 27 | +# Check if target version exists on npm |
| 28 | +if ! npm view "$PACKAGE_NAME@$TARGET_VERSION" version > /dev/null 2>&1; then |
| 29 | + echo "Error: Version $TARGET_VERSION does not exist on npm for $PACKAGE_NAME" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Compare versions |
| 34 | +if [ "$CURRENT_LATEST" = "$TARGET_VERSION" ]; then |
| 35 | + echo "Dist-tag is already set to $TARGET_VERSION. No action needed." |
| 36 | + exit 0 |
| 37 | +fi |
| 38 | + |
| 39 | +# Check if target version is higher |
| 40 | +CURRENT_NUM=$(echo "$CURRENT_LATEST" | sed 's/[^0-9]//g') |
| 41 | +TARGET_NUM=$(echo "$TARGET_VERSION" | sed 's/[^0-9]//g') |
| 42 | + |
| 43 | +if [ "$TARGET_NUM" -lt "$CURRENT_NUM" ] 2>/dev/null; then |
| 44 | + echo "Warning: Target version $TARGET_VERSION appears to be older than current $CURRENT_LATEST" |
| 45 | + read -p "Do you want to continue? (y/N) " -n 1 -r |
| 46 | + echo |
| 47 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +fi |
| 51 | + |
| 52 | +echo "Updating dist-tag 'latest' to $TARGET_VERSION..." |
| 53 | +npm dist-tag add "$PACKAGE_NAME@$TARGET_VERSION" latest |
| 54 | + |
| 55 | +echo "✓ Successfully updated dist-tag 'latest' to $TARGET_VERSION for $PACKAGE_NAME" |
0 commit comments