From b9206627900f813ce25bfe88d5d4150117e87d16 Mon Sep 17 00:00:00 2001 From: Harry Wolff Date: Wed, 20 Nov 2013 08:58:52 -0500 Subject: [PATCH] Create the config module, initially used to standardise getting paths and absolute URLs. Easy to extend for other configurations we may need. --- Gruntfile.js | 4 +- core/ghost.js | 89 ++++++------------- core/server.js | 19 ++-- core/server/config/index.js | 16 ++++ .../config/loader.js} | 12 +-- core/server/config/paths.js | 49 ++++++++++ core/server/controllers/admin.js | 5 +- core/server/controllers/frontend.js | 5 +- core/server/helpers/index.js | 11 ++- core/server/mail.js | 9 +- core/server/middleware/index.js | 19 ++-- core/server/middleware/middleware.js | 10 +-- core/server/plugins/loader.js | 3 +- core/shared/lang/i18n.js | 5 +- core/test/unit/ghost_spec.js | 7 +- core/test/unit/mail_spec.js | 21 +++-- core/test/unit/middleware_spec.js | 4 +- core/test/unit/server_helpers_index_spec.js | 4 +- index.js | 6 +- 19 files changed, 173 insertions(+), 125 deletions(-) create mode 100644 core/server/config/index.js rename core/{config-loader.js => server/config/loader.js} (94%) create mode 100644 core/server/config/paths.js diff --git a/Gruntfile.js b/Gruntfile.js index 4fb070aa79..b1d17a3010 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -6,7 +6,7 @@ var path = require('path'), spawn = require('child_process').spawn, buildDirectory = path.resolve(process.cwd(), '.build'), distDirectory = path.resolve(process.cwd(), '.dist'), - configLoader = require('./core/config-loader.js'), + configLoader = require('./core/server/config/loader'), buildGlob = [ '**', @@ -499,7 +499,7 @@ var path = require('path'), grunt.registerTask('loadConfig', function () { var done = this.async(); - configLoader.loadConfig().then(function () { + configLoader().then(function () { done(); }); }); diff --git a/core/ghost.js b/core/ghost.js index f4655b7f80..93186640d2 100644 --- a/core/ghost.js +++ b/core/ghost.js @@ -2,30 +2,23 @@ // Defines core methods required to build the application // Module dependencies -var config = require('../config'), - when = require('when'), - express = require('express'), - errors = require('./server/errorHandling'), - fs = require('fs'), - path = require('path'), - hbs = require('express-hbs'), - nodefn = require('when/node/function'), - _ = require('underscore'), - url = require('url'), - Polyglot = require('node-polyglot'), - Mailer = require('./server/mail'), - models = require('./server/models'), - requireTree = require('./server/require-tree'), - permissions = require('./server/permissions'), - uuid = require('node-uuid'), +var config = require('./server/config'), + when = require('when'), + express = require('express'), + errors = require('./server/errorHandling'), + fs = require('fs'), + path = require('path'), + hbs = require('express-hbs'), + nodefn = require('when/node/function'), + _ = require('underscore'), + url = require('url'), + Polyglot = require('node-polyglot'), + Mailer = require('./server/mail'), + models = require('./server/models'), + permissions = require('./server/permissions'), + uuid = require('node-uuid'), // Variables - appRoot = path.resolve(__dirname, '../'), - themePath = path.resolve(appRoot + '/content/themes'), - pluginPath = path.resolve(appRoot + '/content/plugins'), - themeDirectories = requireTree(themePath), - pluginDirectories = requireTree(pluginPath), - Ghost, instance, defaults; @@ -60,12 +53,6 @@ Ghost = function () { // Holds the filter hooks (that are built in to Ghost Core) instance.filters = []; - // Holds the theme directories temporarily - instance.themeDirectories = {}; - - // Holds the plugin directories temporarily - instance.pluginDirectories = {}; - // Holds the persistent notifications instance.notifications = []; @@ -78,8 +65,6 @@ Ghost = function () { polyglot = new Polyglot(); _.extend(instance, { - config: function () { return config[process.env.NODE_ENV]; }, - // there's no management here to be sure this has loaded settings: function (key) { if (key) { @@ -89,7 +74,7 @@ Ghost = function () { }, dataProvider: models, blogGlobals: function () { - var localPath = url.parse(instance.config().url).path; + var localPath = url.parse(config().url).path; // Remove trailing slash if (localPath !== '/') { @@ -99,7 +84,7 @@ Ghost = function () { /* this is a bit of a hack until we have a better way to combine settings and config * this data is what becomes globally available to themes */ return { - url: instance.config().url.replace(/\/$/, ''), + url: config().url.replace(/\/$/, ''), path: localPath, title: instance.settings('title'), description: instance.settings('description'), @@ -108,27 +93,7 @@ Ghost = function () { }; }, polyglot: function () { return polyglot; }, - mail: new Mailer(), - getPaths: function () { - return when.all([themeDirectories, pluginDirectories]).then(function (paths) { - instance.themeDirectories = paths[0]; - instance.pluginDirectories = paths[1]; - return; - }); - }, - paths: function () { - return { - 'appRoot': appRoot, - 'themePath': themePath, - 'pluginPath': pluginPath, - 'activeTheme': path.join(themePath, !instance.settingsCache ? '' : instance.settingsCache.activeTheme.value), - 'adminViews': path.join(appRoot, '/core/server/views/'), - 'helperTemplates': path.join(appRoot, '/core/server/helpers/tpl/'), - 'lang': path.join(appRoot, '/core/shared/lang/'), - 'availableThemes': instance.themeDirectories, - 'availablePlugins': instance.pluginDirectories - }; - } + mail: new Mailer() }); } return instance; @@ -146,7 +111,7 @@ Ghost.prototype.init = function () { 'environment.', 'Your URL is set to', - '' + self.config().url + '.', + '' + config().url + '.', 'See http://docs.ghost.org for instructions.' ]; @@ -180,9 +145,11 @@ Ghost.prototype.init = function () { // Initialise the models self.dataProvider.init(), // Calculate paths - self.getPaths(), - // Initialise mail after first run - self.mail.init(self) + config.paths.updatePaths(), + // Initialise mail after first run, + // passing in config module to prevent + // circular dependencies. + self.mail.init(self, config) ).then(function () { // Populate any missing default settings return models.Settings.populateDefaults(); @@ -190,6 +157,8 @@ Ghost.prototype.init = function () { // Initialize the settings cache return self.updateSettingsCache(); }).then(function () { + // Update path to activeTheme + config.paths.setActiveTheme(self); return when.join( // Check for or initialise a dbHash. initDbHashAndFirstRun(), @@ -229,7 +198,7 @@ Ghost.prototype.readSettingsResult = function (result) { settings[member.attributes.key] = val; } })).then(function () { - return when(instance.paths().availableThemes).then(function (themes) { + return when(config.paths().availableThemes).then(function (themes) { var themeKeys = Object.keys(themes), res = [], i, @@ -269,7 +238,7 @@ Ghost.prototype.loadTemplate = function (name) { var self = this, templateFileName = name + '.hbs', // Check for theme specific version first - templatePath = path.join(this.paths().activeTheme, 'partials', templateFileName), + templatePath = path.join(config.paths().activeTheme, 'partials', templateFileName), deferred = when.defer(); // Can't use nodefn here because exists just returns one parameter, true or false @@ -277,7 +246,7 @@ Ghost.prototype.loadTemplate = function (name) { fs.exists(templatePath, function (exists) { if (!exists) { // Fall back to helpers templates location - templatePath = path.join(self.paths().helperTemplates, templateFileName); + templatePath = path.join(config.paths().helperTemplates, templateFileName); } self.compileTemplate(templatePath).then(deferred.resolve, deferred.reject); diff --git a/core/server.js b/core/server.js index d70f4fea7a..fea6a34c42 100644 --- a/core/server.js +++ b/core/server.js @@ -3,7 +3,8 @@ // modules to ensure config gets right setting. // Module dependencies -var express = require('express'), +var config = require('./server/config'), + express = require('express'), when = require('when'), _ = require('underscore'), semver = require('semver'), @@ -34,7 +35,7 @@ if (process.env.NODE_ENV === 'development') { // Finally it starts the http server. function setup(server) { when(ghost.init()).then(function () { - return helpers.loadCoreHelpers(ghost); + return helpers.loadCoreHelpers(ghost, config); }).then(function () { // ##Configuration @@ -63,8 +64,8 @@ function setup(server) { // Are we using sockets? Custom socket or the default? function getSocket() { - if (ghost.config().server.hasOwnProperty('socket')) { - return _.isString(ghost.config().server.socket) ? ghost.config().server.socket : path.join(__dirname, '../content/', process.env.NODE_ENV + '.socket'); + if (config().server.hasOwnProperty('socket')) { + return _.isString(config().server.socket) ? config().server.socket : path.join(__dirname, '../content/', process.env.NODE_ENV + '.socket'); } return false; } @@ -89,7 +90,7 @@ function setup(server) { console.log( "Ghost is running...".green, "\nYour blog is now available on", - ghost.config().url, + config().url, "\nCtrl+C to shut down".grey ); @@ -105,9 +106,9 @@ function setup(server) { console.log( ("Ghost is running in " + process.env.NODE_ENV + "...").green, "\nListening on", - getSocket() || ghost.config().server.host + ':' + ghost.config().server.port, + getSocket() || config().server.host + ':' + config().server.port, "\nUrl configured as:", - ghost.config().url, + config().url, "\nCtrl+C to shut down".grey ); // ensure that Ghost exits correctly on Ctrl+C @@ -144,8 +145,8 @@ function setup(server) { } else { server.listen( - ghost.config().server.port, - ghost.config().server.host, + config().server.port, + config().server.host, startGhost ); } diff --git a/core/server/config/index.js b/core/server/config/index.js new file mode 100644 index 0000000000..253c9a5f7a --- /dev/null +++ b/core/server/config/index.js @@ -0,0 +1,16 @@ + +var ghostConfig = require('../../../config'), + loader = require('./loader'), + paths = require('./paths'); + + +function configIndex() { + return ghostConfig[process.env.NODE_ENV]; +} + + +configIndex.loader = loader; +configIndex.paths = paths; + + +module.exports = configIndex; \ No newline at end of file diff --git a/core/config-loader.js b/core/server/config/loader.js similarity index 94% rename from core/config-loader.js rename to core/server/config/loader.js index b211175785..d913de7005 100644 --- a/core/config-loader.js +++ b/core/server/config/loader.js @@ -1,10 +1,10 @@ var fs = require('fs'), url = require('url'), when = require('when'), - errors = require('./server/errorHandling'), + errors = require('../errorHandling'), path = require('path'), - appRoot = path.resolve(__dirname, '../'), + appRoot = path.resolve(__dirname, '../../../'), configexample = path.join(appRoot, 'config.example.js'), config = path.join(appRoot, 'config.js'); @@ -49,7 +49,7 @@ function validateConfigEnvironment() { parsedUrl; try { - config = require('../config')[envVal]; + config = require('../../../config')[envVal]; } catch (ignore) { } @@ -86,7 +86,7 @@ function validateConfigEnvironment() { return when.resolve(); } -exports.loadConfig = function () { +function loadConfig() { var loaded = when.defer(); /* Check for config file and copy from config.example.js if one doesn't exist. After that, start the server. */ @@ -98,4 +98,6 @@ exports.loadConfig = function () { } }); return loaded.promise; -}; +} + +module.exports = loadConfig; diff --git a/core/server/config/paths.js b/core/server/config/paths.js new file mode 100644 index 0000000000..b5728a560c --- /dev/null +++ b/core/server/config/paths.js @@ -0,0 +1,49 @@ + + +var path = require('path'), + when = require('when'), + requireTree = require('../require-tree'), + appRoot = path.resolve(__dirname, '../../../'), + themePath = path.resolve(appRoot + '/content/themes'), + pluginPath = path.resolve(appRoot + '/content/plugins'), + themeDirectories = requireTree(themePath), + pluginDirectories = requireTree(pluginPath), + activeTheme = '', + availableThemes, + availablePlugins; + + +function getPaths() { + return { + 'appRoot': appRoot, + 'themePath': themePath, + 'pluginPath': pluginPath, + 'activeTheme': path.join(themePath, activeTheme), + 'adminViews': path.join(appRoot, '/core/server/views/'), + 'helperTemplates': path.join(appRoot, '/core/server/helpers/tpl/'), + 'lang': path.join(appRoot, '/core/shared/lang/'), + 'availableThemes': availableThemes, + 'availablePlugins': availablePlugins + }; +} + + +function updatePaths() { + return when.all([themeDirectories, pluginDirectories]).then(function (paths) { + availableThemes = paths[0]; + availablePlugins = paths[1]; + return; + }); +} + +function setActiveTheme(ghost) { + if (ghost && ghost.settingsCache) { + activeTheme = ghost.settingsCache.activeTheme.value; + } +} + +module.exports = getPaths; + +module.exports.updatePaths = updatePaths; + +module.exports.setActiveTheme = setActiveTheme; \ No newline at end of file diff --git a/core/server/controllers/admin.js b/core/server/controllers/admin.js index c931fc3955..efabc39cd6 100644 --- a/core/server/controllers/admin.js +++ b/core/server/controllers/admin.js @@ -1,4 +1,5 @@ var Ghost = require('../../ghost'), + config = require('../config'), _ = require('underscore'), path = require('path'), when = require('when'), @@ -154,8 +155,8 @@ adminControllers = { var email = req.body.email; api.users.generateResetToken(email).then(function (token) { - var siteLink = '' + ghost.config().url + '', - resetUrl = ghost.config().url + '/ghost/reset/' + token + '/', + var siteLink = '' + config().url + '', + resetUrl = config().url + '/ghost/reset/' + token + '/', resetLink = '' + resetUrl + '', message = { to: email, diff --git a/core/server/controllers/frontend.js b/core/server/controllers/frontend.js index 51df1424f4..dfe7c1b0d7 100644 --- a/core/server/controllers/frontend.js +++ b/core/server/controllers/frontend.js @@ -5,6 +5,7 @@ /*global require, module */ var Ghost = require('../../ghost'), + config = require('../config'), api = require('../api'), RSS = require('rss'), _ = require('underscore'), @@ -68,7 +69,7 @@ frontendControllers = { api.posts.read(_.pick(req.params, ['id', 'slug'])).then(function (post) { if (post) { ghost.doFilter('prePostsRender', post).then(function (post) { - var paths = ghost.paths().availableThemes[ghost.settings('activeTheme')]; + var paths = config.paths().availableThemes[ghost.settings('activeTheme')]; if (post.page && paths.hasOwnProperty('page')) { res.render('page', {post: post}); } else { @@ -87,7 +88,7 @@ frontendControllers = { }, 'rss': function (req, res, next) { // Initialize RSS - var siteUrl = ghost.config().url, + var siteUrl = config().url, pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1, feed; //needs refact for multi user to not use first user as default diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 8be120e175..8417d5c6d2 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -97,7 +97,7 @@ coreHelpers.url = function (options) { output += blog.url; } - if (blog.path !== '/') { + if (blog.path && blog.path !== '/') { output += blog.path; } @@ -219,8 +219,8 @@ coreHelpers.excerpt = function (options) { // Returns the config value for fileStorage. coreHelpers.fileStorage = function (context, options) { /*jslint unparam:true*/ - if (coreHelpers.ghost.config().hasOwnProperty('fileStorage')) { - return coreHelpers.ghost.config().fileStorage.toString(); + if (coreHelpers.config().hasOwnProperty('fileStorage')) { + return coreHelpers.config().fileStorage.toString(); } return "true"; }; @@ -531,12 +531,15 @@ coreHelpers.helperMissing = function (arg) { errors.logError('Missing helper: "' + arg + '"'); }; -registerHelpers = function (ghost) { +registerHelpers = function (ghost, config) { var paginationHelper; // Expose this so our helpers can use it in their code. coreHelpers.ghost = ghost; + // And expose config + coreHelpers.config = config; + ghost.registerThemeHelper('date', coreHelpers.date); ghost.registerThemeHelper('encode', coreHelpers.encode); diff --git a/core/server/mail.js b/core/server/mail.js index a944b22390..b68da03785 100644 --- a/core/server/mail.js +++ b/core/server/mail.js @@ -12,13 +12,16 @@ function GhostMailer(opts) { // ## E-mail transport setup // *This promise should always resolve to avoid halting Ghost::init*. -GhostMailer.prototype.init = function (ghost) { +GhostMailer.prototype.init = function (ghost, configModule) { this.ghost = ghost; // TODO: fix circular reference ghost -> mail -> api -> ghost, remove this late require this.api = require('./api'); + // We currently pass in the config module to avoid + // circular references, similar to above. + this.config = configModule; var self = this, - config = ghost.config(); + config = this.config(); if (config.mail && config.mail.transport && config.mail.options) { this.createTransport(config); @@ -95,7 +98,7 @@ GhostMailer.prototype.send = function (message) { return when.reject(new Error('Email Error: Incomplete message data.')); } - var from = this.ghost.config().mail.fromaddress || this.ghost.settings('email'), + var from = this.config().mail.fromaddress || this.ghost.settings('email'), to = message.to || this.ghost.settings('email'), sendMail = nodefn.lift(this.transport.sendMail.bind(this.transport)); diff --git a/core/server/middleware/index.js b/core/server/middleware/index.js index d0c159d7a6..61fb1fcb65 100644 --- a/core/server/middleware/index.js +++ b/core/server/middleware/index.js @@ -7,6 +7,7 @@ var middleware = require('./middleware'), path = require('path'), hbs = require('express-hbs'), Ghost = require('../../ghost'), + config = require('../config'), storage = require('../storage'), packageInfo = require('../../../package.json'), BSStore = require('../../bookshelf-session'), @@ -62,16 +63,16 @@ function initViews(req, res, next) { // self.globals is a hack til we have a better way of getting combined settings & config hbsOptions = {templateOptions: {data: {blog: ghost.blogGlobals()}}}; - if (ghost.themeDirectories[ghost.settings('activeTheme')].hasOwnProperty('partials')) { + if (config.paths().availableThemes[ghost.settings('activeTheme')].hasOwnProperty('partials')) { // Check that the theme has a partials directory before trying to use it - hbsOptions.partialsDir = path.join(ghost.paths().activeTheme, 'partials'); + hbsOptions.partialsDir = path.join(config.paths().activeTheme, 'partials'); } ghost.server.engine('hbs', hbs.express3(hbsOptions)); - ghost.server.set('views', ghost.paths().activeTheme); + ghost.server.set('views', config.paths().activeTheme); } else { - ghost.server.engine('hbs', hbs.express3({partialsDir: ghost.paths().adminViews + 'partials'})); - ghost.server.set('views', ghost.paths().adminViews); + ghost.server.engine('hbs', hbs.express3({partialsDir: config.paths().adminViews + 'partials'})); + ghost.server.set('views', config.paths().adminViews); } next(); @@ -90,7 +91,7 @@ function activateTheme() { ghost.server.set('activeTheme', ghost.settings('activeTheme')); ghost.server.enable(ghost.server.get('activeTheme')); if (stackLocation) { - ghost.server.stack[stackLocation].handle = middleware.whenEnabled(ghost.server.get('activeTheme'), middleware.staticTheme(ghost)); + ghost.server.stack[stackLocation].handle = middleware.whenEnabled(ghost.server.get('activeTheme'), middleware.staticTheme()); } // Update user error template @@ -119,7 +120,7 @@ function manageAdminAndTheme(req, res, next) { // Check if the theme changed if (ghost.settings('activeTheme') !== ghost.server.get('activeTheme')) { // Change theme - if (!ghost.themeDirectories.hasOwnProperty(ghost.settings('activeTheme'))) { + if (!config.paths().availableThemes.hasOwnProperty(ghost.settings('activeTheme'))) { if (!res.isAdmin) { // Throw an error if the theme is not available, but not on the admin UI errors.logAndThrowError('The currently active theme ' + ghost.settings('activeTheme') + ' is missing.'); @@ -135,7 +136,7 @@ function manageAdminAndTheme(req, res, next) { module.exports = function (server) { var oneYear = 31536000000, root = ghost.blogGlobals().path === '/' ? '' : ghost.blogGlobals().path, - corePath = path.join(ghost.paths().appRoot, 'core'); + corePath = path.join(config.paths().appRoot, 'core'); // Logging configuration if (server.get('env') !== 'development') { @@ -165,7 +166,7 @@ module.exports = function (server) { server.use(root + '/ghost', middleware.whenEnabled('admin', express['static'](path.join(corePath, '/client/assets')))); // Theme only config - server.use(middleware.whenEnabled(server.get('activeTheme'), middleware.staticTheme(ghost))); + server.use(middleware.whenEnabled(server.get('activeTheme'), middleware.staticTheme())); // Add in all trailing slashes server.use(slashes()); diff --git a/core/server/middleware/middleware.js b/core/server/middleware/middleware.js index ee91746e48..aa28f126b2 100644 --- a/core/server/middleware/middleware.js +++ b/core/server/middleware/middleware.js @@ -2,6 +2,7 @@ var _ = require('underscore'), express = require('express'), Ghost = require('../../ghost'), + config = require('../config'), path = require('path'), ghost = new Ghost(); @@ -100,20 +101,19 @@ var middleware = { }; }, - staticTheme: function (g) { - var ghost = g; + staticTheme: function () { return function blackListStatic(req, res, next) { if (isBlackListedFileType(req.url)) { return next(); } - return middleware.forwardToExpressStatic(ghost, req, res, next); + return middleware.forwardToExpressStatic(req, res, next); }; }, // to allow unit testing - forwardToExpressStatic: function (ghost, req, res, next) { - return express['static'](ghost.paths().activeTheme)(req, res, next); + forwardToExpressStatic: function (req, res, next) { + return express['static'](config.paths().activeTheme)(req, res, next); } }; diff --git a/core/server/plugins/loader.js b/core/server/plugins/loader.js index cb40a3249e..632fd8ea0a 100644 --- a/core/server/plugins/loader.js +++ b/core/server/plugins/loader.js @@ -3,6 +3,7 @@ var path = require('path'), _ = require('underscore'), when = require('when'), createProxy = require('./proxy'), + config = require('../config'), ghostInstance, loader; @@ -24,7 +25,7 @@ function getPluginRelativePath(name, relativeTo, ghost) { ghost = ghost || getGhostInstance(); relativeTo = relativeTo || __dirname; - return path.relative(relativeTo, path.join(ghost.paths().pluginPath, name)); + return path.relative(relativeTo, path.join(config.paths().pluginPath, name)); } diff --git a/core/shared/lang/i18n.js b/core/shared/lang/i18n.js index 3eb8a4c69d..ec7dca9afa 100644 --- a/core/shared/lang/i18n.js +++ b/core/shared/lang/i18n.js @@ -1,4 +1,5 @@ -var fs = require('fs'), +var fs = require('fs'), + config = require('../../server/config'), /** * Create new Polyglot object * @type {Polyglot} @@ -9,7 +10,7 @@ I18n = function (ghost) { // TODO: validate var lang = ghost.settings('defaultLang'), - path = ghost.paths().lang, + path = config.paths().lang, langFilePath = path + lang + '.json'; return function (req, res, next) { diff --git a/core/test/unit/ghost_spec.js b/core/test/unit/ghost_spec.js index a42e1a67ac..3b12df68e5 100644 --- a/core/test/unit/ghost_spec.js +++ b/core/test/unit/ghost_spec.js @@ -7,7 +7,8 @@ var testUtils = require('../utils'), _ = require('underscore'), // Stuff we are testing - Ghost = require('../../ghost'); + config = require('../../server/config'), + Ghost = require('../../ghost'); describe("Ghost API", function () { var testTemplatePath = 'core/test/utils/fixtures/', @@ -177,7 +178,7 @@ describe("Ghost API", function () { should.exist(ghost.loadTemplate, 'load template function exists'); // In order for the test to work, need to replace the path to the template - pathsStub = sandbox.stub(ghost, "paths", function () { + pathsStub = sandbox.stub(config, "paths", function () { return { // Forcing the theme path to be the same activeTheme: path.join(process.cwd(), testTemplatePath), @@ -209,7 +210,7 @@ describe("Ghost API", function () { should.exist(ghost.loadTemplate, 'load template function exists'); // In order for the test to work, need to replace the path to the template - pathsStub = sandbox.stub(ghost, "paths", function () { + pathsStub = sandbox.stub(config, "paths", function () { return { activeTheme: path.join(process.cwd(), themeTemplatePath), helperTemplates: path.join(process.cwd(), testTemplatePath) diff --git a/core/test/unit/mail_spec.js b/core/test/unit/mail_spec.js index 5651e3a098..bd765070cb 100644 --- a/core/test/unit/mail_spec.js +++ b/core/test/unit/mail_spec.js @@ -16,7 +16,8 @@ var testUtils = require('../utils'), fakeSettings, fakeSendmail, sandbox = sinon.sandbox.create(), - ghost; + ghost, + config; // Mock SMTP config SMTP = { @@ -51,9 +52,7 @@ describe("Mail", function () { ghost = new Ghost(); - sandbox.stub(ghost, "config", function () { - return fakeConfig; - }); + config = sinon.stub().returns(fakeConfig); sandbox.stub(ghost, "settings", function () { return fakeSettings; @@ -81,7 +80,7 @@ describe("Mail", function () { it('should setup SMTP transport on initialization', function (done) { fakeConfig.mail = SMTP; - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { ghost.mail.should.have.property('transport'); ghost.mail.transport.transportType.should.eql('SMTP'); ghost.mail.transport.sendMail.should.be.a.function; @@ -91,7 +90,7 @@ describe("Mail", function () { it('should setup sendmail transport on initialization', function (done) { fakeConfig.mail = SENDMAIL; - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { ghost.mail.should.have.property('transport'); ghost.mail.transport.transportType.should.eql('SENDMAIL'); ghost.mail.transport.sendMail.should.be.a.function; @@ -101,7 +100,7 @@ describe("Mail", function () { it('should fallback to sendmail if no config set', function (done) { fakeConfig.mail = null; - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { ghost.mail.should.have.property('transport'); ghost.mail.transport.transportType.should.eql('SENDMAIL'); ghost.mail.transport.options.path.should.eql(fakeSendmail); @@ -111,7 +110,7 @@ describe("Mail", function () { it('should fallback to sendmail if config is empty', function (done) { fakeConfig.mail = {}; - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { ghost.mail.should.have.property('transport'); ghost.mail.transport.transportType.should.eql('SENDMAIL'); ghost.mail.transport.options.path.should.eql(fakeSendmail); @@ -123,7 +122,7 @@ describe("Mail", function () { fakeConfig.mail = {}; ghost.mail.detectSendmail.restore(); sandbox.stub(ghost.mail, "detectSendmail", when.reject); - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { should.not.exist(ghost.mail.transport); done(); }).then(null, done); @@ -136,7 +135,7 @@ describe("Mail", function () { sandbox.stub(ghost.mail, 'isWindows', function () { return true; }); - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { should.not.exist(ghost.mail.transport); done(); }).then(null, done); @@ -145,7 +144,7 @@ describe("Mail", function () { it('should fail to send messages when no transport is set', function (done) { ghost.mail.detectSendmail.restore(); sandbox.stub(ghost.mail, "detectSendmail", when.reject); - ghost.mail.init(ghost).then(function () { + ghost.mail.init(ghost, config).then(function () { ghost.mail.send().then(function () { should.fail(); done(); diff --git a/core/test/unit/middleware_spec.js b/core/test/unit/middleware_spec.js index 2aa65c6bda..f55d1923dc 100644 --- a/core/test/unit/middleware_spec.js +++ b/core/test/unit/middleware_spec.js @@ -277,9 +277,9 @@ describe('Middleware', function () { url: 'myvalidfile.css' }; - middleware.staticTheme(ghostStub)(req, null, function (req, res, next) { + middleware.staticTheme(null)(req, null, function (reqArg, res, next) { middleware.forwardToExpressStatic.calledOnce.should.be.true; - assert.deepEqual(middleware.forwardToExpressStatic.args[0][0], ghostStub); + assert.deepEqual(middleware.forwardToExpressStatic.args[0][0], req); return done(); }); }); diff --git a/core/test/unit/server_helpers_index_spec.js b/core/test/unit/server_helpers_index_spec.js index 7d795eba68..c9ca6244cf 100644 --- a/core/test/unit/server_helpers_index_spec.js +++ b/core/test/unit/server_helpers_index_spec.js @@ -283,7 +283,7 @@ describe('Core Helpers', function () { }); it('should output an absolute URL if the option is present', function () { - var configStub = sinon.stub(ghost, "config", function () { + var configStub = sinon.stub(ghost, "blogGlobals", function () { return { url: 'http://testurl.com' }; }), @@ -580,4 +580,4 @@ describe('Core Helpers', function () { }); }); -}); +}); \ No newline at end of file diff --git a/index.js b/index.js index 8a4b2e7f09..9f7c4e7121 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,12 @@ // Orchestrates the loading of Ghost // When run from command line. -var configLoader = require('./core/config-loader.js'), - errors = require('./core/server/errorHandling'); +var configLoader = require('./core/server/config/loader'), + errors = require('./core/server/errorHandling'); process.env.NODE_ENV = process.env.NODE_ENV || 'development'; -configLoader.loadConfig().then(function () { +configLoader().then(function () { var ghost = require('./core/server'); ghost(); }).otherwise(errors.logAndThrowError); \ No newline at end of file