From 89154ad9976f76872c1ac3a33aafcab675f5a0f8 Mon Sep 17 00:00:00 2001 From: Harry Wolff Date: Mon, 25 Nov 2013 22:22:59 -0500 Subject: [PATCH] Restore support for using ghost as a npm module fixes #1326 --- Gruntfile.js | 4 ++-- core/index.js | 17 ++++++++++++++ core/{ => server}/bookshelf-session.js | 0 core/server/config/index.js | 32 ++++++++++++++++++-------- core/server/config/loader.js | 19 +++++++-------- core/server/config/paths.js | 2 ++ core/{server.js => server/index.js} | 16 ++++++------- core/server/middleware/index.js | 2 +- core/server/models/base.js | 6 ++--- core/server/models/post.js | 1 - index.js | 10 ++------ package.json | 2 +- 12 files changed, 68 insertions(+), 43 deletions(-) create mode 100644 core/index.js rename core/{ => server}/bookshelf-session.js (100%) rename core/{server.js => server/index.js} (93%) diff --git a/Gruntfile.js b/Gruntfile.js index b1d17a3010..4f53f35e4f 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/server/config/loader'), + config = require('./core/server/config'), buildGlob = [ '**', @@ -499,7 +499,7 @@ var path = require('path'), grunt.registerTask('loadConfig', function () { var done = this.async(); - configLoader().then(function () { + config.load().then(function () { done(); }); }); diff --git a/core/index.js b/core/index.js new file mode 100644 index 0000000000..0872f131f2 --- /dev/null +++ b/core/index.js @@ -0,0 +1,17 @@ +// # Ghost bootloader +// Orchestrates the loading of Ghost +// When run from command line. + +var config = require('./server/config'), + errors = require('./server/errorHandling'); + +process.env.NODE_ENV = process.env.NODE_ENV || 'development'; + +function startGhost(app) { + config.load().then(function () { + var ghost = require('./server'); + ghost(app); + }).otherwise(errors.logAndThrowError); +} + +module.exports = startGhost; \ No newline at end of file diff --git a/core/bookshelf-session.js b/core/server/bookshelf-session.js similarity index 100% rename from core/bookshelf-session.js rename to core/server/bookshelf-session.js diff --git a/core/server/config/index.js b/core/server/config/index.js index 253c9a5f7a..8b785b9e57 100644 --- a/core/server/config/index.js +++ b/core/server/config/index.js @@ -1,16 +1,28 @@ -var ghostConfig = require('../../../config'), - loader = require('./loader'), - paths = require('./paths'); +var loader = require('./loader'), + paths = require('./paths'), + ghostConfig; - -function configIndex() { - return ghostConfig[process.env.NODE_ENV]; +// Returns NODE_ENV config object +function config() { + // @TODO: get rid of require statement. + // This is currently needed for tests to load config file + // successfully. While running application we should never + // have to directly delegate to the config.js file. + return ghostConfig || require(paths().config)[process.env.NODE_ENV]; } +function loadConfig() { + return loader().then(function (config) { + // Cache the config.js object's environment + // object so we can later refer to it. + // Note: this is not the entirity of config.js, + // just the object appropriate for this NODE_ENV + ghostConfig = config; + }); +} -configIndex.loader = loader; -configIndex.paths = paths; +config.load = loadConfig; +config.paths = paths; - -module.exports = configIndex; \ No newline at end of file +module.exports = config; \ No newline at end of file diff --git a/core/server/config/loader.js b/core/server/config/loader.js index d913de7005..47f87ea038 100644 --- a/core/server/config/loader.js +++ b/core/server/config/loader.js @@ -3,10 +3,11 @@ var fs = require('fs'), when = require('when'), errors = require('../errorHandling'), path = require('path'), + paths = require('./paths'), - appRoot = path.resolve(__dirname, '../../../'), - configexample = path.join(appRoot, 'config.example.js'), - config = path.join(appRoot, 'config.js'); + appRoot = paths().appRoot, + configexample = paths().configExample, + config = paths().config; function writeConfigFile() { var written = when.defer(); @@ -83,19 +84,19 @@ function validateConfigEnvironment() { return when.reject(); } - return when.resolve(); + return when.resolve(config); } function loadConfig() { - var loaded = when.defer(); + var loaded = when.defer(), + pendingConfig; /* Check for config file and copy from config.example.js if one doesn't exist. After that, start the server. */ fs.exists(config, function checkConfig(configExists) { - if (configExists) { - validateConfigEnvironment().then(loaded.resolve).otherwise(loaded.reject); - } else { - writeConfigFile().then(validateConfigEnvironment).then(loaded.resolve).otherwise(loaded.reject); + if (!configExists) { + pendingConfig = writeConfigFile(); } + when(pendingConfig).then(validateConfigEnvironment).then(loaded.resolve).otherwise(loaded.reject); }); return loaded.promise; } diff --git a/core/server/config/paths.js b/core/server/config/paths.js index b5728a560c..b35331eb17 100644 --- a/core/server/config/paths.js +++ b/core/server/config/paths.js @@ -16,6 +16,8 @@ var path = require('path'), function getPaths() { return { 'appRoot': appRoot, + 'config': path.join(appRoot, 'config.js'), + 'configExample': path.join(appRoot, 'config.example.js'), 'themePath': themePath, 'pluginPath': pluginPath, 'activeTheme': path.join(themePath, activeTheme), diff --git a/core/server.js b/core/server/index.js similarity index 93% rename from core/server.js rename to core/server/index.js index 26c4771abb..fd1eca1bbc 100644 --- a/core/server.js +++ b/core/server/index.js @@ -3,20 +3,20 @@ // modules to ensure config gets right setting. // Module dependencies -var config = require('./server/config'), +var config = require('./config'), express = require('express'), when = require('when'), _ = require('underscore'), semver = require('semver'), fs = require('fs'), - errors = require('./server/errorHandling'), - plugins = require('./server/plugins'), + errors = require('./errorHandling'), + plugins = require('./plugins'), path = require('path'), - Ghost = require('./ghost'), - helpers = require('./server/helpers'), - middleware = require('./server/middleware'), - routes = require('./server/routes'), - packageInfo = require('../package.json'), + Ghost = require('../ghost'), + helpers = require('./helpers'), + middleware = require('./middleware'), + routes = require('./routes'), + packageInfo = require('../../package.json'), // Variables ghost = new Ghost(), diff --git a/core/server/middleware/index.js b/core/server/middleware/index.js index 3f492db957..2763c4133e 100644 --- a/core/server/middleware/index.js +++ b/core/server/middleware/index.js @@ -14,7 +14,7 @@ var middleware = require('./middleware'), config = require('../config'), storage = require('../storage'), packageInfo = require('../../../package.json'), - BSStore = require('../../bookshelf-session'), + BSStore = require('../bookshelf-session'), ghost = new Ghost(); diff --git a/core/server/models/base.js b/core/server/models/base.js index 6a3b671cf3..2d2df713fd 100644 --- a/core/server/models/base.js +++ b/core/server/models/base.js @@ -4,14 +4,14 @@ var ghostBookshelf, moment = require('moment'), _ = require('underscore'), uuid = require('node-uuid'), - config = require('../../../config'), + config = require('../config'), Validator = require('validator').Validator, unidecode = require('unidecode'), sanitize = require('validator').sanitize; // Initializes a new Bookshelf instance, for reference elsewhere in Ghost. -ghostBookshelf = Bookshelf.ghost = Bookshelf.initialize(config[process.env.NODE_ENV].database); -ghostBookshelf.client = config[process.env.NODE_ENV].database.client; +ghostBookshelf = Bookshelf.ghost = Bookshelf.initialize(config().database); +ghostBookshelf.client = config().database.client; ghostBookshelf.validator = new Validator(); diff --git a/core/server/models/post.js b/core/server/models/post.js index df179a21ec..b471f51568 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -8,7 +8,6 @@ var Post, github = require('../../shared/vendor/showdown/extensions/github'), converter = new Showdown.converter({extensions: [github]}), User = require('./user').User, - config = require('../../../config'), Tag = require('./tag').Tag, Tags = require('./tag').Tags, ghostBookshelf = require('./base'); diff --git a/index.js b/index.js index 9f7c4e7121..6fb52b3b6d 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,6 @@ // Orchestrates the loading of Ghost // When run from command line. -var configLoader = require('./core/server/config/loader'), - errors = require('./core/server/errorHandling'); +var ghost = require('./core'); -process.env.NODE_ENV = process.env.NODE_ENV || 'development'; - -configLoader().then(function () { - var ghost = require('./core/server'); - ghost(); -}).otherwise(errors.logAndThrowError); \ No newline at end of file +ghost(); \ No newline at end of file diff --git a/package.json b/package.json index c37b3fca7e..6d16c4af9b 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "url": "https://raw.github.com/TryGhost/Ghost/master/LICENSE" } ], - "main": "./core/server", + "main": "./core/index", "scripts": { "start": "node index", "test": "grunt validate --verbose"