Skip to content

Instantly share code, notes, and snippets.

@fry69
Last active October 8, 2025 09:47
Show Gist options
  • Select an option

  • Save fry69/72b139ba03228f9fb0a642a40ff352f9 to your computer and use it in GitHub Desktop.

Select an option

Save fry69/72b139ba03228f9fb0a642a40ff352f9 to your computer and use it in GitHub Desktop.
Get the latest version of npm or jsr packages (augment Deno's lack of `@latest` tag)
#!/usr/bin/env bash
# pkg-latest: Get the latest version of npm or jsr packages
# Inspired by: https://github.com/sigmaSd/pkg-latest (written in Rust)
# Requires: curl, jq
# Usage: pkg_latest <npm:package|jsr:package>
# Example: pkg_latest npm:preact
# Example: pkg_latest jsr:@fresh/init
# Usage as a function: deno install $(pkg_latest npm:preact)
pkg_latest() {
local input="$1"
# Check if argument is provided
if [[ -z "$input" ]]; then
echo "Usage: pkg_latest <npm:package|jsr:package>" >&2
return 1
fi
# Handle npm: prefix
if [[ "$input" =~ ^npm:(.+)$ ]]; then
local package_name="${BASH_REMATCH[1]}"
local url="https://registry.npmjs.org/${package_name}"
local response
if ! response=$(curl -fsSL -H "Accept: application/json" "$url" 2>&1); then
echo "Error: Failed to get npm version for ${package_name}: ${response}" >&2
return 1
fi
local version
if ! version=$(echo "$response" | jq -r '.["dist-tags"].latest' 2>&1); then
echo "Error: Failed to parse npm response for ${package_name}" >&2
return 1
fi
if [[ "$version" == "null" || -z "$version" ]]; then
echo "Error: No latest version found for ${package_name}" >&2
return 1
fi
echo "npm:${package_name}@${version}"
# Handle jsr: prefix
elif [[ "$input" =~ ^jsr:(.+)$ ]]; then
local package_name="${BASH_REMATCH[1]}"
local url="https://jsr.io/${package_name}/meta.json"
local response
if ! response=$(curl -fsSL -H "Accept: application/json" "$url" 2>&1); then
echo "Error: Failed to get jsr version for ${package_name}: ${response}" >&2
return 1
fi
local version
if ! version=$(echo "$response" | jq -r '.latest' 2>&1); then
echo "Error: Failed to parse jsr response for ${package_name}" >&2
return 1
fi
if [[ "$version" == "null" || -z "$version" ]]; then
echo "Error: No latest version found for ${package_name}" >&2
return 1
fi
echo "jsr:${package_name}@${version}"
else
echo "Error: Input must start with 'npm:' or 'jsr:'" >&2
return 1
fi
}
# If script is executed directly (not sourced), run the function
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
pkg_latest "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment