源码共读:16期 一行代码统一规范团队包管理器的神器

122 阅读1分钟

前端小白,哎其实连小白的水平都算不上,因为 magento 前端写的最多的还是php,css。今年准备突破一下我的JS水平,所以参加了若川的参加这个源码共读活动。以下是我的笔记,为了方便自己检索,我会大量用英文,尽量补齐中文翻译,原谅我的翻译水平有限,有些也不是很懂。

本来应该读 第14期的,但是callback一直是我的痛点,而且最近工作上不是很顺利,所以没能坚持下来 ,下周接着攻破第14期

Tips:

  1. npm Hook, (locates in package.json):
{
  "scripts": {
    "precompress": "{{ executes BEFORE the `compress` script }}",
    "compress": "{{ run command to compress files }}",
    "postcompress": "{{ executes AFTER `compress` script }}"
		"preinstall" "scripts/install.js", 
		"install" : "scripts/install.js", 
    "postinstall" : "scripts/install.js", 
    "uninstall" : "scripts/uninstall.js"
  }
}
  1. Only allow pnpm, not npm or yarn. This is fucking awesome !

pnpm.io/only-allow-…

Only allow pnpm
When you use pnpm on a project, you don't want others to accidentally run npm install or yarn. To prevent devs from using other package managers, you can add the following preinstall script to your package.json:

{
    "scripts": {
        "preinstall": "npx only-allow pnpm"
    }
}
Now, whenever someone runs npm install or yarn, they'll get an error instead and installation will not proceed.

Github: github.com/pnpm/only-a…

感悟和需要接着学习的点:

  1. only-allow 源码相对简单一点,但是需要补齐 process 对象的学习。( 阮一峰老师 process 对象
  2. which-pm-runs 源码