Skip to content

Instantly share code, notes, and snippets.

@mael
Last active May 16, 2019 12:04
Show Gist options
  • Save mael/045a762de3963b6359ba913196b31fd6 to your computer and use it in GitHub Desktop.
Save mael/045a762de3963b6359ba913196b31fd6 to your computer and use it in GitHub Desktop.
My shortcuts for show npm version package
Open Terminal and type
$vi ~/.bash_profile
Add the following lines at the end:
---
function npmv {
case $# in # number of arguments passed
0) v="$(npm -v)" ; #store output from npm -v in variable
echo "NPM version is: $v"; #can't use single quotes
#${v} would also work
;;
1) s="$(npm list --depth=0 $1 | grep $1 | cut -d @ -f 2)";
echo "$s";
;;
2) case "$2" in # second argument
g) #global|#Syntax to compare bash string to literal
s="$(npm list --depth=0 -g $1 | grep $1 | cut -d @ -f 2)";
echo "$s";
;;
l) #latest
npm view $1 version; #npm info $1 version does same thing
;;
*) echo 'Invalid arguments';
;;
esac;
;;
*) echo 'Invalid arguments';
;;
esac;
}
export -f npmv
---
$source ~/.bash_profile //reload .bash_profile
**USAGE**
* npmv for the version of npm
* npmv <package-name> for the local version
* npmv <package-name> g for global version
* npmv <package-name> l for latest version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment