import-modules
Advanced tools
Comparing version 1.1.0 to 2.0.0
50
index.js
@@ -5,16 +5,23 @@ 'use strict'; | ||
// Prevent caching of this module so module.parent is always accurate | ||
// Prevent caching of this module so module.parent is always accurate. | ||
delete require.cache[__filename]; | ||
const parentFile = module.parent.filename; | ||
const parentDir = path.dirname(parentFile); | ||
const parentFile = module.parent && module.parent.filename; | ||
const parentDirectory = path.dirname(parentFile || '.'); | ||
module.exports = (dir, opts) => { | ||
dir = path.resolve(parentDir, dir || ''); | ||
opts = Object.assign({camelize: true}, opts); | ||
// The default file extensions used by `require()`. | ||
const fileExtensions = new Set(['.js', '.json', '.node']); | ||
module.exports = (directory, options) => { | ||
directory = path.resolve(parentDirectory, directory || ''); | ||
options = { | ||
camelize: true, | ||
fileExtensions, | ||
...options | ||
}; | ||
let files; | ||
try { | ||
files = fs.readdirSync(dir); | ||
} catch (err) { | ||
files = fs.readdirSync(directory); | ||
} catch (_) { | ||
return {}; | ||
@@ -24,27 +31,26 @@ } | ||
const done = new Set(); | ||
const ret = {}; | ||
const returnValue = {}; | ||
// Adhere to the Node.js require algorithm by trying each extension in order | ||
for (const ext of Object.keys(require.extensions)) { | ||
for (const fileExtension of options.fileExtensions) { | ||
for (const file of files) { | ||
const stem = path.basename(file).replace(/\.\w+$/, ''); | ||
const fullPath = path.join(dir, file); | ||
const filenameStem = path.basename(file).replace(/\.\w+$/, ''); | ||
const fullPath = path.join(directory, file); | ||
if (done.has(stem) || | ||
if (done.has(filenameStem) || | ||
fullPath === parentFile || | ||
path.extname(file) !== ext || | ||
stem[0] === '_' || | ||
stem[0] === '.') { | ||
path.extname(file) !== fileExtension || | ||
filenameStem[0] === '_' || | ||
filenameStem[0] === '.') { | ||
continue; | ||
} | ||
const exportKey = opts.camelize ? stem.replace(/-(\w)/g, (m, p1) => p1.toUpperCase()) : stem; | ||
const exportKey = options.camelize ? filenameStem.replace(/-(\w)/g, (m, p1) => p1.toUpperCase()) : filenameStem; | ||
ret[exportKey] = require(fullPath); | ||
done.add(stem); | ||
returnValue[exportKey] = require(fullPath); | ||
done.add(filenameStem); | ||
} | ||
} | ||
return ret; | ||
return returnValue; | ||
}; | ||
{ | ||
"name": "import-modules", | ||
"version": "1.1.0", | ||
"description": "Import all modules in a directory", | ||
"license": "MIT", | ||
"repository": "sindresorhus/import-files", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"import", | ||
"require", | ||
"files", | ||
"modules", | ||
"all", | ||
"directory", | ||
"dir", | ||
"folder", | ||
"js", | ||
"paths", | ||
"multiple", | ||
"index" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
} | ||
"name": "import-modules", | ||
"version": "2.0.0", | ||
"description": "Import all modules in a directory", | ||
"license": "MIT", | ||
"repository": "sindresorhus/import-files", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"import", | ||
"require", | ||
"files", | ||
"modules", | ||
"all", | ||
"directory", | ||
"folder", | ||
"js", | ||
"paths", | ||
"multiple", | ||
"index" | ||
], | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # import-modules [![Build Status](https://travis-ci.org/sindresorhus/import-modules.svg?branch=master)](https://travis-ci.org/sindresorhus/import-modules) | ||
``` | ||
$ npm install --save import-modules | ||
$ npm install import-modules | ||
``` | ||
@@ -27,4 +27,5 @@ | ||
const importModules = require('import-modules'); | ||
const modules = importModules('dir'); | ||
const modules = importModules('directory'); | ||
console.log(modules); | ||
@@ -37,3 +38,3 @@ //=> {fooBar: [Function], bazFaz: [Function]} | ||
### importModules([directory], [options]) | ||
### importModules(directory?, options?) | ||
@@ -45,7 +46,7 @@ #### directory | ||
Directory to import modules from. Unless you've modified [`require.extensions`](https://nodejs.org/api/globals.html#globals_require_extensions), that means any `.js`, `.json`, `.node` files, in that order. Does not recurse. Ignores the caller file and files starting with `.` or `_`. | ||
Directory to import modules from. Unless you've set the `fileExtensions` option, that means any `.js`, `.json`, `.node` files, in that order. Does not recurse. Ignores the caller file and files starting with `.` or `_`. | ||
#### options | ||
Type: `Object` | ||
Type: `object` | ||
@@ -59,3 +60,10 @@ ##### camelize | ||
##### fileExtensions | ||
Type: `string[]`<br> | ||
Default: `['.js', '.json', '.node']` | ||
File extensions to look for. Order matters. | ||
## Related | ||
@@ -66,6 +74,1 @@ | ||
- [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
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
4516
42
69