Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created August 4, 2014 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/dae9b1b48ce0fb74903d to your computer and use it in GitHub Desktop.
Save barneycarroll/dae9b1b48ce0fb74903d to your computer and use it in GitHub Desktop.
Supply a path to return an object with all that path's JS modules exports as properties / methods of the returned object.
var fs = require( 'fs' );
var path = require( 'path' );
module.exports = function( folder ){
var output = {};
fs.readdirSync( folder )
.filter( function isJavascript( filename ){
return '.js' === path.extname( filename );
} )
.forEach( function addModuleToOutput( filename ){
var key = path.basename( filename, path.extname( filename ) );
output[ key ] = require( folder + filename );
} );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment