-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.js
46 lines (39 loc) · 1.1 KB
/
updater.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const semver = require('semver');
const npm = require('.')({
timeout : 3000,
registry: 'https://registry.npmjs.org'
});
module.exports = function(pkg){
npm.fetch(pkg.name).then(package => {
const current = pkg.version;
const latest = package[ 'dist-tags' ][ 'latest' ];
const type = diff(current, latest);
if(pkg.updater && pkg.updater.level == type){
console.warn(color('[upkg-updater] %s %s is available! (you\'re using %s)', 35), pkg.name, latest, current);
console.warn(color('[upkg-updater] To upgrade, run "npm upgrade %s"', 35), pkg.name);
}
}, () => {});
};
function diff(a, b){
if (semver.gt(a, b)) {
return null;
}
a = semver.parse(a);
b = semver.parse(b);
for (var key in a) {
if (key === 'major' || key === 'minor' || key === 'patch') {
if (a[key] !== b[key]) {
return key;
}
}
if (key === 'prerelease' || key === 'build') {
if (JSON.stringify(a[key]) !== JSON.stringify(b[key])) {
return key;
}
}
}
return null;
}
function color(str, c){
return "\x1b[" + c + "m" + str + "\x1b[0m";
};