Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow development with npm link #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 45 additions & 40 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 25 additions & 14 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -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 || {};
Expand All @@ -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;

Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -145,7 +157,6 @@ App.prototype.init = function(callback) {
},
callback
);

};

App.prototype.listen = function() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down