Skip to content

Commit e11e8b8

Browse files
committed
fix(update-check): swap out update-notifier for simpler check
closes #675 - remove update-notifier because we weren't using most of its dependencies - use latest-version and a simple semver check instead
1 parent a0a31a9 commit e11e8b8

File tree

4 files changed

+41
-211
lines changed

4 files changed

+41
-211
lines changed

lib/utils/update-check.js

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
'use strict';
2-
const Promise = require('bluebird');
3-
const updateNotifier = require('update-notifier');
2+
const latestVersion = require('latest-version');
3+
const semver = require('semver');
44
const pkg = require('../../package.json');
55

6-
/*
7-
* Update notifier sends several different types of updates,
8-
* we want to make sure the only ones that trigger a warning are
9-
* major, minor, and patch releases. This allows us to push prereleases
10-
* & such in the future without those triggering a warning for users
11-
*
12-
* see https://github.com/yeoman/update-notifier#comprehensive for the different types
13-
* of updates
14-
*/
15-
const typesToWarn = [
16-
'major',
17-
'minor',
18-
'patch'
19-
];
20-
216
/**
227
* Checks if a version update is available
238
* @param {UI} ui ui instance
249
*/
2510
module.exports = function updateCheck(ui) {
26-
return Promise.fromCallback(cb => updateNotifier({pkg: pkg, callback: cb})).then((update) => {
27-
if (typesToWarn.includes(update.type)) {
11+
return latestVersion(pkg.name).then((latest) => {
12+
if (semver.lt(pkg.version, latest)) {
2813
const chalk = require('chalk');
2914

3015
ui.log(
@@ -34,7 +19,5 @@ module.exports = function updateCheck(ui) {
3419
'yellow'
3520
);
3621
}
37-
38-
return Promise.resolve();
3922
});
4023
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"got": "8.3.1",
6363
"inquirer": "5.2.0",
6464
"is-running": "2.1.0",
65+
"latest-version": "3.1.0",
6566
"listr": "0.14.1",
6667
"lodash": "4.17.10",
6768
"log-symbols": "2.2.0",
@@ -81,7 +82,6 @@
8182
"symlink-or-copy": "1.2.0",
8283
"systeminformation": "3.41.3",
8384
"tail": "1.2.3",
84-
"update-notifier": "2.5.0",
8585
"validator": "7.2.0",
8686
"yargs": "11.0.0",
8787
"yarn": "1.7.0"

test/unit/utils/update-check-spec.js

+16-44
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,55 @@
11
'use strict';
2-
const expect = require('chai').expect;
2+
const {expect} = require('chai');
33
const proxyquire = require('proxyquire').noCallThru();
44
const sinon = require('sinon');
55
const stripAnsi = require('strip-ansi');
66

77
const modulePath = '../../../lib/utils/update-check';
88

99
describe('Unit: Utils > update-check', function () {
10-
it('rejects error if updateNotifier has an error', function (done) {
10+
it('rejects error if latestVersion has an error', function (done) {
1111
const pkg = {name: 'ghost', version: '1.0.0'};
1212
const testError = new Error('update check');
13-
const updateNotifer = sinon.stub().callsFake((options) => {
14-
expect(options).to.exist;
15-
expect(options.pkg).to.deep.equal(pkg);
16-
expect(options.callback).to.be.a('function');
17-
options.callback(testError, null);
18-
});
13+
const latestVersion = sinon.stub().rejects(testError);
1914

2015
const updateCheck = proxyquire(modulePath, {
2116
'../../package.json': pkg,
22-
'update-notifier': updateNotifer
17+
'latest-version': latestVersion
2318
});
2419

2520
updateCheck().catch((err) => {
2621
expect(err.message).to.equal(testError.message);
22+
expect(latestVersion.calledOnce).to.be.true;
23+
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
2724
done();
2825
});
2926
});
3027

31-
it('resolves immediately if there are no updates', function () {
28+
it('doesn\'t do anything if there are no updates', function () {
3229
const pkg = {name: 'ghost', version: '1.0.0'};
33-
const updateNotifer = sinon.stub().callsFake((options) => {
34-
expect(options).to.exist;
35-
expect(options.pkg).to.deep.equal(pkg);
36-
expect(options.callback).to.be.a('function');
37-
options.callback(null, {type: 'latest'});
38-
});
30+
const latestVersion = sinon.stub().resolves('1.0.0');
3931
const logStub = sinon.stub();
4032

4133
const updateCheck = proxyquire(modulePath, {
4234
'../../package.json': pkg,
43-
'update-notifier': updateNotifer
44-
});
45-
46-
return updateCheck({log: logStub}).then(() => {
47-
expect(logStub.called).to.be.false;
48-
});
49-
});
50-
51-
it('doesn\'t log a message if the type of update is not major, minor, or patch', function () {
52-
const pkg = {name: 'ghost', version: '1.0.0'};
53-
const updateNotifer = sinon.stub().callsFake((options) => {
54-
expect(options).to.exist;
55-
expect(options.pkg).to.deep.equal(pkg);
56-
expect(options.callback).to.be.a('function');
57-
options.callback(null, {type: 'prerelease'});
58-
});
59-
const logStub = sinon.stub();
60-
61-
const updateCheck = proxyquire(modulePath, {
62-
'../../package.json': pkg,
63-
'update-notifier': updateNotifer
35+
'latest-version': latestVersion
6436
});
6537

6638
return updateCheck({log: logStub}).then(() => {
6739
expect(logStub.called).to.be.false;
40+
expect(latestVersion.calledOnce).to.be.true;
41+
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
6842
});
6943
});
7044

7145
it('logs a message if an update is available', function () {
7246
const pkg = {name: 'ghost', version: '1.0.0'};
73-
const updateNotifer = sinon.stub().callsFake((options) => {
74-
expect(options).to.exist;
75-
expect(options.pkg).to.deep.equal(pkg);
76-
expect(options.callback).to.be.a('function');
77-
options.callback(null, {type: 'minor'});
78-
});
47+
const latestVersion = sinon.stub().resolves('1.1.0');
7948
const logStub = sinon.stub();
8049

8150
const updateCheck = proxyquire(modulePath, {
8251
'../../package.json': pkg,
83-
'update-notifier': updateNotifer
52+
'latest-version': latestVersion
8453
});
8554

8655
return updateCheck({log: logStub}).then(() => {
@@ -89,6 +58,9 @@ describe('Unit: Utils > update-check', function () {
8958
const log = logStub.args[0][0];
9059

9160
expect(stripAnsi(log)).to.match(/You are running an outdated version of Ghost-CLI/);
61+
62+
expect(latestVersion.calledOnce).to.be.true;
63+
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
9264
});
9365
});
9466
});

0 commit comments

Comments
 (0)