Skip to content

Instantly share code, notes, and snippets.

@dalssoft
Created June 28, 2011 14:06
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 dalssoft/1051200 to your computer and use it in GitHub Desktop.
Save dalssoft/1051200 to your computer and use it in GitHub Desktop.
require many files for node.js
# Bug version - don't use it
fs = require('fs')
util = require('util')
debug = require('util').debug
exp = []
addToTree = (tree, array) ->
return if array.length == 0
for i in [0..array.length - 1]
tree = tree[array[i]] = tree[array[i]] || {}
setToTree = (tree, keys, callback) ->
console.log keys
return if keys.length == 0
if keys.length == 1
tvalue = tree[keys[0]]
tvalue or= []
tvalue.concat([callback()])
tree[keys[0]] = tvalue
nkey = keys.slice(1)
setToTree tree, nkey, callback
addExp = (path, filename) ->
fullPath = "#{path}/#{filename}"
dirs = path.split '/'
dirs = (x for x in dirs when x != '.')
console.log "module namespace:", dirs
addToTree exp, dirs
setToTree exp, dirs, -> require(fullPath)
walkFile = (dir, callback) ->
files = fs.readdirSync dir
for file in files
do (file) ->
fullPath = "#{dir}/#{file}"
console.log "full path:", fullPath
stat = fs.statSync "#{fullPath}"
walkFile fullPath, callback if stat.isDirectory()
callback(dir, file) if stat.isFile()
module.exports.load_module = (path) ->
console.log "load_module path: ", path
walkFile path, (path, filename) -> addExp path, filename
#console.log "exports after: "
#console.log util.inspect exp, true, 5
exp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment