-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-notifier.js
26 lines (23 loc) · 933 Bytes
/
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
import axios from 'axios';
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 && pkg.version !== latestVersion) {
logUpdate(pkg, latestVersion);
return true;
}
return false;
}