include-all
Advanced tools
Comparing version 0.0.2 to 0.0.5
72
index.js
@@ -1,2 +0,2 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs'); | ||
@@ -7,34 +7,72 @@ // Returns false if the directory doesn't exist | ||
var modules = {}; | ||
try { files = fs.readdirSync(options.dirname); } | ||
catch (e) { | ||
if (options.optional) return {}; | ||
else throw new Error('Directory not found: ' + options.dirname); | ||
// Remember the starting directory | ||
if(!options.startDirname) { | ||
options.startDirname = options.dirname; | ||
} | ||
function excludeDirectory(dirname) { | ||
return options.excludeDirs && dirname.match(options.excludeDirs); | ||
try { | ||
files = fs.readdirSync(options.dirname); | ||
} catch(e) { | ||
if(options.optional) return {}; | ||
else throw new Error('Directory not found: ' + options.dirname); | ||
} | ||
// Iterate through files in the current directory | ||
files.forEach(function(file) { | ||
var filepath = options.dirname + '/' + file; | ||
if (fs.statSync(filepath).isDirectory()) { | ||
if (excludeDirectory(file)) return; | ||
// For directories, continue to recursively include modules | ||
if(fs.statSync(filepath).isDirectory()) { | ||
// Ignore explicitly excluded directories | ||
if(excludeDirectory(file)) return; | ||
// Recursively call requireAll on each child directory | ||
modules[file] = requireAll({ | ||
dirname : filepath, | ||
filter : options.filter, | ||
excludeDirs : options.excludeDirs | ||
dirname: filepath, | ||
filter: options.filter, | ||
pathFilter: options.pathFilter, | ||
excludeDirs: options.excludeDirs, | ||
startDirname: options.startDirname | ||
}); | ||
} else { | ||
var match = file.match(options.filter); | ||
if (!match) return; | ||
} | ||
// For files, go ahead and add the code to the module map | ||
else { | ||
modules[match[1]] = require(filepath); | ||
// Key name for module | ||
var identity; | ||
// Filename filter | ||
if (options.filter) { | ||
var match = file.match(options.filter); | ||
if(!match) return; | ||
identity = match[1]; | ||
} | ||
// Full relative path filter | ||
if (options.pathFilter) { | ||
// Peel off relative path | ||
var path = filepath.replace(options.startDirname,''); | ||
// make sure a slash exists on the left side of path | ||
path = '/' + _.str.ltrim(path,'/'); | ||
var pathMatch = path.match(options.pathFilter); | ||
if (!pathMatch) return; | ||
identity = pathMatch[2]; | ||
} | ||
// Load module into memory | ||
modules[identity] = require(filepath); | ||
} | ||
}); | ||
// Pass map of modules back to app code | ||
return modules; | ||
}; | ||
function excludeDirectory(dirname) { | ||
return options.excludeDirs && dirname.match(options.excludeDirs); | ||
} | ||
}; |
{ | ||
"name": "include-all", | ||
"version": "0.0.2", | ||
"version": "0.0.5", | ||
"description": "An easy way to include all node.js modules within a directory. This is a fork of felixge's awesome module, require-all (https://github.com/felixge/node-require-all) which adds the ability to mark an include as **optional**.", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git://github.com/mikermcneil/node-require-all.git" | ||
"url": "git://github.com/mikermcneil/include-all.git" | ||
}, | ||
@@ -17,0 +17,0 @@ "keywords": [ |
@@ -5,3 +5,5 @@ # include-all | ||
This is a fork of felixge's awesome module, require-all (https://github.com/felixge/node-require-all) which adds the ability to mark a call to `include-all` as **optional**. | ||
This is a fork of felixge's awesome module, require-all (https://github.com/felixge/node-require-all) which adds a few extra capabilities: | ||
- the ability to `include-all` a directory as **optional**. | ||
- the ability to filter by path, not just filename (pathFilter) | ||
@@ -24,2 +26,3 @@ | ||
### Optional include | ||
var models = require('include-all')({ | ||
@@ -37,1 +40,8 @@ dirname : __dirname + '/models', | ||
``` | ||
### Filter by filepath | ||
var models = require('include-all')({ | ||
dirname : __dirname + '/controllers', | ||
filterPath : /(.+)\/(.+)\.js$/, | ||
excludeDirs : /^\.(git|svn)$/ | ||
}); |
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
5862
132
44