From 0ec14f5b8641ca6fb38108a21fadc6c18323d75f Mon Sep 17 00:00:00 2001 From: Uzo Olisemeka Date: Mon, 15 May 2017 07:59:19 -0400 Subject: [PATCH] created util for loading directory of modules switched supervisor and citizen modules to use loader util --- lib/modules/citizen/mail/index.js | 52 ++-------------- .../supervisor-noticeboard-bindings/index.js | 60 ++++--------------- lib/modules/supervisor/citizen/index.js | 52 ++-------------- lib/modules/supervisor/mail/index.js | 52 ++-------------- lib/modules/supervisor/signal/index.js | 52 ++-------------- utils/load-directory-of-modules.js | 58 ++++++++++++++++++ 6 files changed, 92 insertions(+), 234 deletions(-) create mode 100644 utils/load-directory-of-modules.js diff --git a/lib/modules/citizen/mail/index.js b/lib/modules/citizen/mail/index.js index 7748384..f648dc9 100644 --- a/lib/modules/citizen/mail/index.js +++ b/lib/modules/citizen/mail/index.js @@ -1,52 +1,12 @@ -var fs = require('fs'), - path = require('path'); - module.exports = function( citizen, config ){ - - var dir_content = fs.readdirSync( __dirname ); - - dir_content.forEach( function( item ){ - - var item_path = path.join( __dirname, item ); - - // is a file - var is_file = fs.lstatSync( item_path ).isFile(); - - if( is_file ){ - - // .js extension - // not index.js - if( - item_path.indexOf( '.js' ) === item_path.length - 3 - && item.toLowerCase() !== 'index.js' - ){ - - try { - - require( item_path )( citizen, config ); - } - - catch( e ){ throw e; } - } - } - - // is a directory - var is_dir = fs.lstatSync( item_path ).isDirectory(); - - if( is_dir ){ - // has index file to configure loading - var index_path = path.join( item_path, 'index.js' ); - - if( fs.existsSync( index_path ) ){ - - try { + require( '../../../../utils/load-directory-of-modules' )( __dirname, { - require( index_path )( citizen, config ); - } + load_args: [ citizen, config ], + file: function( name ){ - catch( e ){ throw e; } - } - } + if( name === 'index.js' ) return false; + return true; + } }); } \ No newline at end of file diff --git a/lib/modules/citizen/supervisor-noticeboard-bindings/index.js b/lib/modules/citizen/supervisor-noticeboard-bindings/index.js index 9a10e84..c3fac1f 100644 --- a/lib/modules/citizen/supervisor-noticeboard-bindings/index.js +++ b/lib/modules/citizen/supervisor-noticeboard-bindings/index.js @@ -1,58 +1,18 @@ -var fs = require('fs'), - path = require('path'), - Noticeboard = require('cjs-noticeboard'), - noticeboard = new Noticeboard({ logOps: false, logging: false }); - module.exports = function( citizen, config ){ - // create public interface for linked noticeboard - citizen.noticeboard = {}; - citizen.noticeboard.pending = {}; - - var dir_content = fs.readdirSync( __dirname ); - - dir_content.forEach( function( item ){ - - var item_path = path.join( __dirname, item ); - - // is a file - var is_file = fs.lstatSync( item_path ).isFile(); - - if( is_file ){ - - // .js extension - // not index.js - if( - item_path.indexOf( '.js' ) === item_path.length - 3 - && item.toLowerCase() !== 'index.js' - ){ - - try { - - require( item_path )( citizen, noticeboard, config ); - } - - catch( e ){ throw e; } - } - } - - // is a directory - var is_dir = fs.lstatSync( item_path ).isDirectory(); + var Noticeboard = require('cjs-noticeboard'), + noticeboard = new Noticeboard({ logOps: false, logging: false }); - if( is_dir ){ + citizen.noticeboard = {}; + citizen.noticeboard.pending = {}; - // has index file to configure loading - var index_path = path.join( item_path, 'index.js' ); - - if( fs.existsSync( index_path ) ){ - - try { + require( '../../../../utils/load-directory-of-modules' )( __dirname, { - require( index_path )( citizen, noticeboard, config ); - } + load_args: [ citizen, noticeboard, config ], + file: function( name ){ - catch( e ){ throw e; } - } - } + if( name === 'index.js' ) return false; + return true; + } }); } \ No newline at end of file diff --git a/lib/modules/supervisor/citizen/index.js b/lib/modules/supervisor/citizen/index.js index 3748d26..e0933c5 100644 --- a/lib/modules/supervisor/citizen/index.js +++ b/lib/modules/supervisor/citizen/index.js @@ -1,52 +1,12 @@ -var fs = require('fs'), - path = require('path'); - module.exports = function( supervisor, config ){ - - var dir_content = fs.readdirSync( __dirname ); - - dir_content.forEach( function( item ){ - - var item_path = path.join( __dirname, item ); - - // is a file - var is_file = fs.lstatSync( item_path ).isFile(); - - if( is_file ){ - - // .js extension - // not index.js - if( - item_path.indexOf( '.js' ) === item_path.length - 3 - && item.toLowerCase() !== 'index.js' - ){ - - try { - - require( item_path )( supervisor, config ); - } - - catch( e ){ throw e; } - } - } - - // is a directory - var is_dir = fs.lstatSync( item_path ).isDirectory(); - - if( is_dir ){ - // has index file to configure loading - var index_path = path.join( item_path, 'index.js' ); - - if( fs.existsSync( index_path ) ){ - - try { + require( '../../../../utils/load-directory-of-modules' )( __dirname, { - require( index_path )( supervisor, config ); - } + load_args: [ supervisor, config ], + file: function( name ){ - catch( e ){ throw e; } - } - } + if( name === 'index.js' ) return false; + return true; + } }); } \ No newline at end of file diff --git a/lib/modules/supervisor/mail/index.js b/lib/modules/supervisor/mail/index.js index 3748d26..e0933c5 100644 --- a/lib/modules/supervisor/mail/index.js +++ b/lib/modules/supervisor/mail/index.js @@ -1,52 +1,12 @@ -var fs = require('fs'), - path = require('path'); - module.exports = function( supervisor, config ){ - - var dir_content = fs.readdirSync( __dirname ); - - dir_content.forEach( function( item ){ - - var item_path = path.join( __dirname, item ); - - // is a file - var is_file = fs.lstatSync( item_path ).isFile(); - - if( is_file ){ - - // .js extension - // not index.js - if( - item_path.indexOf( '.js' ) === item_path.length - 3 - && item.toLowerCase() !== 'index.js' - ){ - - try { - - require( item_path )( supervisor, config ); - } - - catch( e ){ throw e; } - } - } - - // is a directory - var is_dir = fs.lstatSync( item_path ).isDirectory(); - - if( is_dir ){ - // has index file to configure loading - var index_path = path.join( item_path, 'index.js' ); - - if( fs.existsSync( index_path ) ){ - - try { + require( '../../../../utils/load-directory-of-modules' )( __dirname, { - require( index_path )( supervisor, config ); - } + load_args: [ supervisor, config ], + file: function( name ){ - catch( e ){ throw e; } - } - } + if( name === 'index.js' ) return false; + return true; + } }); } \ No newline at end of file diff --git a/lib/modules/supervisor/signal/index.js b/lib/modules/supervisor/signal/index.js index 3748d26..e0933c5 100644 --- a/lib/modules/supervisor/signal/index.js +++ b/lib/modules/supervisor/signal/index.js @@ -1,52 +1,12 @@ -var fs = require('fs'), - path = require('path'); - module.exports = function( supervisor, config ){ - - var dir_content = fs.readdirSync( __dirname ); - - dir_content.forEach( function( item ){ - - var item_path = path.join( __dirname, item ); - - // is a file - var is_file = fs.lstatSync( item_path ).isFile(); - - if( is_file ){ - - // .js extension - // not index.js - if( - item_path.indexOf( '.js' ) === item_path.length - 3 - && item.toLowerCase() !== 'index.js' - ){ - - try { - - require( item_path )( supervisor, config ); - } - - catch( e ){ throw e; } - } - } - - // is a directory - var is_dir = fs.lstatSync( item_path ).isDirectory(); - - if( is_dir ){ - // has index file to configure loading - var index_path = path.join( item_path, 'index.js' ); - - if( fs.existsSync( index_path ) ){ - - try { + require( '../../../../utils/load-directory-of-modules' )( __dirname, { - require( index_path )( supervisor, config ); - } + load_args: [ supervisor, config ], + file: function( name ){ - catch( e ){ throw e; } - } - } + if( name === 'index.js' ) return false; + return true; + } }); } \ No newline at end of file diff --git a/utils/load-directory-of-modules.js b/utils/load-directory-of-modules.js new file mode 100644 index 0000000..323ada5 --- /dev/null +++ b/utils/load-directory-of-modules.js @@ -0,0 +1,58 @@ +var fs = require('fs'), + path_util = require('path'); + +module.exports = function( path, config ){ + + if( !path ) return; + if( !config ) config = {}; + if( typeof path !== 'string' ) throw new Error( 'directory path must be a string' ); + if( !fs.lstatSync( path ).isDirectory() ) throw new Error( 'path must be to a directory' ); + + if( !config.dir_entry_file || typeof config.dir_entry_file !== 'string' ) config.dir_entry_file = 'index.js'; + if( !config.load_args || Object.prototype.toString.call( config.load_args ) !== '[object Array]' ) config.load_args = []; + + var path_content = fs.readdirSync( path ); + + path_content.forEach( function( item ){ + + var item_path = path_util.join( path, item ), + item_stat = fs.lstatSync( item_path ), + item_type; + + if( item_stat.isFile() ) item_type = 'file'; + else if( item_stat.isDirectory() ) item_type = 'dir'; + else item_type = 'unsupported'; + + if( item_type == 'unsupported' ) return; + + // bail if conditions for loading item type are false-y + if( + config.hasOwnProperty( item_type ) + && ( + !config[ item_type ] + || ( typeof config[ item_type ] == 'function' && !config[ item_type ]( item ) ) + ) + ) return; + + switch( item_type ){ + + case 'file': + + try{ + require( item_path ).apply( null, config.load_args ); + } + + catch( e ){ throw e } + break; + + case 'dir': + + try{ + require( path_util.join( item_path, config.dir_entry_file ) ).apply( null, config.load_args ); + } + + catch( e ){ throw e } + break; + } + }); +} \ No newline at end of file