node-dependency-injection 3.0.0-beta.5 → 3.0.0-beta.6
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/README.md +32 -5
- package/dist/lib/ServiceFile.js +17 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,15 +69,11 @@ const mailer = container.get('service.mailer')
|
|
|
69
69
|
Autowire for TypeScript
|
|
70
70
|
------------
|
|
71
71
|
|
|
72
|
-
> WARNING: Is only available for node-ts or node with ts-node-register.
|
|
73
|
-
|
|
74
|
-
> WARNING: It doesn´t work with `babel` or `tsc` or any other transpilation/compilation process.
|
|
75
|
-
|
|
76
72
|
```ts
|
|
77
73
|
import {ContainerBuilder, Autowire} from 'node-dependency-injection'
|
|
78
74
|
|
|
79
75
|
const container = new ContainerBuilder(
|
|
80
|
-
|
|
76
|
+
false,
|
|
81
77
|
'/path/to/src'
|
|
82
78
|
)
|
|
83
79
|
const autowire = new Autowire(container)
|
|
@@ -103,6 +99,37 @@ import SomeService from '@src/service/SomeService'
|
|
|
103
99
|
container.get(SomeService)
|
|
104
100
|
```
|
|
105
101
|
|
|
102
|
+
If you are transpiling your Typescript may you need to dump the some kind of service configuration file.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import {ContainerBuilder, Autowire} from 'node-dependency-injection'
|
|
106
|
+
|
|
107
|
+
const container = new ContainerBuilder(
|
|
108
|
+
false,
|
|
109
|
+
'/path/to/src'
|
|
110
|
+
)
|
|
111
|
+
const autowire = new Autowire(container)
|
|
112
|
+
autowire.enableDump('/some/path/to/dist/services.yaml')
|
|
113
|
+
await autowire.process()
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
My proposal for load configuration file in a production environment with transpiling/babel compilation:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
if (process.env.NODE_ENV === 'dev') {
|
|
121
|
+
this._container = new ContainerBuilder(false, '/src');
|
|
122
|
+
this._autowire = new Autowire(this._container);
|
|
123
|
+
this._autowire.enableDump('/some/path/to/dist/services.yaml');
|
|
124
|
+
await this._autowire.process();
|
|
125
|
+
} else {
|
|
126
|
+
this._container = new ContainerBuilder(false, '/dist');
|
|
127
|
+
this._loader = new YamlFileLoader(this._container);
|
|
128
|
+
await this._loader.load('/some/path/to/dist/services.yaml');
|
|
129
|
+
}
|
|
130
|
+
await this._container.compile();
|
|
131
|
+
```
|
|
132
|
+
|
|
106
133
|
|
|
107
134
|
Configuration files: how to load and use configuration files
|
|
108
135
|
------------
|
package/dist/lib/ServiceFile.js
CHANGED
|
@@ -36,11 +36,11 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
|
|
|
36
36
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
37
37
|
|
|
38
38
|
var ServiceFile = /*#__PURE__*/function () {
|
|
39
|
-
function ServiceFile(
|
|
39
|
+
function ServiceFile(servicesDumpPath) {
|
|
40
40
|
(0, _classCallCheck2["default"])(this, ServiceFile);
|
|
41
41
|
this.validExtensions = ['.yaml', '.yml', '.json', '.js'];
|
|
42
|
-
this.ensureAbsolutePathIsValid(
|
|
43
|
-
this.
|
|
42
|
+
this.ensureAbsolutePathIsValid(servicesDumpPath);
|
|
43
|
+
this.servicesDumpPath = servicesDumpPath;
|
|
44
44
|
this._content = {
|
|
45
45
|
services: {}
|
|
46
46
|
};
|
|
@@ -54,18 +54,18 @@ var ServiceFile = /*#__PURE__*/function () {
|
|
|
54
54
|
}, {
|
|
55
55
|
key: "dumper",
|
|
56
56
|
get: function get() {
|
|
57
|
-
var parsed = _path["default"].parse(this.
|
|
57
|
+
var parsed = _path["default"].parse(this.servicesDumpPath);
|
|
58
58
|
|
|
59
59
|
if (['.yaml', '.yml'].includes(parsed.ext)) {
|
|
60
|
-
return new _YamlDumper["default"](this.
|
|
60
|
+
return new _YamlDumper["default"](this.servicesDumpPath, this._content);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
if (['.json'].includes(parsed.ext)) {
|
|
64
|
-
return new _JsonDumper["default"](this.
|
|
64
|
+
return new _JsonDumper["default"](this.servicesDumpPath, this._content);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
if (['.js'].includes(parsed.ext)) {
|
|
68
|
-
return new _JsDumper["default"](this.
|
|
68
|
+
return new _JsDumper["default"](this.servicesDumpPath, this._content);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
@@ -107,7 +107,7 @@ var ServiceFile = /*#__PURE__*/function () {
|
|
|
107
107
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
108
108
|
_step$value = (0, _slicedToArray2["default"])(_step.value, 2), id = _step$value[0], definition = _step$value[1];
|
|
109
109
|
|
|
110
|
-
this._appendServicesToContent(id, definition);
|
|
110
|
+
this._appendServicesToContent(id, definition, container.defaultDir);
|
|
111
111
|
}
|
|
112
112
|
} catch (err) {
|
|
113
113
|
_iterator.e(err);
|
|
@@ -148,7 +148,9 @@ var ServiceFile = /*#__PURE__*/function () {
|
|
|
148
148
|
}, {
|
|
149
149
|
key: "_appendServicesToContent",
|
|
150
150
|
value: function _appendServicesToContent(id, definition) {
|
|
151
|
-
var
|
|
151
|
+
var defaultDir = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
152
|
+
|
|
153
|
+
var classPath = this._getClassPath(id, definition, defaultDir);
|
|
152
154
|
|
|
153
155
|
var args = [];
|
|
154
156
|
|
|
@@ -190,9 +192,15 @@ var ServiceFile = /*#__PURE__*/function () {
|
|
|
190
192
|
}, {
|
|
191
193
|
key: "_getClassPath",
|
|
192
194
|
value: function _getClassPath(id, definition) {
|
|
195
|
+
var defaultDir = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
|
|
193
196
|
var classPath = id.replace(/__/g, '/');
|
|
194
197
|
var position = classPath.lastIndexOf("/".concat(definition._Object.name));
|
|
195
198
|
classPath = classPath.substring(0, position);
|
|
199
|
+
|
|
200
|
+
if (defaultDir) {
|
|
201
|
+
return classPath.replace(defaultDir, '');
|
|
202
|
+
}
|
|
203
|
+
|
|
196
204
|
return classPath;
|
|
197
205
|
}
|
|
198
206
|
}]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-dependency-injection",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.6",
|
|
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": {
|