Created
June 28, 2011 14:06
-
-
Save dalssoft/1051200 to your computer and use it in GitHub Desktop.
require many files for node.js
This file contains hidden or 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
# 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