Last active
August 29, 2015 14:17
-
-
Save lirsacc/d189b11194f397ab794a to your computer and use it in GitHub Desktop.
Run local npm packages
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
# Helper function to run local npm packages with tab completion | |
# usage: npl pkg options | |
# Will return 1 if ./node_modules/ or ./node_modules/.bin/pkg doesn't exist | |
# Otherwise runs ./node_modules/.bin/pkg options | |
# Run local npm packages | |
# Better completion thanks to http://stackoverflow.com/a/14524311 | |
function _npl () { | |
local cur ff | |
if [ $COMP_CWORD -eq 1 ]; then | |
# First completion word -> list node modules with binary | |
cur="${COMP_WORDS[1]}" | |
COMPREPLY=( $(compgen -W "$([ -d ./node_modules/.bin ] && ls ./node_modules/.bin | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g')" -- $cur) ) | |
else | |
# Back to listing filenames | |
COMPREPLY=($(compgen -f -- "${COMP_WORDS[${COMP_CWORD}]}" )) | |
for ((ff=0; ff<${#COMPREPLY[@]}; ff++)); do | |
test -d "${COMPREPLY[$i]}" && COMPREPLY[$i]="${COMPREPLY[$i]}/" | |
test -f "${COMPREPLY[$i]}" && COMPREPLY[$i]="${COMPREPLY[$i]} " | |
done | |
fi | |
} | |
function npl() { | |
if [ ! -d ./node_modules ]; then | |
echo "Not a npm package directory" && return 1 | |
elif [ ! -f ./node_modules/.bin/$1 ]; then | |
echo "Module $1 not found in $(pwd)/node_modules/.bin" && return 1 | |
fi | |
./node_modules/.bin/$1 ${@:2} | |
} | |
complete -o bashdefault -o nospace -F _npl npl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment