Skip to content

Commit

Permalink
fix(update-check): swap out update-notifier for simpler check
Browse files Browse the repository at this point in the history
closes #675
- remove update-notifier because we weren't using most of its dependencies
- use latest-version and a simple semver check instead
  • Loading branch information
acburdine committed May 28, 2018
1 parent a0a31a9 commit e11e8b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 211 deletions.
25 changes: 4 additions & 21 deletions lib/utils/update-check.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
'use strict';
const Promise = require('bluebird');
const updateNotifier = require('update-notifier');
const latestVersion = require('latest-version');
const semver = require('semver');
const pkg = require('../../package.json');

/*
* Update notifier sends several different types of updates,
* we want to make sure the only ones that trigger a warning are
* major, minor, and patch releases. This allows us to push prereleases
* & such in the future without those triggering a warning for users
*
* see https://github.com/yeoman/update-notifier#comprehensive for the different types
* of updates
*/
const typesToWarn = [
'major',
'minor',
'patch'
];

/**
* Checks if a version update is available
* @param {UI} ui ui instance
*/
module.exports = function updateCheck(ui) {
return Promise.fromCallback(cb => updateNotifier({pkg: pkg, callback: cb})).then((update) => {
if (typesToWarn.includes(update.type)) {
return latestVersion(pkg.name).then((latest) => {
if (semver.lt(pkg.version, latest)) {
const chalk = require('chalk');

ui.log(
Expand All @@ -34,7 +19,5 @@ module.exports = function updateCheck(ui) {
'yellow'
);
}

return Promise.resolve();
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"got": "8.3.1",
"inquirer": "5.2.0",
"is-running": "2.1.0",
"latest-version": "3.1.0",
"listr": "0.14.1",
"lodash": "4.17.10",
"log-symbols": "2.2.0",
Expand All @@ -81,7 +82,6 @@
"symlink-or-copy": "1.2.0",
"systeminformation": "3.41.3",
"tail": "1.2.3",
"update-notifier": "2.5.0",
"validator": "7.2.0",
"yargs": "11.0.0",
"yarn": "1.7.0"
Expand Down
60 changes: 16 additions & 44 deletions test/unit/utils/update-check-spec.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,55 @@
'use strict';
const expect = require('chai').expect;
const {expect} = require('chai');
const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const stripAnsi = require('strip-ansi');

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

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

const updateCheck = proxyquire(modulePath, {
'../../package.json': pkg,
'update-notifier': updateNotifer
'latest-version': latestVersion
});

updateCheck().catch((err) => {
expect(err.message).to.equal(testError.message);
expect(latestVersion.calledOnce).to.be.true;
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
done();
});
});

it('resolves immediately if there are no updates', function () {
it('doesn\'t do anything if there are no updates', function () {
const pkg = {name: 'ghost', version: '1.0.0'};
const updateNotifer = sinon.stub().callsFake((options) => {
expect(options).to.exist;
expect(options.pkg).to.deep.equal(pkg);
expect(options.callback).to.be.a('function');
options.callback(null, {type: 'latest'});
});
const latestVersion = sinon.stub().resolves('1.0.0');
const logStub = sinon.stub();

const updateCheck = proxyquire(modulePath, {
'../../package.json': pkg,
'update-notifier': updateNotifer
});

return updateCheck({log: logStub}).then(() => {
expect(logStub.called).to.be.false;
});
});

it('doesn\'t log a message if the type of update is not major, minor, or patch', function () {
const pkg = {name: 'ghost', version: '1.0.0'};
const updateNotifer = sinon.stub().callsFake((options) => {
expect(options).to.exist;
expect(options.pkg).to.deep.equal(pkg);
expect(options.callback).to.be.a('function');
options.callback(null, {type: 'prerelease'});
});
const logStub = sinon.stub();

const updateCheck = proxyquire(modulePath, {
'../../package.json': pkg,
'update-notifier': updateNotifer
'latest-version': latestVersion
});

return updateCheck({log: logStub}).then(() => {
expect(logStub.called).to.be.false;
expect(latestVersion.calledOnce).to.be.true;
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
});
});

it('logs a message if an update is available', function () {
const pkg = {name: 'ghost', version: '1.0.0'};
const updateNotifer = sinon.stub().callsFake((options) => {
expect(options).to.exist;
expect(options.pkg).to.deep.equal(pkg);
expect(options.callback).to.be.a('function');
options.callback(null, {type: 'minor'});
});
const latestVersion = sinon.stub().resolves('1.1.0');
const logStub = sinon.stub();

const updateCheck = proxyquire(modulePath, {
'../../package.json': pkg,
'update-notifier': updateNotifer
'latest-version': latestVersion
});

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

expect(stripAnsi(log)).to.match(/You are running an outdated version of Ghost-CLI/);

expect(latestVersion.calledOnce).to.be.true;
expect(latestVersion.calledWithExactly('ghost')).to.be.true;
});
});
});
Loading

0 comments on commit e11e8b8

Please sign in to comment.