Comparing version 1.2.0 to 1.2.1
76
index.js
@@ -42,4 +42,9 @@ 'use strict'; | ||
requireAndAdd(filename, name, treater) { | ||
treater === undefined && typeof name === 'function' && (treater = name) && (name = undefined); | ||
!treater && (treater = obj => obj); | ||
if (treater === undefined && typeof name === 'function') { | ||
treater = name; | ||
name = undefined; | ||
} | ||
if (!treater) { | ||
treater = obj => obj; | ||
} | ||
name = name || filename.split('/').pop().split('.').shift(); | ||
@@ -69,4 +74,7 @@ let obj = require(filename); | ||
let type = this.getType(something); | ||
return this[type + 'Injector'] ? this[type + 'Injector'](something) | ||
: throwError(`Injector type '${type}' not defined`); | ||
if (this[type + 'Injector']) { | ||
return this[type + 'Injector'](something); | ||
} else { | ||
return Promise.reject(`Injector type '${type}' not defined`); | ||
} | ||
} | ||
@@ -104,23 +112,8 @@ | ||
stringInjector(something) { | ||
return new Promise((resolve, reject) => { | ||
this.doesPathExist(something) | ||
.then(() => this.getPathInformation(something)) | ||
.then(stats => stats.isDirectory() ? resolve(this.directoryInjector(something)) | ||
: resolve(this.fileInjector(something))) | ||
.catch(() => throwError(`Path '${something}' don't exists or had an access error`)); | ||
}); | ||
return this.getPathInformation(something) | ||
.then(stats => stats.isDirectory() ? this.directoryInjector(something) : this.fileInjector(something)) | ||
.catch(err => Promise.reject(`Path '${something}' does not exist or has an access error: ${err}`)); | ||
} | ||
/** | ||
* checks if the path exists | ||
* @param {string} | ||
* @return {Promise} resolves when exists, rejects if not | ||
*/ | ||
doesPathExist(path) { | ||
return new Promise((resolve, reject) => | ||
fs.exists(path, exists => | ||
exists ? resolve() : reject())); | ||
} | ||
/** | ||
* gets information regarding the path and what it represents | ||
@@ -131,5 +124,3 @@ * @param {string} path | ||
getPathInformation(path) { | ||
return new Promise((resolve, reject) => | ||
fs.stat(path, (err, stats) => | ||
!err ? resolve(stats) : reject())); | ||
return new Promise((resolve, reject) => fs.stat(path, (err, stats) => !err ? resolve(stats) : reject(err))); | ||
} | ||
@@ -141,7 +132,5 @@ | ||
directoryInjector(directoryPath) { | ||
return new Promise((resolve, reject) => { | ||
this.getFilesInDirectory(directoryPath) | ||
.then(files => resolve(Promise.all(files.map(file => this.fileInjector(path.join(directoryPath, file)))))) | ||
.catch(() => throwError(`Error listing files on '${directoryPath}'`)); | ||
}); | ||
return this.getFilesInDirectory(directoryPath) | ||
.then(files => Promise.all(files.map(file => this.fileInjector(path.join(directoryPath, file))))) | ||
.catch(err => Promise.reject(`Error injecting into files on '${directoryPath}': ${err}`)); | ||
} | ||
@@ -155,5 +144,3 @@ | ||
getFilesInDirectory(path) { | ||
return new Promise((resolve, reject) => | ||
fs.readdir(path, (err, files) => | ||
!err ? resolve(files) : reject())); | ||
return new Promise((resolve, reject) => fs.readdir(path, (err, files) => err ? reject(err) : resolve(files))); | ||
} | ||
@@ -165,3 +152,9 @@ | ||
fileInjector(filePath) { | ||
let temp = require(filePath); | ||
let temp; | ||
try { | ||
temp = require(filePath); | ||
} catch (err) { | ||
this.outputMessages && console.error('error loading file', err); | ||
return Promise.reject(err); | ||
} | ||
return this.inject(temp); | ||
@@ -176,9 +169,10 @@ } | ||
let dependencies = this.getDependencies(something); | ||
return Promise.all(Object.keys(dependencies).map(name => { | ||
(this[injectables][name]) ? something[dependencies[name]] = this[injectables][name] | ||
: this.outputMessages && console.warn(`Injectable '${name}' is not defined`); | ||
// always resolve despite the possible warning | ||
// TODO: test if we should reject on warning | ||
Promise.resolve(); | ||
})); | ||
Object.keys(dependencies).forEach(name => { | ||
if (this[injectables][name]) { | ||
something[dependencies[name]] = this[injectables][name]; | ||
} else { | ||
this.outputMessages && console.warn(`Injectable '${name}' is not defined`); | ||
} | ||
}); | ||
return Promise.resolve(something); | ||
} | ||
@@ -185,0 +179,0 @@ |
{ | ||
"name": "yadi", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Yet Another Dependency Injector", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,3 +65,3 @@ # YADI (Yet Another Dependency injector) | ||
[npm-url]: https://npmjs.org/package/yadi | ||
[travis-image]: https://img.shields.io/travis/gabriel-rivero/yadi/master.svg | ||
[travis-image]: https://img.shields.io/travis/gabriel-rivero/yadi/develop.svg | ||
[travis-url]: https://travis-ci.org/gabriel-rivero/yadi | ||
@@ -71,4 +71,4 @@ [downloads-image]: https://img.shields.io/npm/dm/mysql2.svg | ||
[coveralls-image]: https://coveralls.io/repos/github/gabriel-rivero/yadi/badge.svg | ||
[coveralls-url]: https://coveralls.io/github/gabriel-rivero/yadi?branch=master | ||
[coveralls-url]: https://coveralls.io/github/gabriel-rivero/yadi?branch=develop | ||
[npm2-image]: https://nodei.co/npm/yadi.png?downloads=true | ||
[npm2-url]: https://nodei.co/npm/yadi/ |
@@ -127,2 +127,3 @@ 'use strict'; | ||
}, | ||
'file injection': { | ||
@@ -145,2 +146,19 @@ topic: () => { | ||
}, | ||
'negative file injection': { | ||
topic: () => { | ||
let injector = require(injectorPath); | ||
injector.outputMessages = false; | ||
return injector; | ||
}, | ||
'inject into a non existing file': { | ||
topic: function (injector) { | ||
injector.inject(path.join(__dirname, 'patients', 'pepino.js')) | ||
.catch(err => this.callback()); | ||
}, | ||
'file should have received the injection': () => { | ||
} | ||
} | ||
}, | ||
'directory injection': { | ||
@@ -147,0 +165,0 @@ topic: () => { |
14081
13
366
10