-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check-update command (#881)
closes #876 - add check-update command to check for available updates
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const Command = require('../command'); | ||
|
||
class CheckUpdateCommand extends Command { | ||
run({v1}) { | ||
const semver = require('semver'); | ||
const resolveVersion = require('../utils/resolve-version'); | ||
const {CliError} = require('../errors'); | ||
|
||
const instance = this.system.getInstance(); | ||
|
||
if (instance.version) { | ||
return resolveVersion(null, instance.version, v1).then((version) => { | ||
const diff = semver.diff(instance.version, version); | ||
this.ui.log(`New ${diff} version available: ${version}`); | ||
}).catch((error) => { | ||
if (!(error instanceof CliError) || !error.message.match(/No valid versions/)) { | ||
return Promise.reject(error); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
CheckUpdateCommand.description = 'Check if an update is available for a Ghost installation'; | ||
CheckUpdateCommand.allowRoot = true; | ||
CheckUpdateCommand.options = { | ||
v1: { | ||
describe: 'Limit check to Ghost 1.x releases', | ||
type: 'boolean', | ||
default: false | ||
} | ||
}; | ||
|
||
module.exports = CheckUpdateCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const sinon = require('sinon'); | ||
const proxyquire = require('proxyquire'); | ||
const {expect} = require('chai'); | ||
const {CliError} = require('../../../lib/errors'); | ||
|
||
const modulePath = '../../../lib/commands/check-update'; | ||
|
||
describe('Unit: Commands > check-update', function () { | ||
it('doesn\'t output anything if instance doesn\'t exist', function () { | ||
const CheckUpdateCommand = require(modulePath); | ||
const log = sinon.stub(); | ||
const getInstance = sinon.stub().returns({version: null}); | ||
|
||
const cmd = new CheckUpdateCommand({log}, {getInstance}); | ||
|
||
cmd.run({v1: false}); | ||
|
||
expect(getInstance.calledOnce).to.be.true; | ||
expect(log.called).to.be.false; | ||
}); | ||
|
||
it('doesn\'t output anything if no new versions are available', function () { | ||
const resolveVersion = sinon.stub().rejects(new CliError('No valid versions found.')); | ||
|
||
const CheckUpdateCommand = proxyquire(modulePath, { | ||
'../utils/resolve-version': resolveVersion | ||
}); | ||
|
||
const log = sinon.stub(); | ||
const getInstance = sinon.stub().returns({version: '2.0.0'}); | ||
|
||
const cmd = new CheckUpdateCommand({log}, {getInstance}); | ||
|
||
return cmd.run({v1: false}).then(() => { | ||
expect(getInstance.calledOnce).to.be.true; | ||
expect(resolveVersion.calledOnce).to.be.true; | ||
expect(resolveVersion.calledWithExactly(null, '2.0.0', false)).to.be.true; | ||
expect(log.called).to.be.false; | ||
}); | ||
}); | ||
|
||
it('rejects errors from resolveVersion', function () { | ||
const resolveVersion = sinon.stub().rejects(new CliError('An error occurred')); | ||
|
||
const CheckUpdateCommand = proxyquire(modulePath, { | ||
'../utils/resolve-version': resolveVersion | ||
}); | ||
|
||
const log = sinon.stub(); | ||
const getInstance = sinon.stub().returns({version: '2.0.0'}); | ||
|
||
const cmd = new CheckUpdateCommand({log}, {getInstance}); | ||
|
||
return cmd.run({v1: true}).then(() => { | ||
expect(false, 'error should have been thrown').to.be.false; | ||
}).catch((error) => { | ||
expect(error).to.be.an.instanceof(CliError); | ||
expect(getInstance.calledOnce).to.be.true; | ||
expect(resolveVersion.calledOnce).to.be.true; | ||
expect(resolveVersion.calledWithExactly(null, '2.0.0', true)).to.be.true; | ||
expect(log.called).to.be.false; | ||
}); | ||
}); | ||
|
||
it('logs out available new version', function () { | ||
const resolveVersion = sinon.stub().resolves('2.1.0'); | ||
|
||
const CheckUpdateCommand = proxyquire(modulePath, { | ||
'../utils/resolve-version': resolveVersion | ||
}); | ||
|
||
const log = sinon.stub(); | ||
const getInstance = sinon.stub().returns({version: '2.0.0'}); | ||
|
||
const cmd = new CheckUpdateCommand({log}, {getInstance}); | ||
|
||
return cmd.run({v1: true}).then(() => { | ||
expect(getInstance.calledOnce).to.be.true; | ||
expect(resolveVersion.calledOnce).to.be.true; | ||
expect(resolveVersion.calledWithExactly(null, '2.0.0', true)).to.be.true; | ||
expect(log.calledOnce).to.be.true; | ||
}); | ||
}); | ||
}); |