From 42c5080ed5687b58b995c7d455bbfd9be869e431 Mon Sep 17 00:00:00 2001 From: Artem Zhukov Date: Fri, 11 Aug 2017 02:02:55 +0300 Subject: [PATCH 1/4] require plugins relative with https://github.com/kamicane/require-relative, some config.js refactoring --- app/config.js | 85 +++++++++++++++++++++++++++------------------------ app/index.js | 39 ++++++++++++++--------- package.json | 1 + 3 files changed, 71 insertions(+), 54 deletions(-) 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..a8bf3cb 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, + requireRelative = require('require-relative'), + 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 requireRelative(id, cwd); +}; + +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..5ef84c7 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "conform": "0.2.12", "junk": "1.0.3", "nlevel": "1.0.3", + "require-relative": "0.8.7", "through": "2.3.6", "tree-kill": "1.1.0", "twostep": "0.4.2", From 3bfbae8f15c7e604b401c438412175eab4c26c1f Mon Sep 17 00:00:00 2001 From: Artem Zhukov Date: Mon, 14 Aug 2017 02:04:09 +0300 Subject: [PATCH 2/4] change package require-relative to import-cwd --- app/index.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/index.js b/app/index.js index a8bf3cb..39fb4e1 100644 --- a/app/index.js +++ b/app/index.js @@ -5,7 +5,7 @@ var fs = require('fs'), inherits = require('util').inherits, _ = require('underscore'), Steppy = require('twostep').Steppy, - requireRelative = require('require-relative'), + importCwd = require('import-cwd'), db = require('./db'), Reader = require('../lib/reader').Reader, Notifier = require('../lib/notifier').Notifier, @@ -24,7 +24,7 @@ function App(params) { inherits(App, EventEmitter); App.prototype.require = function(id) { - return requireRelative(id, cwd); + return importCwd(id); }; App.prototype.loadPlugins = function(plugins) { diff --git a/package.json b/package.json index 5ef84c7..1e46c1c 100644 --- a/package.json +++ b/package.json @@ -49,9 +49,9 @@ "dependencies": { "colors": "1.1.2", "conform": "0.2.12", + "import-cwd": "2.1.0", "junk": "1.0.3", "nlevel": "1.0.3", - "require-relative": "0.8.7", "through": "2.3.6", "tree-kill": "1.1.0", "twostep": "0.4.2", From db01dff3eccbfa7f5e30c6ad2292c4f44151ba1c Mon Sep 17 00:00:00 2001 From: Artem Zhukov Date: Mon, 14 Aug 2017 22:04:51 +0300 Subject: [PATCH 3/4] rollback import-cwd version to 1.0.1 because we need to support legacy node.js without arrow functions =( --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1e46c1c..277a2d0 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "dependencies": { "colors": "1.1.2", "conform": "0.2.12", - "import-cwd": "2.1.0", + "import-cwd": "1.0.1", "junk": "1.0.3", "nlevel": "1.0.3", "through": "2.3.6", From 0ffc1f868828e983a8b3f24afdcf84a7ea5942de Mon Sep 17 00:00:00 2001 From: Artem Zhukov Date: Mon, 14 Aug 2017 22:08:19 +0300 Subject: [PATCH 4/4] also previous version has name req-cwd instead of import-cwd --- app/index.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/index.js b/app/index.js index 39fb4e1..2e69fa1 100644 --- a/app/index.js +++ b/app/index.js @@ -5,7 +5,7 @@ var fs = require('fs'), inherits = require('util').inherits, _ = require('underscore'), Steppy = require('twostep').Steppy, - importCwd = require('import-cwd'), + importCwd = require('req-cwd'), db = require('./db'), Reader = require('../lib/reader').Reader, Notifier = require('../lib/notifier').Notifier, diff --git a/package.json b/package.json index 277a2d0..3e9f7d8 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "dependencies": { "colors": "1.1.2", "conform": "0.2.12", - "import-cwd": "1.0.1", + "req-cwd": "1.0.1", "junk": "1.0.3", "nlevel": "1.0.3", "through": "2.3.6",