@fastcar/core 0.2.60 → 0.2.61

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