-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (180 loc) · 5.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict';
const fs = require('fs');
const path = require('path');
const singleton = Symbol();
const singletonLock = Symbol();
const injectables = Symbol();
const throwError = error => {
throw error;
};
class InjectorContainer {
/**
* constructor locked to allow only one instance of the singleton
* @param lock
*/
constructor(lock) {
(lock != singletonLock) && throwError("Cannot construct singleton");
this[injectables] = {};
this.outputMessages = true;
}
/**
* Gets the instance of the singleton
* @returns {*}
*/
static get instance() {
this[singleton] = this[singleton] || new InjectorContainer(singletonLock);
return this[singleton];
}
/**
* requires a file, gives the name if specified, the treater is a function to update the requirement
* @param {string} filename
* @param {string} name
* @param {function} treater
* @return {Promise}
*/
requireAndAdd(filename, name, treater) {
if (treater === undefined && typeof name === 'function') {
treater = name;
name = undefined;
}
if (!treater) {
treater = obj => obj;
}
name = name || filename.split('/').pop().split('.').shift();
let obj = require(filename);
obj = treater(obj);
return this.add(obj, name);
}
/**
* adds an object to the container
* @param {object|function} object
* @param {string} name optional
* @return {Promise}
*/
add(object, name) {
name = name || object.constructor.name;
this[injectables][name] = object;
return Promise.resolve(object);
}
/**
* injects dependencies to whatever arrives
* @param something
*/
inject(something) {
let type = this.getType(something);
if (this[type + 'Injector']) {
return this[type + 'Injector'](something);
} else {
return Promise.reject(`Injector type '${type}' not defined`);
}
}
/**
* gets the type of something
* @param something
*/
getType(something) {
let type = typeof something;
type === 'object' && Array.isArray(something) && (type = 'array');
return type;
}
/**
* injects to an array of somethings
* @param somethings
*/
arrayInjector(somethings) {
return Promise.all(somethings.map(something => this.inject(something)));
}
/**
* injects to an object, actually the code for function works just fine
* @param something
*/
objectInjector(something) {
return this.functionInjector(something);
}
/**
* injects the dependencies into the files of a directory or a single file
*/
stringInjector(something) {
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}`));
}
/**
* 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(err)));
}
/**
* injects into all the files of a directory
*/
directoryInjector(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}`));
}
/**
* gets the files in a directory
* @param {string} path
* @return {array}
*/
getFilesInDirectory(path) {
return new Promise((resolve, reject) => fs.readdir(path, (err, files) => err ? reject(err) : resolve(files)));
}
/**
* injects into the file
*/
fileInjector(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);
}
/**
* injector for class definitions
* @param {function} something
*/
functionInjector(something) {
let dependencies = this.getDependencies(something);
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);
}
/**
* gets the dependencies to be injected
* @param dependencies
* @returns {object}
*/
getDependencies(something) {
let dependencies = something.dependencies;
let type = this.getType(dependencies);
// treatments to use to handle the dependencies
let treatments = {
'object': dependencies => dependencies,
'string': dependency => {
let object = {};
object[dependency] = dependency;
return object;
},
'array': dependencies => dependencies.reduce((obj, name) => (obj[name] = name) && obj, {}),
'undefined': () => {
this.outputMessages && console.warn(`No dependencies found, ignoring`);
return {};
}
};
return treatments[type] ? treatments[type](dependencies)
: throwError(`Treatment for dependencies '${type}' not defined`);
}
}
module.exports = InjectorContainer.instance;