require-dir
Advanced tools
Comparing version 0.3.2 to 1.0.0
53
index.js
// requireDir.js | ||
// See README.md for details. | ||
var FS = require('fs'); | ||
var Path = require('path'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -12,3 +12,3 @@ // make a note of the calling file's path, so that we can resolve relative | ||
var parentFile = parent.filename; | ||
var parentDir = Path.dirname(parentFile); | ||
var parentDir = path.dirname(parentFile); | ||
delete require.cache[__filename]; | ||
@@ -22,7 +22,7 @@ | ||
// resolve the path to an absolute one: | ||
dir = Path.resolve(parentDir, dir); | ||
dir = path.resolve(parentDir, dir); | ||
// read the directory's files: | ||
// note that this'll throw an error if the path isn't a directory. | ||
var files = FS.readdirSync(dir); | ||
var files = fs.readdirSync(dir); | ||
@@ -35,5 +35,4 @@ // to prioritize between multiple files with the same basename, we'll | ||
var file = files[i]; | ||
var ext = Path.extname(file); | ||
var base = Path.basename(file, ext); | ||
var ext = path.extname(file); | ||
var base = path.basename(file, ext); | ||
(filesForBase[base] = filesForBase[base] || []).push(file); | ||
@@ -66,10 +65,14 @@ } | ||
var file = files[i]; | ||
var path = Path.resolve(dir, file); | ||
var abs = path.resolve(dir, file); | ||
// ignore the calling file: | ||
if (path === parentFile) { | ||
if (abs === parentFile) { | ||
continue; | ||
} | ||
// apply file filter: | ||
if (opts.filter && !opts.filter(abs)) { | ||
continue; | ||
} | ||
if (FS.statSync(path).isDirectory()) { | ||
if (fs.statSync(abs).isDirectory()) { | ||
if (opts.recurse) { | ||
@@ -80,3 +83,3 @@ if (base === 'node_modules') { | ||
map[base] = requireDir(path, opts); | ||
map[base] = requireDir(abs, opts); | ||
@@ -89,3 +92,3 @@ // if duplicates are wanted, key off the full name too: | ||
} else { | ||
filesMinusDirs[file] = path; | ||
filesMinusDirs[file] = abs; | ||
} | ||
@@ -110,8 +113,8 @@ } | ||
var file = base + ext; | ||
var path = filesMinusDirs[file]; | ||
var abs = filesMinusDirs[file]; | ||
if (path) { | ||
if (abs) { | ||
// ignore TypeScript declaration files. They should never be | ||
// `require`d | ||
if (/\.d\.ts$/.test(path)) { | ||
if (/\.d\.ts$/.test(abs)) { | ||
continue; | ||
@@ -125,3 +128,3 @@ } | ||
if (opts.duplicates) { | ||
map[file] = require(path); | ||
map[file] = require(abs); | ||
if (!map[base]) { | ||
@@ -131,3 +134,3 @@ map[base] = map[file]; | ||
} else { | ||
map[base] = require(path); | ||
map[base] = require(abs); | ||
break; | ||
@@ -139,3 +142,3 @@ } | ||
if (opts.camelcase) { | ||
if (opts.mapKey || opts.mapValue) { | ||
for (var base in map) { | ||
@@ -147,13 +150,9 @@ // protect against enumerable object prototype extensions: | ||
map[toCamelCase(base)] = map[base]; | ||
var newKey = opts.mapKey ? opts.mapKey(map[base], base) : base; | ||
var newVal = opts.mapValue ? opts.mapValue(map[base], newKey) : map[base]; | ||
delete map[base]; | ||
map[newKey] = newVal; | ||
} | ||
} | ||
return map; | ||
}; | ||
function toCamelCase(str) { | ||
return str.replace(/[_-][a-z]/ig, function (s) { | ||
return s.substring(1).toUpperCase(); | ||
}); | ||
} |
@@ -1,24 +0,24 @@ | ||
{ "name": "require-dir" | ||
, "description": "Helper to require() directories." | ||
, "version": "0.3.2" | ||
, "author": "Aseem Kishore <aseem.kishore@gmail.com>" | ||
, "license": "MIT" | ||
, "dependencies": {} | ||
, "devDependencies": | ||
{ "coffee-script": "~1.3.3" | ||
, "mkdirp": "^0.5.0" | ||
, "ts-node": "^1.3.0" | ||
, "typescript": "^1.8.0" | ||
{ | ||
"name": "require-dir", | ||
"description": "Helper to require() directories.", | ||
"version": "1.0.0", | ||
"author": "Aseem Kishore <aseem.kishore@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"coffee-script": "^1.3.3", | ||
"ts-node": "^1.3.0", | ||
"typescript": "^1.8.0" | ||
}, | ||
"engines": { | ||
"node": "*" | ||
}, | ||
"scripts": { | ||
"test": "node test" | ||
}, | ||
"homepage": "https://github.com/aseemk/requireDir", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/aseemk/requireDir.git" | ||
} | ||
, "engines": | ||
{ "node": "*" | ||
} | ||
, "scripts": | ||
{ "test": "node test" | ||
} | ||
, "homepage": "https://github.com/aseemk/requireDir" | ||
, "repository": | ||
{ "type": "git" | ||
, "url": "git://github.com/aseemk/requireDir.git" | ||
} | ||
} |
@@ -31,3 +31,3 @@ [![Build Status](https://travis-ci.org/aseemk/requireDir.svg?branch=master)](https://travis-ci.org/aseemk/requireDir) | ||
And if CoffeeScript has been registered via `require('coffee-script/register')`, | ||
`c.coffee` will also be returned. | ||
`c.coffee` will also be returned. Any extension registered with node will work the same way without any additional configuration. | ||
@@ -64,6 +64,2 @@ ## Installation | ||
`camelcase`: Automatically add camelcase aliases for files with dash- and | ||
underscore-separated names. E.g. `foo-bar.js` will be exposed under both the | ||
original `'foo-bar'` name as well as a `'fooBar'` alias. Default is false. | ||
`duplicates`: By default, if multiple files share the same basename, only the | ||
@@ -88,4 +84,32 @@ highest priority one is `require()`'d and returned. (Priority is determined by | ||
There might be more options in the future. ;) | ||
`filter`: Apply a filter on the filename before require-ing. For example, ignoring files prefixed with `dev` in a production environment: | ||
```js | ||
requireDir('./dir', { | ||
filter: function (fullPath) { | ||
return process.env.NODE_ENV !== 'production' && !fullPath.match(/$dev/); | ||
} | ||
}) | ||
``` | ||
`mapKey`: Apply a transform to the module base name after require-ing. For example, uppercasing any module names: | ||
```js | ||
requireDir('./dir', { | ||
mapKey: function (value, baseName) { | ||
return baseName.toUpperCase(); | ||
} | ||
}) | ||
``` | ||
`mapValue`: Apply a transform to the value after require-ing. For example, uppercasing any text exported: | ||
```js | ||
requireDir('./dir', { | ||
mapValue: function (value, baseName) { | ||
return typeof value === 'string' ? value.toUpperCase() : value; | ||
} | ||
}) | ||
``` | ||
## Tips | ||
@@ -92,0 +116,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
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
15794
3
36
294
0
142
1