公共npm包的自动检测更新
2021-03-31 本文已影响0人
cd2001cjm
当前我们有个问题,npm包更新后,可能业务部门感知不到,升级有延迟。
为了解决该问题,可参考如下步骤:
- npm install npm-check-updates
- 在pacage.json中,做如下变更:覆盖即可
"scripts": {
"checkv": "node .\checkUpdate.js",
"dev": "set NODE_ENV=development && npm run checkv && YYYY",
},
xxxx是用正则匹配出想检测更新的业务npm名称规则
YYYY是其他命令
脚本如下:
const ncu = require('npm-check-updates');
const chalk = require('chalk');
const execa = require('execa');
const ora = require('ora');
function install(packages) {
const installer = 'npm'
const color = chalk.supportsColor ? '--color=always' : null;
const npmArgs = ['install']
.concat(packages)
.concat(color)
.filter(Boolean);
console.log('');
console.log(`$ ${chalk.green(installer)} ${chalk.green(npmArgs.join(' '))}`);
const spinner = ora(`Installing using ${chalk.green(installer)}...`);
spinner.enabled = true
spinner.start();
return execa(installer, npmArgs, {}).then(output => {
spinner.stop();
console.log(output.stdout);
console.log(output.stderr);
// return currentState;
}).catch(err => {
spinner.stop();
throw err;
});
}
ncu.run({
upgrade:true,
filter:"/^@teld/.*$/"
}).then(function(upgraded){
let udpateList =[]
for(var npmName in upgraded){
var npmVersion = upgraded[npmName].substring(1)
var installAgrs = npmName+'@'+npmVersion
udpateList.push(installAgrs)
}
if(udpateList.length > 0)install(udpateList)
})