1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
11 | if (k2 === undefined) k2 = k;
|
12 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
13 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
14 | desc = { enumerable: true, get: function() { return m[k]; } };
|
15 | }
|
16 | Object.defineProperty(o, k2, desc);
|
17 | }) : (function(o, m, k, k2) {
|
18 | if (k2 === undefined) k2 = k;
|
19 | o[k2] = m[k];
|
20 | }));
|
21 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
22 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
23 | }) : function(o, v) {
|
24 | o["default"] = v;
|
25 | });
|
26 | var __importStar = (this && this.__importStar) || function (mod) {
|
27 | if (mod && mod.__esModule) return mod;
|
28 | var result = {};
|
29 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
30 | __setModuleDefault(result, mod);
|
31 | return result;
|
32 | };
|
33 | Object.defineProperty(exports, "__esModule", { value: true });
|
34 | exports.ImportAliases = void 0;
|
35 | const path_1 = require("path");
|
36 | const IocLookupException_1 = require("../Exceptions/IocLookupException");
|
37 |
|
38 |
|
39 |
|
40 | class ImportAliases {
|
41 | constructor(container) {
|
42 | this.container = container;
|
43 | |
44 |
|
45 |
|
46 | this.list = {};
|
47 | |
48 |
|
49 |
|
50 |
|
51 | this.requireCache = new Map();
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | getPathAlias(namespace) {
|
57 | return Object.keys(this.list).find((alias) => {
|
58 | return namespace.startsWith(`${alias}/`);
|
59 | });
|
60 | }
|
61 | |
62 |
|
63 |
|
64 | makeAliasPath(namespace, alias) {
|
65 | return (0, path_1.normalize)(namespace.replace(alias, this.list[alias]));
|
66 | }
|
67 | |
68 |
|
69 |
|
70 | register(absolutePath, alias) {
|
71 | this.list[alias] = absolutePath;
|
72 | return this;
|
73 | }
|
74 | |
75 |
|
76 |
|
77 | has(namespace) {
|
78 | return !!this.getPathAlias(namespace);
|
79 | }
|
80 | |
81 |
|
82 |
|
83 | resolve(namespace) {
|
84 | const alias = this.getPathAlias(namespace);
|
85 | if (!alias) {
|
86 | throw IocLookupException_1.IocLookupException.lookupFailed(namespace);
|
87 | }
|
88 | const cacheItem = this.requireCache.get(namespace);
|
89 | if (cacheItem) {
|
90 | return cacheItem.value;
|
91 | }
|
92 | |
93 |
|
94 |
|
95 | const diskPath = this.makeAliasPath(namespace, alias);
|
96 | |
97 |
|
98 |
|
99 | const value = require(diskPath);
|
100 | |
101 |
|
102 |
|
103 | this.requireCache.set(namespace, { diskPath, value });
|
104 | |
105 |
|
106 |
|
107 | return value;
|
108 | }
|
109 | |
110 |
|
111 |
|
112 | async resolveAsync(namespace) {
|
113 | |
114 |
|
115 |
|
116 | if (this.container.module === 'cjs') {
|
117 | return this.resolve(namespace);
|
118 | }
|
119 | const alias = this.getPathAlias(namespace);
|
120 | if (!alias) {
|
121 | throw IocLookupException_1.IocLookupException.lookupFailed(namespace);
|
122 | }
|
123 | |
124 |
|
125 |
|
126 |
|
127 | return Promise.resolve().then(() => __importStar(require(this.makeAliasPath(namespace, alias))));
|
128 | }
|
129 | }
|
130 | exports.ImportAliases = ImportAliases;
|