require-plus
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -13,3 +13,5 @@ >0.0.1 - initial commit | ||
>0.0.7 - added badges for npm and dependency version, changed directory paramater in options object for constructor(takes array of paths rather than one path string) | ||
@@ -27,5 +27,11 @@ /** | ||
this.settings = Hoek.applyToDefaults(internals.defaults, options); | ||
this.root = Path.basename(this.settings.directory); | ||
this.moduleSet = {}; | ||
this.load(this.settings.directory); | ||
if (Array.isArray(this.settings.directory)) { | ||
var length = this.settings.directory.length; | ||
for (var i = 0, il = length; i < il; ++i) { | ||
var root = Path.basename(this.settings.directory[i]); | ||
this.load(root, this.settings.directory[i]); | ||
} | ||
} else { | ||
throw new Error(this.settings.directory + ' is not an array'); | ||
} | ||
return this; | ||
@@ -45,3 +51,3 @@ | ||
internals.RequirePlus.prototype.load = function (directory) { | ||
internals.RequirePlus.prototype.load = function (root, directory) { | ||
var self = this; | ||
@@ -56,3 +62,3 @@ var mod; | ||
if (!whitelist) { | ||
self.load(joined); | ||
self.load(root, joined); | ||
} | ||
@@ -63,3 +69,3 @@ } else { | ||
var ext = Path.extname(joined); | ||
var index = joined.indexOf(self.root); | ||
var index = joined.indexOf(root); | ||
var paths = joined.slice(index).replace(ext, ''); | ||
@@ -71,3 +77,4 @@ paths = (Os.platform() === 'win32') ? paths.split('\\').slice(1) : paths.split('/').slice(1); | ||
} catch (e) { | ||
throw new Error(e); | ||
//throw new Error('Module cannot be loaded for path ' + joined); | ||
throw new Error('Module cannot be loaded for path'); | ||
} | ||
@@ -74,0 +81,0 @@ self.createTree(self.moduleSet, paths, mod); |
{ | ||
"name": "require-plus", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "better require for modules", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,4 +0,11 @@ | ||
# require-plus [](https://travis-ci.org/simon-p-r/require-plus) | ||
# require-plus | ||
[](https://travis-ci.org/simon-p-r/require-plus) | ||
[](https://www.npmjs.org/package/require-plus) | ||
 | ||
 | ||
Node module for loading js files via require when supplied with a valid directory, if the directory is not valid it will throw. It will also throw if the object is not exported properly or cannot be resolved through require. | ||
@@ -20,3 +27,3 @@ | ||
// if options are not set the standard defaults shown above will be applied, however you must include a directory string in options object which is passed to constructor | ||
options.directory = './test'; | ||
options.directory = ['./test', './some/path', './some/other/path']; | ||
var results = new Plus(options); | ||
@@ -23,0 +30,0 @@ console.log(results); |
@@ -7,2 +7,3 @@ // Load modules | ||
var Path = require('path'); | ||
var Fs = require('fs'); | ||
@@ -47,3 +48,3 @@ // Set-up lab | ||
var options = { | ||
directory: './directory/does/not/exist' | ||
directory: ['./directory/does/not/exist'] | ||
}; | ||
@@ -64,3 +65,3 @@ | ||
var options = { | ||
directory: Path.resolve(__dirname, './fixtures/aim.conf') | ||
directory: [Path.resolve(__dirname, './fixtures/aim.conf')] | ||
}; | ||
@@ -78,3 +79,3 @@ | ||
it('should filter out folders', function (done) { | ||
it('should throw if settings directory is not an array', function (done) { | ||
@@ -85,3 +86,36 @@ var options = { | ||
var fn = function () { | ||
var plus = new Require(options); | ||
}; | ||
expect(fn).to.throw(Path.resolve(__dirname, './fixtures') + ' is not an array'); | ||
done(); | ||
}); | ||
it('should test for existance of directory', function (done) { | ||
var options = { | ||
directory: [Path.resolve(__dirname, './fixtures')] | ||
}; | ||
var plus = new Require(options); | ||
var localExists = plus.exists(options.directory[0]); | ||
var globalExists = Fs.statSync(options.directory[0]) && Fs.statSync(options.directory[0]).isDirectory(); | ||
expect(localExists).to.be.true(); | ||
expect(localExists).to.equal(globalExists); | ||
done(); | ||
}); | ||
it('should filter out folders', function (done) { | ||
var options = { | ||
directory: [Path.resolve(__dirname, './fixtures')] | ||
}; | ||
var plus = new Require(options); | ||
expect(plus.moduleSet.node_modules).to.not.exist(); | ||
@@ -95,7 +129,7 @@ done(); | ||
var options = { | ||
directory: Path.resolve(__dirname, './fixtures') | ||
directory: [Path.resolve(__dirname, './fixtures')] | ||
}; | ||
var plus = new Require(options); | ||
expect(plus.exists(options.directory)).to.be.true(); | ||
expect(plus.exists(options.directory[0])).to.be.true(); | ||
done(); | ||
@@ -108,3 +142,3 @@ | ||
var options = { | ||
directory: Path.resolve(__dirname, './fixtures') | ||
directory: [Path.resolve(__dirname, './fixtures')] | ||
}; | ||
@@ -121,3 +155,3 @@ | ||
var options = { | ||
directory: Path.resolve(__dirname, './fixtures') | ||
directory: [Path.resolve(__dirname, './fixtures')] | ||
}; | ||
@@ -131,4 +165,2 @@ | ||
}); |
11087
250
36
5