├── .gitignore ├── .travis.yml ├── README.md ├── index.js ├── package.json └── test ├── lib ├── .also_private │ └── private.txt ├── Foo.js ├── _private.js ├── bam.js ├── bar │ ├── f.js │ ├── fed │ │ ├── again.js │ │ ├── ignored.js │ │ ├── index.js │ │ └── somemore.js │ ├── fing.js │ └── index.js ├── index.js └── not_javascript.txt └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.10 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | Write minimal node index.js files that require and export siblings by file basename 4 | 5 | # Latest Version 6 | 7 | 1.2.0 8 | 9 | # Installation 10 | ``` 11 | npm install requireindex 12 | ``` 13 | 14 | or in package.json 15 | 16 | ```json 17 | { 18 | ... 19 | "dependencies": { 20 | "requireindex": "1.1.x" 21 | } 22 | } 23 | ``` 24 | 25 | # Usage 26 | Check the [test directory](https://github.com/stephenhandley/requireindex/tree/master/test) for example usage. The [test/lib](https://github.com/stephenhandley/requireindex/tree/master/test/lib) looks like: 27 | 28 | ``` 29 | lib/ 30 | index.js 31 | Foo.js 32 | bar/ 33 | index.js 34 | f.js 35 | fing.js 36 | fed/ 37 | again.js 38 | ignored.js 39 | index.js 40 | somemore.js 41 | bam.js 42 | _private.js 43 | 44 | ``` 45 | 46 | The index.js files in [test/lib/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/index.js) and [test/lib/bar/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/index.js) contain: 47 | 48 | ```js 49 | module.exports = require('requireindex')(__dirname); 50 | ``` 51 | 52 | and the index.js file in [test/lib/bar/fed/](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/fed/index.js) contains: 53 | 54 | ```js 55 | module.exports = require('requireindex')(__dirname, ['again', 'somemore']); 56 | ``` 57 | 58 | The optional second argument allows you to explicitly specify the required files using their basename. In this example [test/lib/bar/fed/ignored.js](https://github.com/stephenhandley/requireindex/tree/master/test/lib/bar/fed/ignored.js) is not included as a public module. The other way to make a module/file private without the need for explicitly naming all the other included files is to prefix the filename with an underscore, as demonstrated by [test/lib/_private.js](https://github.com/stephenhandley/requireindex/tree/master/test/lib/_private.js) which is not exported. 59 | 60 | So, with these index.js files, the result of 61 | 62 | ```js 63 | require('lib'); 64 | ``` 65 | 66 | is: 67 | 68 | ```js 69 | { 70 | bam: { 71 | m: [Function], 72 | n: [Function] 73 | }, 74 | bar: { 75 | f: [Function], 76 | fed: { 77 | again: [Function], 78 | somemore: [Function] 79 | }, 80 | fing: [Function] 81 | }, 82 | Foo: { 83 | l: [Function], 84 | ls: [Function] 85 | } 86 | } 87 | ``` 88 | 89 | # Build status 90 | [![build status](https://secure.travis-ci.org/stephenhandley/requireindex.png)](http://travis-ci.org/stephenhandley/requireindex) 91 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var FS = require('fs'); 2 | var Path = require('path'); 3 | 4 | module.exports = function (dir, basenames) { 5 | var requires = {}; 6 | 7 | if (arguments.length === 2) { 8 | // if basenames argument is passed, explicitly include those files 9 | basenames.forEach(function (basename) { 10 | var filepath = Path.resolve(Path.join(dir, basename)); 11 | requires[basename] = require(filepath); 12 | }); 13 | 14 | } else if (arguments.length === 1) { 15 | // if basenames arguments isn't passed, require all javascript 16 | // files (except for those prefixed with _) and all directories 17 | 18 | var files = FS.readdirSync(dir); 19 | 20 | // sort files in lowercase alpha for linux 21 | files.sort(function (a,b) { 22 | a = a.toLowerCase(); 23 | b = b.toLowerCase(); 24 | 25 | if (a < b) { 26 | return -1; 27 | } else if (b < a) { 28 | return 1; 29 | } else { 30 | return 0; 31 | } 32 | }); 33 | 34 | files.forEach(function (filename) { 35 | // ignore index.js and files prefixed with underscore and 36 | if ((filename === 'index.js') || (filename[0] === '_') || (filename[0] === '.')) { 37 | return; 38 | } 39 | 40 | var filepath = Path.resolve(Path.join(dir, filename)); 41 | var ext = Path.extname(filename); 42 | var stats = FS.statSync(filepath); 43 | 44 | // don't require non-javascript files (.txt .md etc.) 45 | var exts = ['.js', '.node', '.json']; 46 | if (stats.isFile() && (exts.indexOf(ext) === -1)) { 47 | return; 48 | } 49 | 50 | var basename = Path.basename(filename, ext); 51 | 52 | requires[basename] = require(filepath); 53 | }); 54 | 55 | } else { 56 | throw new Error("Must pass directory as first argument"); 57 | } 58 | 59 | return requires; 60 | }; 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "requireindex", 3 | "description": "Write minimal node index.js files that require and export siblings by file basename", 4 | "version": "1.2.0", 5 | "license": "MIT", 6 | "main": "index.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "git://github.com/stephenhandley/requireindex.git" 10 | }, 11 | "scripts": { 12 | "test": "node test/test.js" 13 | }, 14 | "keywords": [ 15 | "require", 16 | "index", 17 | "index.js" 18 | ], 19 | "directories": { 20 | "test": "test" 21 | }, 22 | "bugs": { 23 | "url": "http://github.com/stephenhandley/requireindex/issues" 24 | }, 25 | "engines": { 26 | "node": ">=0.10.5" 27 | }, 28 | "devDependencies": { 29 | "asserts": "4.0.x" 30 | }, 31 | "author": "Stephen Handley (http://person.sh)", 32 | "homepage": "https://github.com/stephenhandley/requireindex" 33 | } 34 | -------------------------------------------------------------------------------- /test/lib/.also_private/private.txt: -------------------------------------------------------------------------------- 1 | barf -------------------------------------------------------------------------------- /test/lib/Foo.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | l: function() { return "yes"; }, 3 | ls: function() { return "yep"; } 4 | }; -------------------------------------------------------------------------------- /test/lib/_private.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return "ack"; 3 | } -------------------------------------------------------------------------------- /test/lib/bam.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | m: function() { return "ok"; }, 3 | n: require('./_private') 4 | }; -------------------------------------------------------------------------------- /test/lib/bar/f.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return "yea"; 3 | } -------------------------------------------------------------------------------- /test/lib/bar/fed/again.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return "again"; 3 | } -------------------------------------------------------------------------------- /test/lib/bar/fed/ignored.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return "ignored"; 3 | } -------------------------------------------------------------------------------- /test/lib/bar/fed/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('../../../../')(__dirname, ['again', 'somemore']) -------------------------------------------------------------------------------- /test/lib/bar/fed/somemore.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return "somemore"; 3 | } -------------------------------------------------------------------------------- /test/lib/bar/fing.js: -------------------------------------------------------------------------------- 1 | module.exports = function() { 2 | return "definitely"; 3 | } -------------------------------------------------------------------------------- /test/lib/bar/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('../../../')(__dirname) -------------------------------------------------------------------------------- /test/lib/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('../../')(__dirname) -------------------------------------------------------------------------------- /test/lib/not_javascript.txt: -------------------------------------------------------------------------------- 1 | asdf 1 2 / @ 123 -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var Assert = require('assert'); 2 | var Asserts = require('asserts'); 3 | 4 | Asserts(function () { 5 | var lib = require('./lib'); 6 | 7 | return { 8 | "requireindex should": { 9 | "properly include files parallel to index.js and maintain structure": function () { 10 | Asserts.all.equal([ 11 | [lib.bam.m, [], "ok"], 12 | [lib.bar.f, [], "yea"], 13 | [lib.bar.fing, [], 'definitely'], 14 | [lib.Foo.l, [], 'yes'], 15 | [lib.Foo.ls, [], 'yep'], 16 | [lib.bam.n, [], 'ack'], 17 | [lib.bar.fed.again, [], 'again'], 18 | [lib.bar.fed.somemore, [], 'somemore'] 19 | ]); 20 | }, 21 | 22 | "ignore _ prefixed files": function () { 23 | Assert.equal(('_private' in lib), false); 24 | }, 25 | 26 | "not include files not mentioned when second array argument is used": function () { 27 | Assert.equal(('ignored' in lib.bar.fed), false); 28 | }, 29 | 30 | "ignore non javascript files": function () { 31 | Assert.equal(('not_javascript' in lib), false); 32 | }, 33 | 34 | "sort files by lowercase alpha of the filename": function () { 35 | Assert.equal(Object.keys(lib)[0], 'bam'); 36 | }, 37 | 38 | "ignore dot files": function () { 39 | Assert.equal(('.also_private' in lib), false); 40 | }, 41 | } 42 | }; 43 | }); --------------------------------------------------------------------------------