[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

posted @ 2020-05-12 14:15  Zhentiw  阅读(143)  评论(0编辑  收藏  举报