Skip to content

Instantly share code, notes, and snippets.

@omrilotan
Created April 5, 2020 14:24
Show Gist options
  • Save omrilotan/09337099c60f6574b15aac320fa543af to your computer and use it in GitHub Desktop.
Save omrilotan/09337099c60f6574b15aac320fa543af to your computer and use it in GitHub Desktop.
function load_nvm {
source "$NVM_DIR/nvm.sh"
if [[ ! -f "$HOME/dotfiles/.npm_completion.sh" ]]; then
npm completion >> ~/dotfiles/.npm_completion.sh
fi
source ~/dotfiles/.npm_completion.sh
}
if [ -f ".nvmrc" ]; then
# Load NVM if needed
load_nvm
else
# Set up idem potent NVM loaders
nodecommands=(nvm npm npx node)
function nvm_load {
# Unset all node command decorators
for nodecommand in "${nodecommands[@]}"; do
eval "unset -f ${nodecommand}"
done
unset -f nvm_load
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
START=$(date +%s)
load_nvm
END=$(date +%s)
DIFF=$(echo "($END - $START)" | bc)
echo -e "\033[0;94mLoading NVM took ${DIFF} seconds\033[0m"
else
echo "Can't find nvm script"
fi
}
# All node commands should load NVM first
for nodecommand in "${nodecommands[@]}"; do
eval "function ${nodecommand} { nvm_load; ${nodecommand} \$@; }"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment