Created
August 4, 2022 13:56
-
-
Save beenotung/604c1efdf1740bf449e0622b88c90834 to your computer and use it in GitHub Desktop.
demo bash error fallback handler for node.js developer
This file contains hidden or 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
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