include-all
Advanced tools
Comparing version 0.1.6 to 1.0.0
192
index.js
@@ -1,140 +0,98 @@ | ||
var fs = require('fs'); | ||
var ltrim = require('underscore.string').ltrim; | ||
/** | ||
* Module dependencies | ||
*/ | ||
var helpBuildDictionary = require('./lib/help-build-dictionary'); | ||
var helpIncludeAllSync = require('./lib/help-include-all-sync'); | ||
// Returns false if the directory doesn't exist | ||
module.exports = function requireAll(options) { | ||
var files; | ||
var modules = {}; | ||
if (typeof(options.force) == 'undefined') { | ||
options.force = true; | ||
} | ||
/** | ||
* includeAll | ||
* | ||
* An easy way to include all node.js modules within a directory. | ||
* | ||
* > Used by the module loader in Sails core. | ||
*/ | ||
// Sane default for `filter` option | ||
if (!options.filter) { | ||
options.filter = /(.*)/; | ||
} | ||
// Reset our depth counter the first time | ||
if (typeof options._depth === 'undefined') { | ||
options._depth = 0; | ||
} | ||
/** | ||
* Build a dictionary of named modules | ||
* (default usage-- see options in `README.md`) | ||
* | ||
* @param {Dictionary} options | ||
*/ | ||
// Bail out if our counter has reached the desired depth | ||
// indicated by the user in options.depth | ||
if (typeof options.depth !== 'undefined' && | ||
options._depth >= options.depth) { | ||
return; | ||
} | ||
module.exports = function includeAllSync(options) { | ||
// This is the original, pre-v1 `include-all` usage. | ||
return helpIncludeAllSync(options); | ||
}; | ||
// Remember the starting directory | ||
if (!options.startDirname) { | ||
options.startDirname = options.dirname; | ||
} | ||
try { | ||
files = fs.readdirSync(options.dirname); | ||
} catch (e) { | ||
if (options.optional) return {}; | ||
else throw new Error('Directory not found: ' + options.dirname); | ||
} | ||
////////////////////////////////////////////////////////////////////////////// | ||
// The rest of the methods below are all originally from sails-build-dictionary. | ||
// They are asynchronous, and besides defaulting certain options, they do a | ||
// handful of extra things. So it's more than just options getting defaulted! | ||
////////////////////////////////////////////////////////////////////////////// | ||
// Iterate through files in the current directory | ||
files.forEach(function(file) { | ||
var filepath = options.dirname + '/' + file; | ||
// For directories, continue to recursively include modules | ||
if (fs.statSync(filepath).isDirectory()) { | ||
/** | ||
* Build a dictionary of named modules | ||
* (responds with an error if the container cannot be loaded) | ||
* | ||
* WARNING: THIS PARTICULAR CONVENIENCE METHOD WILL LIKELY BE DEPRECATED. | ||
* (it's not actually being used anywhere in core) | ||
* | ||
* @param {Dictionary} options | ||
* @param {Function} cb | ||
*/ | ||
// Ignore explicitly excluded directories | ||
if (excludeDirectory(file)) return; | ||
module.exports.required = function(options, cb) { | ||
return helpBuildDictionary(options, cb); | ||
}; | ||
// Recursively call requireAll on each child directory | ||
modules[file] = requireAll({ | ||
dirname: filepath, | ||
filter: options.filter, | ||
pathFilter: options.pathFilter, | ||
excludeDirs: options.excludeDirs, | ||
startDirname: options.startDirname, | ||
dontLoad: options.dontLoad, | ||
markDirectories: options.markDirectories, | ||
flattenDirectories: options.flattenDirectories, | ||
keepDirectoryPath: options.keepDirectoryPath, | ||
force: options.force, | ||
// Keep track of depth | ||
_depth: options._depth+1, | ||
depth: options.depth | ||
}); | ||
if (options.markDirectories || options.flattenDirectories) { | ||
modules[file].isDirectory = true; | ||
} | ||
/** | ||
* Build a dictionary of named modules | ||
* (fails silently-- returns {} if the container cannot be loaded) | ||
* | ||
* @param {Dictionary} options | ||
* @param {Function} cb | ||
*/ | ||
if (options.flattenDirectories) { | ||
module.exports.optional = function(options, cb) { | ||
options.optional = true; | ||
return helpBuildDictionary(options, cb); | ||
}; | ||
modules = (function flattenDirectories(modules, accum, path) { | ||
accum = accum || {}; | ||
Object.keys(modules).forEach(function(identity) { | ||
if (typeof(modules[identity]) !== 'object' && typeof(modules[identity]) !== 'function') { | ||
return; | ||
} | ||
if (modules[identity].isDirectory) { | ||
flattenDirectories(modules[identity], accum, path ? path + '/' + identity : identity ); | ||
} else { | ||
accum[options.keepDirectoryPath ? (path ? path + '/' + identity : identity) : identity] = modules[identity]; | ||
} | ||
}); | ||
return accum; | ||
})(modules); | ||
} | ||
} | ||
// For files, go ahead and add the code to the module map | ||
else { | ||
/** | ||
* Build a dictionary indicating whether the matched modules exist | ||
* (fails silently-- returns {} if the container cannot be loaded) | ||
* | ||
* @param {Dictionary} options | ||
* @param {Function} cb | ||
*/ | ||
// Key name for module | ||
var identity; | ||
module.exports.exists = function(options, cb) { | ||
options.optional = true; | ||
options.dontLoad = false; | ||
return helpBuildDictionary(options, cb); | ||
}; | ||
// 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 = '/' + ltrim(path, '/'); | ||
/** | ||
* Build a single module dictionary by extending {} with the contents of each module | ||
* (fail silently-- returns {} if the container cannot be loaded) | ||
* | ||
* @param {Dictionary} options | ||
* @param {Function} cb | ||
*/ | ||
var pathMatch = path.match(options.pathFilter); | ||
if (!pathMatch) return; | ||
identity = pathMatch[2]; | ||
} | ||
// Load module into memory (unless `dontLoad` is true) | ||
if (options.dontLoad) { | ||
modules[identity] = true; | ||
} else { | ||
if (options.force) { | ||
var resolved = require.resolve(filepath); | ||
if (require.cache[resolved]) delete require.cache[resolved]; | ||
} | ||
modules[identity] = require(filepath); | ||
} | ||
} | ||
}); | ||
// Pass map of modules back to app code | ||
return modules; | ||
function excludeDirectory(dirname) { | ||
return options.excludeDirs && dirname.match(options.excludeDirs); | ||
} | ||
}; | ||
module.exports.aggregate = function(options, cb) { | ||
options.aggregate = true; | ||
options.optional = true; | ||
return helpBuildDictionary(options, cb); | ||
}; |
{ | ||
"name": "include-all", | ||
"version": "0.1.6", | ||
"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**.", | ||
"version": "1.0.0", | ||
"description": "An easy way to include all node.js modules within a directory.", | ||
"main": "index.js", | ||
@@ -10,3 +10,3 @@ "directories": { | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "node ./node_modules/mocha/bin/mocha" | ||
}, | ||
@@ -18,14 +18,16 @@ "repository": { | ||
"keywords": [ | ||
"require-all", | ||
"include-all", | ||
"include", | ||
"require-all", | ||
"require", | ||
"directory" | ||
], | ||
"author": "felixge+thlorenz, adapted by Mike McNeil", | ||
"license": "MIT", | ||
"dependencies": { | ||
"underscore.string": "2.3.1" | ||
"lodash": "3.10.1" | ||
}, | ||
"author": "felixge+thlorenz, adapted by Mike McNeil", | ||
"license": "MIT", | ||
"readmeFilename": "Readme.md" | ||
"devDependencies": { | ||
"mocha": "3.0.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
146972
60
2924
1
329
1
2
+ Addedlodash@3.10.1
+ Addedlodash@3.10.1(transitive)
- Removedunderscore.string@2.3.1
- Removedunderscore.string@2.3.1(transitive)