Created
July 24, 2016 14:44
-
-
Save nsommer/146bb1265e33f0ed4be4780eeb80d2bc to your computer and use it in GitHub Desktop.
Hack to enable `npm exec` command as bash alias, inspired by `bundle exec`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Shim the npm binary to enable custom command `npm exec`. | |
npm_shim() { | |
# Make real npm calls with full path, otherwise the alias is called recursively. | |
npm_binary=$(which npm) | |
if [ $# -eq 0 ] | |
then | |
$npm_binary | |
elif [ $# -eq 1 ] | |
then | |
if [ $1 = 'exec' ] | |
then | |
echo "Binary parameter missing." | |
else | |
$npm_binary $1 | |
fi | |
else | |
if [ $1 = 'exec' ] | |
then | |
# Execute locally installed binary with all arguments. | |
args=($@) | |
unset args[0] | |
unset args[1] | |
eval "./node_modules/.bin/$2 ${args[*]}" | |
else | |
# Execute npm with all arguments. | |
eval "$npm_binary $@" | |
fi | |
fi | |
} | |
alias npm='npm_shim' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment