node-dependency-injection 3.0.0-alpha.6 → 3.0.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/Autowire.js +69 -55
- package/package.json +2 -1
- package/tsconfig.json +2 -1
package/dist/lib/Autowire.js
CHANGED
|
@@ -19,6 +19,8 @@ var _path = _interopRequireDefault(require("path"));
|
|
|
19
19
|
|
|
20
20
|
var _fs = _interopRequireDefault(require("fs"));
|
|
21
21
|
|
|
22
|
+
var _json = _interopRequireDefault(require("json5"));
|
|
23
|
+
|
|
22
24
|
var _typescriptEstree = require("@typescript-eslint/typescript-estree");
|
|
23
25
|
|
|
24
26
|
var _Definition = _interopRequireDefault(require("./Definition"));
|
|
@@ -41,7 +43,7 @@ var Autowire = /*#__PURE__*/function () {
|
|
|
41
43
|
|
|
42
44
|
try {
|
|
43
45
|
this._tsConfigFullPath = tsConfigFullPath || _path["default"].join(process.cwd(), 'tsconfig.json');
|
|
44
|
-
this._tsConfigPaths =
|
|
46
|
+
this._tsConfigPaths = _json["default"].parse(_fs["default"].readFileSync(this._tsConfigFullPath, 'utf-8')).compilerOptions.paths;
|
|
45
47
|
} catch (e) {
|
|
46
48
|
this._tsConfigPaths = null;
|
|
47
49
|
}
|
|
@@ -157,10 +159,13 @@ var Autowire = /*#__PURE__*/function () {
|
|
|
157
159
|
|
|
158
160
|
if (Class && classDeclaration) {
|
|
159
161
|
definition = this._getDefinition(classDeclaration, body, parsedFile, Class);
|
|
160
|
-
serviceId = this._getServiceIdFromPath(filePath, Class.name);
|
|
161
|
-
this.container.setDefinition(serviceId, definition);
|
|
162
162
|
|
|
163
|
-
|
|
163
|
+
if (definition) {
|
|
164
|
+
serviceId = this._getServiceIdFromPath(filePath, Class.name);
|
|
165
|
+
this.container.setDefinition(serviceId, definition);
|
|
166
|
+
|
|
167
|
+
this._interfaceImplementations(classDeclaration, body, parsedFile, serviceId);
|
|
168
|
+
}
|
|
164
169
|
}
|
|
165
170
|
}
|
|
166
171
|
}
|
|
@@ -201,47 +206,52 @@ var Autowire = /*#__PURE__*/function () {
|
|
|
201
206
|
}, {
|
|
202
207
|
key: "_getDefinition",
|
|
203
208
|
value: function _getDefinition(classDeclaration, body, parsedFile, ServiceClass) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
209
|
+
try {
|
|
210
|
+
var definition = new _Definition["default"](ServiceClass);
|
|
211
|
+
var constructorDeclaration = classDeclaration.declaration.body.body.find(function (method) {
|
|
212
|
+
return method.key.name === 'constructor';
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
if (classDeclaration.declaration["abstract"]) {
|
|
216
|
+
definition["abstract"] = true;
|
|
217
|
+
}
|
|
212
218
|
|
|
213
|
-
|
|
214
|
-
|
|
219
|
+
if (classDeclaration.declaration.superClass) {
|
|
220
|
+
var typeParent = classDeclaration.declaration.superClass.name;
|
|
215
221
|
|
|
216
|
-
|
|
222
|
+
var parentId = this._getIdentifierFromImports(typeParent, body, parsedFile);
|
|
217
223
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
var _iterator3 = _createForOfIteratorHelper(constructorDeclaration.value.params),
|
|
223
|
-
_step3;
|
|
224
|
+
if (parentId) {
|
|
225
|
+
definition.parent = parentId;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
224
228
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
229
|
+
if (constructorDeclaration) {
|
|
230
|
+
var _iterator3 = _createForOfIteratorHelper(constructorDeclaration.value.params),
|
|
231
|
+
_step3;
|
|
228
232
|
|
|
229
|
-
|
|
233
|
+
try {
|
|
234
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
235
|
+
var parameterDeclaration = _step3.value;
|
|
230
236
|
var typeNameForArgument = parameterDeclaration.parameter.typeAnnotation.typeAnnotation.typeName.name;
|
|
231
237
|
|
|
232
238
|
var argumentId = this._getIdentifierFromImports(typeNameForArgument, body, parsedFile);
|
|
233
239
|
|
|
234
|
-
|
|
235
|
-
|
|
240
|
+
if (argumentId) {
|
|
241
|
+
definition.addArgument(new _Reference["default"](argumentId), definition["abstract"]);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} catch (err) {
|
|
245
|
+
_iterator3.e(err);
|
|
246
|
+
} finally {
|
|
247
|
+
_iterator3.f();
|
|
236
248
|
}
|
|
237
|
-
} catch (err) {
|
|
238
|
-
_iterator3.e(err);
|
|
239
|
-
} finally {
|
|
240
|
-
_iterator3.f();
|
|
241
249
|
}
|
|
242
|
-
}
|
|
243
250
|
|
|
244
|
-
|
|
251
|
+
return definition;
|
|
252
|
+
} catch (e) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
245
255
|
}
|
|
246
256
|
}, {
|
|
247
257
|
key: "_interfaceImplementations",
|
|
@@ -257,7 +267,7 @@ var Autowire = /*#__PURE__*/function () {
|
|
|
257
267
|
|
|
258
268
|
var aliasId = this._getIdentifierFromImports(interfaceType, body, parsedFile);
|
|
259
269
|
|
|
260
|
-
if (this.container.hasAlias(aliasId) === false) {
|
|
270
|
+
if (aliasId && this.container.hasAlias(aliasId) === false) {
|
|
261
271
|
this.container.setAlias(aliasId, serviceId);
|
|
262
272
|
}
|
|
263
273
|
}
|
|
@@ -271,36 +281,40 @@ var Autowire = /*#__PURE__*/function () {
|
|
|
271
281
|
}, {
|
|
272
282
|
key: "_getIdentifierFromImports",
|
|
273
283
|
value: function _getIdentifierFromImports(type, body, parsedFile) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
return
|
|
279
|
-
|
|
280
|
-
|
|
284
|
+
try {
|
|
285
|
+
var rootDir = parsedFile.dir;
|
|
286
|
+
var relativeImportForImplement = body.find(function (declaration) {
|
|
287
|
+
if (declaration.specifiers) {
|
|
288
|
+
return declaration.specifiers.find(function (specifier) {
|
|
289
|
+
return specifier.local.name === type;
|
|
290
|
+
});
|
|
291
|
+
}
|
|
281
292
|
|
|
282
|
-
|
|
283
|
-
|
|
293
|
+
return null;
|
|
294
|
+
}).source.value;
|
|
284
295
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
296
|
+
if (this._tsConfigPaths) {
|
|
297
|
+
for (var pathConfig in this._tsConfigPaths) {
|
|
298
|
+
var tsConfigPath = pathConfig.replace(/\*/g, '');
|
|
299
|
+
var tsRelativePath = this._tsConfigPaths[pathConfig][0];
|
|
300
|
+
tsRelativePath = tsRelativePath.replace(/\*/g, '');
|
|
290
301
|
|
|
291
|
-
|
|
292
|
-
|
|
302
|
+
if (relativeImportForImplement.includes(tsConfigPath)) {
|
|
303
|
+
relativeImportForImplement = relativeImportForImplement.replace(tsConfigPath, tsRelativePath);
|
|
293
304
|
|
|
294
|
-
|
|
305
|
+
var parsedTsConfigPath = _path["default"].parse(this._tsConfigFullPath);
|
|
295
306
|
|
|
296
|
-
|
|
307
|
+
rootDir = parsedTsConfigPath.dir;
|
|
308
|
+
}
|
|
297
309
|
}
|
|
298
310
|
}
|
|
299
|
-
}
|
|
300
311
|
|
|
301
|
-
|
|
312
|
+
var absolutePathImportForImplement = _path["default"].join(rootDir, relativeImportForImplement);
|
|
302
313
|
|
|
303
|
-
|
|
314
|
+
return this._getServiceIdFromPath(absolutePathImportForImplement, type);
|
|
315
|
+
} catch (e) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
304
318
|
}
|
|
305
319
|
}]);
|
|
306
320
|
return Autowire;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-dependency-injection",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.9",
|
|
4
4
|
"description": "The NodeDependencyInjection component allows you to standardize and centralize the way objects are constructed in your application.",
|
|
5
5
|
"main": "dist/lib/index.js",
|
|
6
6
|
"directories": {
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
"commander": "^8.3.0",
|
|
58
58
|
"console.table": "^0.10.0",
|
|
59
59
|
"js-yaml": "^4.0.0",
|
|
60
|
+
"json5": "^2.2.1",
|
|
60
61
|
"validate-npm-package-name": "^3.0.0"
|
|
61
62
|
},
|
|
62
63
|
"devDependencies": {
|
package/tsconfig.json
CHANGED