Skip to content

Instantly share code, notes, and snippets.

@johnloy
Last active September 26, 2025 19:35
Show Gist options
  • Save johnloy/7cf03b1f87825296b6ba68130086b4d7 to your computer and use it in GitHub Desktop.
Save johnloy/7cf03b1f87825296b6ba68130086b4d7 to your computer and use it in GitHub Desktop.
npm-release-notes bash function

Installation

Copy the two bash functions in this gist into .bashrc (or wherever appropriate for your startup files).

Usage

bash-5.1$ npm-release-notes -h
Usage: npm-release-notes [OPTIONS] PACKAGE [VERSION]

Get release information for an npm package.

Arguments:
  PACKAGE    Name of the npm package (e.g., @tanstack/react-virtual)
  VERSION    Optional version number (e.g., 3.0.0)

Options:
  -o, --open    Open the releases URL in your browser
  -h, --help    Show this help message

Examples:
  npm-release-notes react
  npm-release-notes @tanstack/react-virtual 3.0.0
  npm-release-notes --open react-router-dom
  npm-release-notes -o @types/node 18.0.0

With a Snyk package update PR checked out, open the release notes for the updated package. image

The printed output of pr-package-changes substituted in the above command. image

function npm-release-notes() {
local package=""
local version=""
local open_url=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h)
echo "Usage: npm-release-notes [OPTIONS] PACKAGE [VERSION]"
echo ""
echo "Get release information for an npm package."
echo ""
echo "Arguments:"
echo " PACKAGE Name of the npm package (e.g., @tanstack/react-virtual)"
echo " VERSION Optional version number (e.g., 3.0.0)"
echo ""
echo "Options:"
echo " -o, --open Open the releases URL in your browser"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " npm-release-notes react"
echo " npm-release-notes @tanstack/react-virtual 3.0.0"
echo " npm-release-notes --open react-router-dom"
echo " npm-release-notes -o @types/node 18.0.0"
return 0
;;
--open|-o)
open_url=true
shift
;;
*)
if [[ -z "$package" ]]; then
package="$1"
elif [[ -z "$version" ]]; then
version="$1"
fi
shift
;;
esac
done
# Check if package was provided
if [[ -z "$package" ]]; then
echo "Error: Package name is required"
echo "Use --help for usage information"
return 1
fi
# Get repo URL from npm
local repo_url=$(npm view "$package" repository.url | sed 's/git+//;s/.git$//')
if [[ -z "$repo_url" ]]; then
echo "No repository URL found for package: $package"
return 1
fi
echo "Repository: $repo_url"
echo "Releases: $repo_url/releases"
# If version specified, try to construct direct URL
if [[ -n "$version" ]]; then
echo "Specific release: $repo_url/releases/tag/v$version"
fi
# Check for CHANGELOG.md in GitHub repo
echo ""
echo "Checking for CHANGELOG.md..."
# Extract GitHub API URL from repo URL
local api_url=$(echo "$repo_url" | sed 's|https://github.com/|https://api.github.com/repos/|')/contents/CHANGELOG.md
local github_changelog_exists=false
local local_changelog_exists=false
if curl -s -f "$api_url" > /dev/null 2>&1; then
echo "✓ CHANGELOG.md found in GitHub repository: $repo_url/blob/main/CHANGELOG.md"
github_changelog_exists=true
else
echo "✗ No CHANGELOG.md found in GitHub repository"
fi
# Check for local CHANGELOG.md
local changelog_path="node_modules/$package/CHANGELOG.md"
if [[ -f "$changelog_path" ]]; then
echo "✓ Local CHANGELOG.md found: $changelog_path"
local_changelog_exists=true
else
echo "✗ No local CHANGELOG.md found"
# Check for other changelog files
local other_changelogs=$(ls "node_modules/$package/" 2>/dev/null | grep -i change)
if [[ -n "$other_changelogs" ]]; then
echo " Other changelog files found: $other_changelogs"
fi
fi
# Open URL if requested
if [[ "$open_url" == true ]]; then
local url_to_open=""
# If version specified, always open specific release
if [[ -n "$version" ]]; then
url_to_open="$repo_url/releases/tag/v$version"
# If no version and changelog exists, prioritize changelog
elif [[ "$local_changelog_exists" == true ]]; then
# Open local changelog file
url_to_open="file://$PWD/$changelog_path"
elif [[ "$github_changelog_exists" == true ]]; then
# Open GitHub changelog
url_to_open="$repo_url/blob/main/CHANGELOG.md"
else
# Fallback to releases page
url_to_open="$repo_url/releases"
fi
# Try different open commands based on system
if command -v xdg-open &> /dev/null; then
xdg-open "$url_to_open"
elif command -v open &> /dev/null; then
open "$url_to_open"
else
echo "No suitable open command found. Please open manually: $url_to_open"
fi
fi
}
# Extract package name and version from PR diff package.json changes
# Example output: @tanstack/react-table 8.21.3
function pr-package-changes() {
git diff HEAD~1 package.json | grep -E '^\+.*".*":.*".*"' | sed -E 's/^\+[[:space:]]*"([^"]+)":[[:space:]]*"([^"]+)".*/\1 \2/' | sed -E 's/^([^ ]+) [^0-9]*([0-9].*)$/\1 \2/'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment