Skip to content

Instantly share code, notes, and snippets.

@runlevel5
Last active November 22, 2022 22:50
Show Gist options
  • Save runlevel5/947e55e36ddef45a01c5edbc36c2c96f to your computer and use it in GitHub Desktop.
Save runlevel5/947e55e36ddef45a01c5edbc36c2c96f to your computer and use it in GitHub Desktop.
Just a quick script to create a JSON of which key is the NPM package name and value is the array of all versions
#!/bin/bash
set -euo pipefail
PACKAGE="null"
packages_filepath="packages"
detailed_packages_filepath="detailed_packages"
username="shortlyster"
CLEANUP="false"
main() {
if [[ "$CLEANUP" == "true" ]]; then
cleanup
fi
if [[ ! -f $packages_filepath ]]; then
echo "Fetching the list of all packages.."
list_packages > $packages_filepath
fi
if [[ ! -f $detailed_packages_filepath ]]; then
for package in $(cat $packages_filepath); do
echo "Finding all version for $package.."
PACKAGE="$package" list_versions_for_package >> $detailed_packages_filepath
done
fi
# for versioned_package in $(cat $detailed_packages_filepath); do
# echo "Fetch tarball of $versioned_package.."
# npm pack $versioned_package
# done
}
cleanup() {
truncate -s 0 $packages_filepath
truncate -s 0 $detailed_packages_filepath
}
list_packages() {
curl -s -H "Authorization: Bearer $NPM_TOKEN" "https://registry.npmjs.org/-/user/$username/package" | jq -r "keys | .[]"
}
list_versions_for_package() {
if [[ "$PACKAGE" == "null" ]]; then
echo "Missing \$PACKAGE when invoking list_versions_for_package()"
exit 0
fi
for version in $(curl -s -H "Authorization: Bearer $NPM_TOKEN" "https://registry.npmjs.org/$PACKAGE" | jq -r ".versions | keys | .[]"); do
echo "$PACKAGE@$version" | sed "s/,//"
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment