node-karin 0.12.0 → 0.12.2-5.pr.206.027f2f2

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 (254) hide show
  1. package/CHANGELOG.md +1029 -0
  2. package/default/comment/config.json +34 -0
  3. package/default/comment/friendDirect.json +10 -0
  4. package/default/comment/groupGuild.json +11 -0
  5. package/default/comment/server.json +17 -0
  6. package/default/config/adapter.json +28 -0
  7. package/default/config/config.json +47 -0
  8. package/default/config/groups.json +47 -0
  9. package/default/config/groups.yaml +70 -0
  10. package/default/config/pm2.json +16 -0
  11. package/default/config/privates.json +23 -0
  12. package/default/config/privates.yaml +31 -0
  13. package/default/config/redis.json +10 -0
  14. package/default/config/render.json +19 -0
  15. package/default/view/group.yaml +73 -0
  16. package/dist/index.d.ts +9869 -0
  17. package/dist/index.js +12643 -0
  18. package/dist/module/art-template.cjs +11 -0
  19. package/dist/module/art-template.d.ts +1 -0
  20. package/dist/module/axios.cjs +22 -0
  21. package/dist/module/axios.d.ts +2 -0
  22. package/dist/module/chalk.cjs +22 -0
  23. package/dist/module/chalk.d.ts +2 -0
  24. package/dist/module/chokidar.cjs +22 -0
  25. package/dist/module/chokidar.d.ts +2 -0
  26. package/dist/module/commander.cjs +35 -0
  27. package/dist/module/commander.d.ts +3 -0
  28. package/dist/module/commander.js +3 -0
  29. package/dist/module/dotenv.cjs +22 -0
  30. package/dist/module/dotenv.d.ts +2 -0
  31. package/dist/module/dotenv.js +2 -0
  32. package/dist/module/express.cjs +112 -0
  33. package/dist/module/express.d.ts +1 -0
  34. package/dist/module/express.js +1 -0
  35. package/dist/module/level.cjs +22 -0
  36. package/dist/module/level.d.ts +2 -0
  37. package/dist/module/lodash.cjs +11 -0
  38. package/dist/module/lodash.d.ts +1 -0
  39. package/dist/module/log4js.cjs +22 -0
  40. package/dist/module/log4js.d.ts +2 -0
  41. package/dist/module/moment.cjs +11 -0
  42. package/dist/module/moment.d.ts +1 -0
  43. package/dist/module/node-schedule.cjs +22 -0
  44. package/dist/module/node-schedule.d.ts +2 -0
  45. package/dist/module/redis.cjs +22 -0
  46. package/dist/module/redis.d.ts +2 -0
  47. package/dist/module/ws.cjs +28 -0
  48. package/dist/module/ws.d.ts +1 -0
  49. package/dist/module/ws.js +1 -0
  50. package/dist/module/yaml.cjs +22 -0
  51. package/dist/module/yaml.d.ts +2 -0
  52. package/dist/root.d.ts +40 -0
  53. package/dist/root.js +44 -0
  54. package/package.json +142 -128
  55. package/LICENSE +0 -674
  56. package/README.md +0 -56
  57. package/config/defSet/App.yaml +0 -42
  58. package/config/defSet/config.yaml +0 -65
  59. package/config/defSet/group.yaml +0 -56
  60. package/config/defSet/pm2.yaml +0 -21
  61. package/config/defSet/redis.yaml +0 -18
  62. package/config/defSet/server.yaml +0 -42
  63. package/config/view/group.yaml +0 -92
  64. package/lib/adapter/index.d.ts +0 -2
  65. package/lib/adapter/index.js +0 -2
  66. package/lib/adapter/input/index.d.ts +0 -78
  67. package/lib/adapter/input/index.js +0 -205
  68. package/lib/adapter/onebot/11/convert.d.ts +0 -12
  69. package/lib/adapter/onebot/11/convert.js +0 -167
  70. package/lib/adapter/onebot/11/event.d.ts +0 -25
  71. package/lib/adapter/onebot/11/event.js +0 -442
  72. package/lib/adapter/onebot/11/index.d.ts +0 -508
  73. package/lib/adapter/onebot/11/index.js +0 -1047
  74. package/lib/cli/index.d.ts +0 -92
  75. package/lib/cli/index.js +0 -307
  76. package/lib/cli/init.d.ts +0 -2
  77. package/lib/cli/init.js +0 -22
  78. package/lib/cli/karin.d.ts +0 -2
  79. package/lib/cli/karin.js +0 -16
  80. package/lib/cli/pkg.d.ts +0 -4
  81. package/lib/cli/pkg.js +0 -14
  82. package/lib/cli/start.d.ts +0 -1
  83. package/lib/cli/start.js +0 -8
  84. package/lib/core/index.d.ts +0 -10
  85. package/lib/core/index.js +0 -10
  86. package/lib/core/init/config.d.ts +0 -43
  87. package/lib/core/init/config.js +0 -201
  88. package/lib/core/init/dir.d.ts +0 -4
  89. package/lib/core/init/dir.js +0 -7
  90. package/lib/core/init/init.d.ts +0 -1
  91. package/lib/core/init/init.js +0 -47
  92. package/lib/core/karin/karin.d.ts +0 -198
  93. package/lib/core/karin/karin.js +0 -226
  94. package/lib/core/listener/listener.d.ts +0 -145
  95. package/lib/core/listener/listener.js +0 -235
  96. package/lib/core/plugin/base.d.ts +0 -156
  97. package/lib/core/plugin/base.js +0 -131
  98. package/lib/core/plugin/loader.d.ts +0 -141
  99. package/lib/core/plugin/loader.js +0 -618
  100. package/lib/core/process/process.d.ts +0 -18
  101. package/lib/core/process/process.js +0 -96
  102. package/lib/core/server/server.d.ts +0 -25
  103. package/lib/core/server/server.js +0 -295
  104. package/lib/db/index.d.ts +0 -2
  105. package/lib/db/index.js +0 -2
  106. package/lib/db/level/level.d.ts +0 -19
  107. package/lib/db/level/level.js +0 -36
  108. package/lib/db/redis/redis.d.ts +0 -2
  109. package/lib/db/redis/redis.js +0 -135
  110. package/lib/db/redis/redis_level.d.ts +0 -112
  111. package/lib/db/redis/redis_level.js +0 -281
  112. package/lib/event/handler/base.d.ts +0 -42
  113. package/lib/event/handler/base.js +0 -200
  114. package/lib/event/handler/message.d.ts +0 -29
  115. package/lib/event/handler/message.js +0 -307
  116. package/lib/event/handler/notice.d.ts +0 -18
  117. package/lib/event/handler/notice.js +0 -214
  118. package/lib/event/handler/request.d.ts +0 -18
  119. package/lib/event/handler/request.js +0 -115
  120. package/lib/event/handler/review.d.ts +0 -55
  121. package/lib/event/handler/review.js +0 -386
  122. package/lib/event/index.d.ts +0 -5
  123. package/lib/event/index.js +0 -5
  124. package/lib/index.d.ts +0 -8
  125. package/lib/index.js +0 -8
  126. package/lib/modules/art-template.d.ts +0 -1
  127. package/lib/modules/axios.d.ts +0 -2
  128. package/lib/modules/chalk.d.ts +0 -2
  129. package/lib/modules/chokidar.d.ts +0 -2
  130. package/lib/modules/commander.d.ts +0 -2
  131. package/lib/modules/commander.js +0 -2
  132. package/lib/modules/express.d.ts +0 -3
  133. package/lib/modules/express.js +0 -3
  134. package/lib/modules/level.d.ts +0 -2
  135. package/lib/modules/lodash.d.ts +0 -1
  136. package/lib/modules/log4js.d.ts +0 -2
  137. package/lib/modules/moment.d.ts +0 -1
  138. package/lib/modules/node-schedule.d.ts +0 -2
  139. package/lib/modules/redis.d.ts +0 -2
  140. package/lib/modules/ws.d.ts +0 -3
  141. package/lib/modules/ws.js +0 -3
  142. package/lib/modules/yaml.d.ts +0 -2
  143. package/lib/modules.d.ts +0 -15
  144. package/lib/modules.js +0 -15
  145. package/lib/render/app.d.ts +0 -56
  146. package/lib/render/app.js +0 -120
  147. package/lib/render/base.d.ts +0 -30
  148. package/lib/render/base.js +0 -69
  149. package/lib/render/client.d.ts +0 -41
  150. package/lib/render/client.js +0 -238
  151. package/lib/render/http.d.ts +0 -19
  152. package/lib/render/http.js +0 -50
  153. package/lib/render/index.d.ts +0 -6
  154. package/lib/render/index.js +0 -6
  155. package/lib/render/server.d.ts +0 -25
  156. package/lib/render/server.js +0 -101
  157. package/lib/render/wormhole.d.ts +0 -1
  158. package/lib/render/wormhole.js +0 -156
  159. package/lib/types/adapter/api.d.ts +0 -399
  160. package/lib/types/adapter/api.js +0 -1
  161. package/lib/types/adapter/base.d.ts +0 -560
  162. package/lib/types/adapter/base.js +0 -1
  163. package/lib/types/config/config.d.ts +0 -379
  164. package/lib/types/config/config.js +0 -1
  165. package/lib/types/element/element.d.ts +0 -565
  166. package/lib/types/element/element.js +0 -1
  167. package/lib/types/element/qqbot.d.ts +0 -123
  168. package/lib/types/element/qqbot.js +0 -1
  169. package/lib/types/event/contact.d.ts +0 -28
  170. package/lib/types/event/contact.js +0 -1
  171. package/lib/types/event/event.d.ts +0 -305
  172. package/lib/types/event/event.js +0 -1
  173. package/lib/types/event/index.d.ts +0 -12
  174. package/lib/types/event/index.js +0 -7
  175. package/lib/types/event/message.d.ts +0 -109
  176. package/lib/types/event/message.js +0 -74
  177. package/lib/types/event/notice.d.ts +0 -513
  178. package/lib/types/event/notice.js +0 -52
  179. package/lib/types/event/reply.d.ts +0 -50
  180. package/lib/types/event/reply.js +0 -1
  181. package/lib/types/event/request.d.ts +0 -126
  182. package/lib/types/event/request.js +0 -52
  183. package/lib/types/event/sender.d.ts +0 -44
  184. package/lib/types/event/sender.js +0 -1
  185. package/lib/types/index.d.ts +0 -13
  186. package/lib/types/index.js +0 -13
  187. package/lib/types/logger/logger.d.ts +0 -110
  188. package/lib/types/logger/logger.js +0 -1
  189. package/lib/types/onebot11/api.d.ts +0 -53
  190. package/lib/types/onebot11/api.js +0 -1
  191. package/lib/types/onebot11/event.d.ts +0 -619
  192. package/lib/types/onebot11/event.js +0 -2
  193. package/lib/types/onebot11/index.d.ts +0 -6
  194. package/lib/types/onebot11/index.js +0 -6
  195. package/lib/types/onebot11/params.d.ts +0 -609
  196. package/lib/types/onebot11/params.js +0 -1
  197. package/lib/types/onebot11/response.d.ts +0 -516
  198. package/lib/types/onebot11/response.js +0 -1
  199. package/lib/types/onebot11/segment.d.ts +0 -269
  200. package/lib/types/onebot11/segment.js +0 -1
  201. package/lib/types/onebot11/sender.d.ts +0 -59
  202. package/lib/types/onebot11/sender.js +0 -1
  203. package/lib/types/plugin/app.d.ts +0 -71
  204. package/lib/types/plugin/app.js +0 -1
  205. package/lib/types/plugin/plugin.d.ts +0 -473
  206. package/lib/types/plugin/plugin.js +0 -1
  207. package/lib/types/render/render.d.ts +0 -118
  208. package/lib/types/render/render.js +0 -1
  209. package/lib/types/type/global.d.ts +0 -25
  210. package/lib/types/type/global.js +0 -1
  211. package/lib/utils/common/common.d.ts +0 -236
  212. package/lib/utils/common/common.js +0 -708
  213. package/lib/utils/config/config.d.ts +0 -193
  214. package/lib/utils/config/config.js +0 -328
  215. package/lib/utils/config/updateVersion.d.ts +0 -33
  216. package/lib/utils/config/updateVersion.js +0 -145
  217. package/lib/utils/config/yamlEditor.d.ts +0 -101
  218. package/lib/utils/config/yamlEditor.js +0 -330
  219. package/lib/utils/core/handler.d.ts +0 -18
  220. package/lib/utils/core/handler.js +0 -46
  221. package/lib/utils/core/logger.d.ts +0 -3
  222. package/lib/utils/core/logger.js +0 -105
  223. package/lib/utils/core/segment.d.ts +0 -279
  224. package/lib/utils/core/segment.js +0 -451
  225. package/lib/utils/index.d.ts +0 -13
  226. package/lib/utils/index.js +0 -13
  227. package/lib/utils/tools/button.d.ts +0 -2
  228. package/lib/utils/tools/button.js +0 -27
  229. package/lib/utils/tools/exec.d.ts +0 -20
  230. package/lib/utils/tools/exec.js +0 -54
  231. package/lib/utils/tools/ffmpeg.d.ts +0 -12
  232. package/lib/utils/tools/ffmpeg.js +0 -25
  233. package/lib/utils/tools/restart.d.ts +0 -15
  234. package/lib/utils/tools/restart.js +0 -39
  235. package/lib/utils/tools/stop.d.ts +0 -7
  236. package/lib/utils/tools/stop.js +0 -13
  237. package/lib/utils/tools/update.d.ts +0 -67
  238. package/lib/utils/tools/update.js +0 -151
  239. /package/{config → default}/view/App.yaml +0 -0
  240. /package/{config → default}/view/config.yaml +0 -0
  241. /package/{config → default}/view/pm2.yaml +0 -0
  242. /package/{config → default}/view/redis.yaml +0 -0
  243. /package/{config → default}/view/server.yaml +0 -0
  244. /package/{lib/modules → dist/module}/art-template.js +0 -0
  245. /package/{lib/modules → dist/module}/axios.js +0 -0
  246. /package/{lib/modules → dist/module}/chalk.js +0 -0
  247. /package/{lib/modules → dist/module}/chokidar.js +0 -0
  248. /package/{lib/modules → dist/module}/level.js +0 -0
  249. /package/{lib/modules → dist/module}/lodash.js +0 -0
  250. /package/{lib/modules → dist/module}/log4js.js +0 -0
  251. /package/{lib/modules → dist/module}/moment.js +0 -0
  252. /package/{lib/modules → dist/module}/node-schedule.js +0 -0
  253. /package/{lib/modules → dist/module}/redis.js +0 -0
  254. /package/{lib/modules → dist/module}/yaml.js +0 -0
@@ -1,618 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import lodash from 'lodash';
4
- import chokidar from 'chokidar';
5
- import schedule from 'node-schedule';
6
- import { render } from '../../render/index.js';
7
- import { karin } from '../karin/karin.js';
8
- import { common, logger } from '../../utils/index.js';
9
- class PluginLoader {
10
- dir;
11
- /**
12
- * - 插件索引ID
13
- */
14
- index;
15
- /**
16
- * - 定时任务
17
- */
18
- // task: Array<PluginTask & { App: NewMessagePlugin, schedule?: schedule.Job, file: { dir: dirName, name: fileName } }>
19
- /**
20
- * - 监听器
21
- */
22
- watcher;
23
- /**
24
- * - 热更新列表
25
- */
26
- watchList;
27
- /**
28
- * - 依赖缺失收集
29
- */
30
- dependErr;
31
- /** accept */
32
- accept;
33
- /** button */
34
- button;
35
- /** command规则集信息 */
36
- command;
37
- /** handler */
38
- handler;
39
- /** plugin基本信息 */
40
- plugin;
41
- /** task定时任务信息 */
42
- task;
43
- /** 中间件 */
44
- use;
45
- /** 加载的文件数组 .js .ts */
46
- ext;
47
- constructor() {
48
- this.accept = [];
49
- this.button = [];
50
- this.command = [];
51
- this.handler = {};
52
- this.plugin = new Map();
53
- this.task = [];
54
- this.use = {
55
- recvMsg: [],
56
- replyMsg: [],
57
- sendMsg: [],
58
- };
59
- this.ext = process.env.karin_app_lang === 'ts' ? ['.js', '.ts'] : ['.js'];
60
- this.index = 0;
61
- this.dir = './plugins';
62
- this.watcher = new Map();
63
- this.watchList = [];
64
- this.dependErr = {};
65
- }
66
- /**
67
- * 插件初始化
68
- */
69
- async load() {
70
- karin.once('plugin.watch', () => {
71
- this.watchList.forEach(async ({ plugin, path }) => {
72
- await this.watchDir(plugin, path);
73
- logger.debug(`[热更新][${plugin}][${path}] 监听中...`);
74
- });
75
- this.watchList = [];
76
- });
77
- logger.info(logger.green('-----------'));
78
- logger.info('加载插件中...');
79
- const list = [];
80
- list.push(...await this.getGitPlugin());
81
- list.push(...await this.getNpmPlugin());
82
- await Promise.all(list);
83
- /** 打印依赖缺失 */
84
- this.printDependErr();
85
- /** 优先级排序并打印插件信息 */
86
- this.orderBy(true);
87
- karin.emit('plugin.watch');
88
- return this;
89
- }
90
- /**
91
- * 获取Git插件
92
- */
93
- async getGitPlugin() {
94
- /** 获取所有插件包 */
95
- const plugins = common.getPlugins();
96
- const list = [];
97
- for (const dir of plugins) {
98
- try {
99
- /** 插件包路径 例如: ./plugins/karin-plugin-example */
100
- const root = `${this.dir}/${dir}`;
101
- /** 非插件包 加载该文件夹下全部js 视语言环境加载ts */
102
- if (!common.isPlugin(root)) {
103
- /** 热更新 */
104
- this.watchList.push({ plugin: dir, path: '' });
105
- list.push(...this.loadApps(dir, '', `${"js" /* AppsType.Js */}`));
106
- continue;
107
- }
108
- /** package */
109
- const pkg = common.readJson(`${root}/package.json`, true);
110
- this.loadMain(root, pkg.main, false);
111
- /**
112
- * 插件加载优先级:
113
- * 1. ts环境: 按照 编译产物 > TS源代码 举例: 优先级高的不存在才会加载下一个。
114
- * 2. js环境: 按照 pkg.apps 进行加载。 当前版本兼容apps,apps处于根目录并且没有在pkg.apps中填写会自动加载,后续废弃。
115
- *
116
- * 注: ts编译产物的路径也是根据pkg.apps进行加载的。
117
- *
118
- * 开发环境说明:
119
- * 1. ts环境默认为开发模式
120
- * 2. 在开发环境下 会对所有 `.js`,`.ts` 文件进行热更新 只需要点下保存即可。
121
- * 3. 监察者模式当前暂未适配 待后续适配。
122
- */
123
- /** ts环境 */
124
- if (process.env.karin_app_lang === 'ts') {
125
- /** 加载ts入口 */
126
- if (pkg?.karin?.main)
127
- this.loadMain(root, pkg.karin.main, false);
128
- /** 获取apps */
129
- const apps = pkg?.karin?.apps && Array.isArray(pkg.karin.apps) ? pkg.karin.apps : [];
130
- /** apps为空则跳过 */
131
- if (!apps.length)
132
- continue;
133
- const { outDir, rootDir } = await this.getTsMain(root, pkg);
134
- /** 先瞅瞅编译产物根目录是否存在 */
135
- if (common.exists(path.join(root, outDir))) {
136
- for (const file of apps) {
137
- const rootApps = path.join(root, outDir, file);
138
- if (!common.exists(rootApps)) {
139
- logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
140
- continue;
141
- }
142
- const _path = path.join(outDir, file);
143
- /** 热更新 */
144
- this.watchList.push({ plugin: dir, path: _path });
145
- list.push(...this.loadApps(dir, _path, `${"git" /* AppsType.Git */}`));
146
- }
147
- continue;
148
- }
149
- /** 源码根目录 */
150
- if (common.exists(path.join(root, rootDir))) {
151
- for (const file of apps) {
152
- const rootApps = path.join(root, rootDir, file);
153
- if (!common.exists(rootApps)) {
154
- logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
155
- continue;
156
- }
157
- const _path = path.join(rootDir, file);
158
- /** 热更新 */
159
- this.watchList.push({ plugin: dir, path: _path });
160
- list.push(...this.loadApps(dir, _path, `${"git" /* AppsType.Git */}`));
161
- }
162
- continue;
163
- }
164
- /** 走到这说明有问题 打印错误信息 */
165
- logger.error(`[插件收集][${dir}] 加载错误,未检测到任何文件,请检查配置是否正确`);
166
- continue;
167
- }
168
- /** 全部apps路径 */
169
- const apps = pkg?.karin?.apps && Array.isArray(pkg.karin.apps) ? pkg.karin.apps : [];
170
- /** 暂时兼容旧版本 加入apps 计划在正式版本发布之前废弃 */
171
- if (!apps.includes('apps'))
172
- apps.push('apps');
173
- for (const file of apps) {
174
- const rootApps = path.join(root, file);
175
- if (!common.exists(rootApps)) {
176
- logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
177
- continue;
178
- }
179
- const dev = process.env.karin_app_mode === 'dev';
180
- if (dev) {
181
- /** 热更新 */
182
- this.watchList.push({ plugin: dir, path: file });
183
- }
184
- list.push(...this.loadApps(dir, file, `${"git" /* AppsType.Git */}`));
185
- }
186
- }
187
- catch (error) {
188
- logger.error(`[插件收集][${dir}] 加载错误`);
189
- logger.error(error);
190
- }
191
- }
192
- return list;
193
- }
194
- /**
195
- * 获取Npm插件
196
- */
197
- async getNpmPlugin() {
198
- const list = [];
199
- const info = await common.getNpmPlugins(true);
200
- for (const { plugin, path: _path, file, isMain } of info) {
201
- if (isMain) {
202
- const root = path.join(process.cwd(), 'node_modules', plugin, _path);
203
- this.loadMain(root, file, true);
204
- continue;
205
- }
206
- list.push(this.createdApp(plugin, _path, file, `${"npm" /* AppsType.Npm */}`));
207
- }
208
- return list;
209
- }
210
- /**
211
- * 加载入口文件 只加载
212
- * @param root - 插件根目录
213
- * @param main - 入口文件
214
- * @param isNpm - 是否为npm插件
215
- */
216
- async loadMain(root, main, isNpm) {
217
- const _path = path.resolve(root, main || '');
218
- if (!common.exists(_path) || !main) {
219
- if (isNpm)
220
- return false;
221
- const defRoot = path.resolve(root, 'index.js');
222
- if (common.exists(defRoot)) {
223
- await import(`file://${defRoot}`);
224
- return true;
225
- }
226
- logger.debug(`[插件收集][${main}] 入口文件不存在,已忽略`);
227
- return false;
228
- }
229
- await import(`file://${_path}`);
230
- return true;
231
- }
232
- /**
233
- * 获取ts插件入口 编译入口路径
234
- * @param root - 插件根目录
235
- * @param pkg - package.json
236
- */
237
- async getTsMain(root, pkg) {
238
- let outDir = 'dist';
239
- let rootDir = 'src';
240
- /** 先尝试解析tsconfig.json来进行读取 */
241
- try {
242
- const tsconfig = common.readJson(`${root}/tsconfig.json`);
243
- if (tsconfig?.compilerOptions?.outDir)
244
- outDir = tsconfig.compilerOptions.outDir;
245
- if (tsconfig?.compilerOptions?.rootDir)
246
- rootDir = tsconfig.compilerOptions.rootDir;
247
- }
248
- catch (error) {
249
- logger.error(`[插件收集][${root}] tsconfig.json解析错误,请检查格式`);
250
- logger.error(error);
251
- /** 如果报错 则读取pkg的 */
252
- if (pkg?.karin?.outDir)
253
- outDir = pkg.karin.outDir;
254
- if (pkg?.karin?.rootDir)
255
- rootDir = pkg.karin.rootDir;
256
- }
257
- return { outDir, rootDir };
258
- }
259
- /**
260
- * 加载指定文件夹下的所有插件
261
- * @param plugin - 插件名称
262
- * @param _path - 插件apps相对路径
263
- * @param file - 插件文件名称
264
- * @param isNpm - 是否为npm插件
265
- * @param isWatch - 是否监听热更新
266
- */
267
- loadApps(plugin, _path, type) {
268
- const list = [];
269
- const root = type === 'npm' ? path.join(process.cwd(), 'node_modules', plugin, _path) : path.join(this.dir, plugin, _path);
270
- const files = fs.readdirSync(root, { withFileTypes: true });
271
- for (const file of files) {
272
- const extname = path.extname(file.name);
273
- if (!this.ext.includes(extname))
274
- continue;
275
- list.push(this.createdApp(plugin, _path, file.name, type));
276
- }
277
- return list;
278
- }
279
- /**
280
- * 排序并打印插件信息
281
- * @param isPrint - 是否打印
282
- */
283
- orderBy(isPrint = false) {
284
- let handlerCount = 0;
285
- this.accept = lodash.orderBy(this.accept, ['rank'], ['asc']);
286
- this.button = lodash.orderBy(this.button, ['rank'], ['asc']);
287
- this.command = lodash.orderBy(this.command, ['rank'], ['asc']);
288
- this.task = lodash.orderBy(this.task, ['rank'], ['asc']);
289
- this.use.recvMsg = lodash.orderBy(this.use.recvMsg, ['rank'], ['asc']);
290
- this.use.replyMsg = lodash.orderBy(this.use.replyMsg, ['rank'], ['asc']);
291
- this.use.sendMsg = lodash.orderBy(this.use.sendMsg, ['rank'], ['asc']);
292
- const handler = Object.keys(this.handler);
293
- handler.forEach(key => {
294
- this.handler[key] = lodash.orderBy(this.handler[key], ['rank'], ['asc']);
295
- handlerCount += this.handler[key].length;
296
- });
297
- if (!isPrint)
298
- return;
299
- logger.info(`[插件][${this.plugin.size}个] 加载完成`);
300
- logger.info(`[渲染器][${render.Apps.length}个] 加载完成`);
301
- logger.info(`[command][${this.command.length}个] 加载完成`);
302
- logger.info(`[button][${this.button.length}个] 加载完成`);
303
- logger.info(`[accept][${this.accept.length}个] 加载完成`);
304
- logger.info(`[定时任务][${this.task.length}个] 加载完成`);
305
- logger.info(`[Handler][Key:${handler.length}个][fnc:${handlerCount}个] 加载完成`);
306
- logger.info(logger.green('-----------'));
307
- logger.info(`Karin启动完成:耗时 ${logger.green(process.uptime().toFixed(2))} 秒...`);
308
- }
309
- /**
310
- * 新增插件
311
- * @param plugin - 插件名称 例如: karin-plugin-example
312
- * @param _path - 插件路径 例如: lib/apps 不存在则传空字符串
313
- * @param file - 插件文件 例如: index.js ts目前解释器支持不够,暂不支持ts
314
- * @param type - 插件类型 默认为git
315
- * @param isOrderBy - 是否排序 开启会使用动态导入
316
- */
317
- async createdApp(plugin, _path, file, type = 'git', isOrderBy = false) {
318
- try {
319
- const index = ++this.index;
320
- /** 缓存基本信息 */
321
- this.plugin.set(index, { type, plugin, path: _path, file });
322
- const list = [];
323
- let rootPath = 'file://' + path.join(process.cwd(), type === 'npm' ? 'node_modules' : 'plugins', plugin, _path, file);
324
- if (isOrderBy)
325
- rootPath = rootPath + `?${Date.now()}`;
326
- const tmp = await import(rootPath);
327
- lodash.forEach(tmp, (Fn) => {
328
- /** 函数语法糖 */
329
- if (typeof Fn === 'object' && Fn?.type) {
330
- logger.debug(`载入插件 [${plugin}]${_path ? `${common.getRelPath(_path)}` : ''}[${file}][${Fn.name}]`);
331
- list.push(this.cachePlugin(index, plugin, file, Fn));
332
- return true;
333
- }
334
- if (typeof Fn !== 'function' || !Fn?.prototype?.constructor)
335
- return false;
336
- const Class = new Fn();
337
- if (!Class.name) {
338
- logger.error(`[${plugin}]${_path ? `${common.getRelPath(_path)}` : ''}[${file}] 插件名称错误`);
339
- return false;
340
- }
341
- logger.debug(`载入插件 [${plugin}]${_path ? `${common.getRelPath(_path)}` : ''}[${file}][${Class.name}]`);
342
- if (Class.rule.length) {
343
- for (const val of Class.rule) {
344
- const fnc = async () => {
345
- const log = val.log === false
346
- ? (id, log) => logger.debug('mark', id, log)
347
- : (id, log) => logger.bot('mark', id, log);
348
- const fn = typeof val.fnc === 'function' ? val.fnc : Class[val.fnc];
349
- return this.cachePlugin(index, plugin, file, {
350
- name: Class.name,
351
- type: 'command',
352
- fn,
353
- fnname: typeof val.fnc === 'function' ? val.fnc.name || 'fnc' : val.fnc,
354
- data: Fn,
355
- event: val.event || Class.event || 'message',
356
- log,
357
- perm: val.permission || "all" /* Permission.All */,
358
- rank: val.priority || Class.priority || 10000,
359
- reg: val.reg instanceof RegExp ? val.reg : new RegExp(val.reg),
360
- }, Fn);
361
- };
362
- list.push(fnc());
363
- }
364
- }
365
- if (Class.task.length) {
366
- for (const val of Class.task) {
367
- const fnc = async () => {
368
- if (!val.name)
369
- throw TypeError(`[${plugin}][${file}] 定时任务name错误`);
370
- if (!val.cron)
371
- throw TypeError(`[${plugin}][${file}] 定时任务cron错误:${Class.name}`);
372
- const log = val.log === false ? (log) => logger.debug(log) : (log) => logger.mark(log);
373
- return this.cachePlugin(index, plugin, file, {
374
- name: Class.name,
375
- fnname: val.name,
376
- type: 'task',
377
- fn: typeof val.fnc === 'function' ? val.fnc : Class[val.fnc],
378
- log,
379
- cron: val.cron,
380
- }, Fn);
381
- };
382
- list.push(fnc());
383
- }
384
- }
385
- /** init */
386
- list.push('init' in Class && typeof Class.init === 'function' ? Class.init() : Promise.resolve());
387
- return true;
388
- });
389
- await Promise.all(list);
390
- /** 建立对应的规则索引并排序 */
391
- if (isOrderBy)
392
- this.orderBy();
393
- return true;
394
- }
395
- catch (error) {
396
- if (/Cannot find package '(.+?)'/.exec(error)?.[1]) {
397
- const key = `${plugin}.${_path}.${file}`;
398
- if (this.dependErr[key])
399
- return false;
400
- this.dependErr[key] = {
401
- plugin,
402
- path: _path,
403
- file,
404
- depend: /Cannot find package '(.+?)'/.exec(error)?.[1] || '',
405
- };
406
- }
407
- else {
408
- logger.error(`载入插件错误:${logger.red(`${plugin}/${_path}/${file}`)}`);
409
- logger.error(error);
410
- }
411
- return false;
412
- }
413
- }
414
- /**
415
- * 缓存插件
416
- * @param index - 插件索引
417
- */
418
- async cachePlugin(index, plugin, file, info, App) {
419
- if (!info?.name) {
420
- logger.error(`[${plugin}][${file}][${info.name}] 插件名称错误`);
421
- return false;
422
- }
423
- switch (info.type) {
424
- case 'accept': {
425
- this.accept.push({
426
- name: info.name,
427
- event: info.event,
428
- fn: info.fn,
429
- key: index,
430
- log: info.log,
431
- rank: info.rank,
432
- });
433
- return true;
434
- }
435
- case 'button': {
436
- this.button.push({
437
- name: info.name,
438
- reg: info.reg,
439
- fn: info.fn,
440
- key: index,
441
- rank: info.rank,
442
- });
443
- return true;
444
- }
445
- case 'handler': {
446
- if (!this.handler[info.key])
447
- this.handler[info.key] = [];
448
- this.handler[info.key].push({
449
- name: info.name,
450
- fn: info.fn,
451
- key: index,
452
- rank: info.rank,
453
- });
454
- return true;
455
- }
456
- case 'command': {
457
- this.command.push({
458
- name: info.name,
459
- data: App,
460
- event: info.event,
461
- fn: info.fn,
462
- fnname: info.fnname,
463
- key: index,
464
- log: info.log,
465
- perm: info.perm,
466
- rank: info.rank,
467
- reg: info.reg,
468
- type: App ? 'class' : 'function',
469
- });
470
- return true;
471
- }
472
- case 'task': {
473
- this.task.push({
474
- name: info.name,
475
- cron: info.cron,
476
- fn: info.fn,
477
- key: index,
478
- taskname: info.fnname,
479
- data: App,
480
- log: info.log,
481
- schedule: schedule.scheduleJob(info.cron, async () => {
482
- try {
483
- info.log(`[定时任务][${plugin}][${info.fnname}] 开始执行`);
484
- if (App) {
485
- const app = new App();
486
- await info.fn.call(app);
487
- }
488
- else {
489
- await info.fn();
490
- }
491
- info.log(`[定时任务][${plugin}][${info.fnname}] 执行完毕`);
492
- }
493
- catch (error) {
494
- logger.error(`[定时任务][${plugin}][${info.fnname}] 执行报错`);
495
- logger.error(error);
496
- }
497
- }),
498
- });
499
- return true;
500
- }
501
- case 'use': {
502
- this.use[info.key].push({
503
- name: info.name,
504
- fn: info.fn,
505
- key: index,
506
- rank: info.rank,
507
- });
508
- return true;
509
- }
510
- }
511
- }
512
- /**
513
- * 打印依赖缺失
514
- */
515
- printDependErr() {
516
- try {
517
- const keys = Object.keys(this.dependErr);
518
- if (!keys.length)
519
- return;
520
- const msg = ['-----依赖缺失----'];
521
- keys.forEach(key => {
522
- const { plugin, path, file, depend } = this.dependErr[key];
523
- msg.push(`[${plugin}]${path ? `[${path}]` : ''}[${file}] 缺少依赖:${logger.red(depend)}`);
524
- });
525
- msg.push('-------------------');
526
- logger.error(msg.join('\n'));
527
- }
528
- finally {
529
- /** 回收缓存 */
530
- this.dependErr = {};
531
- }
532
- }
533
- /**
534
- * 卸载插件
535
- */
536
- uninstallApp(plugin, _path, file) {
537
- this.plugin.forEach((info, key) => {
538
- if (info.plugin === plugin && info.path === _path && info.file === file) {
539
- /** 删除缓存 */
540
- this.plugin.delete(key);
541
- this.accept = this.accept.filter(val => val.key !== key);
542
- this.button = this.button.filter(val => val.key !== key);
543
- this.command = this.command.filter(val => val.key !== key);
544
- this.use.recvMsg = this.use.recvMsg.filter(val => val.key !== key);
545
- this.use.replyMsg = this.use.replyMsg.filter(val => val.key !== key);
546
- this.use.sendMsg = this.use.sendMsg.filter(val => val.key !== key);
547
- /** 定时任务需要先停止 */
548
- this.task = this.task.filter(val => {
549
- if (val.key === key) {
550
- val.schedule?.cancel();
551
- return false;
552
- }
553
- return true;
554
- });
555
- /** 处理handler */
556
- Object.keys(this.handler).forEach(keys => {
557
- this.handler[keys].forEach((val, i) => {
558
- if (val.key === key) {
559
- this.handler[keys].splice(i, 1);
560
- if (!this.handler[keys].length)
561
- delete this.handler[keys];
562
- return true;
563
- }
564
- });
565
- });
566
- }
567
- });
568
- /** 重新排序 */
569
- this.orderBy();
570
- }
571
- /**
572
- * 监听文件夹更新
573
- */
574
- async watchDir(plugin, _path) {
575
- const root = path.join(this.dir, plugin, _path);
576
- if (this.watcher.get(root))
577
- return false;
578
- const watcher = chokidar.watch(root);
579
- await common.sleep(1000);
580
- watcher.on('add', async (files) => {
581
- /** 排除掉不符合规则文件新增 */
582
- if (!this.ext.some(ext => files.endsWith(ext)))
583
- return false;
584
- const name = path.basename(files);
585
- await this.createdApp(plugin, _path, name, `${"git" /* AppsType.Git */}`, true);
586
- logger.mark(`[新增插件][${plugin}]${path ? `${common.getRelPath(_path)}` : ''}[${name}]`);
587
- return true;
588
- });
589
- watcher.on('change', async (files) => {
590
- /** 排除掉不符合规则文件新增 */
591
- if (!this.ext.some(ext => files.endsWith(ext)))
592
- return false;
593
- const name = path.basename(files);
594
- /** 卸载 */
595
- this.uninstallApp(plugin, _path, name);
596
- /** 载入插件 */
597
- await this.createdApp(plugin, _path, name, `${"git" /* AppsType.Git */}`, true);
598
- logger.mark(`[修改插件][${plugin}]${path ? `${common.getRelPath(_path)}` : ''}[${name}]`);
599
- return true;
600
- });
601
- watcher.on('unlink', async (files) => {
602
- /** 排除掉不符合规则文件新增 */
603
- if (!this.ext.some(ext => files.endsWith(ext)))
604
- return false;
605
- const name = path.basename(files);
606
- /** 卸载 */
607
- this.uninstallApp(plugin, _path, name);
608
- logger.mark(`[卸载插件][${plugin}]${path ? `${common.getRelPath(_path)}` : ''}[${name}]`);
609
- return true;
610
- });
611
- this.watcher.set(root, watcher);
612
- return true;
613
- }
614
- }
615
- /**
616
- * 加载插件
617
- */
618
- export const pluginLoader = new PluginLoader();
@@ -1,18 +0,0 @@
1
- /**
2
- * 处理基本事件
3
- */
4
- export default class Process {
5
- /**
6
- * 进程初始化
7
- */
8
- static process(): Promise<Process>;
9
- /**
10
- * 检查后台进程
11
- */
12
- static check(): Promise<Process>;
13
- /**
14
- * 退出Karin
15
- * @param code 退出码
16
- */
17
- static exit(code?: any): Promise<void>;
18
- }