require-all
Advanced tools
Comparing version 1.1.0 to 2.0.0
33
index.js
var fs = require('fs'); | ||
var DEFAULT_EXCLUDE_DIR = /^\./; | ||
var DEFAULT_FILTER = /^([^\.].*)\.js(on)?$/; | ||
var DEFAULT_RECURSIVE = true; | ||
module.exports = function requireAll(options) { | ||
if (typeof options === 'string') { | ||
options = { | ||
dirname: options, | ||
filter: /(.+)\.js(on)?$/, | ||
excludeDirs: /^\.(git|svn)$/ | ||
}; | ||
} | ||
var files = fs.readdirSync(options.dirname); | ||
var dirname = typeof options === 'string' ? options : options.dirname; | ||
var excludeDirs = options.excludeDirs === undefined ? DEFAULT_EXCLUDE_DIR : options.excludeDirs; | ||
var filter = options.filter === undefined ? DEFAULT_FILTER : options.filter; | ||
var modules = {}; | ||
var recursive = options.recursive === undefined ? DEFAULT_RECURSIVE : options.recursive; | ||
var resolve = options.resolve || identity; | ||
@@ -18,7 +17,10 @@ var map = options.map || identity; | ||
function excludeDirectory(dirname) { | ||
return options.excludeDirs && dirname.match(options.excludeDirs); | ||
return !recursive || | ||
(excludeDirs && dirname.match(excludeDirs)); | ||
} | ||
var files = fs.readdirSync(dirname); | ||
files.forEach(function (file) { | ||
var filepath = options.dirname + '/' + file; | ||
var filepath = dirname + '/' + file; | ||
if (fs.statSync(filepath).isDirectory()) { | ||
@@ -28,6 +30,7 @@ | ||
modules[file] = requireAll({ | ||
modules[map(file, filepath)] = requireAll({ | ||
dirname: filepath, | ||
filter: options.filter, | ||
excludeDirs: options.excludeDirs, | ||
filter: filter, | ||
excludeDirs: excludeDirs, | ||
map: map, | ||
resolve: resolve | ||
@@ -37,3 +40,3 @@ }); | ||
} else { | ||
var match = file.match(options.filter); | ||
var match = file.match(filter); | ||
if (!match) return; | ||
@@ -40,0 +43,0 @@ |
{ | ||
"name": "require-all", | ||
"description": "An easy way to require all files within a directory.", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"author": "Felix Geisendörfer <felix@debuggable.com> (http://debuggable.com/)", | ||
@@ -13,3 +13,3 @@ "contributors": [ | ||
"devDependencies": { | ||
"semver": "~2.1.0" | ||
"semver": "4.3.2" | ||
}, | ||
@@ -16,0 +16,0 @@ "files": [ |
@@ -11,3 +11,4 @@ # require-all | ||
filter : /(.+Controller)\.js$/, | ||
excludeDirs : /^\.(git|svn)$/ | ||
excludeDirs : /^\.(git|svn)$/, | ||
recursive : true | ||
}); | ||
@@ -36,3 +37,2 @@ | ||
filter : /(.+Controller)\.js$/, | ||
excludeDirs : /^\.(git|svn)$/, | ||
resolve : function (Controller) { | ||
@@ -46,10 +46,9 @@ return new Controller(); | ||
If your directory contains files where the names do not match what you want in the resulting property (for example, you want camelCase but the file names are snake_case), then you can use the `map` function: | ||
If your directory contains files where the names do not match what you want in the resulting property (for example, you want camelCase but the file names are snake_case), then you can use the `map` function. The `map` function is called on both file and directory names, as they are added to the resulting object. | ||
```js | ||
var controllers = require('require-all')({ | ||
dirname : __dirname + '/controllers', | ||
filter : /(.+Controller)\.js$/, | ||
excludeDirs : /^\.(git|svn)$/, | ||
map : function (name, path) { | ||
dirname : __dirname + '/controllers', | ||
filter : /(.+Controller)\.js$/, | ||
map : function (name, path) { | ||
return name.replace(/_([a-z])/g, function (m, c) { | ||
@@ -56,0 +55,0 @@ return c.toUpperCase(); |
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
4738
39
57