Created
August 21, 2017 07:57
-
-
Save ysugimoto/0f5391314954cba2b45e2d68cbb982be to your computer and use it in GitHub Desktop.
Bash-completion for `npm run` command
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
#!/bin/bash | |
# This is bash-completion for 'npm run' command. | |
# Find up package.json and completion 'npm scripts'. | |
_npm_run_completion() { | |
CURRENT="${COMP_WORDS[COMP_CWORD]}" | |
SUBCOMMAND="${COMP_WORDS[COMP_CWORD-1]}" | |
if [ "${SUBCOMMAND}" != "run" ]; then | |
return | |
fi | |
DIR=$(pwd) | |
while [ ! -f "${DIR}/package.json" ]; do | |
if [ "${DIR}" = "/" ]; then | |
return | |
fi | |
DIR=$(cd $(dirname $(readlink $DIR || echo $DIR)) || exit;pwd) | |
done | |
SCRIPTS=$(cat "${DIR}/package.json" | jq '.scripts | keys[]' | sed -e 's/"//g') | |
COMPREPLY=( $(compgen -W "${SCRIPTS}" ${CURRENT}) ) | |
} | |
complete -F _npm_run_completion npm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment