dependency-injection-cat
Version:
DI Cat is a truly clean DI-container, which allows you not to pollute your business logic with decorators from DI/IOC libraries!
48 lines (47 loc) • 2.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathResolver = void 0;
var upath_1 = __importDefault(require("upath"));
var fs_1 = __importDefault(require("fs"));
var tsconfig_paths_1 = require("tsconfig-paths");
var isPathRelative_1 = require("../../utils/isPathRelative");
var constants_1 = require("./constants");
var libraryName_1 = require("../../../constants/libraryName");
var PathResolver = /** @class */ (function () {
function PathResolver() {
}
PathResolver.init = function () {
var config = (0, tsconfig_paths_1.loadConfig)();
if (config.resultType === 'failed') {
throw new Error('Can not load tsconfig file');
}
PathResolver.resolver = (0, tsconfig_paths_1.createMatchPath)(config.absoluteBaseUrl, config.paths);
};
PathResolver.resolve = function (sourceFilePath, targetPath) {
var _a;
if (targetPath === libraryName_1.libraryName) {
return targetPath;
}
var normalizedSourceFilePath = upath_1.default.toUnix(sourceFilePath);
var normalizedTargetPath = upath_1.default.toUnix(targetPath);
if ((0, isPathRelative_1.isPathRelative)(normalizedTargetPath)) {
var newSourceFilePath = upath_1.default.dirname(normalizedSourceFilePath);
var resolved_1 = upath_1.default.resolve(newSourceFilePath, normalizedTargetPath);
return upath_1.default.normalize(resolved_1);
}
var resolved = PathResolver.resolver(normalizedTargetPath, undefined, undefined, constants_1.extensionsToResolve);
if (resolved) {
return upath_1.default.normalize(resolved);
}
var normalizedFromNodeModules = upath_1.default.normalize(upath_1.default.dirname(require.resolve(normalizedTargetPath)));
var resolvedWithExtension = (_a = constants_1.extensionsToResolve
.map(function (it) { return upath_1.default.join(normalizedFromNodeModules, it); })
.find(function (it) { return fs_1.default.existsSync(it); })) !== null && _a !== void 0 ? _a : normalizedTargetPath;
return upath_1.default.normalize(resolvedWithExtension);
};
return PathResolver;
}());
exports.PathResolver = PathResolver;