Skip to content

Instantly share code, notes, and snippets.

@beenotung
Created August 4, 2022 13:56
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 beenotung/604c1efdf1740bf449e0622b88c90834 to your computer and use it in GitHub Desktop.
Save beenotung/604c1efdf1740bf449e0622b88c90834 to your computer and use it in GitHub Desktop.
demo bash error fallback handler for node.js developer
command_not_found_handle() {
# Do not run within a pipe
if test ! -t 1; then
echo "command not found: $1" >&2
return 127
fi
# run local npm package if exist
cmd="node_modules/.bin/$1"
if [ -f "$cmd" ]; then
echo "running $cmd..." >&2
shift
"$cmd" $@
return $?
fi
unset cmd
## check npx
if [ -f ~/.config/npx/whitelist ]; then
if [ $(egrep "^$1$" ~/.config/npx/whitelist | wc -l) != 0 ]; then
if hash npx 2>/dev/null; then
echo "running $1 with npx" >&2
npx $@
return $?
else
echo "optional: install npm for running $1 via npx" >&2
fi
fi
fi
# use thefuck to fix it
hash thefuck 2>/dev/null
if [ $? == 0 ]; then
thefuck $@ | source /dev/stdin
return $?
else
echo "optional: install thefuck for typo suggestions" >&2
fi
# failover
echo "command not found: $1" >&2
return 127
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment