diff --git a/app/config.js b/app/config.js index 3b66bbb..65bcae8 100644 --- a/app/config.js +++ b/app/config.js @@ -1,64 +1,69 @@ 'use strict'; -var Steppy = require('twostep').Steppy, - _ = require('underscore'), - validateConfig = require('../lib/validateConfig'), +var fs = require('fs'), path = require('path'), - fs = require('fs'); + _ = require('underscore'), + Steppy = require('twostep').Steppy, + validateConfig = require('../lib/validateConfig'); + +var preload = function(app, preloadPath) { + var preloadConfig = null; + try { + preloadConfig = app.require(preloadPath); + } catch(error) { + if (error.code !== 'MODULE_NOT_FOUND') throw error; + } + + // register preloadable plugins + if (preloadConfig) { + app.loadPlugins(preloadConfig.plugins); + } +}; module.exports = function(params, callback) { - var config = {}; var configDefaults = { notify: {}, - http: {host: '127.0.0.1', port: 3000, url: 'http://127.0.0.1:3000'} + http: { + host: '127.0.0.1', + port: 3000, + url: 'http://127.0.0.1:3000' + } }; + var paths = {}; + Steppy( function() { - config.paths = {}; + // path to cwd + paths.cwd = process.cwd(); - // path to root dir (with projects, builds etc) - config.paths.data = path.join(process.cwd(), 'data'); + // path to data dir (with projects, builds etc) + paths.data = path.join(paths.cwd, 'data'); - config.paths.preload = path.join( - config.paths.data, - 'preload.json' - ); + // path to preload.json file with preloadable plugins list + paths.preload = path.join(paths.data, 'preload.json'); - var preloadExistsCallback = this.slot(); - fs.exists(config.paths.preload, function(isExists) { - preloadExistsCallback(null, isExists); - }); - }, - function(err, isPreloadExists) { - // preload plugins before read config file coz maybe reader - // plugins will be loaded - if (isPreloadExists) { - var preload = require(config.paths.preload); - // register preloaded plugins - _(preload.plugins).each(function(plugin) { - params.logger.log('Preload plugin "%s"', plugin); - require(plugin).register(params.app); - }); - } + // preload plugins first coz reader plugins could be loaded + preload(params.app, paths.preload); - params.reader.load(config.paths.data, 'config', this.slot()); + // read config with reader help + params.reader.load(paths.data, 'config', this.slot()); }, - function(err, fileConfig) { - validateConfig(fileConfig, this.slot()); + function(err, config) { + validateConfig(config, this.slot()); }, - function(err, fileConfig) { - _(config).defaults(fileConfig); - _(config).defaults(configDefaults); - + function(err, config) { // try to read db and projects paths from config or set default values - _(config.paths).defaults(fileConfig.paths, { - db: path.join(config.paths.data, 'db'), - projects: path.join(config.paths.data, 'projects'), - archivedProjects: path.join(config.paths.data, 'archivedProjects') + _(paths).defaults(config.paths, { + db: path.join(paths.data, 'db'), + projects: path.join(paths.data, 'projects'), + archivedProjects: path.join(paths.data, 'archivedProjects') }); + // combine all parts together + config = _({paths: paths}).defaults(config, configDefaults); + this.pass(config); }, callback diff --git a/app/index.js b/app/index.js index a68a06e..2e69fa1 100644 --- a/app/index.js +++ b/app/index.js @@ -1,17 +1,20 @@ 'use strict'; -var env = process.env.NODE_ENV || 'development', - db = require('./db'), - fs = require('fs'), - Steppy = require('twostep').Steppy, +var fs = require('fs'), + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits, _ = require('underscore'), + Steppy = require('twostep').Steppy, + importCwd = require('req-cwd'), + db = require('./db'), Reader = require('../lib/reader').Reader, Notifier = require('../lib/notifier').Notifier, ProjectsCollection = require('../lib/project').ProjectsCollection, BuildsCollection = require('../lib/build').BuildsCollection, - EventEmitter = require('events').EventEmitter, - utils = require('../lib/utils'), - inherits = require('util').inherits; + utils = require('../lib/utils'); + +var env = process.env.NODE_ENV || 'development'; +var cwd = process.cwd(); function App(params) { params = params || {}; @@ -20,6 +23,18 @@ function App(params) { inherits(App, EventEmitter); +App.prototype.require = function(id) { + return importCwd(id); +}; + +App.prototype.loadPlugins = function(plugins) { + var self = this; + _(plugins).each(function(plugin) { + self.logger.log('Load plugin "%s"', plugin); + self.require(plugin).register(self); + }); +}; + App.prototype.init = function(callback) { var self = this; @@ -43,7 +58,7 @@ App.prototype.init = function(callback) { function(err, config) { self.config = config; - self.logger.log('Server config:', utils.toPrettyJson(self.config)); + self.logger.log('Server config:\n%s', utils.toPrettyJson(self.config)); var dbDirExistsCallback = this.slot(); fs.exists(self.config.paths.db, function(isExists) { @@ -69,7 +84,7 @@ App.prototype.init = function(callback) { } }, function() { - var dbBackend = require(self.config.storage.backend); + var dbBackend = self.require(self.config.storage.backend); // monkey patch memdown to allow save empty strings which is correct // at general but occasionally not allowed at _checkKey @@ -106,10 +121,7 @@ App.prototype.init = function(callback) { self.builds.completeUncompleted({logger: self.logger}, this.slot()); // register other plugins - _(self.config.plugins).each(function(plugin) { - self.logger.log('Load plugin "%s"', plugin); - require(plugin).register(self); - }); + self.loadPlugins(self.config.plugins); distributor.init(); @@ -145,7 +157,6 @@ App.prototype.init = function(callback) { }, callback ); - }; App.prototype.listen = function() { diff --git a/package.json b/package.json index d3c9d0a..3e9f7d8 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "dependencies": { "colors": "1.1.2", "conform": "0.2.12", + "req-cwd": "1.0.1", "junk": "1.0.3", "nlevel": "1.0.3", "through": "2.3.6",