Skip to content

Instantly share code, notes, and snippets.

@TheYarin
Created January 13, 2019 18:02
Show Gist options
  • Save TheYarin/baf2961f260143643a0f1e1ac07124b6 to your computer and use it in GitHub Desktop.
Save TheYarin/baf2961f260143643a0f1e1ac07124b6 to your computer and use it in GitHub Desktop.
Download an npm package, and compress it to a zip archive. No unnecessary files left behind.
# Usage: download-npm.sh <package-name>
#!/bin/bash
set -e
set -u
NC='\033[0m'
LIGHT_GREEN='\033[1;32m'
libName="$1"
function sanitize {
sed 's|[@/]|_|g' <<< "$1" # '/' is bad because of filesystem stuff and '@' is bad because of a reason I already forgot
}
# Sanitize folder name
dirName="$(sanitize $libName)_npm-libs"
mkdir "$dirName"
cd "$dirName"
npm init --yes
# Get lib version
installOutput=$(npm install $libName)
echo "$installOutput"
libVersion=$(echo "$installOutput" | grep "+ $libName@" | grep -oP "@\K[\d.]+$")
cd ..
echo "Compressing to Zip..."
archiveName="$(sanitize $libName)@${libVersion}.zip"
zip -q -r "$archiveName" "./$dirName"
echo "Cleaning up..."
find "./$dirName" -delete
echo -e "${LIGHT_GREEN}Saved package to $archiveName ${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment