pax_global_header00006660000000000000000000000064127755345010014523gustar00rootroot0000000000000052 comment=96940cfd3ed6c4cbbda18da5fd74bf87876fb1be requireDir-0.3.1/000077500000000000000000000000001277553450100136375ustar00rootroot00000000000000requireDir-0.3.1/.gitignore000066400000000000000000000000321277553450100156220ustar00rootroot00000000000000npm-debug.log node_modulesrequireDir-0.3.1/.travis.yml000066400000000000000000000000631277553450100157470ustar00rootroot00000000000000language: node_js node_js: - "6" - "5" - "4" requireDir-0.3.1/LICENSE000066400000000000000000000020751277553450100146500ustar00rootroot00000000000000The 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-0.3.1/README.md000066400000000000000000000070141277553450100151200ustar00rootroot00000000000000[![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') } ``` And if CoffeeScript has been registered via `require('coffee-script/register')`, `c.coffee` will also be returned. ## 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. `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 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. E.g. 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') } ``` There might be more options in the future. ;) ## Tips If you want to `require()` the same directory in multiple places, you can do this in the directory itself! Just make an `index.js` file with the following: ```js module.exports = require('require-dir')(); // defaults to '.' ``` And don't worry, the calling file is always ignored to prevent infinite loops. ## TODO It'd be awesome if this could work with the regular `require()`, e.g. like a regular `require()` hook. Not sure that's possible though; directories are already special-cased to look for an `index` file or `package.json`. An `ignore` option would be nice: a string or regex, or an array of either or both, of paths, relative to the directory, to ignore. String paths can be extensionless to ignore all extensions for that path. Supporting shell-style globs in string paths would be nice. Currently, basenames are derived for directories too — e.g. a directory named `a.txt` will be returned as `a` when recursing — but should that be the case? Maybe directories should always be returned by their full name, and/or maybe this behavior should be customizable. This is hopefully an edge case. ## License MIT. © 2012-2015 Aseem Kishore. requireDir-0.3.1/index.js000066400000000000000000000117341277553450100153120ustar00rootroot00000000000000// 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 = {}; 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 path = Path.resolve(dir, file); // ignore the calling file: if (path === parentFile) { continue; } if (FS.statSync(path).isDirectory()) { if (opts.recurse) { if (base === 'node_modules') { continue; } map[base] = requireDir(path, opts); // if duplicates are wanted, key off the full name too: if (opts.duplicates) { map[file] = map[base]; } } } else { filesMinusDirs[file] = path; } } // 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 in require.extensions) { // again protect against enumerable object prototype extensions: if (!require.extensions.hasOwnProperty(ext)) { continue; } // if a file exists with this extension, we'll require() it: var file = base + ext; var path = filesMinusDirs[file]; if (path) { // ignore TypeScript declaration files. They should never be // `require`d if (/\.d\.ts$/.test(path)) { continue; } // 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(path); if (!map[base]) { map[base] = map[file]; } } else { map[base] = require(path); break; } } } } if (opts.camelcase) { for (var base in map) { // protect against enumerable object prototype extensions: if (!map.hasOwnProperty(base)) { continue; } map[toCamelCase(base)] = map[base]; } } return map; }; function toCamelCase(str) { return str.replace(/[_-][a-z]/ig, function (s) { return s.substring(1).toUpperCase(); }); } requireDir-0.3.1/package.json000066400000000000000000000010331277553450100161220ustar00rootroot00000000000000{ "name": "require-dir" , "description": "Helper to require() directories." , "version": "0.3.1" , "author": "Aseem Kishore " , "license": "MIT" , "dependencies": {} , "devDependencies": { "coffee-script": "~1.3.3" , "mkdirp": "^0.5.0" , "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-0.3.1/test/000077500000000000000000000000001277553450100146165ustar00rootroot00000000000000requireDir-0.3.1/test/camelcase.js000066400000000000000000000006011277553450100170660ustar00rootroot00000000000000var assert = require('assert'); var requireDir = require('..'); assert.deepEqual(requireDir('./camelcase', { recurse: true, camelcase: true }), { aMain: 'a main', 'a_main': 'a main', subDir: { aSub: 'a sub', 'a-sub': 'a sub' }, 'sub-dir': { aSub: 'a sub', 'a-sub': 'a sub' } }); console.log('Camelcase tests passed.'); requireDir-0.3.1/test/camelcase/000077500000000000000000000000001277553450100165335ustar00rootroot00000000000000requireDir-0.3.1/test/camelcase/a_main.js000066400000000000000000000000331277553450100203110ustar00rootroot00000000000000module.exports = 'a main'; requireDir-0.3.1/test/camelcase/sub-dir/000077500000000000000000000000001277553450100201005ustar00rootroot00000000000000requireDir-0.3.1/test/camelcase/sub-dir/a-sub.js000066400000000000000000000000321277553450100214400ustar00rootroot00000000000000module.exports = 'a sub'; requireDir-0.3.1/test/duplicates.js000066400000000000000000000017101277553450100173100ustar00rootroot00000000000000var 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-0.3.1/test/duplicates/000077500000000000000000000000001277553450100167535ustar00rootroot00000000000000requireDir-0.3.1/test/duplicates/a.js000066400000000000000000000000311277553450100175230ustar00rootroot00000000000000module.exports = 'a.js'; requireDir-0.3.1/test/duplicates/b.json000066400000000000000000000000111277553450100200570ustar00rootroot00000000000000"b.json" requireDir-0.3.1/test/duplicates/b/000077500000000000000000000000001277553450100171745ustar00rootroot00000000000000requireDir-0.3.1/test/duplicates/b/1.js000066400000000000000000000000311277553450100176640ustar00rootroot00000000000000module.exports = '1.js'; requireDir-0.3.1/test/duplicates/b/1.txt000066400000000000000000000000101277553450100200640ustar00rootroot00000000000000'1.txt' requireDir-0.3.1/test/duplicates/b/2.js000066400000000000000000000000311277553450100176650ustar00rootroot00000000000000module.exports = '2.js'; requireDir-0.3.1/test/duplicates/b/2.json000066400000000000000000000000111277553450100202200ustar00rootroot00000000000000"2.json" requireDir-0.3.1/test/duplicates/c.txt000066400000000000000000000000101277553450100177250ustar00rootroot00000000000000'c.txt' requireDir-0.3.1/test/duplicates/c/000077500000000000000000000000001277553450100171755ustar00rootroot00000000000000requireDir-0.3.1/test/duplicates/c/3.json000066400000000000000000000000111277553450100202220ustar00rootroot00000000000000"3.json" requireDir-0.3.1/test/duplicates/d.js000066400000000000000000000000311277553450100175260ustar00rootroot00000000000000module.exports = 'd.js'; requireDir-0.3.1/test/duplicates/d.json000066400000000000000000000000111277553450100200610ustar00rootroot00000000000000"d.json" requireDir-0.3.1/test/index.js000066400000000000000000000000701277553450100162600ustar00rootroot00000000000000require('..')('.'); console.log('\nAll tests passed!'); requireDir-0.3.1/test/recurse.js000066400000000000000000000012621277553450100166250ustar00rootroot00000000000000var 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-0.3.1/test/recurse/000077500000000000000000000000001277553450100162665ustar00rootroot00000000000000requireDir-0.3.1/test/recurse/a.js000066400000000000000000000000261277553450100170420ustar00rootroot00000000000000module.exports = 'a'; requireDir-0.3.1/test/recurse/b/000077500000000000000000000000001277553450100165075ustar00rootroot00000000000000requireDir-0.3.1/test/recurse/b/1/000077500000000000000000000000001277553450100166475ustar00rootroot00000000000000requireDir-0.3.1/test/recurse/b/1/bar.json000066400000000000000000000000061277553450100203020ustar00rootroot00000000000000"bar" requireDir-0.3.1/test/recurse/b/1/foo.js000066400000000000000000000000301277553450100177610ustar00rootroot00000000000000module.exports = 'foo'; requireDir-0.3.1/test/recurse/b/2/000077500000000000000000000000001277553450100166505ustar00rootroot00000000000000requireDir-0.3.1/test/recurse/b/2/baz.txt000066400000000000000000000000041277553450100201570ustar00rootroot00000000000000baz requireDir-0.3.1/test/recurse/c/000077500000000000000000000000001277553450100165105ustar00rootroot00000000000000requireDir-0.3.1/test/recurse/c/3.json000066400000000000000000000000021277553450100175350ustar00rootroot000000000000003 requireDir-0.3.1/test/recurse/node_modules/000077500000000000000000000000001277553450100207435ustar00rootroot00000000000000requireDir-0.3.1/test/recurse/node_modules/fake.js000066400000000000000000000000311277553450100222010ustar00rootroot00000000000000module.exports = 'fake'; requireDir-0.3.1/test/simple.js000066400000000000000000000013471277553450100164520ustar00rootroot00000000000000var 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'); 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-0.3.1/test/simple/000077500000000000000000000000001277553450100161075ustar00rootroot00000000000000requireDir-0.3.1/test/simple/a.js000066400000000000000000000000261277553450100166630ustar00rootroot00000000000000module.exports = 'a'; requireDir-0.3.1/test/simple/b.json000066400000000000000000000000041277553450100172150ustar00rootroot00000000000000"b" requireDir-0.3.1/test/simple/c.coffee000066400000000000000000000000251277553450100174770ustar00rootroot00000000000000module.exports = 'c' requireDir-0.3.1/test/simple/d.txt000066400000000000000000000000021277553450100170630ustar00rootroot00000000000000d requireDir-0.3.1/test/simple/e.ts000066400000000000000000000000161277553450100167000ustar00rootroot00000000000000export = 'e'; requireDir-0.3.1/test/simple/f.d.ts000066400000000000000000000000311277553450100171200ustar00rootroot00000000000000export const foo: 'bar';