const pack = require('./package.json');
const { execSync } = require('child_process');
const list = [...Object.keys(pack.devDependencies), ...Object.keys(pack.dependencies)];
// 忽略的项目 (不用升级)
const ignore = ['eslint', 'babel-cli'];
list.forEach((item) => {
if (!ignore.includes(item)) {
console.log(`yarn upgrade ${item}`);
execSync(`yarn upgrade ${item}`);
}
});
比如, 有一些包指定了 Node.js 版本的要求, 如:
"engines": {
"node": "^4.2.2"
}
yarn add PackageName --ignore-engines
以 tim-event-processor-host
包为例, 其中用到了一个子模块 cerulean
来处理 azure-storage
我们想要用最新版本的azure-storage
,但如果要一级级向上去提交Pull Request
并且去等 npm包作者更新, 实在是太慢太傻了.
那么我们就可以利用 postinstall
特性来删除旧版本的 npm包并使用新版本的.
tim-event-processor-host
mkdir DIR && DIR
yarn add tim-event-processor-host --ignore-engines
cd node_modules
tree -L 1
结果显示如下:
.
├── ... # 把一些无关的目录都给忽略了
├── azure-storage
├── cerulean
└── tim-event-processor-host
113 directories, 0 files
cerulean
, azure-storage
就都装在了 node_modules
目录下,
tim-event-processor-host
和azure-storage
mkdir DIR && DIR
yarn add tim-event-processor-host azure-storage --ignore-engines
cd node_modules
tree -L 1
.
├── azure-storage # 最新版本
├── cerulean
│ ├── LICENSE
│ ├── README.md
│ ├── examples
│ │ ├── README.md
│ │ ├── leader_followers.js
│ │ └── servicebus_send_receive.js
│ ├── lib
│ │ ├── blob_lease.js
│ │ ├── blob_lease_manager.js
│ │ └── index.js
│ ├── node_modules
│ │ ├── assert-plus
│ │ ├── azure-storage # 依赖项中的指定版本(旧)
│ │ ├── boom
│ │ ├── caseless
│ │ ├── cryptiles
│ │ ├── delayed-stream
│ │ ├── forever-agent
│ │ ├── form-data
│ │ ├── har-validator
│ │ ├── hawk
│ │ ├── hoek
│ │ ├── http-signature
│ │ ├── qs
│ │ ├── request
│ │ ├── sntp
│ │ └── validator
│ ├── package.json
│ ├── playground.js
│ └── test
│ └── integration
└── tim-azure-event-hubs
406 directories, 1571 files
所以只需要将 node_modules/cerulean/node_modules/azure-storage
目录给删除掉, 让引用的时候自动向上递归查找即可.
"dependencies": {
"azure-storage": "^2.2.1",
"tim-event-processor-host": "^0.0.3"
},
"scripts": {
"postinstall": "rm -rf node_modules/cerulean/node_modules/azure-storage"
}
yarn --ignore-engines
完成.