Skip to content

Instantly share code, notes, and snippets.

@beenotung
Created May 11, 2021 23:28
Show Gist options
  • Save beenotung/dd18bb6f1d75af8c4a91bdfcf8afb051 to your computer and use it in GitHub Desktop.
Save beenotung/dd18bb6f1d75af8c4a91bdfcf8afb051 to your computer and use it in GitHub Desktop.
command not found handler for shell
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 file if exist
if [ -f "$1" ]; then
echo "running ./$1..." >&2
cmd="./$1"
shift
"$cmd" $@
return $?
fi
# run local scripts if exist
cmd="scripts/$1"
if [ -f "$cmd" ]; then
echo "running $cmd..." >&2
shift
"$cmd" $@
return $?
fi
unset cmd
# 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
# run with pnpx or npx if in whitelist
## check pnpx
if [ -f ~/.config/pnpx/whitelist ]; then
if [ $(egrep "^$1$" ~/.config/pnpx/whitelist | wc -l) != 0 ]; then
if hash pnpx 2>/dev/null; then
echo "running $1 with pnpx" >&2
pnpx $@
return $?
else
echo "optional: install pnpm for running $1 via pnpx" >&2
fi
fi
fi
## 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
}
command_not_found_handler() {
# Do not run within a pipe
if test ! -t 1; then
echo "command not found: $1" >&2
return 127
fi
# run local file if exist
if [ -f "$1" ]; then
echo "running ./$1..." >&2
cmd="./$1"
shift
"$cmd" $@
return $?
fi
# run local scripts if exist
cmd="scripts/$1"
if [ -f "$cmd" ]; then
echo "running $cmd..." >&2
shift
"$cmd" $@
return $?
fi
unset cmd
# 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
# run with pnpx or npx if in whitelist
## check pnpx
if [ -f ~/.config/pnpx/whitelist ]; then
if [ $(egrep "^$1$" ~/.config/pnpx/whitelist | wc -l) != 0 ]; then
if hash pnpx 2>/dev/null; then
echo "running $1 with pnpx" >&2
pnpx $@
return $?
else
echo "optional: install pnpm for running $1 via pnpx" >&2
fi
fi
fi
## 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