node-karin 0.9.0 → 0.10.1
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.
- package/config/defSet/group.yaml +40 -2
- package/lib/adapter/onebot/11/index.d.ts +2 -0
- package/lib/adapter/onebot/11/index.js +3 -1
- package/lib/cli/init.js +1 -1
- package/lib/cli/karin.js +1 -1
- package/lib/core/index.d.ts +9 -9
- package/lib/core/index.js +9 -9
- package/lib/core/init/dir.js +7 -0
- package/lib/core/init/init.js +46 -0
- package/lib/core/{karin.d.ts → karin/karin.d.ts} +1 -1
- package/lib/core/karin/karin.js +194 -0
- package/lib/core/{listener.d.ts → listener/listener.d.ts} +1 -1
- package/lib/core/listener/listener.js +213 -0
- package/lib/core/{plugin.app.d.ts → plugin/app.d.ts} +1 -1
- package/lib/core/plugin/app.js +19 -0
- package/lib/core/{plugin.d.ts → plugin/base.d.ts} +1 -1
- package/lib/core/plugin/base.js +140 -0
- package/lib/core/{plugin.loader.d.ts → plugin/loader.d.ts} +3 -3
- package/lib/core/plugin/loader.js +579 -0
- package/lib/core/process/process.js +100 -0
- package/lib/core/server/server.js +283 -0
- package/lib/db/index.d.ts +2 -2
- package/lib/db/index.js +2 -2
- package/lib/db/level/level.js +36 -0
- package/lib/db/redis/redis.js +135 -0
- package/lib/db/redis/redis_level.js +287 -0
- package/lib/event/{event.handler.d.ts → handler/base.d.ts} +2 -2
- package/lib/event/handler/base.js +173 -0
- package/lib/event/{message.handler.d.ts → handler/message.d.ts} +3 -3
- package/lib/event/handler/message.js +270 -0
- package/lib/event/{notice.handler.d.ts → handler/notice.d.ts} +3 -3
- package/lib/event/handler/notice.js +212 -0
- package/lib/event/{request.handler.d.ts → handler/request.d.ts} +3 -3
- package/lib/event/handler/request.js +118 -0
- package/lib/event/{review.handler.d.ts → handler/review.d.ts} +3 -3
- package/lib/event/handler/review.js +391 -0
- package/lib/event/index.d.ts +5 -5
- package/lib/event/index.js +5 -5
- package/lib/render/base.d.ts +1 -1
- package/lib/render/client.d.ts +1 -1
- package/lib/render/client.js +5 -7
- package/lib/render/client_even.d.ts +30 -0
- package/lib/render/client_even.js +153 -0
- package/lib/render/http.d.ts +1 -1
- package/lib/render/http.js +1 -1
- package/lib/render/server.js +1 -1
- package/lib/types/adapter/api.d.ts +5 -1
- package/lib/types/adapter/{adapter.d.ts → base.d.ts} +2 -2
- package/lib/types/config/config.js +1 -0
- package/lib/types/element/element.js +1 -0
- package/lib/types/event/message.d.ts +1 -1
- package/lib/types/event/reply.d.ts +1 -1
- package/lib/types/index.d.ts +6 -6
- package/lib/types/index.js +6 -6
- package/lib/types/logger/logger.js +1 -0
- package/lib/types/{plugin.d.ts → plugin/plugin.d.ts} +3 -3
- package/lib/types/plugin/plugin.js +1 -0
- package/lib/types/render/render.js +1 -0
- package/lib/utils/{common.d.ts → common/common.d.ts} +14 -13
- package/lib/utils/common/common.js +591 -0
- package/lib/utils/{config.d.ts → config/config.d.ts} +37 -19
- package/lib/utils/config/config.js +328 -0
- package/lib/utils/config/updateVersion.js +145 -0
- package/lib/utils/config/yamlEditor.js +292 -0
- package/lib/utils/{handler.d.ts → core/handler.d.ts} +1 -1
- package/lib/utils/core/handler.js +115 -0
- package/lib/utils/core/init.js +213 -0
- package/lib/utils/core/logger.js +105 -0
- package/lib/utils/{segment.d.ts → core/segment.d.ts} +1 -1
- package/lib/utils/core/segment.js +441 -0
- package/lib/utils/index.d.ts +11 -11
- package/lib/utils/index.js +11 -11
- package/lib/utils/{button.d.ts → tools/button.d.ts} +1 -1
- package/lib/utils/tools/button.js +38 -0
- package/lib/utils/tools/exec.js +37 -0
- package/lib/utils/tools/ffmpeg.js +25 -0
- package/lib/utils/tools/update.js +139 -0
- package/package.json +1 -1
- package/lib/core/dir.js +0 -7
- package/lib/core/init.js +0 -42
- package/lib/core/karin.js +0 -194
- package/lib/core/listener.js +0 -217
- package/lib/core/plugin.app.js +0 -19
- package/lib/core/plugin.js +0 -145
- package/lib/core/plugin.loader.js +0 -561
- package/lib/core/process.js +0 -98
- package/lib/core/server.js +0 -269
- package/lib/db/level.js +0 -37
- package/lib/db/redis.js +0 -134
- package/lib/db/redis_level.js +0 -293
- package/lib/event/event.handler.js +0 -167
- package/lib/event/message.handler.js +0 -254
- package/lib/event/notice.handler.js +0 -204
- package/lib/event/request.handler.js +0 -110
- package/lib/event/review.handler.js +0 -386
- package/lib/types/config.js +0 -1
- package/lib/types/element.js +0 -1
- package/lib/types/logger.js +0 -1
- package/lib/types/plugin.js +0 -1
- package/lib/types/render.js +0 -1
- package/lib/utils/button.js +0 -34
- package/lib/utils/common.js +0 -596
- package/lib/utils/config.js +0 -318
- package/lib/utils/exec.js +0 -36
- package/lib/utils/ffmpeg.js +0 -25
- package/lib/utils/handler.js +0 -109
- package/lib/utils/init.js +0 -208
- package/lib/utils/logger.js +0 -104
- package/lib/utils/segment.js +0 -470
- package/lib/utils/update.js +0 -135
- package/lib/utils/updateVersion.js +0 -145
- package/lib/utils/yamlEditor.js +0 -279
- /package/lib/core/{dir.d.ts → init/dir.d.ts} +0 -0
- /package/lib/core/{init.d.ts → init/init.d.ts} +0 -0
- /package/lib/core/{process.d.ts → process/process.d.ts} +0 -0
- /package/lib/core/{server.d.ts → server/server.d.ts} +0 -0
- /package/lib/db/{level.d.ts → level/level.d.ts} +0 -0
- /package/lib/db/{redis.d.ts → redis/redis.d.ts} +0 -0
- /package/lib/db/{redis_level.d.ts → redis/redis_level.d.ts} +0 -0
- /package/lib/types/adapter/{adapter.js → base.js} +0 -0
- /package/lib/types/{config.d.ts → config/config.d.ts} +0 -0
- /package/lib/types/{element.d.ts → element/element.d.ts} +0 -0
- /package/lib/types/{logger.d.ts → logger/logger.d.ts} +0 -0
- /package/lib/types/{render.d.ts → render/render.d.ts} +0 -0
- /package/lib/utils/{updateVersion.d.ts → config/updateVersion.d.ts} +0 -0
- /package/lib/utils/{yamlEditor.d.ts → config/yamlEditor.d.ts} +0 -0
- /package/lib/utils/{init.d.ts → core/init.d.ts} +0 -0
- /package/lib/utils/{logger.d.ts → core/logger.d.ts} +0 -0
- /package/lib/utils/{exec.d.ts → tools/exec.d.ts} +0 -0
- /package/lib/utils/{ffmpeg.d.ts → tools/ffmpeg.d.ts} +0 -0
- /package/lib/utils/{update.d.ts → tools/update.d.ts} +0 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* YamlEditor - 一个用于编辑 YAML 文件的类
|
|
3
|
+
* 本代码由 ChatGPT4 提供
|
|
4
|
+
* https://github.com/OpenAI
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import lodash from 'lodash';
|
|
8
|
+
import logger from '../core/logger.js';
|
|
9
|
+
import Yaml, { isMap, isSeq, isPair } from 'yaml';
|
|
10
|
+
export class YamlEditor {
|
|
11
|
+
filePath;
|
|
12
|
+
doc;
|
|
13
|
+
document;
|
|
14
|
+
constructor(file) {
|
|
15
|
+
this.filePath = file;
|
|
16
|
+
const data = Yaml.parseDocument(fs.readFileSync(file, 'utf8'));
|
|
17
|
+
this.doc = data;
|
|
18
|
+
this.document = data;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 获取指定路径的值
|
|
22
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
23
|
+
*/
|
|
24
|
+
get(path) {
|
|
25
|
+
try {
|
|
26
|
+
if (!path)
|
|
27
|
+
return this.document.toJSON();
|
|
28
|
+
return lodash.get(this.document.toJSON(), path);
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
logger.error(`[YamlEditor] 获取数据时出错:${error}`);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 设置指定路径的值
|
|
37
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
38
|
+
* @param value - 要设置的值 允许的类型:`string`, `boolean`, `number`, `object`, `array`
|
|
39
|
+
*/
|
|
40
|
+
set(path, value) {
|
|
41
|
+
try {
|
|
42
|
+
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
43
|
+
this.document.setIn(_path, value);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
logger.error(`[YamlEditor] 设置数据时出错:${error}`);
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 向指定路径添加新值
|
|
52
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
53
|
+
* @param value - 要添加的值
|
|
54
|
+
*/
|
|
55
|
+
add(path, value) {
|
|
56
|
+
try {
|
|
57
|
+
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
58
|
+
this.document.addIn(_path, value);
|
|
59
|
+
logger.debug(`[YamlEditor] 已在 ${path} 添加新的值`);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
logger.error(`[YamlEditor] 添加数据时出错:${error}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 删除指定路径
|
|
67
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
68
|
+
* @returns 是否删除成功
|
|
69
|
+
*/
|
|
70
|
+
del(path) {
|
|
71
|
+
try {
|
|
72
|
+
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
73
|
+
this.document.deleteIn(_path);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
logger.error(`[YamlEditor] 删除数据时出错:${error}`);
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* 向指定路径的数组添加新值,可以选择添加到数组的开始或结束
|
|
83
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
84
|
+
* @param value - 要添加的值
|
|
85
|
+
* @param prepend - 如果为 true,则添加到数组的开头,否则添加到末尾
|
|
86
|
+
*/
|
|
87
|
+
append(path, value, prepend = false) {
|
|
88
|
+
try {
|
|
89
|
+
const _path = typeof path === 'string' ? path.split('.') : path || [];
|
|
90
|
+
let current = this.document.getIn(_path);
|
|
91
|
+
if (!current) {
|
|
92
|
+
current = new Yaml.YAMLSeq();
|
|
93
|
+
this.document.setIn(_path, current);
|
|
94
|
+
}
|
|
95
|
+
else if (!(current instanceof Yaml.YAMLSeq)) {
|
|
96
|
+
throw new Error('[YamlEditor] 指定的路径不是数组');
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
prepend ? current.items.unshift(value) : current.add(value);
|
|
100
|
+
}
|
|
101
|
+
logger.debug(`[YamlEditor] 已向 ${path} 数组${prepend ? '开头' : '末尾'}添加新元素:${value}`);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
logger.error(`[YamlEditor] 向数组添加元素时出错:${error}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 检查指定路径的键是否存在
|
|
109
|
+
* @param path - 路径,用点号分隔
|
|
110
|
+
*/
|
|
111
|
+
has(path) {
|
|
112
|
+
try {
|
|
113
|
+
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
114
|
+
return this.document.hasIn(_path);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
logger.error(`[YamlEditor] 检查路径是否存在时出错:${error}`);
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 查询指定路径中是否包含指定的值
|
|
123
|
+
* @param path - 路径,用点号分隔
|
|
124
|
+
* @param value - 要查询的值
|
|
125
|
+
*/
|
|
126
|
+
hasval(path, value) {
|
|
127
|
+
try {
|
|
128
|
+
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
129
|
+
const current = this.document.getIn(_path);
|
|
130
|
+
if (!current)
|
|
131
|
+
return false;
|
|
132
|
+
/** 检查当前节点是否包含指定的值 */
|
|
133
|
+
if (current instanceof Yaml.YAMLSeq) {
|
|
134
|
+
/** 如果是序列,遍历序列查找值 */
|
|
135
|
+
return current.items.some(item => lodash.isEqual(item.toJSON(), value));
|
|
136
|
+
}
|
|
137
|
+
else if (current instanceof Yaml.YAMLMap) {
|
|
138
|
+
/** 如果是映射,检查每个值 */
|
|
139
|
+
return Array.from(current.values()).some((v) => lodash.isEqual(v.toJSON(), value));
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
/** 否则,直接比较值 */
|
|
143
|
+
return lodash.isEqual(current, value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
logger.error(`[YamlEditor] 检查路径 ${path} 是否包含值时出错:${error}`);
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 查询指定路径中是否包含指定的值
|
|
153
|
+
* @param path - 路径,用点号分隔
|
|
154
|
+
* @param value - 要查询的值
|
|
155
|
+
* @deprecated 请使用 `hasval` 代替
|
|
156
|
+
*/
|
|
157
|
+
hasVal(path, value) {
|
|
158
|
+
return this.hasval(path, value);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 向根节点新增元素,如果根节点不是数组,则将其转换为数组再新增元素
|
|
162
|
+
* @param value - 要新增的元素
|
|
163
|
+
*/
|
|
164
|
+
pusharr(value) {
|
|
165
|
+
try {
|
|
166
|
+
if (!(this.document.contents instanceof Yaml.YAMLSeq)) {
|
|
167
|
+
// 如果根节点不是数组,则将其转换为数组
|
|
168
|
+
this.document.contents = new Yaml.YAMLSeq();
|
|
169
|
+
logger.debug('[YamlEditor] 根节点已转换为数组');
|
|
170
|
+
}
|
|
171
|
+
this.document.contents.add(value);
|
|
172
|
+
logger.debug('[YamlEditor] 已向根节点数组新增元素:', value);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
logger.error(`[YamlEditor] 向根节点数组新增元素时出错:${error}`);
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* 根据索引从根节点数组删除元素
|
|
181
|
+
* @param index - 要删除元素的索引
|
|
182
|
+
*/
|
|
183
|
+
delarr(index) {
|
|
184
|
+
try {
|
|
185
|
+
if (!(this.document.contents instanceof Yaml.YAMLSeq)) {
|
|
186
|
+
throw new Error('[YamlEditor] 根节点不是数组');
|
|
187
|
+
}
|
|
188
|
+
if (index < 0 || index >= this.document.contents.items.length) {
|
|
189
|
+
throw new Error('[YamlEditor] 索引超出范围');
|
|
190
|
+
}
|
|
191
|
+
this.document.contents.items.splice(index, 1);
|
|
192
|
+
logger.debug('[YamlEditor] 已根据索引从根节点数组删除元素,索引:', index);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
logger.error(`[YamlEditor] 根据索引删除根节点数组元素时出错:${error}`);
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 获取指定路径的pair对象
|
|
202
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
203
|
+
*/
|
|
204
|
+
getpair(path) {
|
|
205
|
+
if (!path)
|
|
206
|
+
throw new Error('path is required');
|
|
207
|
+
const keys = path.split('.');
|
|
208
|
+
// 好多any啊,我要当any糕手~
|
|
209
|
+
let pair = this.document.contents;
|
|
210
|
+
keys.forEach(key => {
|
|
211
|
+
if (isMap(pair)) {
|
|
212
|
+
pair = pair.items.find((item) => item.key.value === key);
|
|
213
|
+
}
|
|
214
|
+
else if (isSeq(pair)) {
|
|
215
|
+
pair = pair.items.find((item) => item.value === key);
|
|
216
|
+
}
|
|
217
|
+
else if (isPair(pair)) {
|
|
218
|
+
pair = pair.value.items.find((item) => item.key.value === key);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
return pair;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 设置指定键的注释
|
|
225
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
226
|
+
* @param comment - 要设置的注释
|
|
227
|
+
* @param prepend - 如果为 true,则添加到注释的开头,否则添加到同一行的末尾
|
|
228
|
+
*/
|
|
229
|
+
comment(path, comment, prepend) {
|
|
230
|
+
if (!path)
|
|
231
|
+
throw new Error('[YamlEditor] path 不能为空');
|
|
232
|
+
const pair = this.getpair(path);
|
|
233
|
+
if (!pair)
|
|
234
|
+
throw new Error(`[YamlEditor] 未找到节点 ${path}`);
|
|
235
|
+
comment = ` ${comment}`;
|
|
236
|
+
if (prepend) {
|
|
237
|
+
pair.key.commentBefore = comment;
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
pair.key.comment = comment;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 删除指定键的注释
|
|
245
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
246
|
+
* @param type - 要删除的注释类型,`before` 为注释前,`after` 为注释后,`all` 为全部
|
|
247
|
+
*/
|
|
248
|
+
uncomment(path, type = 'all') {
|
|
249
|
+
if (!path)
|
|
250
|
+
throw new Error('[YamlEditor] path 不能为空');
|
|
251
|
+
const pair = this.getpair(path);
|
|
252
|
+
if (!pair)
|
|
253
|
+
throw new Error(`[YamlEditor] 未找到节点 ${path}`);
|
|
254
|
+
if (type === 'all') {
|
|
255
|
+
delete pair.key.comment;
|
|
256
|
+
delete pair.key.commentBefore;
|
|
257
|
+
}
|
|
258
|
+
else if (type === 'before') {
|
|
259
|
+
delete pair.key.commentBefore;
|
|
260
|
+
}
|
|
261
|
+
else if (type === 'after') {
|
|
262
|
+
delete pair.key.comment;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 检查注释是否存在
|
|
267
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
268
|
+
* @param type - 要检查的注释类型,`before` 为注释前,`after` 为注释后
|
|
269
|
+
*/
|
|
270
|
+
hascomment(path, type) {
|
|
271
|
+
if (!path)
|
|
272
|
+
throw new Error('[YamlEditor] path 不能为空');
|
|
273
|
+
const pair = this.getpair(path);
|
|
274
|
+
if (!pair)
|
|
275
|
+
throw new Error(`[YamlEditor] 未找到节点 ${path}`);
|
|
276
|
+
if (type === 'before') {
|
|
277
|
+
return !!pair.key.commentBefore;
|
|
278
|
+
}
|
|
279
|
+
else if (type === 'after') {
|
|
280
|
+
return !!pair.key.comment;
|
|
281
|
+
}
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* 保存文件
|
|
286
|
+
* 保存失败会抛出异常
|
|
287
|
+
*/
|
|
288
|
+
save() {
|
|
289
|
+
fs.writeFileSync(this.filePath, this.document.toString());
|
|
290
|
+
logger.debug('[YamlEditor] 文件已保存');
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import lodash from 'lodash';
|
|
2
|
+
import logger from './logger.js';
|
|
3
|
+
import { pluginLoader as loader } from '../../core/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* 事件处理器类
|
|
6
|
+
*/
|
|
7
|
+
export const handler = new (class EventHandler {
|
|
8
|
+
/**
|
|
9
|
+
* 添加事件处理器
|
|
10
|
+
* @param index 插件索引
|
|
11
|
+
* @param Class 插件类
|
|
12
|
+
*/
|
|
13
|
+
async add(index, Class) {
|
|
14
|
+
lodash.forEach(Class.handler, val => {
|
|
15
|
+
if (!val.key)
|
|
16
|
+
logger.error(`[Handler][Add]: [${Class.name}] 缺少 key`);
|
|
17
|
+
if (!val.fnc)
|
|
18
|
+
logger.error(`[Handler][Add]: [${Class.name}] 缺少 fnc`);
|
|
19
|
+
logger.debug(`[Handler][Reg]: [${Class.name}][${val.key}]`);
|
|
20
|
+
if (!Array.isArray(loader.handlerIds[val.key]))
|
|
21
|
+
loader.handlerIds[val.key] = [];
|
|
22
|
+
loader.handlerIds[val.key].push({ index, fnc: val.fnc, priority: val.priority || Class.priority });
|
|
23
|
+
loader.handlerIds[val.key] = lodash.orderBy(loader.handlerIds[val.key], ['priority'], ['asc']);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 删除事件处理器
|
|
28
|
+
* 如果不传参数则删除所有处理器
|
|
29
|
+
* @param index 插件索引
|
|
30
|
+
* @param key 事件键
|
|
31
|
+
*/
|
|
32
|
+
del(index = '', key = '') {
|
|
33
|
+
/** 无参 */
|
|
34
|
+
if (!key && !index) {
|
|
35
|
+
loader.handlerIds = {};
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
/** 删除指定索引插件 */
|
|
39
|
+
if (!key) {
|
|
40
|
+
for (const v of Object.keys(loader.handlerIds)) {
|
|
41
|
+
loader.handlerIds[v] = loader.handlerIds[v].filter(v => v.index !== index);
|
|
42
|
+
// 如果处理器为空则删除键
|
|
43
|
+
if (!loader.handlerIds[v].length) {
|
|
44
|
+
delete loader.handlerIds[v];
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
loader.handlerIds[v] = lodash.orderBy(loader.handlerIds[v], ['priority'], ['asc']);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
/** 删除指定key */
|
|
53
|
+
if (!index) {
|
|
54
|
+
loader.handlerIds[key] && delete loader.handlerIds[key];
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
/** 删除指定key的index */
|
|
58
|
+
if (!loader.handlerIds[key])
|
|
59
|
+
return false;
|
|
60
|
+
loader.handlerIds[key] = loader.handlerIds[key].filter(v => v.index !== index);
|
|
61
|
+
if (!loader.handlerIds[key].length) {
|
|
62
|
+
delete loader.handlerIds[key];
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
loader.handlerIds[key] = lodash.orderBy(loader.handlerIds[key], ['priority'], ['asc']);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 调用事件处理器
|
|
70
|
+
* @param key 事件键
|
|
71
|
+
* @param args 自定义参数 一般用来传递e之类的
|
|
72
|
+
*/
|
|
73
|
+
async call(key, args) {
|
|
74
|
+
let res;
|
|
75
|
+
for (const v of loader.handlerIds[key] || []) {
|
|
76
|
+
const info = loader.PluginList[v.index];
|
|
77
|
+
try {
|
|
78
|
+
let done = true;
|
|
79
|
+
/**
|
|
80
|
+
* 拒绝处理器 调用后则不再继续执行下一个处理器
|
|
81
|
+
* @param msg 错误信息
|
|
82
|
+
*/
|
|
83
|
+
const reject = (msg = '') => {
|
|
84
|
+
if (msg)
|
|
85
|
+
logger.mark(`[Handler][Reject]: [${info.file.dir}][${info.file.name}][${key}] ${msg}`);
|
|
86
|
+
done = false;
|
|
87
|
+
};
|
|
88
|
+
if (info.file.type === 'function' && typeof v.fnc === 'function') {
|
|
89
|
+
res = await v.fnc(args, reject);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
const cla = new info.file.Fnc();
|
|
93
|
+
if (args.e)
|
|
94
|
+
cla.e = args.e;
|
|
95
|
+
res = await cla[v.fnc](args, reject);
|
|
96
|
+
}
|
|
97
|
+
if (done) {
|
|
98
|
+
logger.mark(`[Handler][Done]: [${info.file.dir}][${info.file.name}][${key}]`);
|
|
99
|
+
return res;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
// 产生错误继续下一个处理器
|
|
104
|
+
logger.error(`[Handler][Error]: [${info.file.dir}][${info.file.name}][${key}] ${e}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return res;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 检查是否存在指定键的事件处理器
|
|
111
|
+
*/
|
|
112
|
+
has(key) {
|
|
113
|
+
return !!loader.handlerIds[key];
|
|
114
|
+
}
|
|
115
|
+
})();
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { exec } from 'child_process';
|
|
4
|
+
import { karinDir } from '../../core/index.js';
|
|
5
|
+
export class KarinInit {
|
|
6
|
+
/**
|
|
7
|
+
* 基础目录
|
|
8
|
+
*/
|
|
9
|
+
baseDir;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.baseDir = [
|
|
12
|
+
'./config/config',
|
|
13
|
+
'./config/view',
|
|
14
|
+
'./config/plugin',
|
|
15
|
+
'./temp/input',
|
|
16
|
+
'./temp/html',
|
|
17
|
+
'./plugins/karin-plugin-example',
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
init() {
|
|
21
|
+
this.baseDir.forEach(dir => this.mkdir(dir));
|
|
22
|
+
/** 非src源代码环境才创建 */
|
|
23
|
+
if (!fs.existsSync('./src')) {
|
|
24
|
+
this.copyFile();
|
|
25
|
+
this.modifyPackage();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 递归创建目录
|
|
30
|
+
* @param dirname - 路径
|
|
31
|
+
*/
|
|
32
|
+
mkdir(dirname) {
|
|
33
|
+
if (fs.existsSync(dirname))
|
|
34
|
+
return true;
|
|
35
|
+
if (this.mkdir(path.dirname(dirname)))
|
|
36
|
+
fs.mkdirSync(dirname);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 解析json
|
|
41
|
+
*/
|
|
42
|
+
readJson(file) {
|
|
43
|
+
return JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 获取插件列表
|
|
47
|
+
*/
|
|
48
|
+
getPlugins() {
|
|
49
|
+
const pluginsDir = './plugins';
|
|
50
|
+
const plugins = fs.readdirSync(pluginsDir);
|
|
51
|
+
return plugins.filter(dir => dir.startsWith('karin-plugin-'));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 从npm包内复制默认配置出来
|
|
55
|
+
*/
|
|
56
|
+
copyFile() {
|
|
57
|
+
/** 配置 */
|
|
58
|
+
const list = ['config/view'];
|
|
59
|
+
list.forEach(dir => {
|
|
60
|
+
const pkgDir = path.join(karinDir, dir);
|
|
61
|
+
const projDir = path.join(process.cwd(), dir);
|
|
62
|
+
/** 清空projDir目录下的文件 保留目录 */
|
|
63
|
+
if (fs.existsSync(projDir))
|
|
64
|
+
fs.rmSync(projDir, { recursive: true });
|
|
65
|
+
this.mkdir(projDir);
|
|
66
|
+
/** 读取pkgDir目录下的所有文件 复制到projDir下 */
|
|
67
|
+
const files = fs.readdirSync(pkgDir).filter(file => file.endsWith('.yaml'));
|
|
68
|
+
files.forEach(file => {
|
|
69
|
+
const destPath = path.join(projDir, file);
|
|
70
|
+
const srcPath = path.join(pkgDir, file);
|
|
71
|
+
fs.copyFileSync(srcPath, destPath);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
/** 创建pnpm工作区配置 */
|
|
75
|
+
fs.writeFileSync('./pnpm-workspace.yaml', `packages:\n - 'plugins/**'\n`);
|
|
76
|
+
/** 为每个插件包创建统一存储的文件夹 */
|
|
77
|
+
const plugins = this.getPlugins();
|
|
78
|
+
const DataList = [
|
|
79
|
+
'data',
|
|
80
|
+
'temp',
|
|
81
|
+
'resources',
|
|
82
|
+
'temp/html',
|
|
83
|
+
'config/plugin',
|
|
84
|
+
];
|
|
85
|
+
DataList.forEach(_path => {
|
|
86
|
+
_path = path.join(process.cwd(), _path);
|
|
87
|
+
plugins.forEach(plugin => this.mkdir(path.join(_path, plugin)));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 修改package.json以支持运行
|
|
92
|
+
*/
|
|
93
|
+
modifyPackage() {
|
|
94
|
+
const pkg = this.readJson(path.join(karinDir, 'package.json'));
|
|
95
|
+
const projPkg = this.readJson('./package.json');
|
|
96
|
+
const main = 'node_modules/node-karin/lib/index.js';
|
|
97
|
+
delete pkg.bin;
|
|
98
|
+
pkg.main = `./${main}`;
|
|
99
|
+
pkg.types = `./${main.replace('.js', '.d.ts')}`;
|
|
100
|
+
pkg.dependencies = { ...projPkg.dependencies, ...pkg.dependencies };
|
|
101
|
+
pkg.devDependencies = { ...projPkg.devDependencies, ...pkg.devDependencies };
|
|
102
|
+
projPkg.type = 'module';
|
|
103
|
+
projPkg.private = true;
|
|
104
|
+
projPkg.main = `./${main}`;
|
|
105
|
+
projPkg.dependencies = { ...projPkg.dependencies, ...pkg.dependencies };
|
|
106
|
+
projPkg.dependencies['node-karin'] = 'latest';
|
|
107
|
+
projPkg.dependencies['kritor-proto'] = 'latest';
|
|
108
|
+
if (!projPkg.scripts)
|
|
109
|
+
projPkg.scripts = {};
|
|
110
|
+
if (!projPkg.devDependencies)
|
|
111
|
+
projPkg.devDependencies = {};
|
|
112
|
+
projPkg.scripts.debug = `node --watch ${main}`;
|
|
113
|
+
projPkg.scripts.dev = `tsx --watch ${main}`;
|
|
114
|
+
fs.writeFileSync('./package.json', JSON.stringify(projPkg, null, 2));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 更换镜像源为淘宝源
|
|
118
|
+
*/
|
|
119
|
+
async changeRegistry(host) {
|
|
120
|
+
const cmd = `npm config set registry ${host}`;
|
|
121
|
+
// 检查当前镜像源是否为淘宝源
|
|
122
|
+
const registry = await this.shell('npm config get registry');
|
|
123
|
+
if (registry === host) {
|
|
124
|
+
console.log(`当前npm源已经是 ${host} ~`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (!await this.shell(cmd)) {
|
|
128
|
+
console.log('更换npm源失败,请手动更换npm源!');
|
|
129
|
+
console.log(`可尝试手动执行 【 npm config set registry ${host} 】 更换镜像源~`);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
console.log('更换npm源成功~');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 安装pnpm或者yarn
|
|
137
|
+
* @param type - 包管理器名称
|
|
138
|
+
*/
|
|
139
|
+
async install(type) {
|
|
140
|
+
/** 检查是否已经安装对应的包管理器 */
|
|
141
|
+
if (!(await this.shell(`${type} -v`))) {
|
|
142
|
+
console.log(`检测到未安装${type},开始安装...`);
|
|
143
|
+
if (!await this.shell(`npm install -g ${type}`)) {
|
|
144
|
+
console.log(`${type}安装失败,请手动安装${type}!`);
|
|
145
|
+
console.log(`可尝试手动执行 【 npm install -g ${type} 】 安装${type}~`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
console.log(`${type}安装完成~`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
console.log('检测到已安装pnpm,开始安装依赖...');
|
|
154
|
+
}
|
|
155
|
+
/** 安装依赖 */
|
|
156
|
+
if (!(await this.shell(`${type} -P --force`))) {
|
|
157
|
+
console.log('安装依赖失败,请手动安装依赖!');
|
|
158
|
+
console.log(`可尝试手动执行 【 ${type} install -P 】 安装依赖~`);
|
|
159
|
+
console.log('如中国大陆用户安装失败,请尝试执行换源 【 npm config set registry https://registry.npmmirror.com 】后再安装依赖~');
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
console.log('依赖安装完成~');
|
|
163
|
+
}
|
|
164
|
+
/** 检查安装pm2 */
|
|
165
|
+
if (!(await this.shell('pm2 -v'))) {
|
|
166
|
+
console.log('检测到未安装pm2,开始安装pm2...');
|
|
167
|
+
if (!(await this.shell(`${type} install -g pm2`))) {
|
|
168
|
+
console.log('安装pm2失败,请手动安装pm2!');
|
|
169
|
+
console.log('可尝试手动执行 【 npm install -g pm2 】 安装pm2~');
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
console.log('pm2安装完成~');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
console.log('检测到已安装pm2~');
|
|
177
|
+
}
|
|
178
|
+
console.log('依赖环境初始化完成~');
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* 执行命令
|
|
182
|
+
* @param cmd - 命令
|
|
183
|
+
*/
|
|
184
|
+
shell(cmd) {
|
|
185
|
+
return new Promise(resolve => {
|
|
186
|
+
exec(cmd, { env: process.env, cwd: process.cwd() }, (err, stdout, stderr) => {
|
|
187
|
+
if (stdout)
|
|
188
|
+
return resolve(stdout.trim());
|
|
189
|
+
if (err) {
|
|
190
|
+
console.error(err);
|
|
191
|
+
return resolve(false);
|
|
192
|
+
}
|
|
193
|
+
if (stderr) {
|
|
194
|
+
console.error(stderr);
|
|
195
|
+
return resolve(false);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 获取当前的包管理器 根据锁文件判断
|
|
202
|
+
*/
|
|
203
|
+
getRegistry() {
|
|
204
|
+
if (fs.existsSync('./pnpm-lock.yaml'))
|
|
205
|
+
return 'pnpm';
|
|
206
|
+
if (fs.existsSync('./yarn.lock'))
|
|
207
|
+
return 'yarn';
|
|
208
|
+
if (fs.existsSync('./package-lock.json'))
|
|
209
|
+
return 'npm';
|
|
210
|
+
// cnpm 没有锁文件
|
|
211
|
+
return 'cnpm';
|
|
212
|
+
}
|
|
213
|
+
}
|