2016-07-01 12:01:33 -08:00
|
|
|
/*global fetch:false*/
|
|
|
|
|
import { version as currentVersion } from '../package';
|
|
|
|
|
import compare from 'semver-compare';
|
2016-07-04 17:13:44 -08:00
|
|
|
import ms from 'ms';
|
|
|
|
|
|
|
|
|
|
const DEFAULT_INTERVAL = ms('5m');
|
2016-07-01 12:01:33 -08:00
|
|
|
|
|
|
|
|
export default class UpdateChecker {
|
|
|
|
|
|
2016-07-04 17:13:44 -08:00
|
|
|
constructor (fn, { interval = DEFAULT_INTERVAL } = {}) {
|
2016-07-01 12:01:33 -08:00
|
|
|
this.callback = fn;
|
|
|
|
|
this.interval = interval;
|
|
|
|
|
this.check();
|
|
|
|
|
this.lastKnown = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check () {
|
|
|
|
|
const done = () => {
|
|
|
|
|
this.checkTimer = setTimeout(() => {
|
|
|
|
|
this.check();
|
|
|
|
|
}, this.interval);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log('checking for update');
|
2016-07-03 14:12:35 -08:00
|
|
|
fetch('https://hyperterm.now.sh/updates.json')
|
2016-07-01 12:01:33 -08:00
|
|
|
.then((res) => {
|
|
|
|
|
if (200 !== res.status) {
|
|
|
|
|
console.error('Update check error. Status (%d)', res.status);
|
|
|
|
|
return done();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json()
|
2016-07-03 14:12:35 -08:00
|
|
|
.then(({ version, note }) => {
|
2016-07-01 12:01:33 -08:00
|
|
|
if (this.lastKnown !== version) {
|
|
|
|
|
this.lastKnown = version;
|
|
|
|
|
|
|
|
|
|
if (1 === compare(version, currentVersion)) {
|
|
|
|
|
console.log('update found');
|
2016-07-03 14:12:35 -08:00
|
|
|
this.callback(version, note);
|
2016-07-01 12:01:33 -08:00
|
|
|
} else {
|
|
|
|
|
console.log('no update. latest:', version);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
done();
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error('Update JSON parse error', err.stack);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
console.error('Update check error', err.stack);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destroy () {
|
|
|
|
|
this.aborted = true;
|
|
|
|
|
clearTimeout(this.checkTimer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|