[Tools] Make a zsh function alias to enforce using yarn commands when in a yarn project

When you or your team work on various npm or yarn projects it gets hard to remember which one is which. This lesson shows you how to create a custom npm zsh function alias to remind yourself to use yarn commands when there is a yarn.lock file in the directory.

~/.zshrc

npm() {
  # if yarn.lock file exists then
  if [ -e yarn.lock ]; then
    # print out a warning message
    echo "⚠️  This is a yarn project. Why don't you use it ⁉️ "
  else
    # `command` lets you run a shell command ignoring any shell functions
    # $@ represents all the arguments. It's is equivalent to $1 $2, etc...
    command npm $@
  fi
}

Run:

source ~/.zshrc

原文地址:https://www.cnblogs.com/Answer1215/p/12875903.html