Skip to content

Commit

Permalink
created util for loading directory of modules
Browse files Browse the repository at this point in the history
switched supervisor and citizen modules to use loader util
  • Loading branch information
Akamaozu committed May 15, 2017
1 parent 0a39da5 commit 0ec14f5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 234 deletions.
52 changes: 6 additions & 46 deletions 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;
}
});
}
60 changes: 10 additions & 50 deletions 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;
}
});
}
52 changes: 6 additions & 46 deletions 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;
}
});
}
52 changes: 6 additions & 46 deletions 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;
}
});
}
52 changes: 6 additions & 46 deletions 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;
}
});
}
58 changes: 58 additions & 0 deletions 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;
}
});
}

0 comments on commit 0ec14f5

Please sign in to comment.