-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-notifier.js
31 lines (27 loc) · 1.06 KB
/
update-notifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import axios from 'axios';
import semverLt from 'semver/functions/lt.js'
async function fetchLatestRelease(pkg) {
const url = `https://api.github.com/repos/${pkg.author}/${pkg.repository}/releases/latest`;
try {
return (await axios.get(url)).data.tag_name;
} catch (error) {
console.warn(`Error fetching latest release. Check manually: https://github.com/${pkg.author}/${pkg.repository}/releases/latest`);
return null;
}
}
function logUpdate(pkg, latestVersion) {
console.warn(`Update available ${pkg.version} -> ${latestVersion}`);
console.warn(`Run 'npm i -g ${pkg.name}' to update`);
console.warn(`GitHub -> https://github.com/${pkg.author}/${pkg.repository}/releases/latest`);
}
export async function checkUpdate(pkg) {
const latestVersion = await fetchLatestRelease(pkg);
if (latestVersion && semverLt(pkg.version, latestVersion)) {
logUpdate(pkg, latestVersion);
return true;
}
return false;
}
export async function checkUpdateExit(pkg) {
if (await checkUpdate(pkg)) process.exit();
}