Skip to content

Instantly share code, notes, and snippets.

@dhernandez
Last active November 16, 2023 09:51
Show Gist options
  • Save dhernandez/7e53e55aadb7f16bf41780620911f3fc to your computer and use it in GitHub Desktop.
Save dhernandez/7e53e55aadb7f16bf41780620911f3fc to your computer and use it in GitHub Desktop.
yarn <tab><tab> autocomplete with yarn commands from yarn --help. yarn run <tab><tab> autocomplete with scripts from package.json
#/bin/bash
_yarn(){
if [ "$2" = "" ]; then SEARCH_PATTERN="."; else SEARCH_PATTERN=$2
fi
if [ "$3" == "run" ]; then
COMPREPLY=( $(cat package.json | jq '.scripts | keys[]' | tr -d \" | grep -E ^$SEARCH_PATTERN | tr \\n ' ') )
else
COMPREPLY=( $(yarn --help | grep -e "-\s" | tr -d '\- ' | grep -E ^$SEARCH_PATTERN | tr -d ' ' | tr \\n ' ') )
fi
}
complete -F _yarn yarn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment