@fastcar/core 0.2.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +379 -0
  3. package/annotation.d.ts +157 -0
  4. package/db.d.ts +272 -0
  5. package/index.d.ts +262 -0
  6. package/package.json +53 -0
  7. package/src/FastCarApplication.ts +761 -0
  8. package/src/annotation/Application.ts +46 -0
  9. package/src/annotation/ExceptionMonitor.ts +15 -0
  10. package/src/annotation/bind/AddRequireModule.ts +18 -0
  11. package/src/annotation/bind/AliasInjection.ts +24 -0
  12. package/src/annotation/bind/Autowired.ts +11 -0
  13. package/src/annotation/bind/CallDependency.ts +24 -0
  14. package/src/annotation/data/DBType.ts +9 -0
  15. package/src/annotation/data/DS.ts +58 -0
  16. package/src/annotation/data/DSIndex.ts +7 -0
  17. package/src/annotation/data/Entity.ts +8 -0
  18. package/src/annotation/data/Field.ts +15 -0
  19. package/src/annotation/data/PrimaryKey.ts +7 -0
  20. package/src/annotation/data/SqlSession.ts +7 -0
  21. package/src/annotation/data/Table.ts +37 -0
  22. package/src/annotation/data/Transactional.ts +60 -0
  23. package/src/annotation/env/ApplicationSetting.ts +24 -0
  24. package/src/annotation/env/BaseFilePath.ts +12 -0
  25. package/src/annotation/env/BasePath.ts +12 -0
  26. package/src/annotation/env/ENV.ts +8 -0
  27. package/src/annotation/env/ResourcePath.ts +7 -0
  28. package/src/annotation/lifeCycle/AddLifeCycleItem.ts +25 -0
  29. package/src/annotation/lifeCycle/ApplicationDestory.ts +13 -0
  30. package/src/annotation/lifeCycle/ApplicationInit.ts +13 -0
  31. package/src/annotation/lifeCycle/ApplicationRunner.ts +5 -0
  32. package/src/annotation/lifeCycle/ApplicationStart.ts +23 -0
  33. package/src/annotation/lifeCycle/ApplicationStop.ts +18 -0
  34. package/src/annotation/property/Deprecate.ts +17 -0
  35. package/src/annotation/property/NotImplemented.ts +5 -0
  36. package/src/annotation/property/Override.ts +4 -0
  37. package/src/annotation/property/Readonly.ts +12 -0
  38. package/src/annotation/scan/ComponentInjection.ts +22 -0
  39. package/src/annotation/scan/ComponentScan.ts +7 -0
  40. package/src/annotation/scan/ComponentScanExclusion.ts +26 -0
  41. package/src/annotation/scan/Hotter.ts +6 -0
  42. package/src/annotation/stereotype/BeanName.ts +9 -0
  43. package/src/annotation/stereotype/Component.ts +6 -0
  44. package/src/annotation/stereotype/Configure.ts +12 -0
  45. package/src/annotation/stereotype/Controller.ts +7 -0
  46. package/src/annotation/stereotype/Injection.ts +10 -0
  47. package/src/annotation/stereotype/Log.ts +17 -0
  48. package/src/annotation/stereotype/Repository.ts +7 -0
  49. package/src/annotation/stereotype/Service.ts +7 -0
  50. package/src/annotation/valid/AddChildValid.ts +61 -0
  51. package/src/annotation/valid/DefaultVal.ts +8 -0
  52. package/src/annotation/valid/NotNull.ts +7 -0
  53. package/src/annotation/valid/Rule.ts +104 -0
  54. package/src/annotation/valid/Size.ts +13 -0
  55. package/src/annotation/valid/Type.ts +8 -0
  56. package/src/annotation/valid/ValidCustom.ts +21 -0
  57. package/src/annotation/valid/ValidForm.ts +146 -0
  58. package/src/annotation.ts +103 -0
  59. package/src/config/ApplicationConfig.ts +5 -0
  60. package/src/config/SysConfig.ts +28 -0
  61. package/src/constant/AppStatusEnum.ts +5 -0
  62. package/src/constant/BootPriority.ts +7 -0
  63. package/src/constant/CommonConstant.ts +14 -0
  64. package/src/constant/ComponentKind.ts +7 -0
  65. package/src/constant/DataTypes.ts +15 -0
  66. package/src/constant/FastCarMetaData.ts +25 -0
  67. package/src/constant/LifeCycleModule.ts +5 -0
  68. package/src/db.ts +182 -0
  69. package/src/index.ts +11 -0
  70. package/src/interface/ApplicationHook.ts +9 -0
  71. package/src/interface/ApplicationRunnerService.ts +3 -0
  72. package/src/interface/DataSourceManager.ts +14 -0
  73. package/src/interface/Logger.ts +9 -0
  74. package/src/model/BaseMapper.ts +115 -0
  75. package/src/model/DataMap.ts +103 -0
  76. package/src/model/FormRuleModel.ts +23 -0
  77. package/src/model/ValidError.ts +1 -0
  78. package/src/model/WinstonLogger.ts +119 -0
  79. package/src/type/ComponentDesc.ts +5 -0
  80. package/src/type/DesignMeta.ts +11 -0
  81. package/src/type/FileHotterDesc.ts +4 -0
  82. package/src/type/MapperType.ts +8 -0
  83. package/src/type/ProcessType.ts +12 -0
  84. package/src/type/SqlError.ts +3 -0
  85. package/src/type/WinstonLoggerType.ts +18 -0
  86. package/src/utils/ClassLoader.ts +72 -0
  87. package/src/utils/ClassUtils.ts +38 -0
  88. package/src/utils/CryptoUtil.ts +106 -0
  89. package/src/utils/DataFormat.ts +97 -0
  90. package/src/utils/DateUtil.ts +85 -0
  91. package/src/utils/FileUtil.ts +172 -0
  92. package/src/utils/FormatStr.ts +13 -0
  93. package/src/utils/Mix.ts +69 -0
  94. package/src/utils/ReflectUtil.ts +22 -0
  95. package/src/utils/TypeUtil.ts +56 -0
  96. package/src/utils/ValidationUtil.ts +138 -0
  97. package/src/utils.ts +13 -0
  98. package/target/FastCarApplication.js +661 -0
  99. package/target/annotation/AddRequireModule.js +21 -0
  100. package/target/annotation/Application.js +45 -0
  101. package/target/annotation/Autowired.js +15 -0
  102. package/target/annotation/Calldependency.js +18 -0
  103. package/target/annotation/ExceptionMonitor.js +16 -0
  104. package/target/annotation/bind/AddRequireModule.js +21 -0
  105. package/target/annotation/bind/AliasInjection.js +23 -0
  106. package/target/annotation/bind/Autowired.js +13 -0
  107. package/target/annotation/bind/CallDependency.js +23 -0
  108. package/target/annotation/data/DBType.js +11 -0
  109. package/target/annotation/data/DS.js +54 -0
  110. package/target/annotation/data/DSIndex.js +9 -0
  111. package/target/annotation/data/Entity.js +10 -0
  112. package/target/annotation/data/Field.js +18 -0
  113. package/target/annotation/data/PrimaryKey.js +9 -0
  114. package/target/annotation/data/SqlSession.js +9 -0
  115. package/target/annotation/data/Table.js +34 -0
  116. package/target/annotation/data/Transactional.js +52 -0
  117. package/target/annotation/env/ApplicationSetting.js +25 -0
  118. package/target/annotation/env/BaseFilePath.js +14 -0
  119. package/target/annotation/env/BasePath.js +14 -0
  120. package/target/annotation/env/ENV.js +10 -0
  121. package/target/annotation/env/ResourcePath.js +9 -0
  122. package/target/annotation/lifeCycle/AddLifeCycleItem.js +18 -0
  123. package/target/annotation/lifeCycle/ApplicationDestory.js +15 -0
  124. package/target/annotation/lifeCycle/ApplicationInit.js +15 -0
  125. package/target/annotation/lifeCycle/ApplicationRunner.js +7 -0
  126. package/target/annotation/lifeCycle/ApplicationStart.js +24 -0
  127. package/target/annotation/lifeCycle/ApplicationStop.js +20 -0
  128. package/target/annotation/property/Deprecate.js +19 -0
  129. package/target/annotation/property/NotImplemented.js +8 -0
  130. package/target/annotation/property/Override.js +7 -0
  131. package/target/annotation/property/Readonly.js +16 -0
  132. package/target/annotation/scan/ComponentInjection.js +21 -0
  133. package/target/annotation/scan/ComponentScan.js +9 -0
  134. package/target/annotation/scan/ComponentScanExclusion.js +25 -0
  135. package/target/annotation/scan/Hotter.js +8 -0
  136. package/target/annotation/stereotype/BeanName.js +11 -0
  137. package/target/annotation/stereotype/Component.js +8 -0
  138. package/target/annotation/stereotype/Configure.js +14 -0
  139. package/target/annotation/stereotype/Controller.js +9 -0
  140. package/target/annotation/stereotype/Injection.js +12 -0
  141. package/target/annotation/stereotype/Log.js +16 -0
  142. package/target/annotation/stereotype/Repository.js +9 -0
  143. package/target/annotation/stereotype/Service.js +9 -0
  144. package/target/annotation/valid/AddChildValid.js +56 -0
  145. package/target/annotation/valid/DefaultVal.js +10 -0
  146. package/target/annotation/valid/NotNull.js +8 -0
  147. package/target/annotation/valid/Rule.js +100 -0
  148. package/target/annotation/valid/Size.js +10 -0
  149. package/target/annotation/valid/Type.js +10 -0
  150. package/target/annotation/valid/ValidCustom.js +17 -0
  151. package/target/annotation/valid/ValidForm.js +131 -0
  152. package/target/annotation.js +101 -0
  153. package/target/config/ApplicationConfig.js +2 -0
  154. package/target/config/SysConfig.js +19 -0
  155. package/target/constant/AppStatusEnum.js +9 -0
  156. package/target/constant/BootPriority.js +11 -0
  157. package/target/constant/CommonConstant.js +16 -0
  158. package/target/constant/ComponentKind.js +11 -0
  159. package/target/constant/DataTypes.js +19 -0
  160. package/target/constant/FastCarMetaData.js +29 -0
  161. package/target/constant/LifeCycleModule.js +9 -0
  162. package/target/db.js +46 -0
  163. package/target/index.js +21 -0
  164. package/target/interface/ApplicationHook.js +2 -0
  165. package/target/interface/ApplicationRunnerService.js +2 -0
  166. package/target/interface/DataSourceManager.js +2 -0
  167. package/target/interface/Logger.js +5 -0
  168. package/target/model/BaseMapper.js +98 -0
  169. package/target/model/DataMap.js +87 -0
  170. package/target/model/FormRuleModel.js +2 -0
  171. package/target/model/ValidError.js +5 -0
  172. package/target/model/WinstonLogger.js +95 -0
  173. package/target/type/ComponentDesc.js +2 -0
  174. package/target/type/DesignMeta.js +15 -0
  175. package/target/type/FileHotterDesc.js +2 -0
  176. package/target/type/MapperType.js +2 -0
  177. package/target/type/ProcessType.js +2 -0
  178. package/target/type/SqlError.js +5 -0
  179. package/target/type/WinstonLoggerType.js +9 -0
  180. package/target/utils/ClassUtils.js +35 -0
  181. package/target/utils/CryptoUtil.js +84 -0
  182. package/target/utils/DataFormat.js +84 -0
  183. package/target/utils/DateUtil.js +71 -0
  184. package/target/utils/FileUtil.js +153 -0
  185. package/target/utils/FormatStr.js +14 -0
  186. package/target/utils/Mix.js +62 -0
  187. package/target/utils/ReflectUtil.js +22 -0
  188. package/target/utils/TypeUtil.js +47 -0
  189. package/target/utils/ValidationUtil.js +118 -0
  190. package/target/utils/classLoader.js +65 -0
  191. package/target/utils.js +23 -0
  192. package/test/example/logs/app.log +0 -0
  193. package/test/example/logs/logger.log +12 -0
  194. package/test/example/logs/logger1.log +12 -0
  195. package/test/example/logs/logger2.log +12 -0
  196. package/test/example/logs/logger3.log +12 -0
  197. package/test/example/logs/logger4.log +12 -0
  198. package/test/example/logs/logger5.log +6 -0
  199. package/test/example/logs/sys.log +7 -0
  200. package/test/example/logs/sys1.log +14 -0
  201. package/test/example/logs/sys10.log +12 -0
  202. package/test/example/logs/sys11.log +5 -0
  203. package/test/example/logs/sys13.log +5 -0
  204. package/test/example/logs/sys2.log +3 -0
  205. package/test/example/logs/sys4.log +15 -0
  206. package/test/example/logs/sys5.log +9 -0
  207. package/test/example/logs/sys6.log +10 -0
  208. package/test/example/logs/sys7.log +15 -0
  209. package/test/example/logs/sys8.log +11 -0
  210. package/test/example/logs/sys9.log +10 -0
  211. package/test/example/resource/application-test.yml +0 -0
  212. package/test/example/resource/application.yml +7 -0
  213. package/test/example/resource/evnconfig-test.yml +1 -0
  214. package/test/example/resource/hello.yml +1 -0
  215. package/test/example/simple/app.ts +99 -0
  216. package/test/example/simple/component/StartRunner.ts +10 -0
  217. package/test/example/simple/component/StopRunner.ts +14 -0
  218. package/test/example/simple/config/EnvConfig.ts +6 -0
  219. package/test/example/simple/config/HelloConfig.ts +8 -0
  220. package/test/example/simple/controller/AliasController.ts +6 -0
  221. package/test/example/simple/controller/HelloController.ts +39 -0
  222. package/test/example/simple/controller/NotFoundController.ts +20 -0
  223. package/test/example/simple/service/CallService.ts +11 -0
  224. package/test/example/simple/service/HelloService.ts +10 -0
  225. package/test/example/simple/service/LogService.ts +19 -0
  226. package/test/logs/logger.log +0 -0
  227. package/test/logs/sys.log +227 -0
  228. package/test/multi/app.ts +15 -0
  229. package/test/multi/service/aService.ts +16 -0
  230. package/test/multi/service/bService.ts +18 -0
  231. package/test/multi/service/cService.ts +21 -0
  232. package/test/unit/dataMap-test.ts +48 -0
  233. package/test/unit/decorators-test.ts +38 -0
  234. package/test/unit/ds-test.ts +33 -0
  235. package/test/unit/logs/sys.log +24 -0
  236. package/test/unit/reflectMetadata-test.ts +15 -0
  237. package/test/unit/valid-test.ts +65 -0
  238. package/test/unit/winston-test.ts +15 -0
  239. package/utils.d.ts +166 -0
@@ -0,0 +1,661 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var FastCarApplication_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ require("reflect-metadata");
14
+ const process = require("process");
15
+ const Events = require("events");
16
+ const path = require("path");
17
+ const classLoader_1 = require("./utils/classLoader");
18
+ const FileUtil_1 = require("./utils/FileUtil");
19
+ const Mix_1 = require("./utils/Mix");
20
+ const TypeUtil_1 = require("./utils/TypeUtil");
21
+ const SysConfig_1 = require("./config/SysConfig");
22
+ const FastCarMetaData_1 = require("./constant/FastCarMetaData");
23
+ const CommonConstant_1 = require("./constant/CommonConstant");
24
+ const LifeCycleModule_1 = require("./constant/LifeCycleModule");
25
+ const fs = require("fs");
26
+ const AppStatusEnum_1 = require("./constant/AppStatusEnum");
27
+ const ValidationUtil_1 = require("./utils/ValidationUtil");
28
+ const Component_1 = require("./annotation/stereotype/Component");
29
+ const WinstonLogger_1 = require("./model/WinstonLogger");
30
+ const DateUtil_1 = require("./utils/DateUtil");
31
+ let FastCarApplication = FastCarApplication_1 = class FastCarApplication extends Events {
32
+ constructor() {
33
+ super();
34
+ this.resourcePath = ""; //资源路径
35
+ this.sysConfig = SysConfig_1.SYSDefaultConfig;
36
+ this.componentMap = new Map();
37
+ this.componentDeatils = new Map();
38
+ this.applicationStatus = AppStatusEnum_1.AppStatusEnum.READY;
39
+ this.liveTime = Date.now();
40
+ this.watchFiles = new Map();
41
+ this.delayHotIds = new Map();
42
+ this.reloadTimerId = null;
43
+ this.loadSelf();
44
+ this.addHot();
45
+ }
46
+ /***
47
+ * @version 1.0 根据原型加载注入的方法
48
+ *
49
+ */
50
+ getInjectionUniqueKey(target) {
51
+ let key = Reflect.getMetadata(FastCarMetaData_1.FastCarMetaData.InjectionUniqueKey, target);
52
+ return key;
53
+ }
54
+ loadSelf() {
55
+ let key = this.getInjectionUniqueKey(FastCarApplication_1);
56
+ this.componentMap.set(key, this);
57
+ this.componentDeatils.set("FastCarApplication", {
58
+ id: key,
59
+ name: "FastCarApplication",
60
+ path: __filename,
61
+ });
62
+ //暴露一个全局的app 以便调用
63
+ Reflect.set(global, CommonConstant_1.CommonConstant.FastcarApp, this);
64
+ }
65
+ /***
66
+ * @version 1.0 热更新组件
67
+ * @version 1.1 热更新配置文件
68
+ */
69
+ addHot() {
70
+ this.on("reload", (fp) => {
71
+ if (this.applicationStatus != AppStatusEnum_1.AppStatusEnum.RUN) {
72
+ return;
73
+ }
74
+ this.addDelayHot(fp, 1);
75
+ });
76
+ this.on("sysReload", (fp) => {
77
+ if (fp.indexOf(CommonConstant_1.CommonConstant.Application) != -1) {
78
+ this.addDelayHot(fp, 2);
79
+ }
80
+ });
81
+ }
82
+ addDelayHot(fp, loadType) {
83
+ if (this.delayHotIds.has(fp)) {
84
+ return;
85
+ }
86
+ this.delayHotIds.set(fp, {
87
+ fp,
88
+ loadType,
89
+ });
90
+ if (!this.reloadTimerId) {
91
+ this.reloadTimerId = setTimeout(() => {
92
+ this.reloadFiles();
93
+ }, 1000);
94
+ }
95
+ }
96
+ reloadFiles() {
97
+ this.delayHotIds.forEach(({ fp, loadType }) => {
98
+ switch (loadType) {
99
+ case 1: {
100
+ let moduleClass = classLoader_1.default.loadModule(fp, true);
101
+ this.sysLogger.info("hot update---" + fp);
102
+ if (moduleClass != null) {
103
+ moduleClass.forEach((func) => {
104
+ this.convertInstance(func, fp);
105
+ });
106
+ }
107
+ break;
108
+ }
109
+ case 2: {
110
+ this.sysLogger.info("sysConfig hot update----" + fp);
111
+ this.loadSysConfig();
112
+ break;
113
+ }
114
+ default: {
115
+ }
116
+ }
117
+ });
118
+ this.delayHotIds.clear();
119
+ this.reloadTimerId = null;
120
+ }
121
+ /***
122
+ * @version 1.0 获取资源路径
123
+ */
124
+ getResourcePath() {
125
+ if (!!this.resourcePath) {
126
+ return this.resourcePath;
127
+ }
128
+ let resourcePath = path.join(this.basePath, "../", CommonConstant_1.CommonConstant.Resource);
129
+ this.resourcePath = resourcePath;
130
+ return resourcePath;
131
+ }
132
+ /***
133
+ * @version 1.0 获取项目的基本路径
134
+ *
135
+ */
136
+ getBasePath() {
137
+ return this.basePath;
138
+ }
139
+ /***
140
+ * @version 1.0 加载系统配置 加载顺序为 default json < yaml < env
141
+ *
142
+ */
143
+ loadSysConfig() {
144
+ this.sysConfig = FileUtil_1.default.getApplicationConfig(this.getResourcePath(), CommonConstant_1.CommonConstant.Application, this.sysConfig);
145
+ let env = Reflect.get(this, CommonConstant_1.CommonConstant.ENV) || this.sysConfig.application.env;
146
+ this.sysConfig = FileUtil_1.default.getApplicationConfig(this.getResourcePath(), `${CommonConstant_1.CommonConstant.Application}-${env}`, this.sysConfig);
147
+ //自定义环境变量设置
148
+ process.env.NODE_ENV = env;
149
+ //判断程序内是否有配置
150
+ let applicationSesstings = Reflect.getMetadata(CommonConstant_1.CommonConstant.FastcarSetting, this);
151
+ if (applicationSesstings) {
152
+ applicationSesstings.forEach((value, key) => {
153
+ let afterConfig = value;
154
+ let beforeConfig = this.sysConfig.settings.get(key);
155
+ if (beforeConfig) {
156
+ //对settings的属性进行覆盖
157
+ if (typeof beforeConfig == "object") {
158
+ afterConfig = Object.assign(beforeConfig, afterConfig);
159
+ }
160
+ }
161
+ this.sysConfig.settings.set(key, afterConfig);
162
+ });
163
+ }
164
+ //读取app的必要信息 name和版本号 根据 package.json
165
+ let packagePath = path.join(this.basePath, "../", "package.json");
166
+ if (fs.existsSync(packagePath)) {
167
+ let packageInfo = require(packagePath);
168
+ if (packageInfo.name) {
169
+ this.sysConfig.application.name = packageInfo.name;
170
+ }
171
+ if (packageInfo.version) {
172
+ this.sysConfig.application.version = packageInfo.version;
173
+ }
174
+ }
175
+ }
176
+ setSetting(key, value) {
177
+ this.sysConfig.settings.set(key, value);
178
+ }
179
+ /***
180
+ * @version 1.0 获取自定义设置 设置优先级 配置自定义>系统配置>初始化
181
+ *
182
+ */
183
+ getSetting(key) {
184
+ let res = this.sysConfig.settings.get(key);
185
+ if (ValidationUtil_1.default.isNotNull(res)) {
186
+ return res;
187
+ }
188
+ res = Reflect.get(this.sysConfig, key);
189
+ if (ValidationUtil_1.default.isNotNull(res)) {
190
+ res;
191
+ }
192
+ return Reflect.get(this, key);
193
+ }
194
+ /***
195
+ * @version 1.0 获取应用配置
196
+ */
197
+ getapplicationConfig() {
198
+ return this.sysConfig.application;
199
+ }
200
+ /***
201
+ * @version 1.0 扫描组件
202
+ * @version 1.1 新增手动注入组件
203
+ * @version 1.2 改成统一入口
204
+ */
205
+ loadClass() {
206
+ //加载文件扫描下的bean
207
+ let tmpFilePath = Array.of();
208
+ let includeList = Reflect.get(this, FastCarMetaData_1.FastCarMetaData.ComponentScan);
209
+ if (includeList) {
210
+ includeList.forEach((item) => {
211
+ let tmpList = FileUtil_1.default.getFilePathList(item);
212
+ tmpFilePath = tmpFilePath.concat(tmpList);
213
+ });
214
+ }
215
+ let filePathList = FileUtil_1.default.getFilePathList(this.basePath);
216
+ filePathList = tmpFilePath.concat(filePathList);
217
+ filePathList = [...new Set(filePathList)];
218
+ let excludeList = Reflect.get(this, FastCarMetaData_1.FastCarMetaData.ComponentScanExclusion);
219
+ if (excludeList) {
220
+ let excludAllPath = [];
221
+ excludeList.forEach((item) => {
222
+ let exlist = FileUtil_1.default.getFilePathList(item);
223
+ excludAllPath = [...excludAllPath, ...exlist];
224
+ });
225
+ filePathList = filePathList.filter((item) => {
226
+ return !excludAllPath.includes(item);
227
+ });
228
+ }
229
+ let resp = this.getResourcePath();
230
+ for (let f of filePathList) {
231
+ if (f == this.baseFileName) {
232
+ continue;
233
+ }
234
+ //如果是资源路径下的则自动过滤掉
235
+ if (f.startsWith(resp)) {
236
+ continue;
237
+ }
238
+ let moduleClass = classLoader_1.default.loadModule(f);
239
+ if (moduleClass != null) {
240
+ moduleClass.forEach((func, name) => {
241
+ if (this.componentMap.has(name)) {
242
+ let repeatError = new Error(`Duplicate ${name} instance objects are not allowed `);
243
+ this.sysLogger.error(repeatError.message);
244
+ throw repeatError;
245
+ }
246
+ this.convertInstance(func, f);
247
+ });
248
+ }
249
+ }
250
+ }
251
+ getInjectionUniqueKeyByFilePath(fp, name) {
252
+ let list = this.watchFiles.get(fp);
253
+ if (list) {
254
+ for (let item of list) {
255
+ if (item.name == name) {
256
+ return item.key;
257
+ }
258
+ }
259
+ }
260
+ return null;
261
+ }
262
+ /***
263
+ * @version 1.0 转成实例对象
264
+ * @version 1.0.1 新增加载时识别载入配置选项
265
+ *
266
+ */
267
+ convertInstance(classZ, fp) {
268
+ //只有依赖注入的组件才能被实例化
269
+ let Target = classZ;
270
+ let instanceKey = Reflect.getMetadata(FastCarMetaData_1.FastCarMetaData.InjectionUniqueKey, classZ);
271
+ if (!instanceKey) {
272
+ Target = Reflect.get(classZ, "__target__");
273
+ if (!Target) {
274
+ return;
275
+ }
276
+ instanceKey = Reflect.getMetadata(FastCarMetaData_1.FastCarMetaData.InjectionUniqueKey, Target);
277
+ }
278
+ if (!!instanceKey) {
279
+ let iname = classZ?.name || FileUtil_1.default.getFileName(fp);
280
+ let beforeKey = this.getInjectionUniqueKeyByFilePath(fp, iname);
281
+ if (beforeKey) {
282
+ let beforeInstance = this.componentMap.get(beforeKey);
283
+ if (!!beforeInstance) {
284
+ Mix_1.default.assign(beforeInstance, classZ);
285
+ return;
286
+ }
287
+ }
288
+ //判断是否需要加载对应配置
289
+ let cp = Reflect.getMetadata(LifeCycleModule_1.LifeCycleModule.LoadConfigure, Target);
290
+ if (cp) {
291
+ let rfp = path.join(this.getResourcePath(), cp);
292
+ let tmpConfig = FileUtil_1.default.getResource(rfp);
293
+ //进行实例化赋值
294
+ if (tmpConfig) {
295
+ //进行赋值不改变基础属性
296
+ if (TypeUtil_1.default.isFunction(classZ)) {
297
+ Mix_1.default.copPropertyValue(classZ.prototype, tmpConfig);
298
+ }
299
+ else {
300
+ Mix_1.default.copPropertyValue(classZ, tmpConfig);
301
+ }
302
+ }
303
+ }
304
+ let instance = TypeUtil_1.default.isFunction(classZ) ? new classZ() : classZ;
305
+ this.componentMap.set(instanceKey, instance);
306
+ this.componentDeatils.set(instanceKey, {
307
+ id: instanceKey,
308
+ name: classZ?.name || FileUtil_1.default.getFileName(fp),
309
+ path: fp,
310
+ });
311
+ //判断是否有别名
312
+ let aliasName = Reflect.getMetadata(FastCarMetaData_1.FastCarMetaData.Alias, instance);
313
+ if (aliasName) {
314
+ this.componentMap.set(aliasName, instance);
315
+ this.componentDeatils.set(aliasName, {
316
+ id: aliasName,
317
+ name: aliasName,
318
+ path: fp,
319
+ });
320
+ }
321
+ //判断是否需要热更加载
322
+ if (this.isHotter() || Reflect.getMetadata(FastCarMetaData_1.FastCarMetaData.Hotter, instance)) {
323
+ let fpObj = this.watchFiles.get(fp);
324
+ let fpdesc = {
325
+ key: instanceKey,
326
+ name: iname,
327
+ };
328
+ if (!fpObj) {
329
+ fpObj = [fpdesc];
330
+ classLoader_1.default.watchServices(fp, this);
331
+ this.watchFiles.set(fp, fpObj);
332
+ }
333
+ else {
334
+ fpObj.push(fpdesc);
335
+ }
336
+ }
337
+ }
338
+ }
339
+ /***
340
+ * @version 1.0 装配模块
341
+ * @version 1.0 装配日志模块
342
+ * @version 1.1 移除装配日志模块 改为随用随取
343
+ * @deprecated 弃用系统自动装配
344
+ */
345
+ // injectionModule(instance: any, instanceName: string | symbol): void {
346
+ // let relyname = FastCarMetaData.IocModule;
347
+ // let moduleList: Map<string, string> = Reflect.getMetadata(relyname, instance);
348
+ // let detailInfo = this.componentDeatils.get(instanceName);
349
+ // if (moduleList && moduleList.size > 0) {
350
+ // moduleList.forEach((name: string, propertyKey: string) => {
351
+ // let func = this.componentMap.get(name);
352
+ // //如果等于自身则进行注入
353
+ // if (name === FastCarApplication.name || name === FastCarMetaData.APP) {
354
+ // func = this;
355
+ // } else {
356
+ // if (!this.componentMap.has(name)) {
357
+ // //找不到依赖项
358
+ // let injectionError = new Error(`Unsatisfied dependency expressed through [${propertyKey}] in ${detailInfo?.path} `);
359
+ // this.sysLogger.error(injectionError.message);
360
+ // throw injectionError;
361
+ // }
362
+ // }
363
+ // Reflect.set(instance, propertyKey, func);
364
+ // });
365
+ // }
366
+ // }
367
+ /**
368
+ * @version 1.0 加载需要注入的类
369
+ */
370
+ // loadInjectionModule() {
371
+ // this.componentMap.forEach((instance, instanceName) => {
372
+ // //补充实例找不到时 不能被注解
373
+ // if (!instance) {
374
+ // let insatnceError = new Error(`instance not found by ${instanceName.toString()}`);
375
+ // this.sysLogger.error(insatnceError.message);
376
+ // throw insatnceError;
377
+ // }
378
+ // this.injectionModule(instance, instanceName);
379
+ // });
380
+ // }
381
+ /***
382
+ * @version 1.0 根据类型获取组件
383
+ */
384
+ getComponentByType(name) {
385
+ let instanceList = Array.of();
386
+ this.componentMap.forEach((instance) => {
387
+ let flag = Reflect.hasMetadata(name, instance);
388
+ if (flag) {
389
+ instanceList.push(instance);
390
+ }
391
+ });
392
+ return instanceList;
393
+ }
394
+ /***
395
+ * @version 1.0 获取全部的组件列表
396
+ */
397
+ getComponentList() {
398
+ return [...this.componentMap.values()];
399
+ }
400
+ /***
401
+ * @version 1.0 根据名称组件
402
+ */
403
+ getComponentByName(name) {
404
+ return this.componentMap.get(name);
405
+ }
406
+ /***
407
+ * @version 1.0 判断是否拥有组件名称
408
+ */
409
+ hasComponentByName(name) {
410
+ return this.componentMap.has(name);
411
+ }
412
+ /***
413
+ * @version 1.0 根据原型获取实例
414
+ */
415
+ getComponentByTarget(target) {
416
+ let key = this.getInjectionUniqueKey(target);
417
+ return this.componentMap.get(key);
418
+ }
419
+ /**
420
+ * @version 1.0 获取组件详情列表
421
+ *
422
+ */
423
+ getComponentDetailsList() {
424
+ return [...this.componentDeatils.values()];
425
+ }
426
+ /***
427
+ * @version 1.0 根据名称获取组件的加载情况
428
+ *
429
+ */
430
+ getComponentDetailByName(name) {
431
+ return this.componentDeatils.get(name);
432
+ }
433
+ /***
434
+ * @version 1.0 根据原型获取组件的加载信息
435
+ *
436
+ */
437
+ getComponentDetailByTarget(target) {
438
+ let key = this.getInjectionUniqueKey(target);
439
+ return this.componentDeatils.get(key);
440
+ }
441
+ /**
442
+ * @version 1.0 开启日志系统
443
+ * @version 1.1 更改为采用winston日志
444
+ */
445
+ startLog() {
446
+ let logConfig = this.getSetting("log");
447
+ let defaultConfig = Object.assign({}, SysConfig_1.LogDefaultConfig, { rootPath: path.join(this.basePath, "../logs") });
448
+ if (logConfig) {
449
+ Object.assign(defaultConfig, logConfig);
450
+ }
451
+ this.loggerFactory = new WinstonLogger_1.default(defaultConfig);
452
+ //添加系统日志
453
+ this.sysLogger = this.loggerFactory.addLogger(CommonConstant_1.CommonConstant.SYSLOGGER);
454
+ }
455
+ /***
456
+ * @version 1.0 初始化应用
457
+ */
458
+ init() {
459
+ //加载配置
460
+ this.basePath = Reflect.get(this, CommonConstant_1.CommonConstant.BasePath) || require.main?.path || module.path;
461
+ this.baseFileName = Reflect.get(this, CommonConstant_1.CommonConstant.BaseFileName) || require.main?.filename || module.filename;
462
+ this.beforeStartServer();
463
+ this.startServer();
464
+ this.addExitEvent();
465
+ this.addExecptionEvent();
466
+ }
467
+ async exitEvent(msg) {
468
+ //防止多次停止 原则上不会发生
469
+ if (this.applicationStatus == AppStatusEnum_1.AppStatusEnum.RUN) {
470
+ this.applicationStatus = AppStatusEnum_1.AppStatusEnum.STOP;
471
+ this.sysLogger.info("exit reason", msg);
472
+ await this.beforeStopServer();
473
+ process.exit();
474
+ }
475
+ }
476
+ addExitEvent() {
477
+ process.on("beforeExit", () => {
478
+ this.exitEvent("beforeExit exit");
479
+ });
480
+ process.on("SIGINT", () => {
481
+ this.exitEvent("sigint exit");
482
+ });
483
+ process.on("message", async (msg) => {
484
+ if (msg == "shutdown") {
485
+ this.exitEvent("shutdown");
486
+ }
487
+ });
488
+ process.on("exit", () => {
489
+ this.stopServer();
490
+ });
491
+ }
492
+ addExecptionEvent() {
493
+ process.on("uncaughtException", (err, origin) => {
494
+ this.sysLogger.error(`Caught exception: ${err.message}`);
495
+ this.sysLogger.error(`Exception origin: ${origin}`);
496
+ this.sysLogger.error(`stack: ${err.stack}`);
497
+ });
498
+ process.on("unhandledRejection", (reason, promise) => {
499
+ this.sysLogger.error("Unhandled Rejection at:", promise);
500
+ this.sysLogger.error("reason:", reason);
501
+ });
502
+ }
503
+ /***
504
+ * @version 1.0 自动调用方法
505
+ */
506
+ async automaticRun(name) {
507
+ let list = [];
508
+ this.componentMap.forEach((item) => {
509
+ let runInfo = Reflect.hasMetadata(name, item);
510
+ if (runInfo) {
511
+ let childList = Reflect.getMetadata(name, item);
512
+ childList.forEach((citem) => {
513
+ list.push({
514
+ order: citem.order,
515
+ exec: citem.exec,
516
+ item,
517
+ });
518
+ });
519
+ }
520
+ });
521
+ list.sort((a, b) => {
522
+ return a.order - b.order;
523
+ });
524
+ for (let { exec, item } of list) {
525
+ let fn = item[exec];
526
+ if (TypeUtil_1.default.isPromise(fn)) {
527
+ await Promise.resolve(Reflect.apply(fn, item, []));
528
+ }
529
+ else {
530
+ Reflect.apply(fn, item, []);
531
+ }
532
+ }
533
+ }
534
+ /**
535
+ * @version 1.0 开启应用前执行的操作 加载配置,扫描组件,注入依赖组件
536
+ */
537
+ beforeStartServer() {
538
+ //加载日志
539
+ this.loadSysConfig();
540
+ //监听系统配置
541
+ if (this.isHotterSysConfig()) {
542
+ classLoader_1.default.watchServices(this.getResourcePath(), this, "sysReload");
543
+ }
544
+ //开启日志
545
+ this.startLog();
546
+ this.sysLogger.info("Start scanning component");
547
+ this.loadClass();
548
+ this.sysLogger.info("Complete component scan");
549
+ // this.sysLogger.info("Start component injection");
550
+ // this.loadInjectionModule();
551
+ // this.sysLogger.info("Complete component injection");
552
+ }
553
+ /***
554
+ * @version 1.0 启动服务
555
+ */
556
+ async startServer() {
557
+ this.sysLogger.info("Call application initialization method");
558
+ await this.automaticRun(LifeCycleModule_1.LifeCycleModule.ApplicationStart);
559
+ this.sysLogger.info(`start server ${this.sysConfig.application.name} is run`);
560
+ this.sysLogger.info(`version ${this.sysConfig.application.version}`);
561
+ this.applicationStatus = AppStatusEnum_1.AppStatusEnum.RUN;
562
+ if (process.send && TypeUtil_1.default.isFunction(process.send)) {
563
+ process.send("ready");
564
+ }
565
+ }
566
+ /***
567
+ * @version 1.0 停止服务前自动调用服务
568
+ */
569
+ async beforeStopServer() {
570
+ this.sysLogger.info("Call the method before the application stops");
571
+ await this.automaticRun(LifeCycleModule_1.LifeCycleModule.ApplicationStop);
572
+ }
573
+ /***
574
+ * @version 1.0 停止服务
575
+ */
576
+ stopServer() {
577
+ this.sysLogger.info("application stop");
578
+ }
579
+ /**
580
+ * @version 1.0 获取app名称
581
+ */
582
+ getApplicationName() {
583
+ return this.sysConfig.application.name;
584
+ }
585
+ /***
586
+ * @version 1.0 获取系统日志
587
+ *
588
+ */
589
+ getSysLogger() {
590
+ return this.sysLogger;
591
+ }
592
+ /***
593
+ * @version 1.0 获取文件内容
594
+ */
595
+ getFileContent(fp) {
596
+ if (!fs.existsSync(fp)) {
597
+ fp = path.join(this.getResourcePath(), fp);
598
+ if (!fs.existsSync(fp)) {
599
+ return "";
600
+ }
601
+ }
602
+ let currStats = fs.statSync(fp);
603
+ if (!currStats.isFile()) {
604
+ return "";
605
+ }
606
+ return fs.readFileSync(fp).toString();
607
+ }
608
+ /***
609
+ * @version 1.0 是否支持热更
610
+ *
611
+ */
612
+ isHotter() {
613
+ return !!this.getSetting("hotter");
614
+ }
615
+ /***
616
+ * @version 1.0 是否支持资源文件热更
617
+ */
618
+ isHotterSysConfig() {
619
+ return !!this.getSetting(FastCarMetaData_1.FastCarMetaData.HotterSysConfig);
620
+ }
621
+ /***
622
+ * @version 1.0 指定热更新文件
623
+ *
624
+ */
625
+ specifyHotUpdate(fp) {
626
+ if (fs.existsSync(fp)) {
627
+ this.emit("reload", fp);
628
+ }
629
+ }
630
+ /***
631
+ * @version 1.0 获取进程的信息
632
+ *
633
+ */
634
+ getMemoryUsage() {
635
+ let { rss, heapTotal, heapUsed, arrayBuffers, external } = process.memoryUsage();
636
+ return {
637
+ pid: process.pid,
638
+ name: this.sysConfig.application.name,
639
+ env: this.sysConfig.application.env,
640
+ version: this.sysConfig.application.version,
641
+ rss: FileUtil_1.default.formatBytes(rss),
642
+ heapTotal: FileUtil_1.default.formatBytes(heapTotal),
643
+ heapUsed: FileUtil_1.default.formatBytes(heapUsed),
644
+ arrayBuffers: FileUtil_1.default.formatBytes(arrayBuffers),
645
+ external: FileUtil_1.default.formatBytes(external),
646
+ uptime: DateUtil_1.default.getTimeStr(Date.now() - this.liveTime), //运行时间
647
+ };
648
+ }
649
+ getLogger(category = CommonConstant_1.CommonConstant.SYSLOGGER) {
650
+ let logger = this.loggerFactory.getLogger(category);
651
+ if (!logger) {
652
+ return this.loggerFactory.addLogger(category);
653
+ }
654
+ return logger;
655
+ }
656
+ };
657
+ FastCarApplication = FastCarApplication_1 = __decorate([
658
+ Component_1.default,
659
+ __metadata("design:paramtypes", [])
660
+ ], FastCarApplication);
661
+ exports.default = FastCarApplication;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const FastCarMetaData_1 = require("../constant/FastCarMetaData");
4
+ require("reflect-metadata");
5
+ /***
6
+ * @version 1.0 依赖模块注入
7
+ *
8
+ */
9
+ function AddRequireModule(target, m, alias) {
10
+ let relyname = FastCarMetaData_1.FastCarMetaData.IocModule;
11
+ if (Reflect.hasMetadata(relyname, target)) {
12
+ let iocMap = Reflect.getMetadata(relyname, target);
13
+ iocMap.set(m, alias);
14
+ }
15
+ else {
16
+ let modules = new Map();
17
+ modules.set(m, alias);
18
+ Reflect.defineMetadata(relyname, modules, target);
19
+ }
20
+ }
21
+ exports.default = AddRequireModule;