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,13 +0,0 @@
1
- export * from './tools/button.js';
2
- export * from './common/common.js';
3
- export * from './config/config.js';
4
- export * from './tools/ffmpeg.js';
5
- export * from './core/handler.js';
6
- export * from './tools/exec.js';
7
- export * from './core/segment.js';
8
- export * from './tools/update.js';
9
- export * from './config/yamlEditor.js';
10
- export * from './core/logger.js';
11
- export * from './config/updateVersion.js';
12
- export * from './tools/restart.js';
13
- export * from './tools/stop.js';
@@ -1,13 +0,0 @@
1
- export * from './tools/button.js';
2
- export * from './common/common.js';
3
- export * from './config/config.js';
4
- export * from './tools/ffmpeg.js';
5
- export * from './core/handler.js';
6
- export * from './tools/exec.js';
7
- export * from './core/segment.js';
8
- export * from './tools/update.js';
9
- export * from './config/yamlEditor.js';
10
- export * from './core/logger.js';
11
- export * from './config/updateVersion.js';
12
- export * from './tools/restart.js';
13
- export * from './tools/stop.js';
@@ -1,2 +0,0 @@
1
- import { KarinMessageType } from '../../types/index.js';
2
- export declare const button: (msg: string, e?: KarinMessageType) => Promise<any>;
@@ -1,27 +0,0 @@
1
- import logger from '../core/logger.js';
2
- import { pluginLoader as loader } from '../../core/index.js';
3
- export const button = async (msg, e) => {
4
- const button = [];
5
- for (const info of loader.button) {
6
- const reg = info.reg;
7
- if (reg.test(msg)) {
8
- try {
9
- let done = true;
10
- /**
11
- * 标记函数 如果调用则继续执行 循环下一个按钮插件处理
12
- */
13
- const reject = () => { done = false; };
14
- const res = await info.fn(reject, e);
15
- if (res)
16
- button.push(res);
17
- if (done)
18
- return res;
19
- }
20
- catch (error) {
21
- logger.error(error);
22
- }
23
- }
24
- }
25
- /** 理论上不会走到这里,但是还是要稳一手,不排除有所有插件都false... */
26
- return button;
27
- };
@@ -1,20 +0,0 @@
1
- import { ExecOptions } from 'child_process';
2
- /**
3
- * 执行 shell 命令
4
- * @param cmd 命令
5
- * @param log 是否打印日志
6
- * @param options 选项
7
- */
8
- export declare const exec: (cmd: string, log?: boolean, options?: ExecOptions) => Promise<{
9
- status: "ok" | "failed";
10
- error: Error | null;
11
- stdout: string;
12
- stderr: string;
13
- }>;
14
- /**
15
- * 执行 shell 命令
16
- * @param cmd 命令
17
- * @param options 选项
18
- */
19
- export declare const execs: (cmd: string, options?: ExecOptions) => Promise<string>;
20
- export default exec;
@@ -1,54 +0,0 @@
1
- import logger from '../core/logger.js';
2
- import { exec as execCmd } from 'child_process';
3
- /**
4
- * 执行 shell 命令
5
- * @param cmd 命令
6
- * @param log 是否打印日志
7
- * @param options 选项
8
- */
9
- export const exec = async (cmd, log = true, options = { cwd: process.cwd() }) => {
10
- const logType = (status) => ({
11
- start: '[exec][开始执行]',
12
- ok: '[exec][执行成功]',
13
- failed: '[exec][执行失败]',
14
- })[status];
15
- const logMessage = (status, details = '') => {
16
- if (log) {
17
- const colorFunc = {
18
- start: logger.yellow,
19
- ok: logger.green,
20
- failed: logger.red,
21
- }[status];
22
- logger.info([
23
- colorFunc(logType(status)),
24
- `cmd: ${cmd}`,
25
- `cwd: ${options.cwd}`,
26
- details,
27
- '--------',
28
- ].join('\n'));
29
- }
30
- };
31
- logMessage('start');
32
- return new Promise(resolve => {
33
- execCmd(cmd, options, (error, stdout = '', stderr = '') => {
34
- const status = error ? 'failed' : 'ok';
35
- logMessage(status, error ? `Error: ${error.message}` : `stdout: ${stdout}\nstderr: ${stderr}`);
36
- resolve({ status, error, stdout, stderr });
37
- });
38
- });
39
- };
40
- /**
41
- * 执行 shell 命令
42
- * @param cmd 命令
43
- * @param options 选项
44
- */
45
- export const execs = (cmd, options = { cwd: process.cwd() }) => {
46
- return new Promise((resolve, reject) => {
47
- execCmd(cmd, options, (error, stdout) => {
48
- if (error)
49
- return reject(error);
50
- resolve(stdout.trim());
51
- });
52
- });
53
- };
54
- export default exec;
@@ -1,12 +0,0 @@
1
- /**
2
- * 执行 ffmpeg 命令
3
- */
4
- export declare function ffmpeg(): Promise<false | ((cmd: string, log?: boolean, options?: {
5
- cwd: string;
6
- encoding: string;
7
- }) => Promise<{
8
- status: "ok" | "failed";
9
- error: Error | null;
10
- stdout: string;
11
- stderr: string;
12
- }>)>;
@@ -1,25 +0,0 @@
1
- import exec from './exec.js';
2
- import logger from '../core/logger.js';
3
- import { config } from '../config/config.js';
4
- /**
5
- * 执行 ffmpeg 命令
6
- */
7
- export async function ffmpeg() {
8
- let ffmpeg = 'ffmpeg';
9
- const { status } = await exec('ffmpeg -version', false);
10
- if (status !== 'ok') {
11
- logger.debug('ffmpeg 未安装,开始尝试读取配置文件是否存在ffmpeg路径');
12
- const ffmpegPath = config.Config.ffmpeg_path;
13
- if (!ffmpegPath) {
14
- logger.warn('ffmpeg 未安装,请安装 ffmpeg 或手动配置 ffmpeg 路径后重启');
15
- return false;
16
- }
17
- ffmpeg = `"${ffmpegPath}"`;
18
- }
19
- // 返回函数
20
- return async (cmd, log = true, options = { cwd: process.cwd(), encoding: 'utf-8' }) => {
21
- cmd = cmd.replace(/^ffmpeg/, '').trim();
22
- cmd = `${ffmpeg} ${cmd}`;
23
- return await exec(cmd, log, options);
24
- };
25
- }
@@ -1,15 +0,0 @@
1
- import { Contact } from '../../types/index.js';
2
- /**
3
- * 重启Bot
4
- * @param self_id - 机器人的id 传e.self_id
5
- * @param contact - 事件联系人信息 也就是从哪来的这条消息 传e.contact即可
6
- * @param message_id - 消息id 传e.message_id
7
- * @param isFront - 是否为前台重启 默认是
8
- */
9
- export declare const restart: (self_id: string, contact: Contact, message_id: string, isFront?: boolean) => Promise<{
10
- status: string;
11
- data: string;
12
- } | {
13
- status: string;
14
- data: Error | null;
15
- }>;
@@ -1,39 +0,0 @@
1
- import exec from './exec.js';
2
- import { level } from '../../db/index.js';
3
- import { config } from '../config/config.js';
4
- /**
5
- * 重启Bot
6
- * @param self_id - 机器人的id 传e.self_id
7
- * @param contact - 事件联系人信息 也就是从哪来的这条消息 传e.contact即可
8
- * @param message_id - 消息id 传e.message_id
9
- * @param isFront - 是否为前台重启 默认是
10
- */
11
- export const restart = async (self_id, contact, message_id, isFront = true) => {
12
- const options = {
13
- id: self_id,
14
- contact,
15
- message_id,
16
- time: Date.now(),
17
- };
18
- const key = `karin:restart:${options.id}`;
19
- await level.set(key, options);
20
- if (isFront) {
21
- if (!process.send)
22
- return { status: 'failed', data: '前台重启失败,当前环境不支持,仅支持【npx karin start】启动的环境' };
23
- process.send({ action: 'result', env: process.env });
24
- process.exit();
25
- }
26
- if (!config.Config.pm2Restart)
27
- process.exit();
28
- if (process.env.pm_id) {
29
- const pm2 = await exec('npx karin rs');
30
- if (pm2.status === 'ok')
31
- process.exit();
32
- return { status: 'failed', data: pm2.error };
33
- }
34
- /** 调用pm2启动 */
35
- const pm2 = await exec('npx karin pm2');
36
- if (pm2.status === 'ok')
37
- process.exit();
38
- return { status: 'failed', data: pm2.error };
39
- };
@@ -1,7 +0,0 @@
1
- /**
2
- * 停止Bot
3
- */
4
- export declare const stop: () => Promise<{
5
- status: string;
6
- data: Error | null;
7
- }>;
@@ -1,13 +0,0 @@
1
- import exec from './exec.js';
2
- /**
3
- * 停止Bot
4
- */
5
- export const stop = async () => {
6
- if (process.env.pm_id) {
7
- const pm2 = await exec('pm2 delete ' + process.env.pm_id);
8
- if (pm2.status === 'ok')
9
- process.exit();
10
- return { status: 'failed', data: pm2.error };
11
- }
12
- process.exit();
13
- };
@@ -1,67 +0,0 @@
1
- export declare class Updates {
2
- dir: string;
3
- constructor();
4
- /**
5
- * 更新npm包
6
- * @param name - 包名
7
- * @param timeout - 超时时间 默认120s
8
- */
9
- updatePkg(name: string, timeout?: number): Promise<{
10
- status: boolean;
11
- data: string;
12
- }>;
13
- /**
14
- * 更新框架或插件
15
- * @param path - 插件相对路径
16
- * @param cmd - 更新命令 默认git pull
17
- * @param timeout - 超时时间 默认120s
18
- */
19
- update(path: string, cmd?: string, timeout?: number): Promise<{
20
- status: string;
21
- data: string;
22
- }>;
23
- /**
24
- * 获取指定仓库最后一次提交时间日期
25
- * @param path - 插件相对路径
26
- */
27
- getTime(path: string): Promise<string>;
28
- /**
29
- * 获取指定仓库最后一次提交哈希值
30
- * @param path - 插件相对路径
31
- * @param short - 是否获取短哈希 默认true
32
- */
33
- getHash(path: string, short?: boolean): Promise<string>;
34
- /**
35
- * 获取指定仓库的提交记录
36
- */
37
- getCommit(options: {
38
- /** 指令命令路径 */
39
- path: string;
40
- /** 获取几次提交 默认1次 */
41
- count?: number;
42
- /** 指定哈希 */
43
- hash?: string;
44
- /** 指定分支 */
45
- branch?: string;
46
- }): Promise<string>;
47
- /**
48
- * 检查插件是否有更新
49
- * @param path - 插件相对路径
50
- * @param time - 超时时间 默认120s
51
- */
52
- checkUpdate(path: string, time?: number): Promise<{
53
- status: string;
54
- data: string | undefined;
55
- count?: undefined;
56
- } | {
57
- status: string;
58
- data: boolean;
59
- count?: undefined;
60
- } | {
61
- status: string;
62
- data: string;
63
- count: number;
64
- }>;
65
- }
66
- export declare const update: Updates;
67
- export declare const Update: Updates;
@@ -1,151 +0,0 @@
1
- import fs from 'fs';
2
- import exec from './exec.js';
3
- import { KarinCli } from '../../cli/index.js';
4
- import { getRegistry } from '../../cli/pkg.js';
5
- export class Updates {
6
- dir;
7
- constructor() {
8
- this.dir = './plugins';
9
- }
10
- /**
11
- * 更新npm包
12
- * @param name - 包名
13
- * @param timeout - 超时时间 默认120s
14
- */
15
- async updatePkg(name, timeout = 120) {
16
- const pkg = getRegistry();
17
- const cmd = (pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`) + `${name}@latest`;
18
- /** 检查是否已经是最新版本 */
19
- const tools = new KarinCli();
20
- const local = await tools.getLocalVersion(name, pkg);
21
- const remote = await tools.getRemoteVersion(name, pkg);
22
- logger.info(`[依赖更新] ${name} 当前版本: ${local} 最新版本: ${remote}`);
23
- if (local === remote)
24
- return { status: true, data: `${name} 已经是最新版本~` };
25
- const npm = await exec(cmd, true, { cwd: process.cwd(), timeout: timeout * 1000 });
26
- if (npm.status === 'ok')
27
- return { status: true, data: `${name} 更新完成~` };
28
- return { status: false, data: `${name} 更新失败: ${npm.error?.message}` };
29
- }
30
- /**
31
- * 更新框架或插件
32
- * @param path - 插件相对路径
33
- * @param cmd - 更新命令 默认git pull
34
- * @param timeout - 超时时间 默认120s
35
- */
36
- async update(path, cmd = 'git pull', timeout = 120) {
37
- /** 检查一下路径是否存在 */
38
- if (!fs.existsSync(path))
39
- return { status: 'failed', data: '路径不存在' };
40
- /** 检查是否有.git文件夹 */
41
- if (!fs.existsSync(`${path}/.git`))
42
- return { status: 'failed', data: '该路径不是一个git仓库' };
43
- /** 设置超时时间 */
44
- const timer = setTimeout(() => {
45
- return { status: 'failed', data: '执行超时' };
46
- }, timeout * 1000);
47
- const options = { env: process.env, cwd: path, encoding: 'utf-8' };
48
- /** 记录当前短哈希 */
49
- const hash = await this.getHash(path);
50
- /** 执行更新 */
51
- const { status, error } = await exec(cmd, true, options);
52
- /** 执行成功 */
53
- if (status === 'ok') {
54
- clearTimeout(timer);
55
- /** 再次获取短哈希 查看是否有更新 */
56
- const newHash = await this.getHash(path);
57
- if (hash === newHash) {
58
- const time = await this.getTime(path);
59
- return {
60
- status: 'ok',
61
- data: `\n当前版本已是最新版本\n最后更新时间:${time}\n更新详情:${await this.getCommit({ path, count: 1 })}`,
62
- };
63
- }
64
- const Commit = await this.getCommit({ path, hash });
65
- return {
66
- status: 'ok',
67
- data: `\n更新成功\n更新日志:\n${Commit}`,
68
- };
69
- }
70
- const data = `\n更新失败\n错误信息:${error?.stack?.toString() || error?.message?.toString() || error?.toString() || error}\n请解决错误后重试或执行【#强制更新】`;
71
- return { status: 'failed', data };
72
- }
73
- /**
74
- * 获取指定仓库最后一次提交时间日期
75
- * @param path - 插件相对路径
76
- */
77
- async getTime(path) {
78
- const cmd = 'git log -1 --format=%cd --date=format:"%Y-%m-%d %H:%M:%S"';
79
- const data = await exec(cmd, false, { cwd: path });
80
- if (data.status === 'failed')
81
- return '获取时间失败,请重试或更新Git~';
82
- return data.stdout.trim();
83
- }
84
- /**
85
- * 获取指定仓库最后一次提交哈希值
86
- * @param path - 插件相对路径
87
- * @param short - 是否获取短哈希 默认true
88
- */
89
- async getHash(path, short = true) {
90
- const cmd = short ? 'git rev-parse --short HEAD' : 'git rev-parse HEAD';
91
- const data = await exec(cmd, false, { cwd: path });
92
- if (data.status === 'failed') {
93
- const text = data.error;
94
- throw new Error(text);
95
- }
96
- return data.stdout.trim();
97
- }
98
- /**
99
- * 获取指定仓库的提交记录
100
- */
101
- async getCommit(options) {
102
- const { path, count = 1, hash, branch } = options;
103
- let cmd = `git log -${count} --format="[%ad]%s %n" --date="format:%m-%d %H:%M"`;
104
- /** 键入HEAD */
105
- if (hash)
106
- cmd = `git log ${hash}..HEAD --format="[%ad] %s %n" --date="format:%m-%d %H:%M"`;
107
- /** 指定分支 */
108
- if (branch)
109
- cmd = `git log -${count} ${branch} --format="[%ad] %s %n" --date="format:%m-%d %H:%M"`;
110
- const data = await exec(cmd, false, { cwd: path });
111
- if (data.status === 'failed') {
112
- const text = data.error;
113
- throw new Error(text);
114
- }
115
- return data.stdout.trim();
116
- }
117
- /**
118
- * 检查插件是否有更新
119
- * @param path - 插件相对路径
120
- * @param time - 超时时间 默认120s
121
- */
122
- async checkUpdate(path, time = 120) {
123
- /** 检查一下路径是否存在 */
124
- if (!fs.existsSync(path))
125
- return { status: 'failed', data: '路径不存在' };
126
- /** 检查是否有.git文件夹 */
127
- if (!fs.existsSync(`${path}/.git`))
128
- return { status: 'failed', data: '该路径不是一个git仓库' };
129
- /** 设置超时时间 */
130
- const timer = setTimeout(() => {
131
- return { status: 'failed', data: '执行超时' };
132
- }, time * 1000);
133
- const options = { env: process.env, cwd: path, encoding: 'utf-8' };
134
- /** git fetch origin */
135
- const { status, error } = await exec('git fetch origin', false, options);
136
- if (status === 'failed')
137
- return { status: 'failed', data: error?.message };
138
- /** git status -uno */
139
- const { stdout } = await exec('git status -uno', false, options);
140
- clearTimeout(timer);
141
- /** 检查是否有更新 没更新直接返回 */
142
- if (stdout.includes('Your branch is up to date with'))
143
- return { status: 'ok', data: false };
144
- /** 获取落后几次更新 */
145
- const count = Number(stdout.match(/Your branch is behind '.*' by (\d+) commits/)?.[1]) || 1;
146
- const data = await this.getCommit({ path, count, branch: 'origin' });
147
- return { status: 'ok', data, count };
148
- }
149
- }
150
- export const update = new Updates();
151
- export const Update = update;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes