17.14
This commit is contained in:
30
lib/update.js
Normal file
30
lib/update.js
Normal file
@ -0,0 +1,30 @@
|
||||
import color from "picocolors";
|
||||
import { createRequire } from "module";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const pkg = require("../package.json");
|
||||
|
||||
export async function checkUpdate() {
|
||||
try {
|
||||
const res = await fetch(`https://registry.npmmirror.com/${pkg.name}/latest`, {
|
||||
signal: AbortSignal.timeout(3000)
|
||||
});
|
||||
if (!res.ok) return;
|
||||
const data = await res.json();
|
||||
const latest = data.version;
|
||||
if (latest && latest !== pkg.version && isNewer(latest, pkg.version)) {
|
||||
console.log(color.yellow(`\n📦 发现新版本: ${color.red(pkg.version)} → ${color.green(latest)}`));
|
||||
console.log(color.cyan(` 运行 ${color.bold("yinx upgrade")} 进行更新\n`));
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function isNewer(latest, current) {
|
||||
const l = latest.split(".").map(Number);
|
||||
const c = current.split(".").map(Number);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if ((l[i] || 0) > (c[i] || 0)) return true;
|
||||
if ((l[i] || 0) < (c[i] || 0)) return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user