diff --git a/lib/consign.js b/lib/consign.js index 8132617..0786bbe 100644 --- a/lib/consign.js +++ b/lib/consign.js @@ -12,7 +12,7 @@ var fs = require('fs') , util = require('util') , path = require('path') , pack = require(path.join(__dirname, '..', 'package')) -; + ; /** * Consign constructor. @@ -61,7 +61,7 @@ function Consign(options) { * @param push */ -Consign.prototype._setLocations = function(parent, entity, push) { +Consign.prototype._setLocations = function (parent, entity, push) { if (!entity) { this._log(['! Entity name not provided or doesn\'t exist', location], 'error'); return this; @@ -101,7 +101,7 @@ Consign.prototype._setLocations = function(parent, entity, push) { var regex = new RegExp(this._options.extensions.join('$|'), 'gi') , extension = path.extname(location) - ; + ; if (!regex.test(extension)) { this._log(['!', this._['Ignoring extension'], ':', extension]); @@ -126,7 +126,7 @@ Consign.prototype._setLocations = function(parent, entity, push) { * @returns */ -Consign.prototype._getRelativeTo = function(location) { +Consign.prototype._getRelativeTo = function (location) { return '.' + location.split(this._options.cwd).pop(); }; @@ -138,7 +138,7 @@ Consign.prototype._getRelativeTo = function(location) { * @returns */ -Consign.prototype._createNamespace = function(parent, parts, mod) { +Consign.prototype._createNamespace = function (parent, parts, mod) { var part = parts.shift(); if (!parent[part]) { @@ -159,7 +159,7 @@ Consign.prototype._createNamespace = function(parent, parts, mod) { * @returns */ -Consign.prototype._getKeyName = function(name) { +Consign.prototype._getKeyName = function (name) { return path.basename(name, path.extname(name)); }; @@ -171,7 +171,7 @@ Consign.prototype._getKeyName = function(name) { * @returns */ -Consign.prototype._log = function(message, type) { +Consign.prototype._log = function (message, type) { if (this._options.verbose) { this._options.logger[type || this._options.loggingType](message.join(' ')); } @@ -186,7 +186,7 @@ Consign.prototype._log = function(message, type) { * @returns */ -Consign.prototype.include = function(entity) { +Consign.prototype.include = function (entity) { this._lastOperation = 'include'; return this._setLocations(this._options.cwd, entity, true); }; @@ -198,7 +198,7 @@ Consign.prototype.include = function(entity) { * @returns */ -Consign.prototype.exclude = function(entity) { +Consign.prototype.exclude = function (entity) { this._lastOperation = 'exclude'; return this._setLocations(this._options.cwd, entity, false); }; @@ -210,7 +210,7 @@ Consign.prototype.exclude = function(entity) { * @returns */ -Consign.prototype.then = function(entity) { +Consign.prototype.then = function (entity) { this[this._lastOperation].call(this, entity); return this; }; @@ -222,19 +222,19 @@ Consign.prototype.then = function(entity) { * @returns */ -Consign.prototype.into = function(object) { +Consign.prototype.into = function (object) { for (var f in this._files) { delete require.cache[this._files[f]]; var script = this._files[f] , parts = this._getRelativeTo(script).split(path.sep).slice(1) , args = [] - ; + ; try { var mod = require(script); - } catch(err) { - throw('Failed to require: ' + script + ', because: ' + err.message); + } catch (err) { + throw ('Failed to require: ' + script + ', because: ' + err.message); } // Handle ES6 default exports (ie. named export called default) @@ -247,7 +247,11 @@ Consign.prototype.into = function(object) { } if ('function' === typeof mod) { - mod = mod.apply(mod, args); + try { + mod = mod.apply(mod, args); + } catch (e) { + mod = new mod(args); + } } var ns = this._createNamespace(object, parts, mod); @@ -264,6 +268,6 @@ Consign.prototype.into = function(object) { * @returns */ -module.exports = function(options) { +module.exports = function (options) { return new Consign(options); };