include-all
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -7,6 +7,6 @@ /** | ||
var fs = require('fs'); | ||
var _ = require('lodash'); | ||
/** | ||
@@ -20,10 +20,12 @@ * helpIncludeAllSync() | ||
* @optional {RegExp} filter | ||
* A regular expression used to filter modules by filenames. | ||
* A regular expression used to filter modules by filename. | ||
* | ||
* @optional {RegExp} pathFilter | ||
* A regular expression used to filter modules by their entire paths. | ||
* | ||
* @optional {RegExp} excludeDirs | ||
* A regular expression used to EXCLUDE directories by name. | ||
* (the opposite of `filter`) | ||
* | ||
* @optional {Array} exclude | ||
* An array of regular expressions used to certain EXCLUDE relative paths. | ||
* (the opposite of the old `pathFilter`) | ||
* | ||
* @optional {Number} depth | ||
@@ -122,2 +124,5 @@ * The maximum depth to traverse. A depth of `1` means only the top-level contents of the initial directory will be returned. | ||
} | ||
if (typeof options.exclude !== 'undefined' && (typeof options.exclude !== 'object' || options.exclude === null)) { | ||
throw new Error('If specified, `exclude` must be an array of RegExps.'); | ||
} | ||
@@ -133,3 +138,10 @@ | ||
// For readability in the code below, track the initial "dirname" as a local | ||
// variable called `contextPath`. | ||
// | ||
// Here, we also ensure that it is an absolute path. | ||
var contextPath = path.resolve(options.dirname); | ||
// Define and invoke a self-calling recursive function. | ||
var modules = (function _recursivelyIncludeAll(thisDirname, _depth){ | ||
@@ -172,2 +184,14 @@ | ||
// Get the relative path of this module. | ||
// (i.e. peel off just the relative path -- remove the initial dirname) | ||
var relativePath = path.relative(contextPath, filepath); | ||
// Relative path "exclude" filter (blacklist) | ||
if (options.exclude) { | ||
var shouldBeExcluded = _.any(options.exclude, function (regexp) { | ||
return relativePath.match(regexp); | ||
}); | ||
if (shouldBeExcluded) { return; } | ||
} | ||
// For directories, continue to recursively include modules | ||
@@ -180,3 +204,3 @@ if (fs.statSync(filepath).isDirectory()) { | ||
// Recursively call `_recursivelyIncludeAll` on this child directory. | ||
_modules[file] = _recursivelyIncludeAll( | ||
var descendantModules = _recursivelyIncludeAll( | ||
filepath, // new dirname for recursive step | ||
@@ -186,38 +210,21 @@ _depth+1 // new depth for recursive step | ||
if (options.markDirectories || options.flattenDirectories) { | ||
_modules[file].isDirectory = true; | ||
// If we're flattening, then fold _our_ direct child modules | ||
// (grandchildren, if you will) onto ourselves. | ||
if (options.flatten) { | ||
_.each(descendantModules, function (rhs, grandchildKey){ | ||
if (options.keepDirectoryPath) { | ||
_modules[path.join(relativePath, grandchildKey)] = rhs; | ||
} | ||
else { | ||
if (_modules[grandchildKey]) { throw new Error('Attempting to flatten modules but duplicate key detected (`'+grandchildKey+'`). Enable `keepDirectoryPath: true` to enable namepspacing based on hierarchy.'); } | ||
_modules[grandchildKey] = rhs; | ||
} | ||
});//</each key in dictionary of all descendant module> | ||
} | ||
// Otherwise, we're leaving things denormalized. | ||
else { | ||
_modules[file] = descendantModules; | ||
} | ||
if (options.flattenDirectories) { | ||
_modules = (function _recursivelyFlattenDirectories(_modules, accum, thisPath) { | ||
accum = accum || {}; | ||
Object.keys(_modules).forEach(function (keyName) { | ||
if (typeof(_modules[keyName]) !== 'object' && typeof(_modules[keyName]) !== 'function') { | ||
return; | ||
} | ||
var nextPath; | ||
if (thisPath) { | ||
nextPath = path.join(thisPath, keyName); | ||
// `path.join()` does not preserve `./`-- but since that | ||
// symbols has a special meaning when at the beginning of | ||
// a `require()` path, we bring it back here. | ||
if (thisPath.match(/^\.\//) && !nextPath.match(/^\.\//)) { | ||
nextPath = './' + nextPath; | ||
} | ||
} | ||
else { nextPath = keyName; } | ||
if (_modules[keyName].isDirectory) { | ||
_recursivelyFlattenDirectories(_modules[keyName], accum, nextPath); | ||
} else { | ||
accum[options.keepDirectoryPath ? nextPath : keyName] = _modules[keyName]; | ||
} | ||
}); | ||
return accum; | ||
})(_modules); | ||
}//</if options.flattenDirectories> | ||
}//</if (this is a directory)> | ||
@@ -232,3 +239,13 @@ | ||
// Filename filter | ||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
// For debugging: | ||
// | ||
// console.log('contextPath:',contextPath); | ||
// console.log('file:',file); | ||
// console.log('filepath:',filepath); | ||
// console.log('relativePath:',relativePath); | ||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
// Filename "include" filter (whitelist) | ||
// Note that this also identifies the appropriate key name. | ||
if (options.filter) { | ||
@@ -240,17 +257,2 @@ var match = file.match(options.filter); | ||
// ADDITIONAL full relative path filter. | ||
// (only relevant if we made it past the first filter. | ||
// Note that the key name from here takes precedence.) | ||
if (options.pathFilter) { | ||
// Peel off just the relative path (remove the initial dirname) | ||
var relPath = filepath.replace(options.dirname, ''); | ||
// Make sure exactly one slash exists on the left side of path. | ||
relPath = relPath.replace(/^\/*/, '/'); | ||
var pathMatch = relPath.match(options.pathFilter); | ||
if (!pathMatch) { return; } | ||
keyName = pathMatch[2]; | ||
} | ||
// If `dontLoad` is true, then don't load anything-- | ||
@@ -290,3 +292,3 @@ // instead just set the RHS to `true`. | ||
}//</else (this is a file)> | ||
});//</each file> | ||
});//</each direct child inode in this directory> | ||
@@ -296,3 +298,3 @@ | ||
return _modules; | ||
})(options.dirname, 0); | ||
})(contextPath, 0);//</initial call to self-calling, recursive function> | ||
// ^set up dirname, and start the depth counter at 0 | ||
@@ -299,0 +301,0 @@ |
{ | ||
"name": "include-all", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "An easy way to include all node.js modules within a directory.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,3 +26,3 @@ # include-all | ||
```js | ||
var path = require('include-all'); | ||
var path = require('path'); | ||
var includeAll = require('include-all'); | ||
@@ -115,3 +115,3 @@ | ||
The following convenience methods take the same "options,cb" signature as the default `includeAll` function, and they support all of the same options. | ||
The following convenience methods take all the same options as the default `includeAll` function, but they also support a few _additional_ options. Also, since they're asynchronous, they work a bit differently: they use the conventional Node.js "options,cb" function signature. | ||
@@ -118,0 +118,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
147550
2930
0