[Pkg-javascript-commits] [node-require-dir] 01/02: Imported Upstream version 0.3.0

Thorsten Alteholz alteholz at moszumanska.debian.org
Sun Mar 27 15:36:51 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-require-dir.

commit fe1e89fb3466358a071572050534d95a96ef1bab
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sun Mar 27 17:36:48 2016 +0200

    Imported Upstream version 0.3.0
---
 .gitignore                        |   2 +
 .travis.yml                       |   5 ++
 LICENSE                           |  21 ++++++
 README.md                         | 117 +++++++++++++++++++++++++++++++
 index.js                          | 141 ++++++++++++++++++++++++++++++++++++++
 package.json                      |  22 ++++++
 test/camelcase.js                 |  20 ++++++
 test/camelcase/a_main.js          |   1 +
 test/camelcase/sub-dir/a-sub.js   |   1 +
 test/duplicates.js                |  43 ++++++++++++
 test/duplicates/a.js              |   1 +
 test/duplicates/b.json            |   1 +
 test/duplicates/b/1.js            |   1 +
 test/duplicates/b/1.txt           |   1 +
 test/duplicates/b/2.js            |   1 +
 test/duplicates/b/2.json          |   1 +
 test/duplicates/c.txt             |   1 +
 test/duplicates/c/3.json          |   1 +
 test/duplicates/d.js              |   1 +
 test/duplicates/d.json            |   1 +
 test/index.js                     |   2 +
 test/recurse.js                   |  30 ++++++++
 test/recurse/a.js                 |   1 +
 test/recurse/b/1/bar.json         |   1 +
 test/recurse/b/1/foo.js           |   1 +
 test/recurse/b/2/baz.txt          |   1 +
 test/recurse/c/3.json             |   1 +
 test/recurse/node_modules/fake.js |   1 +
 test/simple.js                    |  20 ++++++
 test/simple/a.js                  |   1 +
 test/simple/b.json                |   1 +
 test/simple/c.coffee              |   1 +
 test/simple/d.txt                 |   1 +
 33 files changed, 445 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..688ef23
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+npm-debug.log
+node_modules
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..6064ca0
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - "0.10"
+  - "0.12"
+  - "iojs"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..57789fb
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..b102855
--- /dev/null
+++ b/README.md
@@ -0,0 +1,117 @@
+[![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.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..d923b98
--- /dev/null
+++ b/index.js
@@ -0,0 +1,141 @@
+// 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) {
+                // 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();
+    });
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c0a4973
--- /dev/null
+++ b/package.json
@@ -0,0 +1,22 @@
+{ "name": "require-dir"
+, "description": "Helper to require() directories."
+, "version": "0.3.0"
+, "author": "Aseem Kishore <aseem.kishore at gmail.com>"
+, "license": "MIT"
+, "dependencies": {}
+, "devDependencies":
+  { "coffee-script": "~1.3.3"
+  , "mkdirp": "^0.5.0"
+  }
+, "engines":
+  { "node": "*"
+  }
+, "scripts":
+  { "test": "node test"
+  }
+, "homepage": "https://github.com/aseemk/requireDir"
+, "repository":
+  { "type": "git"
+  , "url": "git://github.com/aseemk/requireDir.git"
+  }
+}
diff --git a/test/camelcase.js b/test/camelcase.js
new file mode 100644
index 0000000..83be585
--- /dev/null
+++ b/test/camelcase.js
@@ -0,0 +1,20 @@
+var 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.');
diff --git a/test/camelcase/a_main.js b/test/camelcase/a_main.js
new file mode 100644
index 0000000..4b0ed58
--- /dev/null
+++ b/test/camelcase/a_main.js
@@ -0,0 +1 @@
+module.exports = 'a main';
diff --git a/test/camelcase/sub-dir/a-sub.js b/test/camelcase/sub-dir/a-sub.js
new file mode 100644
index 0000000..8d25d5e
--- /dev/null
+++ b/test/camelcase/sub-dir/a-sub.js
@@ -0,0 +1 @@
+module.exports = 'a sub';
diff --git a/test/duplicates.js b/test/duplicates.js
new file mode 100644
index 0000000..ec0db97
--- /dev/null
+++ b/test/duplicates.js
@@ -0,0 +1,43 @@
+var 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.');
diff --git a/test/duplicates/a.js b/test/duplicates/a.js
new file mode 100644
index 0000000..735d820
--- /dev/null
+++ b/test/duplicates/a.js
@@ -0,0 +1 @@
+module.exports = 'a.js';
diff --git a/test/duplicates/b.json b/test/duplicates/b.json
new file mode 100644
index 0000000..fb6f7a0
--- /dev/null
+++ b/test/duplicates/b.json
@@ -0,0 +1 @@
+"b.json"
diff --git a/test/duplicates/b/1.js b/test/duplicates/b/1.js
new file mode 100644
index 0000000..ca7daaa
--- /dev/null
+++ b/test/duplicates/b/1.js
@@ -0,0 +1 @@
+module.exports = '1.js';
diff --git a/test/duplicates/b/1.txt b/test/duplicates/b/1.txt
new file mode 100644
index 0000000..d1a2268
--- /dev/null
+++ b/test/duplicates/b/1.txt
@@ -0,0 +1 @@
+'1.txt'
diff --git a/test/duplicates/b/2.js b/test/duplicates/b/2.js
new file mode 100644
index 0000000..9ba2ca7
--- /dev/null
+++ b/test/duplicates/b/2.js
@@ -0,0 +1 @@
+module.exports = '2.js';
diff --git a/test/duplicates/b/2.json b/test/duplicates/b/2.json
new file mode 100644
index 0000000..f4bf2d0
--- /dev/null
+++ b/test/duplicates/b/2.json
@@ -0,0 +1 @@
+"2.json"
diff --git a/test/duplicates/c.txt b/test/duplicates/c.txt
new file mode 100644
index 0000000..123457d
--- /dev/null
+++ b/test/duplicates/c.txt
@@ -0,0 +1 @@
+'c.txt'
diff --git a/test/duplicates/c/3.json b/test/duplicates/c/3.json
new file mode 100644
index 0000000..7d718fa
--- /dev/null
+++ b/test/duplicates/c/3.json
@@ -0,0 +1 @@
+"3.json"
diff --git a/test/duplicates/d.js b/test/duplicates/d.js
new file mode 100644
index 0000000..2b74cb6
--- /dev/null
+++ b/test/duplicates/d.js
@@ -0,0 +1 @@
+module.exports = 'd.js';
diff --git a/test/duplicates/d.json b/test/duplicates/d.json
new file mode 100644
index 0000000..bfa3928
--- /dev/null
+++ b/test/duplicates/d.json
@@ -0,0 +1 @@
+"d.json"
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..14e6fb7
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,2 @@
+require('..')('.');
+console.log('\nAll tests passed!');
diff --git a/test/recurse.js b/test/recurse.js
new file mode 100644
index 0000000..39e8e76
--- /dev/null
+++ b/test/recurse.js
@@ -0,0 +1,30 @@
+var 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.');
diff --git a/test/recurse/a.js b/test/recurse/a.js
new file mode 100644
index 0000000..2e7700b
--- /dev/null
+++ b/test/recurse/a.js
@@ -0,0 +1 @@
+module.exports = 'a';
diff --git a/test/recurse/b/1/bar.json b/test/recurse/b/1/bar.json
new file mode 100644
index 0000000..196e587
--- /dev/null
+++ b/test/recurse/b/1/bar.json
@@ -0,0 +1 @@
+"bar"
diff --git a/test/recurse/b/1/foo.js b/test/recurse/b/1/foo.js
new file mode 100644
index 0000000..2651774
--- /dev/null
+++ b/test/recurse/b/1/foo.js
@@ -0,0 +1 @@
+module.exports = 'foo';
diff --git a/test/recurse/b/2/baz.txt b/test/recurse/b/2/baz.txt
new file mode 100644
index 0000000..7601807
--- /dev/null
+++ b/test/recurse/b/2/baz.txt
@@ -0,0 +1 @@
+baz
diff --git a/test/recurse/c/3.json b/test/recurse/c/3.json
new file mode 100644
index 0000000..00750ed
--- /dev/null
+++ b/test/recurse/c/3.json
@@ -0,0 +1 @@
+3
diff --git a/test/recurse/node_modules/fake.js b/test/recurse/node_modules/fake.js
new file mode 100644
index 0000000..f514ea1
--- /dev/null
+++ b/test/recurse/node_modules/fake.js
@@ -0,0 +1 @@
+module.exports = 'fake';
diff --git a/test/simple.js b/test/simple.js
new file mode 100644
index 0000000..31e9458
--- /dev/null
+++ b/test/simple.js
@@ -0,0 +1,20 @@
+var 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',
+});
+
+console.log('Simple tests passed.');
diff --git a/test/simple/a.js b/test/simple/a.js
new file mode 100644
index 0000000..2e7700b
--- /dev/null
+++ b/test/simple/a.js
@@ -0,0 +1 @@
+module.exports = 'a';
diff --git a/test/simple/b.json b/test/simple/b.json
new file mode 100644
index 0000000..19010cc
--- /dev/null
+++ b/test/simple/b.json
@@ -0,0 +1 @@
+"b"
diff --git a/test/simple/c.coffee b/test/simple/c.coffee
new file mode 100644
index 0000000..7212f4d
--- /dev/null
+++ b/test/simple/c.coffee
@@ -0,0 +1 @@
+module.exports = 'c'
diff --git a/test/simple/d.txt b/test/simple/d.txt
new file mode 100644
index 0000000..4bcfe98
--- /dev/null
+++ b/test/simple/d.txt
@@ -0,0 +1 @@
+d

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-require-dir.git



More information about the Pkg-javascript-commits mailing list