Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created util for loading directory of modules
switched supervisor and citizen modules to use loader util
- Loading branch information
Showing
6 changed files
with
92 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
60
lib/modules/citizen/supervisor-noticeboard-bindings/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); | ||
} |