const updater = require('pkg-updater');
const pkg = require('./package.json'); // 命令行工具自己的 package 信息
updater({'pkg': pkg}) .then(() => { /* 在这里启动命令行工具 */ });
updater({
'pkg': pkg,
// 自定义 registry
'registry': 'http://xxx.registry.com',
// 自定义请求的 dist-tag,默认是 latest
'tag': 'next',
// 自定义检查间隔,默认是 1h
'checkInterval': 24 * 60 * 60 * 1000,
// 自定义更新提示信息
'updateMessage': 'package update from <%=current%> to <%=latest%>.',
// 自定义强制更新的版本更新级别,默认是 major
'level': 'minor'
}).then(() => { /* 在这里启动命令行工具 */ });
updater({
'pkg': pkg,
// 完全自定义版本更新时的逻辑
'onVersionChange': function* (opts) {
}
}).then(() => { /* 在这里启动命令行工具 */ });
// https://registry.npmjs.org/co/latest
{
"name": "co",
"version": "4.5.0"
}
// 读取本地缓存的检查结果
const checkInfo = yield updater.readCheckInfo(opts);
const lastCheck = checkInfo.lastCheck;
const lastVersion = checkInfo.lastVersion;
// 根据版本信息提示用户
// ...
// 在时间间隔内,直接返回
if (Date.now() - lastCheck < opts.checkInterval) {
return;
}
// 创建后台检查进程
try {
require('child_process').spawn(
process.execPath,
[require('path').join(__dirname, '_check.js'), JSON.stringify({
'pkg': opts.pkg, // package 信息
'tag': opts.tag, // 检查的 dist-tag
'logFile': opts.logFile, // 缓存文件路径
'registry': opts.registry // registry 地址
})],
{'stdio': ['ignore', 'ignore', 'ignore'], 'detached': true}
).unref();
} catch(e) {}
const opts = JSON.parse(process.argv[2]);
let lastVersion = '';
try {
// 发送请求获取最新版本
const url = normalizeUrl(opts.registry + '/' + opts.pkg.name + '/' + (opts.tag || 'latest'));
const res = yield got.get(url, {
'json': true,
'timeout': 60 * 1000
});
if (res && res.body && res.body.version) {
lastVersion = res.body.version;
}
} catch(e) {}
// 如果获取失败了,最新版本就是当前版本(package.version)
if (!lastVersion) {
lastVersion = opts.pkg.version;
}
let data = yield util.readJson(opts.logFile);
if (!data[opts.pkg.name]) {
data[opts.pkg.name] = {};
}
data[opts.pkg.name].lastVersion = lastVersion; // 最新版本
data[opts.pkg.name].lastCheck = Date.now(); // 检查时间
// 写入缓存
yield util.writeJson(opts.logFile, data);
// 比对版本
const type = updater.diffType(opts.pkg.version, lastVersion, opts.level);
if (type) {
// 根据模板渲染提示信息
const str = updater.template(opts.updateMessage || updater.defaultOpts.updateMessage)({
'colors': updater.colors,
'name': opts.pkg.name,
'current': opts.pkg.version,
'latest': opts.lastVersion,
'command': 'npm i -g ' + opts.pkg.name
});
// 进行提示
console.log(
updater.boxen(str, {
'padding': 1,
'margin': 1,
'borderStyle': 'classic'
})
);
}
// 比对版本
const type = updater.diffType(opts.pkg.version, lastVersion, opts.level);
if (type) {
// 根据模板渲染提示信息
const str = updater.template(opts.updateMessage || updater.defaultOpts.updateMessage)({
'colors': updater.colors,
'name': opts.pkg.name,
'current': opts.pkg.version,
'latest': opts.lastVersion,
'command': 'npm i -g ' + opts.pkg.name
});
// 进行提示
console.log(
updater.boxen(str, {
'padding': 1,
'margin': 1,
'borderStyle': 'classic'
})
);
// 不兼容的更新,直接让进程退出
if (type == 'incompatible') {
process.exit(1);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有