|
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 |
|
} |