diff --git a/core/index.js b/core/index.js index ceb0e650d2..c1c95ff1ad 100644 --- a/core/index.js +++ b/core/index.js @@ -1,6 +1,8 @@ // ## Server Loader // Passes options through the boot process to get a server instance back -var server = require('./server'); +const server = require('./server'); +const common = require('./server/lib/common'); +const GhostServer = require('./server/ghost-server'); // Set the default environment to be `development` process.env.NODE_ENV = process.env.NODE_ENV || 'development'; @@ -8,7 +10,17 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'development'; function makeGhost(options) { options = options || {}; - return server(options); + return server(options) + .catch((err) => { + if (!common.errors.utils.isIgnitionError(err)) { + err = new common.errors.GhostError({message: err.message, err: err}); + } + + return GhostServer.announceServerStopped(err) + .finally(() => { + throw err; + }); + }); } module.exports = makeGhost; diff --git a/core/server/ghost-server.js b/core/server/ghost-server.js index 2481f63a2a..5e9562de12 100644 --- a/core/server/ghost-server.js +++ b/core/server/ghost-server.js @@ -94,9 +94,12 @@ GhostServer.prototype.start = function (externalApp) { self.httpServer.on('connection', self.connection.bind(self)); self.httpServer.on('listening', function () { debug('...Started'); - self.logStartMessages(); - resolve(self); + + return GhostServer.announceServerStart() + .finally(() => { + resolve(self); + }); }); }); }; @@ -312,7 +315,19 @@ const connectToBootstrapSocket = (message) => { }); }; +/** + * @NOTE announceServerStartCalled: + * + * - backwards compatible logic, because people complained that not all themes were loaded when using Ghost as NPM module + * - we told them to call `announceServerStart`, which is not required anymore, because we restructured the code + */ +let announceServerStartCalled = false; module.exports.announceServerStart = function announceServerStart() { + if (announceServerStartCalled || config.get('maintenance:enabled')) { + return Promise.resolve(); + } + announceServerStartCalled = true; + common.events.emit('server.start'); // CASE: IPC communication to the CLI via child process. @@ -332,7 +347,19 @@ module.exports.announceServerStart = function announceServerStart() { return Promise.resolve(); }; +/** + * @NOTE announceServerStopCalled: + * + * - backwards compatible logic, because people complained that not all themes were loaded when using Ghost as NPM module + * - we told them to call `announceServerStart`, which is not required anymore, because we restructured code + */ +let announceServerStopCalled = false; module.exports.announceServerStopped = function announceServerStopped(error) { + if (announceServerStopCalled) { + return Promise.resolve(); + } + announceServerStopCalled = true; + // CASE: IPC communication to the CLI via child process. if (process.send) { process.send({ diff --git a/index.js b/index.js index 1157b0bd4a..a25ece9f1a 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ var startTime = Date.now(), debug = require('ghost-ignition').debug('boot:index'), - ghost, express, common, urlService, parentApp, config, GhostServer; + ghost, express, common, urlService, parentApp; debug('First requires...'); @@ -12,8 +12,6 @@ ghost = require('./core'); debug('Required ghost'); express = require('express'); -GhostServer = require('./core/server/ghost-server'); -config = require('./core/server/config'); common = require('./core/server/lib/common'); urlService = require('./core/server/services/url'); parentApp = express(); @@ -28,21 +26,10 @@ ghost().then(function (ghostServer) { return ghostServer.start(parentApp) .then(function afterStart() { common.logging.info('Ghost boot', (Date.now() - startTime) / 1000 + 's'); - - if (!config.get('maintenance:enabled')) { - return GhostServer.announceServerStart(); - } }); }).catch(function (err) { - if (!common.errors.utils.isIgnitionError(err)) { - err = new common.errors.GhostError({message: err.message, err: err}); - } - - return GhostServer.announceServerStopped(err) - .finally(() => { - common.logging.error(err); - setTimeout(() => { - process.exit(-1); - }, 100); - }); + common.logging.error(err); + setTimeout(() => { + process.exit(-1); + }, 100); });