load-from-directory
Advanced tools
Comparing version 1.0.0 to 1.0.1
44
index.js
@@ -16,2 +16,8 @@ /** | ||
var _removeExtension = function( filename ) { | ||
var filename_split = filename.split( '.' ); | ||
filename_split.pop(); // Remove the last thing, which is the extension | ||
return filename_split.join( '.' ); | ||
}; | ||
/** | ||
@@ -21,16 +27,17 @@ * Loads all modules from directory, returns array | ||
* @param options object containing options | ||
* @param options.exclude (Default=["index.js", /\..* /])File names to exclude from import | ||
* @param options.exclude (Default=["index.js", /\..* /]) File names to exclude from import | ||
* @param options.caller_directory (Default=callsite()[ 1 ].getFileName) The directory to use as the root directory for the import (The __dirname of the person calling this method) | ||
* @returns {Array} | ||
*/ | ||
module.exports.load = function( root_directory, options ) { | ||
exports.load = function( root_directory, options ) { | ||
options = options || {}; | ||
_.defaults( options, { | ||
exclude: [ 'index.js', /^\..*/ ] | ||
exclude: [ 'index.js', /^\..*/ ], | ||
caller_directory: path.dirname( callsite()[ 1 ].getFileName() ) | ||
} ); | ||
var caller_directory = path.dirname( callsite()[ 1 ].getFileName() ); | ||
var root_directory_resolved = path.resolve( caller_directory, root_directory ); | ||
var root_directory_resolved = path.resolve( options.caller_directory, root_directory ); | ||
var file_names = fs | ||
.readdirSync( path.resolve( caller_directory, root_directory ) ); | ||
.readdirSync( path.resolve( options.caller_directory, root_directory ) ); | ||
@@ -45,3 +52,4 @@ return _.chain( file_names ) | ||
// Get full absolute path to the module | ||
var root_directory_resolved_file = path.resolve( root_directory_resolved, name ); | ||
var name_without_extension = _removeExtension( name ); | ||
var root_directory_resolved_file = path.resolve( root_directory_resolved, name_without_extension ); | ||
@@ -52,6 +60,26 @@ // Get it's relative path to here, prepend with ./ so that require() will understand it | ||
// Require the module | ||
return require( relative ); | ||
return [ name_without_extension, require( relative ) ]; // Return both, so we can reduce it back into an object | ||
} ) | ||
.reduce( function( full, part ) { | ||
if( !full ) full = {}; | ||
full[ part[ 0 ] ] = part[ 1 ]; | ||
return full; | ||
}, {} ) // start with empty object | ||
.value(); | ||
}; | ||
exports.loadArray = function( root_directory, options ) { | ||
options = options || {}; | ||
_.defaults( options, { | ||
caller_directory: path.dirname( callsite()[ 1 ].getFileName() ) // Set this here, because when we call load we fuck up the stacke | ||
} ); | ||
// Load the modules | ||
var loaded = exports.load( root_directory, options ); | ||
// Use map to convert the object into a value array | ||
return _.map( loaded, function( item ) { | ||
return item | ||
} ); | ||
}; | ||
})(); |
{ | ||
"name": "load-from-directory", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Loads all modules from the given directory into an array", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,30 +19,42 @@ /** | ||
it( 'should load modules in test directory', function() { | ||
var modules = load_modules.load( 'test_folder' ); | ||
assert.equal( modules.length, 1, 'Should have imported one module' ); | ||
assert.property( modules[ 0 ], 'method', 'Should have method' ); | ||
assert.equal( modules[ 0 ].method(), 'method_response', 'Should respond' ); | ||
describe( 'load', function() { | ||
it( 'Should load modules into object', function() { | ||
var modules = load_modules.load( 'test_folder' ); | ||
assert.isObject( modules ); | ||
assert.property( modules, 'module' ); | ||
assert.isFunction( modules[ 'module' ].method ); | ||
assert.equal( modules[ 'module' ].method(), 'method_response' ); | ||
} ); | ||
} ); | ||
it( 'should load modules in test directory', function() { | ||
var modules = load_modules.load( 'test_folder' ); | ||
assert.equal( modules.length, 1, 'Should have imported one module' ); | ||
assert.property( modules[ 0 ], 'method', 'Should have method' ); | ||
assert.equal( modules[ 0 ].method(), 'method_response', 'Should respond' ); | ||
} ); | ||
describe( 'loadArray', function() { | ||
it( 'should load modules in test directory', function() { | ||
var modules = load_modules.loadArray( 'test_folder' ); | ||
assert.equal( modules.length, 1, 'Should have imported one module' ); | ||
assert.property( modules[ 0 ], 'method', 'Should have method' ); | ||
assert.equal( modules[ 0 ].method(), 'method_response', 'Should respond' ); | ||
} ); | ||
it( 'should ignore dotfiles by default', function() { | ||
var modules = load_modules.load( 'test_folder_ignore' ); | ||
assert.equal( modules.length, 1, 'Should have imported one module' ); | ||
assert.property( modules[ 0 ], 'method', 'Should have method' ); | ||
assert.equal( modules[ 0 ].method(), 'method_response', 'Should respond' ); | ||
} ); | ||
it( 'should load modules in test directory', function() { | ||
var modules = load_modules.loadArray( 'test_folder' ); | ||
assert.equal( modules.length, 1, 'Should have imported one module' ); | ||
assert.property( modules[ 0 ], 'method', 'Should have method' ); | ||
assert.equal( modules[ 0 ].method(), 'method_response', 'Should respond' ); | ||
} ); | ||
it( 'should ignore more things if specified', function() { | ||
var modules = load_modules.load( 'test_folder_ignore', { | ||
exclude: [ /.*/ ] // Ignore everything | ||
it( 'should ignore dotfiles by default', function() { | ||
var modules = load_modules.loadArray( 'test_folder_ignore' ); | ||
assert.equal( modules.length, 1, 'Should have imported one module' ); | ||
assert.property( modules[ 0 ], 'method', 'Should have method' ); | ||
assert.equal( modules[ 0 ].method(), 'method_response', 'Should respond' ); | ||
} ); | ||
assert.equal( modules.length, 0, 'Should have imported one module' ); | ||
it( 'should ignore more things if specified', function() { | ||
var modules = load_modules.loadArray( 'test_folder_ignore', { | ||
exclude: [ /.*/ ] // Ignore everything | ||
} ); | ||
assert.equal( modules.length, 0, 'Should have imported one module' ); | ||
} ); | ||
} ); | ||
} ) | ||
})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8898
140