node-karin 0.11.14 → 0.12.0
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/config.yaml +3 -3
- package/lib/adapter/input/index.d.ts +1 -1
- package/lib/adapter/input/index.js +1 -1
- package/lib/adapter/onebot/11/index.d.ts +1 -1
- package/lib/adapter/onebot/11/index.js +1 -1
- package/lib/cli/index.d.ts +7 -3
- package/lib/cli/index.js +38 -10
- package/lib/cli/init.js +2 -1
- package/lib/cli/pkg.d.ts +4 -0
- package/lib/cli/pkg.js +14 -0
- package/lib/core/init/config.d.ts +0 -4
- package/lib/core/init/config.js +0 -13
- package/lib/core/karin/karin.d.ts +19 -3
- package/lib/core/karin/karin.js +26 -15
- package/lib/core/listener/listener.js +1 -1
- package/lib/core/process/process.js +1 -5
- package/lib/render/client.d.ts +11 -0
- package/lib/render/client.js +91 -11
- package/lib/types/adapter/base.d.ts +1 -1
- package/lib/types/config/config.d.ts +2 -2
- package/lib/types/logger/logger.d.ts +1 -1
- package/lib/utils/common/common.js +8 -3
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +2 -0
- package/lib/utils/tools/exec.d.ts +5 -17
- package/lib/utils/tools/exec.js +28 -26
- package/lib/utils/tools/ffmpeg.d.ts +2 -2
- package/lib/utils/tools/restart.d.ts +15 -0
- package/lib/utils/tools/restart.js +39 -0
- package/lib/utils/tools/stop.d.ts +7 -0
- package/lib/utils/tools/stop.js +13 -0
- package/lib/utils/tools/update.d.ts +26 -84
- package/lib/utils/tools/update.js +40 -28
- package/package.json +9 -3
- package/lib/render/client_even.d.ts +0 -30
- package/lib/render/client_even.js +0 -156
package/lib/utils/tools/exec.js
CHANGED
|
@@ -6,41 +6,43 @@ import { exec as execCmd } from 'child_process';
|
|
|
6
6
|
* @param log 是否打印日志
|
|
7
7
|
* @param options 选项
|
|
8
8
|
*/
|
|
9
|
-
export const exec = (cmd, log = true, options = { cwd: process.cwd()
|
|
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');
|
|
10
32
|
return new Promise(resolve => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const logType = (status) => {
|
|
16
|
-
switch (status) {
|
|
17
|
-
case '开始执行':
|
|
18
|
-
return logger.yellow('[exec][开始执行]');
|
|
19
|
-
case 'ok':
|
|
20
|
-
return logger.green('[exec][执行成功]');
|
|
21
|
-
case 'failed':
|
|
22
|
-
return logger.red('[exec][执行失败]');
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
const formatMessage = (status, details) => [logType(status), `cmd: ${cmd}`, `cwd: ${options.cwd || process.cwd()}`, details, '--------'].join('\n');
|
|
26
|
-
logMessage('info', formatMessage('开始执行', ''));
|
|
27
|
-
execCmd(cmd, options, (error, stdout, stderr) => {
|
|
28
|
-
if (error) {
|
|
29
|
-
logMessage('error', formatMessage('failed', `Error: ${error.message || error.stack || error.toString()}`));
|
|
30
|
-
return resolve({ status: 'failed', error, stdout, stderr });
|
|
31
|
-
}
|
|
32
|
-
logMessage('mark', formatMessage('ok', `stdout: ${stdout}\nstderr: ${stderr}`));
|
|
33
|
-
resolve({ status: 'ok', error, stdout, stderr });
|
|
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 });
|
|
34
37
|
});
|
|
35
38
|
});
|
|
36
39
|
};
|
|
37
40
|
/**
|
|
38
41
|
* 执行 shell 命令
|
|
39
42
|
* @param cmd 命令
|
|
40
|
-
* @param log 是否打印日志
|
|
41
43
|
* @param options 选项
|
|
42
44
|
*/
|
|
43
|
-
export const execs = (cmd, options = { cwd: process.cwd()
|
|
45
|
+
export const execs = (cmd, options = { cwd: process.cwd() }) => {
|
|
44
46
|
return new Promise((resolve, reject) => {
|
|
45
47
|
execCmd(cmd, options, (error, stdout) => {
|
|
46
48
|
if (error)
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
}>;
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
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,75 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const update: {
|
|
1
|
+
export declare class Updates {
|
|
3
2
|
dir: string;
|
|
3
|
+
constructor();
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
* @param time - 超时时间 默认120s
|
|
9
|
-
*/
|
|
10
|
-
update(path: string, cmd?: string, time?: number): Promise<{
|
|
11
|
-
status: string;
|
|
12
|
-
data: string;
|
|
13
|
-
}>;
|
|
14
|
-
/**
|
|
15
|
-
* 获取指定仓库最后一次提交时间日期
|
|
16
|
-
* @param path - 插件相对路径
|
|
17
|
-
*/
|
|
18
|
-
getTime(path: string): Promise<string>;
|
|
19
|
-
/**
|
|
20
|
-
* 获取指定仓库最后一次提交哈希值
|
|
21
|
-
* @param {string} path - 插件相对路径
|
|
22
|
-
* @param {boolean} [short] - 是否获取短哈希 默认true
|
|
23
|
-
* @returns {Promise<string>}
|
|
24
|
-
*/
|
|
25
|
-
getHash(path: string, short?: boolean): Promise<string>;
|
|
26
|
-
/**
|
|
27
|
-
* 获取指定仓库的提交记录
|
|
28
|
-
* @param {{
|
|
29
|
-
* path: string,
|
|
30
|
-
* count?: number,
|
|
31
|
-
* hash?: string
|
|
32
|
-
* }} options - 参数
|
|
33
|
-
* @param {string} options.path - 插件相对路径
|
|
34
|
-
* @param {number} [options.count] - 获取日志条数 默认1条
|
|
35
|
-
* @param {string} [options.hash] - 指定HEAD
|
|
36
|
-
* @returns {Promise<string>}
|
|
5
|
+
* 更新npm包
|
|
6
|
+
* @param name - 包名
|
|
7
|
+
* @param timeout - 超时时间 默认120s
|
|
37
8
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
count?: any;
|
|
41
|
-
hash?: any;
|
|
42
|
-
branch?: any;
|
|
43
|
-
}): Promise<string>;
|
|
44
|
-
/**
|
|
45
|
-
* 检查插件是否有更新
|
|
46
|
-
* @param {string} path - 插件相对路径
|
|
47
|
-
* @param {number} [time] - 超时时间 默认120s
|
|
48
|
-
* @returns {Promise<{status: 'ok'|'failed', data: string|boolean}>}
|
|
49
|
-
*/
|
|
50
|
-
checkUpdate(path: fs.PathLike, time?: number): Promise<{
|
|
51
|
-
status: string;
|
|
52
|
-
data: string | undefined;
|
|
53
|
-
count?: undefined;
|
|
54
|
-
} | {
|
|
55
|
-
status: string;
|
|
56
|
-
data: boolean;
|
|
57
|
-
count?: undefined;
|
|
58
|
-
} | {
|
|
59
|
-
status: string;
|
|
9
|
+
updatePkg(name: string, timeout?: number): Promise<{
|
|
10
|
+
status: boolean;
|
|
60
11
|
data: string;
|
|
61
|
-
count: string | number;
|
|
62
12
|
}>;
|
|
63
|
-
};
|
|
64
|
-
export declare const Update: {
|
|
65
|
-
dir: string;
|
|
66
13
|
/**
|
|
67
14
|
* 更新框架或插件
|
|
68
15
|
* @param path - 插件相对路径
|
|
69
16
|
* @param cmd - 更新命令 默认git pull
|
|
70
|
-
* @param
|
|
17
|
+
* @param timeout - 超时时间 默认120s
|
|
71
18
|
*/
|
|
72
|
-
update(path: string, cmd?: string,
|
|
19
|
+
update(path: string, cmd?: string, timeout?: number): Promise<{
|
|
73
20
|
status: string;
|
|
74
21
|
data: string;
|
|
75
22
|
}>;
|
|
@@ -80,36 +27,29 @@ export declare const Update: {
|
|
|
80
27
|
getTime(path: string): Promise<string>;
|
|
81
28
|
/**
|
|
82
29
|
* 获取指定仓库最后一次提交哈希值
|
|
83
|
-
* @param
|
|
84
|
-
* @param
|
|
85
|
-
* @returns {Promise<string>}
|
|
30
|
+
* @param path - 插件相对路径
|
|
31
|
+
* @param short - 是否获取短哈希 默认true
|
|
86
32
|
*/
|
|
87
33
|
getHash(path: string, short?: boolean): Promise<string>;
|
|
88
34
|
/**
|
|
89
35
|
* 获取指定仓库的提交记录
|
|
90
|
-
* @param {{
|
|
91
|
-
* path: string,
|
|
92
|
-
* count?: number,
|
|
93
|
-
* hash?: string
|
|
94
|
-
* }} options - 参数
|
|
95
|
-
* @param {string} options.path - 插件相对路径
|
|
96
|
-
* @param {number} [options.count] - 获取日志条数 默认1条
|
|
97
|
-
* @param {string} [options.hash] - 指定HEAD
|
|
98
|
-
* @returns {Promise<string>}
|
|
99
36
|
*/
|
|
100
37
|
getCommit(options: {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
38
|
+
/** 指令命令路径 */
|
|
39
|
+
path: string;
|
|
40
|
+
/** 获取几次提交 默认1次 */
|
|
41
|
+
count?: number;
|
|
42
|
+
/** 指定哈希 */
|
|
43
|
+
hash?: string;
|
|
44
|
+
/** 指定分支 */
|
|
45
|
+
branch?: string;
|
|
105
46
|
}): Promise<string>;
|
|
106
47
|
/**
|
|
107
48
|
* 检查插件是否有更新
|
|
108
|
-
* @param
|
|
109
|
-
* @param
|
|
110
|
-
* @returns {Promise<{status: 'ok'|'failed', data: string|boolean}>}
|
|
49
|
+
* @param path - 插件相对路径
|
|
50
|
+
* @param time - 超时时间 默认120s
|
|
111
51
|
*/
|
|
112
|
-
checkUpdate(path:
|
|
52
|
+
checkUpdate(path: string, time?: number): Promise<{
|
|
113
53
|
status: string;
|
|
114
54
|
data: string | undefined;
|
|
115
55
|
count?: undefined;
|
|
@@ -120,6 +60,8 @@ export declare const Update: {
|
|
|
120
60
|
} | {
|
|
121
61
|
status: string;
|
|
122
62
|
data: string;
|
|
123
|
-
count:
|
|
63
|
+
count: number;
|
|
124
64
|
}>;
|
|
125
|
-
}
|
|
65
|
+
}
|
|
66
|
+
export declare const update: Updates;
|
|
67
|
+
export declare const Update: Updates;
|
|
@@ -1,17 +1,39 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import exec from './exec.js';
|
|
3
|
-
|
|
3
|
+
import { KarinCli } from '../../cli/index.js';
|
|
4
|
+
import { getRegistry } from '../../cli/pkg.js';
|
|
5
|
+
export class Updates {
|
|
4
6
|
dir;
|
|
5
7
|
constructor() {
|
|
6
8
|
this.dir = './plugins';
|
|
7
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
|
+
}
|
|
8
30
|
/**
|
|
9
31
|
* 更新框架或插件
|
|
10
32
|
* @param path - 插件相对路径
|
|
11
33
|
* @param cmd - 更新命令 默认git pull
|
|
12
|
-
* @param
|
|
34
|
+
* @param timeout - 超时时间 默认120s
|
|
13
35
|
*/
|
|
14
|
-
async update(path, cmd = 'git pull',
|
|
36
|
+
async update(path, cmd = 'git pull', timeout = 120) {
|
|
15
37
|
/** 检查一下路径是否存在 */
|
|
16
38
|
if (!fs.existsSync(path))
|
|
17
39
|
return { status: 'failed', data: '路径不存在' };
|
|
@@ -21,7 +43,7 @@ export const update = new (class Update {
|
|
|
21
43
|
/** 设置超时时间 */
|
|
22
44
|
const timer = setTimeout(() => {
|
|
23
45
|
return { status: 'failed', data: '执行超时' };
|
|
24
|
-
},
|
|
46
|
+
}, timeout * 1000);
|
|
25
47
|
const options = { env: process.env, cwd: path, encoding: 'utf-8' };
|
|
26
48
|
/** 记录当前短哈希 */
|
|
27
49
|
const hash = await this.getHash(path);
|
|
@@ -36,17 +58,17 @@ export const update = new (class Update {
|
|
|
36
58
|
const time = await this.getTime(path);
|
|
37
59
|
return {
|
|
38
60
|
status: 'ok',
|
|
39
|
-
data:
|
|
61
|
+
data: `\n当前版本已是最新版本\n最后更新时间:${time}\n更新详情:${await this.getCommit({ path, count: 1 })}`,
|
|
40
62
|
};
|
|
41
63
|
}
|
|
42
64
|
const Commit = await this.getCommit({ path, hash });
|
|
43
65
|
return {
|
|
44
66
|
status: 'ok',
|
|
45
|
-
data:
|
|
67
|
+
data: `\n更新成功\n更新日志:\n${Commit}`,
|
|
46
68
|
};
|
|
47
69
|
}
|
|
48
|
-
const
|
|
49
|
-
return { status: 'failed', data
|
|
70
|
+
const data = `\n更新失败\n错误信息:${error?.stack?.toString() || error?.message?.toString() || error?.toString() || error}\n请解决错误后重试或执行【#强制更新】`;
|
|
71
|
+
return { status: 'failed', data };
|
|
50
72
|
}
|
|
51
73
|
/**
|
|
52
74
|
* 获取指定仓库最后一次提交时间日期
|
|
@@ -54,20 +76,19 @@ export const update = new (class Update {
|
|
|
54
76
|
*/
|
|
55
77
|
async getTime(path) {
|
|
56
78
|
const cmd = 'git log -1 --format=%cd --date=format:"%Y-%m-%d %H:%M:%S"';
|
|
57
|
-
const data = await exec(cmd, false, { cwd: path
|
|
79
|
+
const data = await exec(cmd, false, { cwd: path });
|
|
58
80
|
if (data.status === 'failed')
|
|
59
81
|
return '获取时间失败,请重试或更新Git~';
|
|
60
82
|
return data.stdout.trim();
|
|
61
83
|
}
|
|
62
84
|
/**
|
|
63
85
|
* 获取指定仓库最后一次提交哈希值
|
|
64
|
-
* @param
|
|
65
|
-
* @param
|
|
66
|
-
* @returns {Promise<string>}
|
|
86
|
+
* @param path - 插件相对路径
|
|
87
|
+
* @param short - 是否获取短哈希 默认true
|
|
67
88
|
*/
|
|
68
89
|
async getHash(path, short = true) {
|
|
69
90
|
const cmd = short ? 'git rev-parse --short HEAD' : 'git rev-parse HEAD';
|
|
70
|
-
const data = await exec(cmd, false, { cwd: path
|
|
91
|
+
const data = await exec(cmd, false, { cwd: path });
|
|
71
92
|
if (data.status === 'failed') {
|
|
72
93
|
const text = data.error;
|
|
73
94
|
throw new Error(text);
|
|
@@ -76,15 +97,6 @@ export const update = new (class Update {
|
|
|
76
97
|
}
|
|
77
98
|
/**
|
|
78
99
|
* 获取指定仓库的提交记录
|
|
79
|
-
* @param {{
|
|
80
|
-
* path: string,
|
|
81
|
-
* count?: number,
|
|
82
|
-
* hash?: string
|
|
83
|
-
* }} options - 参数
|
|
84
|
-
* @param {string} options.path - 插件相对路径
|
|
85
|
-
* @param {number} [options.count] - 获取日志条数 默认1条
|
|
86
|
-
* @param {string} [options.hash] - 指定HEAD
|
|
87
|
-
* @returns {Promise<string>}
|
|
88
100
|
*/
|
|
89
101
|
async getCommit(options) {
|
|
90
102
|
const { path, count = 1, hash, branch } = options;
|
|
@@ -95,7 +107,7 @@ export const update = new (class Update {
|
|
|
95
107
|
/** 指定分支 */
|
|
96
108
|
if (branch)
|
|
97
109
|
cmd = `git log -${count} ${branch} --format="[%ad] %s %n" --date="format:%m-%d %H:%M"`;
|
|
98
|
-
const data = await exec(cmd, false, { cwd: path
|
|
110
|
+
const data = await exec(cmd, false, { cwd: path });
|
|
99
111
|
if (data.status === 'failed') {
|
|
100
112
|
const text = data.error;
|
|
101
113
|
throw new Error(text);
|
|
@@ -104,9 +116,8 @@ export const update = new (class Update {
|
|
|
104
116
|
}
|
|
105
117
|
/**
|
|
106
118
|
* 检查插件是否有更新
|
|
107
|
-
* @param
|
|
108
|
-
* @param
|
|
109
|
-
* @returns {Promise<{status: 'ok'|'failed', data: string|boolean}>}
|
|
119
|
+
* @param path - 插件相对路径
|
|
120
|
+
* @param time - 超时时间 默认120s
|
|
110
121
|
*/
|
|
111
122
|
async checkUpdate(path, time = 120) {
|
|
112
123
|
/** 检查一下路径是否存在 */
|
|
@@ -131,9 +142,10 @@ export const update = new (class Update {
|
|
|
131
142
|
if (stdout.includes('Your branch is up to date with'))
|
|
132
143
|
return { status: 'ok', data: false };
|
|
133
144
|
/** 获取落后几次更新 */
|
|
134
|
-
const count = stdout.match(/Your branch is behind '.*' by (\d+) commits/)?.[1] || 1;
|
|
145
|
+
const count = Number(stdout.match(/Your branch is behind '.*' by (\d+) commits/)?.[1]) || 1;
|
|
135
146
|
const data = await this.getCommit({ path, count, branch: 'origin' });
|
|
136
147
|
return { status: 'ok', data, count };
|
|
137
148
|
}
|
|
138
|
-
}
|
|
149
|
+
}
|
|
150
|
+
export const update = new Updates();
|
|
139
151
|
export const Update = update;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-karin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "基于 Kritor 进行开发的nodejs机器人框架",
|
|
6
6
|
"homepage": "https://github.com/KarinJS/Karin",
|
|
@@ -133,7 +133,7 @@
|
|
|
133
133
|
],
|
|
134
134
|
"scripts": {
|
|
135
135
|
".": "node lib/cli/start.js .",
|
|
136
|
-
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
|
|
136
|
+
"build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json && npm run fix:all",
|
|
137
137
|
"build:npm": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
|
|
138
138
|
"dev": "node lib/cli/start.js dev",
|
|
139
139
|
"fix": "eslint lib/**/*.js --fix",
|
|
@@ -143,12 +143,18 @@
|
|
|
143
143
|
"start": "node lib/cli/start.js start"
|
|
144
144
|
},
|
|
145
145
|
"dependencies": {
|
|
146
|
+
"@types/express": "^4.17.21",
|
|
147
|
+
"@types/lodash": "^4.17.7",
|
|
148
|
+
"@types/node": "^22.5.0",
|
|
149
|
+
"@types/node-schedule": "^2.1.7",
|
|
150
|
+
"@types/ws": "^8.5.12",
|
|
146
151
|
"art-template": "4.13.2",
|
|
147
|
-
"axios": "1.7.
|
|
152
|
+
"axios": "1.7.5",
|
|
148
153
|
"chalk": "5.3.0",
|
|
149
154
|
"chokidar": "3.6.0",
|
|
150
155
|
"commander": "^12.1.0",
|
|
151
156
|
"express": "4.19.2",
|
|
157
|
+
"jimp": "^0.22.12",
|
|
152
158
|
"level": "8.0.1",
|
|
153
159
|
"lodash": "4.17.21",
|
|
154
160
|
"log4js": "6.9.1",
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import WebSocket from 'ws';
|
|
2
|
-
import { RenderBase } from './base.js';
|
|
3
|
-
import { KarinRenderType, RenderResult } from '../types/index.js';
|
|
4
|
-
export declare class RenderClientEven extends RenderBase {
|
|
5
|
-
url: string;
|
|
6
|
-
type: string;
|
|
7
|
-
id: string;
|
|
8
|
-
index: number;
|
|
9
|
-
retry: number;
|
|
10
|
-
reg: RegExp;
|
|
11
|
-
ws: WebSocket;
|
|
12
|
-
constructor(url: string);
|
|
13
|
-
/**
|
|
14
|
-
* 初始化
|
|
15
|
-
*/
|
|
16
|
-
start(): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
* 创建连接
|
|
19
|
-
*/
|
|
20
|
-
link(): Promise<void>;
|
|
21
|
-
/**
|
|
22
|
-
* 接受消息
|
|
23
|
-
*/
|
|
24
|
-
message(str: string): Promise<void>;
|
|
25
|
-
/**
|
|
26
|
-
* 渲染标准方法
|
|
27
|
-
* @param options 渲染参数
|
|
28
|
-
*/
|
|
29
|
-
render<T extends KarinRenderType>(options: T): Promise<RenderResult<T>>;
|
|
30
|
-
}
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import axios from 'axios';
|
|
3
|
-
import WebSocket from 'ws';
|
|
4
|
-
import { render } from './app.js';
|
|
5
|
-
import { RenderBase } from './base.js';
|
|
6
|
-
import { createHash, randomUUID } from 'crypto';
|
|
7
|
-
import { listener } from '../core/index.js';
|
|
8
|
-
import { logger } from '../utils/index.js';
|
|
9
|
-
export class RenderClientEven extends RenderBase {
|
|
10
|
-
url;
|
|
11
|
-
type;
|
|
12
|
-
id;
|
|
13
|
-
index;
|
|
14
|
-
retry;
|
|
15
|
-
reg;
|
|
16
|
-
ws;
|
|
17
|
-
constructor(url) {
|
|
18
|
-
super();
|
|
19
|
-
this.url = url;
|
|
20
|
-
this.type = 'image';
|
|
21
|
-
this.id = 'puppeteer';
|
|
22
|
-
this.index = 0;
|
|
23
|
-
this.retry = 0;
|
|
24
|
-
this.reg = new RegExp(`(${process.cwd().replace(/\\/g, '\\\\')}|${process.cwd().replace(/\\/g, '/')})`, 'g');
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* 初始化
|
|
28
|
-
*/
|
|
29
|
-
async start() {
|
|
30
|
-
try {
|
|
31
|
-
const response = await axios.head(this.url);
|
|
32
|
-
if (response.status === 200) {
|
|
33
|
-
logger.mark(`[渲染器:${this.id}][WebSocket] 注册渲染器:${logger.green(this.url)}`);
|
|
34
|
-
try {
|
|
35
|
-
this.index = render.app({ id: this.id, type: this.type, render: this.render.bind(this) });
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
logger.error(`[渲染器:${this.id}] 注册渲染器失败:`, error);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
logger.error(`[渲染器:${this.id}] 注册渲染器失败:渲染器发生错误`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
logger.error(`[渲染器:${this.id}] 注册渲染器失败:`, error);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* 创建连接
|
|
51
|
-
*/
|
|
52
|
-
async link() {
|
|
53
|
-
return new Promise((resolve, reject) => {
|
|
54
|
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
55
|
-
return resolve();
|
|
56
|
-
}
|
|
57
|
-
/** 连接ws */
|
|
58
|
-
this.ws = new WebSocket(this.url);
|
|
59
|
-
/** 建立连接 */
|
|
60
|
-
this.ws.on('open', () => {
|
|
61
|
-
logger.mark(`[渲染器:${this.id}][WebSocket] 建立连接:${logger.green(this.url)}`);
|
|
62
|
-
/** 监听消息 */
|
|
63
|
-
this.ws.on('message', data => this.message(data.toString()));
|
|
64
|
-
resolve();
|
|
65
|
-
});
|
|
66
|
-
/** 监听断开 */
|
|
67
|
-
this.ws.once('close', async () => {
|
|
68
|
-
/** 停止监听 */
|
|
69
|
-
this.ws.removeAllListeners();
|
|
70
|
-
});
|
|
71
|
-
/** 监听错误 */
|
|
72
|
-
this.ws.on('error', async (e) => {
|
|
73
|
-
this.ws.close();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* 接受消息
|
|
79
|
-
*/
|
|
80
|
-
async message(str) {
|
|
81
|
-
const data = JSON.parse(str);
|
|
82
|
-
switch (data.action) {
|
|
83
|
-
/** 静态文件 */
|
|
84
|
-
case 'static': {
|
|
85
|
-
const filePath = decodeURIComponent(data.params.file);
|
|
86
|
-
logger.debug(`[渲染器:${this.id}][正向WS] 访问静态文件:${filePath}`);
|
|
87
|
-
const file = fs.readFileSync('.' + filePath);
|
|
88
|
-
const md5 = createHash('md5').update(file).digest('hex');
|
|
89
|
-
const params = data.params.md5?.includes(md5)
|
|
90
|
-
? { echo: data.echo, action: 'static', status: 'ok', data: { verifiedMd5: md5 } }
|
|
91
|
-
: { echo: data.echo, action: 'static', status: 'ok', data: { file } };
|
|
92
|
-
return this.ws.send(JSON.stringify(params));
|
|
93
|
-
}
|
|
94
|
-
/** 渲染结果 */
|
|
95
|
-
case 'renderRes': {
|
|
96
|
-
listener.emit(data.echo, data);
|
|
97
|
-
break;
|
|
98
|
-
}
|
|
99
|
-
/** 超时 */
|
|
100
|
-
case 'timeout': {
|
|
101
|
-
logger.debug(`[渲染器:${this.id}][正向WS] 处理超时`);
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
/** 未知数据 */
|
|
105
|
-
default: {
|
|
106
|
-
logger.warn(`[渲染器:${this.id}] 收到未知数据:`, data);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* 渲染标准方法
|
|
112
|
-
* @param options 渲染参数
|
|
113
|
-
*/
|
|
114
|
-
async render(options) {
|
|
115
|
-
/** 渲染模板 */
|
|
116
|
-
let file = options.file;
|
|
117
|
-
let action = 'renderHtml';
|
|
118
|
-
if (options.file.includes('http') || options.vue) {
|
|
119
|
-
action = 'render';
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
file = this.dealTpl(options);
|
|
123
|
-
/** 判断是本地karin-puppeteer还是远程 */
|
|
124
|
-
if (!/127\.0\.0\.1|localhost/.test(this.url)) {
|
|
125
|
-
file = fs.readFileSync(file, 'utf-8').replace(this.reg, '');
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
action = 'render';
|
|
129
|
-
file = 'file://' + file;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (!file) {
|
|
133
|
-
logger.error(`[渲染器:${this.id}:${this.index}] 渲染文件不存在:${options.file}`);
|
|
134
|
-
return '';
|
|
135
|
-
}
|
|
136
|
-
/** 编码 */
|
|
137
|
-
file = encodeURIComponent(file);
|
|
138
|
-
const data = options;
|
|
139
|
-
const echo = randomUUID();
|
|
140
|
-
/** 移除掉模板参数 */
|
|
141
|
-
if (data.data)
|
|
142
|
-
delete data.data;
|
|
143
|
-
data.file = file;
|
|
144
|
-
const req = JSON.stringify({ echo, action, data });
|
|
145
|
-
logger.debug(`[渲染器:${this.id}:${this.index}][正向WS] 请求:${this.url} \nhtml: ${options.file} \ndata: ${JSON.stringify(data)}`);
|
|
146
|
-
await this.link();
|
|
147
|
-
this.ws.send(req);
|
|
148
|
-
return new Promise((resolve, reject) => {
|
|
149
|
-
listener.once(echo, (data) => {
|
|
150
|
-
if (data.ok)
|
|
151
|
-
return resolve(data.data);
|
|
152
|
-
reject(new Error(JSON.stringify(data)));
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
}
|