Skip to content

Instantly share code, notes, and snippets.

@nsommer
Created July 24, 2016 14:44
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 nsommer/146bb1265e33f0ed4be4780eeb80d2bc to your computer and use it in GitHub Desktop.
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`
# 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