From 1464aa9d602b534820bb756cd5b19b524e10ec56 Mon Sep 17 00:00:00 2001 From: turtleDev Date: Sat, 17 Sep 2016 22:49:56 +0530 Subject: [PATCH 1/2] add controllers plugin --- app/routes/auth.js | 7 +---- app/routes/core.js | 7 +---- app/routes/pages.js | 6 +---- config/manifest.js | 9 +++++++ lib/controllers.js | 63 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 lib/controllers.js diff --git a/app/routes/auth.js b/app/routes/auth.js index 9d59861..8f4b66e 100644 --- a/app/routes/auth.js +++ b/app/routes/auth.js @@ -2,12 +2,7 @@ exports.register = function(plugin, options, next) { - let Controllers = { - auth: { - login: require('../controllers/auth/login'), - logout: require('../controllers/auth/logout') - } - }; + let Controllers = plugin.controllers(); plugin.route([ diff --git a/app/routes/core.js b/app/routes/core.js index 7e74824..146f5a3 100644 --- a/app/routes/core.js +++ b/app/routes/core.js @@ -2,12 +2,7 @@ exports.register = function(plugin, options, next) { - var Controllers = { - core: { - fallback: require('../controllers/core/fallback'), - static: require('../controllers/core/static') - } - }; + var Controllers = plugin.controllers(); plugin.route([ diff --git a/app/routes/pages.js b/app/routes/pages.js index f67b79c..5a660ee 100644 --- a/app/routes/pages.js +++ b/app/routes/pages.js @@ -2,11 +2,7 @@ exports.register = function(plugin, options, next) { - var Controllers = { - pages: { - home: require('../controllers/pages/home') - } - }; + var Controllers = plugin.controllers(); plugin.route([ diff --git a/config/manifest.js b/config/manifest.js index 14663bd..f2416ea 100644 --- a/config/manifest.js +++ b/config/manifest.js @@ -113,6 +113,15 @@ internals.manifest = { } }, + { + plugin: { + register: './lib/controllers', + options: { + path: './app/controllers' + } + } + }, + // Core routes { plugin: './app/routes/core.js' diff --git a/lib/controllers.js b/lib/controllers.js new file mode 100644 index 0000000..3014723 --- /dev/null +++ b/lib/controllers.js @@ -0,0 +1,63 @@ +'use strict'; + +/** + * this plugin wraps up the controller module(s) into + * a server method called `.controllers()` that can then be accessed later + * by route plugins. + */ + +const Path = require('path'); +const Glob = require('glob'); + +/** + * create an object representation of a directory containing + * javascript source files + * + * @param {String} fpath folder javascript files + * @returns {Object} representing a directory + */ +const pack = function(fpath) { + fpath = Path.resolve(fpath); + const pattern = Path.join(fpath, '**/**.js'); + const files = Glob.sync(pattern); + const result = {}; + files.forEach((file) => { + const module = require(file); + + // remove the common abspath + file = file.replace(fpath, ''); + + // remove the file extension at the end + file = file.replace(/\.js$/, ''); + + // remove the trailing slash + file = file[0] === Path.sep?file.slice(1):file; + + let components = file.split('/').reverse(); + let head = result; + while ( components.length > 0 ) { + const el = components.pop(); + if ( !components.length ) { + head[el] = module; + } else if ( head[el] === undefined ) { + head[el] = {}; + } + head = head[el]; + } + }); + return result; +}; + +exports.register = function(server, options, next) { + const controllers = pack(options.path); + server.decorate('server', 'controllers', function() { + return controllers; + }); + next(); +}; + +exports.register.attributes = { + name: 'controller_package', + version: require('../package.json').version +}; + From 4c24e8fa2e4f4a70c2cb5014777012eb8a407e13 Mon Sep 17 00:00:00 2001 From: Saravjeet 'Aman' Singh Date: Sat, 17 Sep 2016 23:25:50 +0530 Subject: [PATCH 2/2] fix typo --- lib/controllers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/controllers.js b/lib/controllers.js index 3014723..6f312cd 100644 --- a/lib/controllers.js +++ b/lib/controllers.js @@ -13,7 +13,7 @@ const Glob = require('glob'); * create an object representation of a directory containing * javascript source files * - * @param {String} fpath folder javascript files + * @param {String} fpath folder containing javascript files * @returns {Object} representing a directory */ const pack = function(fpath) {