@allspark-js/core
Version:
Core library to create js applications.
91 lines (90 loc) • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AwilixDependencyInjector = void 0;
var awilix_1 = require("awilix");
var dependency_injector_1 = require("./dependency-injector");
var AwilixDependencyInjector = /** @class */ (function () {
function AwilixDependencyInjector(options) {
this.options = options;
}
AwilixDependencyInjector.prototype.initialize = function () {
this.container = (0, awilix_1.createContainer)();
this.container.register('process', (0, awilix_1.asValue)(process));
return this.container.cradle;
};
AwilixDependencyInjector.prototype.loadModules = function (_a) {
var _this = this;
var path = _a.path, suffix = _a.suffix, index = _a.index;
var srcPath = this.options.srcPath;
var pattern = "".concat(srcPath, "/").concat(path, "/**/!(*.test|*.d).@(ts|js)");
var camelCase = function (tokens) { return tokens.map(function (token, idx) {
if (idx === 0) {
return token;
}
return token.charAt(0).toUpperCase() + token.substring(1);
}).join(''); };
var loadedModules = [];
this.container.loadModules([pattern], {
resolverOptions: { lifetime: awilix_1.Lifetime.SINGLETON },
formatName: function (name, descriptor) {
var tokens = [];
var regex = new RegExp(".*".concat(path, "/?(.*)/").concat(name, ".*"));
var subpath = regex.exec(descriptor.path);
if (subpath && subpath[1] !== '') {
var splat = subpath[1].split('/');
splat.forEach(function (pathToken) {
tokens.push.apply(tokens, pathToken.split('-'));
});
}
if (name !== 'index') {
var nameTokens = name.split('-');
tokens.push.apply(tokens, nameTokens);
}
if (suffix) {
tokens.push(suffix);
}
var camelCaseName = camelCase(tokens);
loadedModules.push("".concat(camelCaseName));
return "".concat(camelCaseName);
},
});
if (index) {
this.register([{
name: index,
dependency: loadedModules.map(function (module) { return _this.container.cradle[module]; }),
type: dependency_injector_1.DependencyType.VALUE,
}]);
}
return loadedModules;
};
AwilixDependencyInjector.prototype.register = function (dependencies) {
var _this = this;
dependencies.forEach(function (_a) {
var name = _a.name, dependency = _a.dependency, _b = _a.type, type = _b === void 0 ? dependency_injector_1.DependencyType.CLASS : _b, _c = _a.singleton, singleton = _c === void 0 ? true : _c;
var resolver;
var lifetime = singleton ? awilix_1.Lifetime.SINGLETON : awilix_1.Lifetime.TRANSIENT;
if (type === dependency_injector_1.DependencyType.VALUE) {
resolver = (0, awilix_1.asValue)(dependency);
}
else {
resolver = type === dependency_injector_1.DependencyType.CLASS
? (0, awilix_1.asClass)(dependency).setLifetime(lifetime)
: (0, awilix_1.asFunction)(dependency).setLifetime(lifetime);
}
_this.container.register(name, resolver);
});
};
AwilixDependencyInjector.prototype.registerThirdPartyDependencies = function (dependencies) {
var _this = this;
Object.keys(dependencies).forEach(function (dependencyName) {
var dependency = dependencies[dependencyName];
_this.register([{
name: dependencyName,
dependency: dependency,
type: dependency_injector_1.DependencyType.VALUE,
}]);
});
};
return AwilixDependencyInjector;
}());
exports.AwilixDependencyInjector = AwilixDependencyInjector;