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

栏目: IT技术 · 发布时间: 4年前

内容简介:The above video is hosted onIf you’re anything like me, you have multiple projects (between work and side projects) where some are managed withIn order to create the

The above video is hosted on egghead.io .

If you’re anything like me, you have multiple projects (between work and side projects) where some are managed with npm and others with yarn . As the number of projects increase, the harder it gets to remember which one uses which. In this post, we will create a custom npm zsh function alias to warn yourself to use yarn commands if the a yarn.lock file exists in the same directory.

Creating the Function Alias

In order to create the npm function alias, let’s first open up your .zshrc configuration file, which can be found in your user directory. You should be able to use one of the following commands to Open up the file in your preferred editor ( code ~/.zshrc , vim ~/.zshrc , etc…).

The following is the script we’d like to execute each time a user types in the npm command. We are basicaly hijacking npm , so that it’ll run our function alias instead of the globally installed npm package manager. The script first detects if the directory has a yarn.lock file present and if it does, then a warning message is displayed. Otherwise, the script executes the underlying npm function passing the origional parameters.

# ... more configuration ...

npm() {
  if [ -e yarn.lock ]; then
    echo ":warning: This is a yarn project. Why don't you use it :interrobang:"
  else
    command npm $@
  fi
}

# ... more configuration ...

Note: This alias should be able to work both in bash or in zsh

You may be familiar with the bash function arguments $1 , $2 , etc… that represent the first and second parameters. We could have used those, but since we don’t know how many arguments will be passed to our script, we are using $@ which represents all the arguments.

Another important things to notice is the command command. This is very important because if we had not provided it this function alias would have recursively called itself over and over again.

Sourcing our Configuration

Once you’ve saved your configuration file, you’ll either want close and reopen your terminal, create a new tab, or reload your configuration in your current terminal/tab in order for new function alias to be recognized.

You should be able to reload your configuration by running this command in your terminal source ~/.zshrc .

Using our Function Alias

Project using npm

When we are in a project maintained by the npm package manager (there is no yarn.lock file), our function alias should just pass our commands along to the underlying npm executable. You shouldn’t really notice any difference in behavior.

~/npm-project ❯ ls yarn*
zsh: no matches found: yarn*

~/npm-project ❯ npm run
Lifecycle scripts included in devpun:
  test
    echo "Error: no test specified" && exit 0

Project using yarn

However, if you happen to be in a yarn project, then our new function alias should stop you in your tracks if you use an npm command to manage the project. At that point, just switch gears and use a support yarn command instead. It might be handy to find a good cheat sheet to help you know which command is appropriate.

~/yarn-project ❯ ls yarn*
yarn.lock

~/yarn-project ❯ npm run
⚠ ️This is a yarn project. Why don't you use it :interrobang:

~/yarn-project ❯ yarn clean
yarn run v1.19.1
$ rm -rf packages/*/{dist,.cache}
:sparkles:  Done in 0.08s.

Conclusion

I hope you find this function alias helpful or some of the techniques used to create it. You could possibly extend this by also making a yarn function alias or by expanding the idea to recommend or auto-translate one command to an appropriate counterpart.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

走出电商困局

走出电商困局

黄若 / 东方出版社 / 2013-11-1 / 38.00

高速增长的时代趋于结束,迅猛运转的加速器早已锈蚀 这是一场转型,更是一次新的机遇 《走出电商困局》是本年度最畅销管理书籍《我看电商》作者黄若的最新力作,意在深度剖析电商行业发展 ,破解电商困局。经历过10年超常规的快速增长,电商行业即将进入较为平稳的发展期。多年来这个行业不断融资不断烧钱却大多无法盈利的怪圈怎样突破?在很多企业面临估值下跌,资金吃紧,用户流失的关键节点,怎样从零售经营的......一起来看看 《走出电商困局》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码