Skip to content

Instantly share code, notes, and snippets.

@alekzonder
Created February 9, 2016 04:12
Show Gist options
  • Save alekzonder/3ad645e57181e515e387 to your computer and use it in GitHub Desktop.
Save alekzonder/3ad645e57181e515e387 to your computer and use it in GitHub Desktop.
download npm package script
#!/bin/bash
if [ -z $1 ]; then
echo ""
echo "Usage:"
echo " dnpm <package>"
echo ""
echo "examples:"
echo " dnpm express@4.0.0"
echo ""
exit 1
fi
echo "get package tarball ..."
url=$(npm view $1 dist.tarball)
if [ ! $? -eq 0 ]; then
echo "[ERROR] unknown package $1"
exit 1
fi
echo "downloading ${url} ..."
tarName="$1.tgz"
wget ${url} -O ${tarName}
if [ ! $? -eq 0 ]; then
echo "[ERROR] can't download package $1"
exit 1
fi
echo "unpacking ..."
dirName="${1/@/-}"
mkdir $dirName
if [ ! $? -eq 0 ]; then
echo "[ERROR] cant mkdir $dirName"
exit 1
fi
tar -C ${dirName} -xvzf ${tarName} --strip-components=1 package/
if [ ! $? -eq 0 ]; then
echo "[ERROR] cant untar archive ${tarName}"
exit 1
fi
rm ${tarName}
if [ ! $? -eq 0 ]; then
echo "[ERROR] cant rm tarball ${tarName}"
exit 1
fi
echo ""
echo "[DONE] $dirName"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment