Skip to content

Commit e6e1ada

Browse files
vikaspotluri123acburdine
authored andcommitted
feat(state commands): gracefully exit in noop situations
1 parent d4c358c commit e6e1ada

File tree

6 files changed

+38
-32
lines changed

6 files changed

+38
-32
lines changed

lib/commands/restart.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ class RestartCommand extends Command {
66
const instance = this.system.getInstance();
77

88
if (!instance.running()) {
9-
return Promise.reject(new Error('Ghost instance is not currently running.'));
9+
const StartCommand = require('./start');
10+
this.ui.log('Ghost instance is not running! Starting...', 'yellow');
11+
return this.runCommand(StartCommand);
1012
}
1113

1214
instance.loadRunningEnvironment(true);
13-
1415
return this.ui.run(instance.process.restart(process.cwd(), this.system.environment), 'Restarting Ghost');
1516
}
1617
}

lib/commands/start.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class StartCommand extends Command {
2626
const runOptions = {quiet: argv.quiet};
2727

2828
if (instance.running()) {
29-
return Promise.reject(new Error('Ghost is already running. Use `ghost ls` to see details.'));
29+
this.ui.log('Ghost is already running! Run `ghost ls` for more information', 'green');
30+
return Promise.resolve();
3031
}
3132

3233
instance.checkEnvironment();

lib/commands/stop.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class StopCommand extends Command {
4242
checkValidInstall('stop');
4343

4444
if (!instance.running()) {
45-
return Promise.reject(new errors.SystemError('No running Ghost instance found here.'));
45+
this.ui.log('Ghost is already stopped! Nothing to do here.', 'green');
46+
return Promise.resolve();
4647
}
4748

4849
instance.loadRunningEnvironment();

test/unit/commands/restart-spec.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22
const expect = require('chai').expect;
33
const sinon = require('sinon');
44

5-
const RestartCommand = require('../../../lib/commands/restart');
5+
const modulePath = '../../../lib/commands/restart';
6+
const RestartCommand = require(modulePath);
67
const Instance = require('../../../lib/instance');
78

89
describe('Unit: Command > Restart', function () {
9-
it('throws error if instance is not running', function () {
10-
class TestInstance extends Instance {
11-
running() { return false; }
10+
it('warns of stopped instance and starts instead', function () {
11+
const instance = {running: () => false};
12+
const logStub = sinon.stub();
13+
const ctx = {
14+
ui: {log: logStub},
15+
system: {getInstance: () => instance},
16+
runCommand: sinon.stub().resolves()
1217
}
13-
const testInstance = new TestInstance();
14-
15-
const command = new RestartCommand({}, {
16-
getInstance: () => testInstance
17-
});
18+
const command = new RestartCommand({}, {});
1819

19-
return command.run().then(() => {
20-
throw new Error('Run method should have thrown');
21-
}).catch((error) => {
22-
expect(error.message).to.match(/instance is not currently running/);
20+
return command.run.call(ctx).then(() => {
21+
expect(ctx.runCommand.calledOnce).to.be.true;
22+
expect(ctx.runCommand.args[0][0].description).to.equal('Start an instance of Ghost');
23+
expect(logStub.calledOnce).to.be.true;
24+
expect(logStub.args[0][0]).to.match(/not running!/);
2325
});
2426
});
2527

test/unit/commands/start-spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ describe('Unit: Commands > Start', function () {
2222
};
2323
});
2424

25-
it('doesn\'t start a running instance', function () {
25+
it('gracefully notifies of already running instance', function () {
2626
const runningStub = sinon.stub().returns(true)
27+
const logStub = sinon.stub();
28+
const ui = {log: logStub};
29+
const start = new StartCommand(ui, mySystem);
2730
myInstance.running = runningStub;
28-
const start = new StartCommand({}, mySystem);
2931

3032
return start.run({}).then(() => {
31-
expect(false, 'Promise should have rejected').to.be.true;
32-
}).catch((error) => {
33-
expect(error).to.be.ok;
34-
expect(error.message).to.match(/^Ghost is already running/);
3533
expect(runningStub.calledOnce).to.be.true;
34+
expect(logStub.calledOnce).to.be.true;
35+
expect(logStub.args[0][0]).to.match(/Ghost is already running!/);
3636
});
3737
});
3838

test/unit/commands/stop-spec.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,19 @@ describe('Unit: Commands > Stop', function () {
5555
}
5656
});
5757

58-
it('doesn\'t stop stopped instances', function () {
59-
const runningStub = sinon.stub().returns(false);
60-
const gIstub = sinon.stub().returns({running: runningStub});
61-
const context = {system: {getInstance: gIstub}};
58+
it('gracefully notifies of already stopped instance', function () {
59+
const runningFake = () => false;
60+
const gIfake = () => ({running: runningFake});
61+
const logStub = sinon.stub();
6262
const stop = proxiedCommand();
63+
const context = {
64+
system: {getInstance: gIfake},
65+
ui: {log: logStub}
66+
};
6367

6468
return stop.run.call(context, {}).then(() => {
65-
expect(false, 'Promise should have rejected').to.be.true;
66-
}).catch((error) => {
67-
expect(error).to.be.ok;
68-
expect(error instanceof errors.SystemError).to.be.true;
69-
expect(error.message).to.match(/No running Ghost instance/);
69+
expect(logStub.calledOnce).to.be.true;
70+
expect(logStub.args[0][0]).to.match(/Ghost is already stopped!/);
7071
});
7172
});
7273

0 commit comments

Comments
 (0)