Skip to content

Instantly share code, notes, and snippets.

@mtavkhelidze
Created June 4, 2018 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtavkhelidze/35a4b7c6a359108351227e30a17c95f0 to your computer and use it in GitHub Desktop.
Save mtavkhelidze/35a4b7c6a359108351227e30a17c95f0 to your computer and use it in GitHub Desktop.
Update local or global npm packages
#!/usr/bin/env bash
function usage() {
echo "Usage: $(basename $0) -l/-u [-g -h]" 1>&2
echo "Options are:" 1>&2
echo " -l list packages" 1>&2
echo " -u update packages" 1>&2
echo " -g go global" 1>&2
echo " -h show this help" 1>&2
exit 1
}
list=""
global=""
update=""
while getopts "lug" o; do
case "${o}" in
l)
[[ -n $update ]] && usage || list="1"
;;
u)
[[ -n $list ]] && usage || update="1"
;;
g)
global="--global"
;;
*)
usage
;;
esac
done
[[ -z $list && -z $update && -z $global ]] && usage
dirs=($(npm list $global --depth 0 --parseable 2>/dev/null))
base=${dirs[0]}
rest=(${dirs[@]:1})
declare -a packages
for i in ${rest[@]}; do
packages+=($(echo $i | sed -e "s:${base}::" -e "s:/node_modules/::"))
done
if [ -n "$list" ]; then
echo ${packages[@]} | tr " " "\n"
elif [ -n "$update" ]; then
npm $global install ${packages[@]}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment