Skip to content

Instantly share code, notes, and snippets.

@ysugimoto
Created August 21, 2017 07:57
Show Gist options
  • Save ysugimoto/0f5391314954cba2b45e2d68cbb982be to your computer and use it in GitHub Desktop.
Save ysugimoto/0f5391314954cba2b45e2d68cbb982be to your computer and use it in GitHub Desktop.
Bash-completion for `npm run` command
#!/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