Skip to content

Commit

Permalink
Merge 7415859 into 8bca756
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Sep 1, 2014
2 parents 8bca756 + 7415859 commit 4ce63a5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -20,10 +20,13 @@ var debug = require('debug')('loading');
var getMods = require('./lib/mods');
var inject = require('./lib/inject');

function Loader(dirpath) {
function Loader(dirpath, opt) {
if (!(this instanceof Loader)) {
return new Loader(dirpath);
return new Loader(dirpath, opt);
}
this.opt = opt || {};
// whether call the function when module.exports is a function, default: true
this.opt.call = this.opt.call !== false;
this._mods = [];
this.concat(dirpath);
}
Expand All @@ -32,6 +35,7 @@ var proto = Loader.prototype;

proto._load = function (target, field) {
var mods = this._mods;
var isCall = this.opt.call;
var self = this;

if (!target) {
Expand All @@ -48,7 +52,7 @@ proto._load = function (target, field) {
var properties = item.properties;
var mod = require(fullpath);

inject(target[field], properties, mod, target);
inject(target[field], properties, mod, target, isCall);
debug('loading #%d:%s into %s', index++, properties.join('.'), field);
});
};
Expand Down
6 changes: 3 additions & 3 deletions lib/inject.js
Expand Up @@ -12,7 +12,7 @@

var is = require('is-type-of');

module.exports = function inject(obj, properties, exports, target) {
module.exports = function inject(obj, properties, exports, target, isCall) {
if (!properties || properties.length === 0) {
return;
}
Expand All @@ -23,7 +23,7 @@ module.exports = function inject(obj, properties, exports, target) {
if (obj[property]) {
throw new Error('can\'t overwrite property ' + property);
}
if (is.function(exports) && !is.generatorFunction(exports)) {
if (isCall && is.function(exports) && !is.generatorFunction(exports)) {
obj[property] = exports(target);
} else {
obj[property] = exports;
Expand All @@ -32,5 +32,5 @@ module.exports = function inject(obj, properties, exports, target) {
}

obj[property] || (obj[property] = {});
inject(obj[property], properties, exports);
inject(obj[property], properties, exports, isCall);
};
7 changes: 7 additions & 0 deletions test/loading.test.js
Expand Up @@ -66,4 +66,11 @@ describe('loading.test.js', function () {
it('should just return when no target', function() {
loading(path.join(__dirname, 'fixtures', 'services')).into();
});

it('should loading without call function', function() {
var app = {};
loading(path.join(__dirname, 'fixtures', 'services'), {call: false})
.into(app, 'services');
app.services.fooService().should.eql({a: 1});
});
});

0 comments on commit 4ce63a5

Please sign in to comment.