@fastcar/core 0.2.62 → 0.2.63

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 (233) hide show
  1. package/package.json +1 -1
  2. package/src/FastCarApplication.ts +30 -9
  3. package/target/FastCarApplication.js +708 -692
  4. package/target/annotation/Application.js +45 -45
  5. package/target/annotation/ExceptionMonitor.js +16 -16
  6. package/target/annotation/bind/AddRequireModule.js +21 -21
  7. package/target/annotation/bind/AliasInjection.js +23 -23
  8. package/target/annotation/bind/Autowired.js +13 -13
  9. package/target/annotation/bind/CallDependency.js +23 -23
  10. package/target/annotation/data/DBType.js +11 -11
  11. package/target/annotation/data/DS.js +54 -54
  12. package/target/annotation/data/DSIndex.js +9 -9
  13. package/target/annotation/data/Entity.js +10 -10
  14. package/target/annotation/data/Field.js +18 -18
  15. package/target/annotation/data/PrimaryKey.js +9 -9
  16. package/target/annotation/data/SqlSession.js +9 -9
  17. package/target/annotation/data/Table.js +35 -35
  18. package/target/annotation/data/Transactional.js +52 -52
  19. package/target/annotation/env/ApplicationSetting.js +25 -25
  20. package/target/annotation/env/BaseFilePath.js +14 -14
  21. package/target/annotation/env/BaseName.js +9 -9
  22. package/target/annotation/env/BasePath.js +14 -14
  23. package/target/annotation/env/ENV.js +10 -10
  24. package/target/annotation/env/ResourcePath.js +9 -9
  25. package/target/annotation/lifeCycle/AddLifeCycleItem.js +18 -18
  26. package/target/annotation/lifeCycle/ApplicationDestory.js +15 -15
  27. package/target/annotation/lifeCycle/ApplicationInit.js +15 -15
  28. package/target/annotation/lifeCycle/ApplicationRunner.js +7 -7
  29. package/target/annotation/lifeCycle/ApplicationStart.js +24 -24
  30. package/target/annotation/lifeCycle/ApplicationStop.js +20 -20
  31. package/target/annotation/property/Deprecate.js +19 -19
  32. package/target/annotation/property/NotImplemented.js +8 -8
  33. package/target/annotation/property/Override.js +7 -7
  34. package/target/annotation/property/Readonly.js +16 -16
  35. package/target/annotation/scan/ComponentInjection.js +21 -21
  36. package/target/annotation/scan/ComponentScan.js +9 -9
  37. package/target/annotation/scan/ComponentScanExclusion.js +25 -25
  38. package/target/annotation/scan/ComponentScanMust.js +25 -25
  39. package/target/annotation/scan/Hotter.js +8 -8
  40. package/target/annotation/stereotype/BeanName.js +11 -11
  41. package/target/annotation/stereotype/Component.js +8 -8
  42. package/target/annotation/stereotype/Configure.js +14 -14
  43. package/target/annotation/stereotype/Controller.js +9 -9
  44. package/target/annotation/stereotype/Injection.js +12 -12
  45. package/target/annotation/stereotype/Log.js +17 -17
  46. package/target/annotation/stereotype/Repository.js +9 -9
  47. package/target/annotation/stereotype/Service.js +9 -9
  48. package/target/annotation/valid/AddChildValid.js +56 -56
  49. package/target/annotation/valid/CustomType.js +11 -11
  50. package/target/annotation/valid/DefaultVal.js +10 -10
  51. package/target/annotation/valid/NotNull.js +8 -8
  52. package/target/annotation/valid/Rule.js +100 -100
  53. package/target/annotation/valid/Size.js +10 -10
  54. package/target/annotation/valid/Type.js +10 -10
  55. package/target/annotation/valid/ValidCustom.js +17 -17
  56. package/target/annotation/valid/ValidForm.js +133 -133
  57. package/target/annotation.js +108 -108
  58. package/target/config/ApplicationConfig.js +2 -2
  59. package/target/config/SysConfig.js +19 -19
  60. package/target/constant/AppStatusEnum.js +9 -9
  61. package/target/constant/BootPriority.js +11 -11
  62. package/target/constant/CommonConstant.js +18 -18
  63. package/target/constant/ComponentKind.js +11 -11
  64. package/target/constant/DataTypes.js +19 -19
  65. package/target/constant/FastCarMetaData.js +31 -31
  66. package/target/constant/LifeCycleModule.js +9 -9
  67. package/target/db.js +49 -49
  68. package/target/index.js +21 -21
  69. package/target/interface/ApplicationHook.js +2 -2
  70. package/target/interface/ApplicationInterface.js +2 -2
  71. package/target/interface/ApplicationRunnerService.js +2 -2
  72. package/target/interface/DataSourceManager.js +2 -2
  73. package/target/interface/Logger.js +5 -5
  74. package/target/model/BaseMapper.js +98 -98
  75. package/target/model/DataMap.js +87 -87
  76. package/target/model/FormRuleModel.js +2 -2
  77. package/target/model/ValidError.js +5 -5
  78. package/target/model/WinstonLogger.js +96 -96
  79. package/target/type/ComponentDesc.js +2 -2
  80. package/target/type/DesignMeta.js +15 -15
  81. package/target/type/FileHotterDesc.js +2 -2
  82. package/target/type/MapperType.js +2 -2
  83. package/target/type/ProcessType.js +2 -2
  84. package/target/type/SqlError.js +5 -5
  85. package/target/type/WinstonLoggerType.js +9 -9
  86. package/target/utils/ClassLoader.js +65 -65
  87. package/target/utils/ClassUtils.js +35 -35
  88. package/target/utils/CryptoUtil.js +86 -86
  89. package/target/utils/DataFormat.js +88 -88
  90. package/target/utils/DateUtil.js +71 -71
  91. package/target/utils/FileUtil.js +153 -153
  92. package/target/utils/FormatStr.js +14 -14
  93. package/target/utils/IPUtils.js +34 -34
  94. package/target/utils/Id.js +9 -9
  95. package/target/utils/Mix.js +62 -62
  96. package/target/utils/ReflectUtil.js +22 -22
  97. package/target/utils/TypeUtil.js +53 -53
  98. package/target/utils/ValidationUtil.js +123 -123
  99. package/target/utils.js +25 -25
  100. package/test/unit/decorators-test.ts +3 -3
  101. package/test/unit/valid-test.ts +49 -54
  102. package/target/src/FastCarApplication.js +0 -704
  103. package/target/src/annotation/Application.js +0 -45
  104. package/target/src/annotation/ExceptionMonitor.js +0 -16
  105. package/target/src/annotation/bind/AddRequireModule.js +0 -21
  106. package/target/src/annotation/bind/AliasInjection.js +0 -23
  107. package/target/src/annotation/bind/Autowired.js +0 -13
  108. package/target/src/annotation/bind/CallDependency.js +0 -23
  109. package/target/src/annotation/data/DBType.js +0 -11
  110. package/target/src/annotation/data/DS.js +0 -54
  111. package/target/src/annotation/data/DSIndex.js +0 -9
  112. package/target/src/annotation/data/Entity.js +0 -10
  113. package/target/src/annotation/data/Field.js +0 -18
  114. package/target/src/annotation/data/PrimaryKey.js +0 -9
  115. package/target/src/annotation/data/SqlSession.js +0 -9
  116. package/target/src/annotation/data/Table.js +0 -35
  117. package/target/src/annotation/data/Transactional.js +0 -52
  118. package/target/src/annotation/env/ApplicationSetting.js +0 -25
  119. package/target/src/annotation/env/BaseFilePath.js +0 -14
  120. package/target/src/annotation/env/BaseName.js +0 -9
  121. package/target/src/annotation/env/BasePath.js +0 -14
  122. package/target/src/annotation/env/ENV.js +0 -10
  123. package/target/src/annotation/env/ResourcePath.js +0 -9
  124. package/target/src/annotation/lifeCycle/AddLifeCycleItem.js +0 -18
  125. package/target/src/annotation/lifeCycle/ApplicationDestory.js +0 -15
  126. package/target/src/annotation/lifeCycle/ApplicationInit.js +0 -15
  127. package/target/src/annotation/lifeCycle/ApplicationRunner.js +0 -7
  128. package/target/src/annotation/lifeCycle/ApplicationStart.js +0 -24
  129. package/target/src/annotation/lifeCycle/ApplicationStop.js +0 -20
  130. package/target/src/annotation/property/Deprecate.js +0 -19
  131. package/target/src/annotation/property/NotImplemented.js +0 -8
  132. package/target/src/annotation/property/Override.js +0 -7
  133. package/target/src/annotation/property/Readonly.js +0 -16
  134. package/target/src/annotation/scan/ComponentInjection.js +0 -21
  135. package/target/src/annotation/scan/ComponentScan.js +0 -9
  136. package/target/src/annotation/scan/ComponentScanExclusion.js +0 -25
  137. package/target/src/annotation/scan/ComponentScanMust.js +0 -25
  138. package/target/src/annotation/scan/Hotter.js +0 -8
  139. package/target/src/annotation/stereotype/BeanName.js +0 -11
  140. package/target/src/annotation/stereotype/Component.js +0 -8
  141. package/target/src/annotation/stereotype/Configure.js +0 -14
  142. package/target/src/annotation/stereotype/Controller.js +0 -9
  143. package/target/src/annotation/stereotype/Injection.js +0 -12
  144. package/target/src/annotation/stereotype/Log.js +0 -22
  145. package/target/src/annotation/stereotype/Repository.js +0 -9
  146. package/target/src/annotation/stereotype/Service.js +0 -9
  147. package/target/src/annotation/valid/AddChildValid.js +0 -56
  148. package/target/src/annotation/valid/CustomType.js +0 -11
  149. package/target/src/annotation/valid/DefaultVal.js +0 -10
  150. package/target/src/annotation/valid/NotNull.js +0 -8
  151. package/target/src/annotation/valid/Rule.js +0 -100
  152. package/target/src/annotation/valid/Size.js +0 -10
  153. package/target/src/annotation/valid/Type.js +0 -10
  154. package/target/src/annotation/valid/ValidCustom.js +0 -17
  155. package/target/src/annotation/valid/ValidForm.js +0 -133
  156. package/target/src/annotation.js +0 -108
  157. package/target/src/config/ApplicationConfig.js +0 -2
  158. package/target/src/config/SysConfig.js +0 -19
  159. package/target/src/constant/AppStatusEnum.js +0 -9
  160. package/target/src/constant/BootPriority.js +0 -11
  161. package/target/src/constant/CommonConstant.js +0 -18
  162. package/target/src/constant/ComponentKind.js +0 -11
  163. package/target/src/constant/DataTypes.js +0 -19
  164. package/target/src/constant/FastCarMetaData.js +0 -31
  165. package/target/src/constant/LifeCycleModule.js +0 -9
  166. package/target/src/db.js +0 -49
  167. package/target/src/index.js +0 -21
  168. package/target/src/interface/ApplicationHook.js +0 -2
  169. package/target/src/interface/ApplicationInterface.js +0 -2
  170. package/target/src/interface/ApplicationRunnerService.js +0 -2
  171. package/target/src/interface/DataSourceManager.js +0 -2
  172. package/target/src/interface/Logger.js +0 -5
  173. package/target/src/model/BaseMapper.js +0 -103
  174. package/target/src/model/DataMap.js +0 -87
  175. package/target/src/model/FormRuleModel.js +0 -2
  176. package/target/src/model/ValidError.js +0 -5
  177. package/target/src/model/WinstonLogger.js +0 -97
  178. package/target/src/type/ComponentDesc.js +0 -2
  179. package/target/src/type/DesignMeta.js +0 -15
  180. package/target/src/type/FileHotterDesc.js +0 -2
  181. package/target/src/type/MapperType.js +0 -2
  182. package/target/src/type/ProcessType.js +0 -2
  183. package/target/src/type/SqlError.js +0 -5
  184. package/target/src/type/WinstonLoggerType.js +0 -9
  185. package/target/src/utils/ClassLoader.js +0 -65
  186. package/target/src/utils/ClassUtils.js +0 -35
  187. package/target/src/utils/CryptoUtil.js +0 -86
  188. package/target/src/utils/DataFormat.js +0 -88
  189. package/target/src/utils/DateUtil.js +0 -71
  190. package/target/src/utils/FileUtil.js +0 -153
  191. package/target/src/utils/FormatStr.js +0 -14
  192. package/target/src/utils/IPUtils.js +0 -34
  193. package/target/src/utils/Id.js +0 -9
  194. package/target/src/utils/Mix.js +0 -62
  195. package/target/src/utils/ReflectUtil.js +0 -22
  196. package/target/src/utils/TypeUtil.js +0 -53
  197. package/target/src/utils/ValidationUtil.js +0 -118
  198. package/target/src/utils.js +0 -25
  199. package/target/test/example/simple/app-test.js +0 -57
  200. package/target/test/example/simple/app.js +0 -116
  201. package/target/test/example/simple/component/StartRunner.js +0 -19
  202. package/target/test/example/simple/component/StopRunner.js +0 -23
  203. package/target/test/example/simple/config/EnvConfig.js +0 -16
  204. package/target/test/example/simple/config/HelloConfig.js +0 -16
  205. package/target/test/example/simple/config/HotConfig.js +0 -18
  206. package/target/test/example/simple/controller/AliasController.js +0 -17
  207. package/target/test/example/simple/controller/HelloController.js +0 -55
  208. package/target/test/example/simple/controller/NotFoundController.js +0 -36
  209. package/target/test/example/simple/noclude/excludeApp.js +0 -17
  210. package/target/test/example/simple/service/CallService.js +0 -24
  211. package/target/test/example/simple/service/HelloService.js +0 -18
  212. package/target/test/example/simple/service/LogService.js +0 -32
  213. package/target/test/multi/app.js +0 -29
  214. package/target/test/multi/service/aService.js +0 -33
  215. package/target/test/multi/service/bService.js +0 -35
  216. package/target/test/multi/service/cService.js +0 -40
  217. package/target/test/unit/dataMap-test.js +0 -35
  218. package/target/test/unit/decorators-test.js +0 -59
  219. package/target/test/unit/ds-test.js +0 -58
  220. package/target/test/unit/reflectMetadata-test.js +0 -27
  221. package/target/test/unit/valid-test.js +0 -99
  222. package/target/test/unit/winston-test.js +0 -15
  223. package/test/example/logs/fastcar-server.sys.log +0 -3
  224. package/test/example/logs/other-server.app.log +0 -0
  225. package/test/example/logs/other-server.sys.log +0 -0
  226. package/test/example/logs/other-server.sys1.log +0 -0
  227. package/test/example/logs/other-server.sys2.log +0 -0
  228. package/test/example/logs/other-server.sys3.log +0 -6
  229. package/test/example/logs/other-server.sys4.log +0 -0
  230. package/test/example/logs/other-server.sys5.log +0 -0
  231. package/test/example/logs/other-server.sys6.log +0 -2
  232. package/test/logs/logger.log +0 -0
  233. package/test/logs/sys.log +0 -227
@@ -1,704 +0,0 @@
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;