diff --git a/lib/commands/restart.js b/lib/commands/restart.js index ccdea61d6..049129e70 100644 --- a/lib/commands/restart.js +++ b/lib/commands/restart.js @@ -4,15 +4,15 @@ const Command = require('../command'); class RestartCommand extends Command { async run() { const instance = this.system.getInstance(); + const isRunning = await instance.isRunning(); if (!isRunning) { - const StartCommand = require('./start'); this.ui.log('Ghost instance is not running! Starting...', 'yellow'); - return this.runCommand(StartCommand); + return this.ui.run(() => instance.start(), 'Starting Ghost'); } instance.loadRunningEnvironment(true); - return this.ui.run(instance.process.restart(process.cwd(), this.system.environment), 'Restarting Ghost'); + await this.ui.run(() => instance.restart(), 'Restarting Ghost'); } } diff --git a/lib/instance.js b/lib/instance.js index febe178ca..c4b3d37fd 100644 --- a/lib/instance.js +++ b/lib/instance.js @@ -296,6 +296,17 @@ class Instance { await this.process.disable(); } + /** + * Restarts the Ghost instance + * + * @returns {Promise} + * @method restart + * @public + */ + async restart() { + await this.process.restart(this.dir, this.system.environment); + } + /** * Gets a summary of this instance, comprised of various values from * the cliConfig and the ghost config diff --git a/test/unit/commands/restart-spec.js b/test/unit/commands/restart-spec.js index 64ad5482b..f92d28311 100644 --- a/test/unit/commands/restart-spec.js +++ b/test/unit/commands/restart-spec.js @@ -7,40 +7,49 @@ const RestartCommand = require(modulePath); describe('Unit: Command > Restart', function () { it('warns of stopped instance and starts instead', async function () { - const instance = {isRunning: () => Promise.resolve(false)}; - const logStub = sinon.stub(); - - const command = new RestartCommand({log: logStub}, {getInstance: () => instance}); - const runCommand = sinon.stub(command, 'runCommand').resolves(); + const instance = { + isRunning: sinon.stub().resolves(false), + start: sinon.stub().resolves() + }; + const ui = { + log: sinon.stub(), + run: sinon.stub().callsFake(fn => fn()) + }; + const system = { + getInstance: sinon.stub().returns(instance) + }; + const command = new RestartCommand(ui, system); await command.run(); - expect(runCommand.calledOnce).to.be.true; - expect(runCommand.args[0][0].description).to.equal('Start an instance of Ghost'); - expect(logStub.calledOnce).to.be.true; - expect(logStub.args[0][0]).to.match(/not running!/); + + expect(instance.isRunning.calledOnce).to.be.true; + expect(ui.log.calledOnce).to.be.true; + expect(ui.log.args[0][0]).to.match(/not running!/); + expect(ui.run.calledOnce).to.be.true; + expect(instance.start.calledOnce).to.be.true; }); it('calls process restart method if instance is running', async function () { - const runStub = sinon.stub().resolves(); - const restartStub = sinon.stub().resolves(); - const lreStub = sinon.stub(); const instance = { - process: {restart: restartStub}, - loadRunningEnvironment: lreStub, - isRunning: () => Promise.resolve(true) + isRunning: sinon.stub().resolves(true), + loadRunningEnvironment: sinon.stub(), + restart: sinon.stub().resolves() + }; + const ui = { + log: sinon.stub(), + run: sinon.stub().callsFake(fn => fn()) + }; + const system = { + getInstance: sinon.stub().returns(instance) }; - const command = new RestartCommand({run: runStub}, { - environment: 'testing', - getInstance: () => instance - }); - + const command = new RestartCommand(ui, system); await command.run(); - expect(lreStub.calledOnce).to.be.true; - expect(lreStub.args[0][0]).to.be.true; - expect(restartStub.calledOnce).to.be.true; - expect(restartStub.args[0]).to.deep.equal([process.cwd(), 'testing']); - expect(runStub.calledOnce).to.be.true; + expect(instance.isRunning.calledOnce).to.be.true; + expect(ui.log.called).to.be.false; + expect(instance.loadRunningEnvironment.calledOnce).to.be.true; + expect(ui.run.calledOnce).to.be.true; + expect(instance.restart.calledOnce).to.be.true; }); }); diff --git a/test/unit/instance-spec.js b/test/unit/instance-spec.js index e3bb797c0..cb87175d1 100644 --- a/test/unit/instance-spec.js +++ b/test/unit/instance-spec.js @@ -579,6 +579,19 @@ describe('Unit: Instance', function () { }); }); + it('restart', async function () { + const restart = sinon.stub().resolves(); + const instance = new Instance({}, {environment: 'testing'}, '/var/www/ghost'); + + instance._process = { + name: 'local', + restart + }; + + await instance.restart(); + expect(restart.calledOnceWithExactly('/var/www/ghost', 'testing')).to.be.true; + }); + describe('summary', function () { it('returns shortened object if running is false', async function () { const get = sinon.stub();