inversitron
Version:
Simple framework based on InversifyJS
70 lines (69 loc) • 3.57 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildContainer = void 0;
var fs_1 = require("fs");
var glob_1 = require("glob");
var inversify_1 = require("inversify");
var path_1 = require("path");
var process_1 = require("process");
var readConfigFile_1 = __importDefault(require("../cli/utils/readConfigFile"));
function buildContainer() {
var _a, _b, _c;
var dir = process_1.cwd();
var configFile = readConfigFile_1.default();
var _d = JSON.parse(fs_1.readFileSync(path_1.join(dir, 'tsconfig.json')).toString('utf-8')).compilerOptions, outDir = _d.outDir, rootDir = _d.rootDir;
var buildPath = function (path, defaultPath) { var _a; return path_1.join(outDir, (_a = path === null || path === void 0 ? void 0 : path.replace(rootDir, '')) !== null && _a !== void 0 ? _a : defaultPath); };
var reposPath = (_a = process.env.REPOS_P) !== null && _a !== void 0 ? _a : buildPath(configFile.repositoriesPath, 'repositories');
var servsPath = (_b = process.env.SERVS_P) !== null && _b !== void 0 ? _b : buildPath(configFile.servicesPath, 'services');
var ctrlsPath = (_c = process.env.CTRLS_P) !== null && _c !== void 0 ? _c : buildPath(configFile.controllersPath, 'controllers');
var repositoriesPath = path_1.join(dir, reposPath);
var servicesPath = path_1.join(dir, servsPath);
var controllersPath = path_1.join(dir, ctrlsPath);
var promiseContainer = new Promise(function (resolve, reject) {
try {
var controllersReposAndServices = glob_1.glob.sync("{" + repositoriesPath + "," + servicesPath + "," + controllersPath + "}/**/*{Repository,Service,Controller}.{ts,js}");
var importControllersReposAndServices = controllersReposAndServices.map(function (file) { return Promise.resolve().then(function () { return __importStar(require(file)); }); });
Promise
.all(importControllersReposAndServices)
.then(function (values) {
var container = new inversify_1.Container();
values.forEach(function (val) {
for (var key in val) {
if (Object.prototype.hasOwnProperty.call(val, key)) {
container.bind(val[key]).toSelf();
}
}
});
resolve(container);
})
.catch(reject);
}
catch (err) {
reject(err);
}
});
return promiseContainer;
}
exports.buildContainer = buildContainer;