1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const path_1 = require("path");
|
5 | const js_magic_1 = require("js-magic");
|
6 | const di_1 = require("./di");
|
7 | const fs_1 = require("fs");
|
8 | const cloneDeep = require("lodash/cloneDeep");
|
9 | const merge = require("lodash/merge");
|
10 | const values = require("lodash/values");
|
11 | const could_be_class_1 = require("could-be-class");
|
12 | const util_1 = require("./util");
|
13 | const fallbackToLocal = Symbol("fallbackToLocal");
|
14 | const cmd = process.execArgv.concat(process.argv).join(" ");
|
15 | const isTsNode = cmd.includes("ts-node");
|
16 | exports.defaultLoader = {
|
17 | extension: isTsNode ? ".ts" : ".js",
|
18 | cache: require.cache,
|
19 | load: require,
|
20 | unload(filename) {
|
21 | delete this.cache[filename];
|
22 | }
|
23 | };
|
24 | function createModuleProxy(name, path, loader = exports.defaultLoader, singletons = util_1.dict(), root = void 0) {
|
25 | let proxy = function (...args) {
|
26 | if (!new.target) {
|
27 | return proxy.instance(args[0]);
|
28 | }
|
29 | else {
|
30 | return proxy.create(...args);
|
31 | }
|
32 | };
|
33 | (!loader && root) && (loader = root["loader"]);
|
34 | (!singletons && root) && (singletons = root["singletons"]);
|
35 | Object.setPrototypeOf(proxy, ModuleProxy.prototype);
|
36 | util_1.set(proxy, "name", name);
|
37 | util_1.patchProperties(proxy, path, loader, singletons);
|
38 | proxy[fallbackToLocal] = true;
|
39 | proxy[util_1.proxyRoot] = root;
|
40 | proxy[Symbol.toStringTag] = "ModuleProxy";
|
41 | proxy[Symbol.hasInstance] = function ModuleProxy(ins) {
|
42 | return ins instanceof proxy.ctor;
|
43 | };
|
44 | return js_magic_1.applyMagic(proxy, true);
|
45 | }
|
46 | exports.createModuleProxy = createModuleProxy;
|
47 | let ModuleProxy = class ModuleProxy extends di_1.Injectable {
|
48 | get exports() {
|
49 | if (typeof this.loader.extension === "string") {
|
50 | return this.loader.load(this.path + this.loader.extension);
|
51 | }
|
52 | else {
|
53 | let dir = path_1.dirname(this.path);
|
54 | let name = path_1.basename(this.path);
|
55 | let files = fs_1.readdirSync(dir);
|
56 | for (let file of files) {
|
57 | let ext = path_1.extname(file);
|
58 | let _name = path_1.basename(file, ext);
|
59 | if (_name === name && this.loader.extension.includes(ext)) {
|
60 | return this.loader.load(this.path + ext);
|
61 | }
|
62 | }
|
63 | throw new Error(`Cannot find module '${this.path}'`);
|
64 | }
|
65 | }
|
66 | get proto() {
|
67 | let { exports } = this;
|
68 | if (typeof exports === "object") {
|
69 | if (typeof exports.default === "object") {
|
70 | return exports.default;
|
71 | }
|
72 | else if (typeof exports.default === "function" &&
|
73 | could_be_class_1.default(exports.default, true)) {
|
74 | return exports.default.prototype;
|
75 | }
|
76 | return exports;
|
77 | }
|
78 | else if (typeof exports === "function" && could_be_class_1.default(exports, true)) {
|
79 | return exports.prototype;
|
80 | }
|
81 | else {
|
82 | return null;
|
83 | }
|
84 | }
|
85 | get ctor() {
|
86 | let { exports } = this;
|
87 | if (typeof exports === "object" &&
|
88 | could_be_class_1.default(exports.default, true)) {
|
89 | return exports.default;
|
90 | }
|
91 | else if (typeof exports === "function" && could_be_class_1.default(exports, true)) {
|
92 | return exports;
|
93 | }
|
94 | else {
|
95 | return null;
|
96 | }
|
97 | }
|
98 | create(...args) {
|
99 | if (this.ctor) {
|
100 | return new this.ctor(...args);
|
101 | }
|
102 | else if (this.proto) {
|
103 | return merge(cloneDeep(this.proto), args[0]);
|
104 | }
|
105 | else {
|
106 | throw new TypeError(`${this.name} is not a valid module`);
|
107 | }
|
108 | }
|
109 | instance(route = util_1.local) {
|
110 | if (route === util_1.local) {
|
111 | return this.singletons[this.name] || (this.singletons[this.name] = util_1.getInstance(this));
|
112 | }
|
113 | if (typeof route === "string" && this.remoteSingletons[route]) {
|
114 | return this.remoteSingletons[route];
|
115 | }
|
116 | let singletons = values(this.remoteSingletons);
|
117 | if (singletons.length > 0) {
|
118 | let id = util_1.evalRouteId(route);
|
119 | let availableSingletons = singletons.filter(item => {
|
120 | return item[util_1.readyState] === 2;
|
121 | });
|
122 | if (availableSingletons.length > 0) {
|
123 | return availableSingletons[id % availableSingletons.length];
|
124 | }
|
125 | else {
|
126 | return singletons[id % singletons.length];
|
127 | }
|
128 | }
|
129 | else {
|
130 | util_1.throwUnavailableError(this.name);
|
131 | }
|
132 | }
|
133 | fallbackToLocal(enable = void 0) {
|
134 | if (enable === void 0) {
|
135 | return this[fallbackToLocal];
|
136 | }
|
137 | else {
|
138 | this[fallbackToLocal] = Boolean(enable);
|
139 | return this;
|
140 | }
|
141 | }
|
142 | noLocal() {
|
143 | return this.fallbackToLocal(false);
|
144 | }
|
145 | __get(prop) {
|
146 | if (prop in this) {
|
147 | return this[prop];
|
148 | }
|
149 | else if (prop in this.children) {
|
150 | return this.children[prop];
|
151 | }
|
152 | else if (typeof prop != "symbol") {
|
153 | return this.children[prop] = createModuleProxy(this.name + "." + String(prop), this.path + path_1.sep + String(prop), this.loader, this.singletons, this[util_1.proxyRoot] || this);
|
154 | }
|
155 | }
|
156 | __has(prop) {
|
157 | return (prop in this) || (prop in this.children);
|
158 | }
|
159 | toString() {
|
160 | return this.name;
|
161 | }
|
162 | toJSON() {
|
163 | return this.name;
|
164 | }
|
165 | };
|
166 | ModuleProxy = tslib_1.__decorate([
|
167 | js_magic_1.applyMagic
|
168 | ], ModuleProxy);
|
169 | exports.ModuleProxy = ModuleProxy;
|
170 |
|
\ | No newline at end of file |