Comparing version 1.0.1 to 1.1.0
102
index.js
@@ -49,6 +49,15 @@ 'use strict'; | ||
inject(something) { | ||
let type = this.getType(something); | ||
return this[type + 'Injector'] ? this[type + 'Injector'](something) | ||
: throwError(`Injector type '${type}' not defined`); | ||
} | ||
/** | ||
* gets the type of something | ||
* @param something | ||
*/ | ||
getType(something) { | ||
let type = typeof something; | ||
type === 'object' && _.isArray(something) && (type = 'array'); | ||
this[type + 'Injector'] ? this[type + 'Injector'](something) | ||
: throwError(`Injector type '${type}' not defined`); | ||
return type; | ||
} | ||
@@ -61,3 +70,3 @@ | ||
arrayInjector(somethings) { | ||
_.forEach(somethings, something => this.inject(something)); | ||
return Promise.all(somethings.map(something => this.inject(something))); | ||
} | ||
@@ -70,3 +79,3 @@ | ||
objectInjector(something) { | ||
this.functionInjector(something); | ||
return this.functionInjector(something); | ||
} | ||
@@ -78,27 +87,56 @@ | ||
stringInjector(something) { | ||
fs.exists(something, (exists) => | ||
!exists ? throwError(`Path '${something}' don't exists`) : ( | ||
fs.stat(something, (err, stats) => | ||
err ? throwError(`Error reading '${something}'`) : ( | ||
stats.isDirectory() ? this.directoryInjector(something) | ||
: this.fileInjector(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`)); | ||
}); | ||
} | ||
/** | ||
* 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 | ||
* @param {string} path | ||
* @return {Promise} rejects on error | ||
*/ | ||
getPathInformation(path) { | ||
return new Promise((resolve, reject) => | ||
fs.stat(path, (err, stats) => | ||
!err ? resolve(stats) : reject())); | ||
} | ||
/** | ||
* injects into all the files of a directory | ||
*/ | ||
directoryInjector(directoryPath) { | ||
fs.readdir(directoryPath, (err, files) => | ||
err ? throwError(`Error listing files on '${directoryPath}'`) | ||
: files.forEach(file => | ||
this.fileInjector(path.join(directoryPath, file)) | ||
) | ||
) | ||
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}'`)); | ||
}); | ||
} | ||
/** | ||
* gets the files in a directory | ||
* @param {string} path | ||
* @return {array} | ||
*/ | ||
getFilesInDirectory(path) { | ||
return new Promise((resolve, reject) => | ||
fs.readdir(path, (err, files) => | ||
!err ? resolve(files) : reject())); | ||
} | ||
/** | ||
* injects into the file | ||
@@ -108,3 +146,3 @@ */ | ||
let temp = require(filePath); | ||
this.inject(temp); | ||
return this.inject(temp); | ||
} | ||
@@ -118,6 +156,9 @@ | ||
let dependencies = this.getDependencies(something); | ||
_.forEach(dependencies, (value, index) => { | ||
(this[injectables][index]) ? something[value] = this[injectables][index] | ||
: console.warn(`Injectable '${index}' is not defined`); | ||
}); | ||
return Promise.all(Object.keys(dependencies).map(name => { | ||
(this[injectables][name]) ? something[dependencies[name]] = this[injectables][name] | ||
: console.warn(`Injectable '${name}' is not defined`); | ||
// always resolve despite the possible warning | ||
// TODO: test if we should reject on warning | ||
Promise.resolve(); | ||
})); | ||
} | ||
@@ -131,5 +172,4 @@ | ||
getDependencies(something) { | ||
let dependencies = something['dependencies']; | ||
let type = typeof dependencies; | ||
type === 'object' && _.isArray(dependencies) && (type = 'array'); | ||
let dependencies = something.dependencies; | ||
let type = this.getType(dependencies); | ||
// treatments to use to handle the dependencies | ||
@@ -143,7 +183,3 @@ let treatments = { | ||
}, | ||
'array': dependencies => { | ||
let object = {}; | ||
_.forEach(dependencies, value => object[value] = value); | ||
return object; | ||
}, | ||
'array': dependencies => dependencies.reduce((obj, name) => (obj[name] = name) && obj, {}), | ||
'undefined': () => { | ||
@@ -150,0 +186,0 @@ console.warn(`No dependencies found, ignoring`); |
{ | ||
"name": "yadi", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Yet Another Dependency Injector", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
8682
237