Skip to content

Instantly share code, notes, and snippets.

@nanodeath
Created October 2, 2011 16:59
Show Gist options
  • Save nanodeath/1257631 to your computer and use it in GitHub Desktop.
Save nanodeath/1257631 to your computer and use it in GitHub Desktop.
fs = require 'fs'
path = require 'path'
isDirectory = (base, p) ->
fullPath = path.resolve base, p
fs.statSync(fullPath).isDirectory()
mergeExports = (theExports, otherModule) ->
for otherExport, func of otherModule
theExports[otherExport] = otherModule[otherExport]
loadDirectory = (directory, recursive=true) ->
blacklist = []
files = fs.readdirSync directory
theExports = {}
for file in files
if blacklist.indexOf(file) < 0
if isDirectory(directory, file)
mergeExports(theExports, loadDirectory(file)) if recursive
else if path.extname(file) == ".coffee"
fullPath = path.resolve path.join(directory, path.basename(file, ".coffee"))
mergeExports(theExports, require(fullPath))
theExports
module.exports = loadDirectory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment