pax_global_header00006660000000000000000000000064133770226220014516gustar00rootroot0000000000000052 comment=21600ade210ad00ba144658188c588e06e53d7c6 requireDir-1.2.0/000077500000000000000000000000001337702262200136315ustar00rootroot00000000000000requireDir-1.2.0/.gitignore000066400000000000000000000000671337702262200156240ustar00rootroot00000000000000npm-debug.log node_modules package-lock.json yarn.lock requireDir-1.2.0/.travis.yml000066400000000000000000000000621337702262200157400ustar00rootroot00000000000000language: node_js node_js: - "node" - "lts/*" requireDir-1.2.0/CHANGELOG.md000066400000000000000000000002751337702262200154460ustar00rootroot00000000000000## 1.2.0 - extensions option added ## 1.0.0 - mapValue option added - mapKey option added - filter option added - camelcase option removed, use mapKey to achieve the same functionality requireDir-1.2.0/LICENSE000066400000000000000000000020751337702262200146420ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2012-2015 Aseem Kishore Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. requireDir-1.2.0/README.md000066400000000000000000000067701337702262200151220ustar00rootroot00000000000000[![Build Status](https://travis-ci.org/aseemk/requireDir.svg?branch=master)](https://travis-ci.org/aseemk/requireDir) [![npm version](https://badge.fury.io/js/require-dir.svg)](http://badge.fury.io/js/require-dir) # requireDir() Node helper to `require()` directories. The directory's files are examined, and each one that can be `require()`'d is `require()`'d and returned as part of a hash from that file's basename to its exported contents. ## Example Given this directory structure: ``` dir + a.js + b.json + c.coffee + d.txt ``` `requireDir('./dir')` will return the equivalent of: ```js { a: require('./dir/a.js'), b: require('./dir/b.json') } ``` If CoffeeScript is registered via `require('coffee-script/register')`, `c.coffee` will also be returned. Any extension registered with node will work the same way without any additional configuration. ## Installation ``` npm install require-dir ``` Note that this package is *not* `requireDir` — turns out that's already [taken](https://github.com/JamesEggers1/node-requiredir)! ;) ## Usage Basic usage that examines only directories' immediate files: ```js var requireDir = require('require-dir'); var dir = requireDir('./path/to/dir'); ``` You can optionally customize the behavior by passing an extra options object: ```js var dir = requireDir('./path/to/dir', { recurse: true }); ``` ## Options `recurse`: Whether to recursively `require()` subdirectories too. (`node_modules` within subdirectories will be ignored.) Default is false. `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; } }) ``` `duplicates`: By default, if multiple files share the same basename, only the highest priority one is `require()`'d and returned. (Priority is determined by the order of `require.extensions` keys, with directories taking precedence over files if `recurse` is true.) Specifying this option `require()`'s all files and returns full filename keys in addition to basename keys. Default is false. In the example above, if there were also an `a.json`, the behavior would be the same by default, but specifying `duplicates: true` would yield: ```js { a: require('./dir/a.js'), 'a.js': require('./dir/a.js'), 'a.json': require('./dir/a.json'), b: require('./dir/b.json'), 'b.json': require('./dir/b.json') } ``` `noCache`: Prevent file caching. Could be useful using gulp.watch or other watch requiring refreshed file content Default is false. ```js requireDir('./dir', { noCache: true }) ``` `extensions`: Array of extensions to look for instead of using `require.extensions`. ```js requireDir('./dir', { extensions: ['.js', '.json'] }) ``` ## Tips Make an `index.js` in a directory with this code to clean things up: ```js module.exports = require('require-dir')(); // defaults to '.' ``` And don't worry, the calling file is always ignored to prevent infinite loops. requireDir-1.2.0/index.js000066400000000000000000000123231337702262200152770ustar00rootroot00000000000000// requireDir.js // See README.md for details. var fs = require('fs'); var path = require('path'); // make a note of the calling file's path, so that we can resolve relative // paths. this only works if a fresh version of this module is run on every // require(), so important: we clear the require() cache each time! var parent = module.parent; var parentFile = parent.filename; var parentDir = path.dirname(parentFile); delete require.cache[__filename]; module.exports = function requireDir(dir, opts) { // default arguments: dir = dir || '.'; opts = opts || {}; // resolve the path to an absolute one: 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); // to prioritize between multiple files with the same basename, we'll // first derive all the basenames and create a map from them to files: var filesForBase = {}; for (var i = 0; i < files.length; i++) { var file = files[i]; var ext = path.extname(file); var base = path.basename(file, ext); (filesForBase[base] = filesForBase[base] || []).push(file); } // then we'll go through each basename, and first check if any of the // basenames' files are directories, since directories take precedence if // we're recursing and can be ignored if we're not. if a basename has no // directory, then we'll follow Node's own require() algorithm of going // through and trying the require.extension keys in order. in the process, // we create and return a map from basename to require()'d contents! and // if duplicates are asked for, we'll never short-circuit; we'll just add // to the map using the full filename as a key also. var map = {}; // get the array of extensions we need to require var extensions = opts.extensions || Object.keys(require.extensions); for (var base in filesForBase) { // protect against enumerable object prototype extensions: if (!filesForBase.hasOwnProperty(base)) { continue; } // go through the files for this base and check for directories. we'll // also create a hash "set" of the non-dir files so that we can // efficiently check for existence in the next step: var files = filesForBase[base]; var filesMinusDirs = {}; for (var i = 0; i < files.length; i++) { var file = files[i]; var abs = path.resolve(dir, file); // ignore the calling file: if (abs === parentFile) { continue; } // apply file filter: if (opts.filter && !opts.filter(abs)) { continue; } if (fs.statSync(abs).isDirectory()) { if (opts.recurse) { if (base === 'node_modules') { continue; } map[base] = requireDir(abs, opts); // if duplicates are wanted, key off the full name too: if (opts.duplicates) { map[file] = map[base]; } } } else { filesMinusDirs[file] = abs; } } // if we're recursing and we already encountered a directory for this // basename, we're done for this base if we're ignoring duplicates: if (map[base] && !opts.duplicates) { continue; } // otherwise, go through and try each require.extension key! for (ext of extensions) { // if a file exists with this extension, we'll require() it: var file = base + ext; var abs = filesMinusDirs[file]; if (abs) { // ignore TypeScript declaration files. They should never be // `require`d if (/\.d\.ts$/.test(abs)) { continue; } // delete cache if (opts.noCache) { delete require.cache[abs]; } // if duplicates are wanted, key off the full name always, and // also the base if it hasn't been taken yet (since this ext // has higher priority than any that follow it). if duplicates // aren't wanted, we're done with this basename. if (opts.duplicates) { map[file] = require(abs); if (!map[base]) { map[base] = map[file]; } } else { map[base] = require(abs); break; } } } } if (opts.mapKey || opts.mapValue) { for (var base in map) { // protect against enumerable object prototype extensions: if (!map.hasOwnProperty(base)) { continue; } 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; }; requireDir-1.2.0/package.json000066400000000000000000000010331337702262200161140ustar00rootroot00000000000000{ "name": "require-dir", "description": "Helper to require() directories.", "version": "1.2.0", "author": "Aseem Kishore ", "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" } } requireDir-1.2.0/test/000077500000000000000000000000001337702262200146105ustar00rootroot00000000000000requireDir-1.2.0/test/camelcase/000077500000000000000000000000001337702262200165255ustar00rootroot00000000000000requireDir-1.2.0/test/camelcase/a_main.js000066400000000000000000000000331337702262200203030ustar00rootroot00000000000000module.exports = 'a main'; requireDir-1.2.0/test/camelcase/sub-dir/000077500000000000000000000000001337702262200200725ustar00rootroot00000000000000requireDir-1.2.0/test/camelcase/sub-dir/a-sub.js000066400000000000000000000000321337702262200214320ustar00rootroot00000000000000module.exports = 'a sub'; requireDir-1.2.0/test/duplicates.js000066400000000000000000000017101337702262200173020ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); // first test without recursing *or* duplicates: assert.deepEqual(requireDir('./duplicates'), { a: 'a.js', b: 'b.json', d: 'd.js', }); // then test with duplicates but without recursing: assert.deepEqual(requireDir('./duplicates', {duplicates: true}), { a: 'a.js', 'a.js': 'a.js', b: 'b.json', 'b.json': 'b.json', d: 'd.js', 'd.js': 'd.js', 'd.json': 'd.json', }); // finally, test with duplicates while recursing: assert.deepEqual(requireDir('./duplicates', {duplicates: true, recurse: true}), { a: 'a.js', 'a.js': 'a.js', b: { '1': '1.js', '1.js': '1.js', '2': '2.js', '2.js': '2.js', '2.json': '2.json', }, 'b.json': 'b.json', c: { '3': '3.json', '3.json': '3.json', }, d: 'd.js', 'd.js': 'd.js', 'd.json': 'd.json', }); console.log('Duplicate tests passed.'); requireDir-1.2.0/test/duplicates/000077500000000000000000000000001337702262200167455ustar00rootroot00000000000000requireDir-1.2.0/test/duplicates/a.js000066400000000000000000000000311337702262200175150ustar00rootroot00000000000000module.exports = 'a.js'; requireDir-1.2.0/test/duplicates/b.json000066400000000000000000000000111337702262200200510ustar00rootroot00000000000000"b.json" requireDir-1.2.0/test/duplicates/b/000077500000000000000000000000001337702262200171665ustar00rootroot00000000000000requireDir-1.2.0/test/duplicates/b/1.js000066400000000000000000000000311337702262200176560ustar00rootroot00000000000000module.exports = '1.js'; requireDir-1.2.0/test/duplicates/b/1.txt000066400000000000000000000000101337702262200200560ustar00rootroot00000000000000'1.txt' requireDir-1.2.0/test/duplicates/b/2.js000066400000000000000000000000311337702262200176570ustar00rootroot00000000000000module.exports = '2.js'; requireDir-1.2.0/test/duplicates/b/2.json000066400000000000000000000000111337702262200202120ustar00rootroot00000000000000"2.json" requireDir-1.2.0/test/duplicates/c.txt000066400000000000000000000000101337702262200177170ustar00rootroot00000000000000'c.txt' requireDir-1.2.0/test/duplicates/c/000077500000000000000000000000001337702262200171675ustar00rootroot00000000000000requireDir-1.2.0/test/duplicates/c/3.json000066400000000000000000000000111337702262200202140ustar00rootroot00000000000000"3.json" requireDir-1.2.0/test/duplicates/d.js000066400000000000000000000000311337702262200175200ustar00rootroot00000000000000module.exports = 'd.js'; requireDir-1.2.0/test/duplicates/d.json000066400000000000000000000000111337702262200200530ustar00rootroot00000000000000"d.json" requireDir-1.2.0/test/extensions.js000066400000000000000000000013601337702262200173450ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); // first test regularly: assert.deepEqual(requireDir('./simple'), { a: 'a', b: 'b', }); // only js files assert.deepEqual(requireDir('./simple', {extensions: ['.js']}), { a: 'a' }); // both js and json files assert.deepEqual(requireDir('./simple', {extensions: ['.js', '.json']}), { a: 'a', b: 'b' }); // then test with recursing: assert.deepEqual(requireDir('./recurse', {recurse: true, extensions: ['.js']}), { a: 'a', b: { '1': { foo: 'foo' }, '2': {} // note how the directory is always returned }, c: {} // note that node_modules was explicitly ignored }); console.log('Extensions tests passed.'); requireDir-1.2.0/test/filter.js000066400000000000000000000004231337702262200164320ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); // filter the results to a particular file: assert.deepEqual(requireDir('./simple', { filter: function (filename) { return filename.match(/a\.js$/); } }), { a: 'a' }); console.log('Filter tests passed.'); requireDir-1.2.0/test/index.js000066400000000000000000000000701337702262200162520ustar00rootroot00000000000000require('..')('.'); console.log('\nAll tests passed!'); requireDir-1.2.0/test/mapKey.js000066400000000000000000000012001337702262200163650ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); var mapper = function(v, f) { return f.toUpperCase(); }; // first test without recursing: assert.deepEqual(requireDir('./recurse', { mapKey: mapper }), { A: 'a', }); // then test with recursing: assert.deepEqual(requireDir('./recurse', { recurse: true, mapKey: mapper }), { A: 'a', B: { '1': { FOO: 'foo', BAR: 'bar', }, '2': {} // note how the directory is always returned }, C: { '3': 3 }, // note that node_modules was explicitly ignored }); console.log('mapKey tests passed.'); requireDir-1.2.0/test/mapValue.js000066400000000000000000000012551337702262200167230ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); var mapper = function(v, f) { if (typeof v === 'string') return v.toUpperCase(); return v; }; // first test without recursing: assert.deepEqual(requireDir('./recurse', { mapValue: mapper }), { a: 'A', }); // then test with recursing: assert.deepEqual(requireDir('./recurse', { recurse: true, mapValue: mapper }), { a: 'A', b: { '1': { foo: 'FOO', bar: 'BAR', }, '2': {} // note how the directory is always returned }, c: { '3': 3 }, // note that node_modules was explicitly ignored }); console.log('mapValue tests passed.'); requireDir-1.2.0/test/noCache.js000066400000000000000000000024351337702262200165120ustar00rootroot00000000000000var assert = require("assert"); var requireDir = require(".."); var fs = require("fs"); var cachedResult = { a: "a", b: "b" }; var notCachedResult = { a: "c", b: "b" }; // filter the results to a particular file: assert.deepEqual(requireDir("./noCache", { noCache: false }), cachedResult); var promiseFileModification = new Promise(function(resolve, reject) { fs.writeFile("test/noCache/a.js", "module.exports = 'c';", "ascii", function( error ) { if (error) { reject(error); } else { resolve(); } }); }); promiseFileModification.then( function() { // Check if cache is active that it is the same result assert.deepEqual(requireDir("./noCache", { noCache: false }), cachedResult); // Check by removing cache that the result is the new content assert.deepEqual( requireDir("./noCache", { noCache: true }), notCachedResult ); console.log("noCache tests passed."); fs.writeFile( "test/noCache/a.js", "module.exports = 'a';", "ascii", function(error) { if (error) { console.error("noCache tests, issue to reset test."); console.error(error); } } ); }, function(error) { console.error("noCache tests failed."); console.error(error); } ); requireDir-1.2.0/test/noCache/000077500000000000000000000000001337702262200161505ustar00rootroot00000000000000requireDir-1.2.0/test/noCache/a.js000066400000000000000000000000251337702262200167230ustar00rootroot00000000000000module.exports = 'a';requireDir-1.2.0/test/noCache/b.json000066400000000000000000000000041337702262200172560ustar00rootroot00000000000000"b" requireDir-1.2.0/test/recurse.js000066400000000000000000000012621337702262200166170ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); // first test without recursing: assert.deepEqual(requireDir('./recurse'), { a: 'a', }); // then test with recursing: assert.deepEqual(requireDir('./recurse', {recurse: true}), { a: 'a', b: { '1': { foo: 'foo', bar: 'bar', }, '2': {} // note how the directory is always returned }, c: { '3': 3 }, // note that node_modules was explicitly ignored }); // finally, test that node_modules can still be required directly: assert.deepEqual(requireDir('./recurse/node_modules'), { fake: 'fake', }); console.log('Recurse tests passed.'); requireDir-1.2.0/test/recurse/000077500000000000000000000000001337702262200162605ustar00rootroot00000000000000requireDir-1.2.0/test/recurse/a.js000066400000000000000000000000261337702262200170340ustar00rootroot00000000000000module.exports = 'a'; requireDir-1.2.0/test/recurse/b/000077500000000000000000000000001337702262200165015ustar00rootroot00000000000000requireDir-1.2.0/test/recurse/b/1/000077500000000000000000000000001337702262200166415ustar00rootroot00000000000000requireDir-1.2.0/test/recurse/b/1/bar.json000066400000000000000000000000061337702262200202740ustar00rootroot00000000000000"bar" requireDir-1.2.0/test/recurse/b/1/foo.js000066400000000000000000000000301337702262200177530ustar00rootroot00000000000000module.exports = 'foo'; requireDir-1.2.0/test/recurse/b/2/000077500000000000000000000000001337702262200166425ustar00rootroot00000000000000requireDir-1.2.0/test/recurse/b/2/baz.txt000066400000000000000000000000041337702262200201510ustar00rootroot00000000000000baz requireDir-1.2.0/test/recurse/c/000077500000000000000000000000001337702262200165025ustar00rootroot00000000000000requireDir-1.2.0/test/recurse/c/3.json000066400000000000000000000000021337702262200175270ustar00rootroot000000000000003 requireDir-1.2.0/test/recurse/node_modules/000077500000000000000000000000001337702262200207355ustar00rootroot00000000000000requireDir-1.2.0/test/recurse/node_modules/fake.js000066400000000000000000000000311337702262200221730ustar00rootroot00000000000000module.exports = 'fake'; requireDir-1.2.0/test/simple.js000066400000000000000000000013601337702262200164370ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); // first test regularly: assert.deepEqual(requireDir('./simple'), { a: 'a', b: 'b', }); // now register CoffeeScript and do it again: // note that CoffeeScript shouldn't be used by any other tests! we can't rely // on ordering of tests, and require.extensions and require.cache are global. require('coffee-script/register'); assert.deepEqual(requireDir('./simple'), { a: 'a', b: 'b', c: 'c', }); // now register TypeScript and do it again: // note that we include typescript files but not declarations. require('ts-node/register'); assert.deepEqual(requireDir('./simple'), { a: 'a', b: 'b', c: 'c', e: 'e', }); console.log('Simple tests passed.'); requireDir-1.2.0/test/simple/000077500000000000000000000000001337702262200161015ustar00rootroot00000000000000requireDir-1.2.0/test/simple/a.js000066400000000000000000000000261337702262200166550ustar00rootroot00000000000000module.exports = 'a'; requireDir-1.2.0/test/simple/b.json000066400000000000000000000000041337702262200172070ustar00rootroot00000000000000"b" requireDir-1.2.0/test/simple/c.coffee000066400000000000000000000000251337702262200174710ustar00rootroot00000000000000module.exports = 'c' requireDir-1.2.0/test/simple/d.txt000066400000000000000000000000021337702262200170550ustar00rootroot00000000000000d requireDir-1.2.0/test/simple/e.ts000066400000000000000000000000161337702262200166720ustar00rootroot00000000000000export = 'e'; requireDir-1.2.0/test/simple/f.d.ts000066400000000000000000000000311337702262200171120ustar00rootroot00000000000000export const foo: 'bar';