node-karin 0.11.13 → 0.11.15
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/config/view/config.yaml +5 -5
- package/lib/adapter/index.js +2 -2
- package/lib/cli/index.d.ts +7 -3
- package/lib/cli/index.js +258 -233
- package/lib/cli/init.js +15 -14
- package/lib/cli/karin.js +15 -15
- package/lib/cli/pkg.d.ts +4 -0
- package/lib/cli/pkg.js +14 -0
- package/lib/cli/start.js +8 -0
- package/lib/core/index.js +10 -10
- package/lib/core/init/config.d.ts +0 -4
- package/lib/core/init/config.js +0 -13
- package/lib/core/init/init.js +1 -0
- package/lib/core/listener/listener.js +1 -1
- package/lib/core/process/process.js +2 -6
- package/lib/core/server/server.js +13 -1
- package/lib/db/index.js +2 -2
- package/lib/db/level/level.js +0 -1
- package/lib/db/redis/redis_level.d.ts +2 -0
- package/lib/db/redis/redis_level.js +12 -11
- package/lib/event/index.js +5 -5
- package/lib/modules/art-template.js +1 -1
- package/lib/modules/axios.js +2 -2
- package/lib/modules/chalk.js +2 -2
- package/lib/modules/chokidar.js +2 -2
- package/lib/modules/commander.js +2 -2
- package/lib/modules/express.js +3 -3
- package/lib/modules/level.js +2 -2
- package/lib/modules/lodash.js +1 -1
- package/lib/modules/log4js.js +2 -2
- package/lib/modules/moment.js +1 -1
- package/lib/modules/node-schedule.js +2 -2
- package/lib/modules/redis.js +2 -2
- package/lib/modules/ws.js +3 -3
- package/lib/modules/yaml.js +2 -2
- package/lib/render/app.js +82 -81
- package/lib/render/base.js +54 -54
- package/lib/render/client.js +144 -142
- package/lib/render/client_even.js +140 -137
- package/lib/render/http.js +40 -41
- package/lib/render/index.js +6 -6
- package/lib/render/server.js +93 -88
- package/lib/render/wormhole.js +153 -151
- package/lib/types/config/config.d.ts +2 -2
- package/lib/types/index.js +13 -13
- package/lib/types/type/global.d.ts +2 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +13 -11
- 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 +6 -3
- package/lib/cli/dev.js +0 -3
- /package/lib/cli/{dev.d.ts → start.d.ts} +0 -0
|
@@ -11,12 +11,12 @@ log4jsCfg:
|
|
|
11
11
|
# 日志文件最大大小 MB
|
|
12
12
|
maxLogSize: 30
|
|
13
13
|
|
|
14
|
-
# 关闭后台进程失败后是否继续启动 继续启动会导致多进程
|
|
15
|
-
multi_progress: false
|
|
16
|
-
|
|
17
14
|
# 控制台触发插件日志颜色 十六进制 默认#FFFF00 不支持热更新
|
|
18
15
|
log_color: "#E1D919"
|
|
19
16
|
|
|
17
|
+
# 重启是否调用pm2 如果不调用则会直接关机 此配置适合有进程守护的程序
|
|
18
|
+
pm2Restart: true
|
|
19
|
+
|
|
20
20
|
# 私聊设置
|
|
21
21
|
private:
|
|
22
22
|
# 关闭私聊后回复的提示词 为空则不回复
|
package/config/view/config.yaml
CHANGED
|
@@ -103,13 +103,13 @@ view:
|
|
|
103
103
|
comment: 主人列表 主权限
|
|
104
104
|
path: master
|
|
105
105
|
type: array
|
|
106
|
-
arrayType:
|
|
106
|
+
arrayType: string
|
|
107
107
|
|
|
108
108
|
- key: 管理员列表
|
|
109
109
|
comment: 管理员列表 子权限
|
|
110
110
|
path: admin
|
|
111
111
|
type: array
|
|
112
|
-
arrayType:
|
|
112
|
+
arrayType: string
|
|
113
113
|
|
|
114
114
|
- key: 黑名单
|
|
115
115
|
comment: 黑名单相关
|
|
@@ -119,7 +119,7 @@ view:
|
|
|
119
119
|
- key: 黑名单用户
|
|
120
120
|
path: BlackList.users
|
|
121
121
|
type: array
|
|
122
|
-
arrayType:
|
|
122
|
+
arrayType: string
|
|
123
123
|
associated:
|
|
124
124
|
- file: App
|
|
125
125
|
path: BlackList.users
|
|
@@ -127,7 +127,7 @@ view:
|
|
|
127
127
|
- key: 黑名单群聊
|
|
128
128
|
path: BlackList.groups
|
|
129
129
|
type: array
|
|
130
|
-
arrayType:
|
|
130
|
+
arrayType: string
|
|
131
131
|
associated:
|
|
132
132
|
- file: App
|
|
133
133
|
path: BlackList.groups
|
|
@@ -136,7 +136,7 @@ view:
|
|
|
136
136
|
comment: 设置后不会打印该群的消息日志
|
|
137
137
|
path: BlackList.GroupMsgLog
|
|
138
138
|
type: array
|
|
139
|
-
arrayType:
|
|
139
|
+
arrayType: string
|
|
140
140
|
associated:
|
|
141
141
|
- file: App
|
|
142
142
|
path: BlackList.GroupMsgLog
|
package/lib/adapter/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './input/index.js'
|
|
2
|
-
export * from './onebot/11/index.js'
|
|
1
|
+
export * from './input/index.js';
|
|
2
|
+
export * from './onebot/11/index.js';
|
package/lib/cli/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ChildProcess } from 'child_process';
|
|
1
|
+
import { ChildProcess, ExecOptions } from 'child_process';
|
|
2
2
|
export declare const enum Runner {
|
|
3
3
|
Node = "node",
|
|
4
4
|
Tsx = "tsx",
|
|
@@ -19,7 +19,7 @@ export declare class KarinCli {
|
|
|
19
19
|
file: string;
|
|
20
20
|
constructor();
|
|
21
21
|
/**
|
|
22
|
-
* 获取pkg
|
|
22
|
+
* 获取pkg配置
|
|
23
23
|
* @param isNpm - 是否是npm包
|
|
24
24
|
*/
|
|
25
25
|
pkg(isNpm: boolean): any;
|
|
@@ -73,11 +73,15 @@ export declare class KarinCli {
|
|
|
73
73
|
* @param pkg - 包管理器
|
|
74
74
|
*/
|
|
75
75
|
getRemoteVersion(name: string, pkg: 'pnpm' | 'cnpm' | 'yarn' | 'npm'): Promise<string>;
|
|
76
|
+
/**
|
|
77
|
+
* 获取git插件列表
|
|
78
|
+
*/
|
|
79
|
+
getGitPlugins(): Array<string>;
|
|
76
80
|
/**
|
|
77
81
|
* 封装exec
|
|
78
82
|
* @param cmd - 命令
|
|
79
83
|
*/
|
|
80
|
-
exec(cmd: string): Promise<string>;
|
|
84
|
+
exec(cmd: string, options?: ExecOptions): Promise<string>;
|
|
81
85
|
/**
|
|
82
86
|
* 封装axios 超时返回false
|
|
83
87
|
* @param url - 请求地址
|
package/lib/cli/index.js
CHANGED
|
@@ -1,282 +1,307 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import yaml from 'yaml'
|
|
4
|
-
import axios from 'axios'
|
|
5
|
-
import { fileURLToPath } from 'url'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import yaml from 'yaml';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { getRegistry } from './pkg.js';
|
|
7
|
+
import { exec as execCmd, spawn } from 'child_process';
|
|
8
8
|
export class KarinCli {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
9
|
+
child;
|
|
10
|
+
filename;
|
|
11
|
+
karinDir;
|
|
12
|
+
file;
|
|
13
|
+
constructor() {
|
|
14
|
+
process.env.karin_app_start_count = '0';
|
|
15
|
+
process.env.karin_app_watch = 'no';
|
|
16
|
+
/** 当前文件绝对路径 */
|
|
17
|
+
this.filename = fileURLToPath(import.meta.url);
|
|
18
|
+
/** karin目录 */
|
|
19
|
+
this.karinDir = path.join(path.dirname(this.filename), '../..');
|
|
20
|
+
/** 入口文件(注意后缀) */
|
|
21
|
+
this.file = path.join(path.dirname(this.filename), '../index.js');
|
|
22
|
+
this.child = null;
|
|
23
|
+
process.env.karin_app_pkg = getRegistry();
|
|
24
|
+
process.env.karin_app_version = this.pkg(true).version;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 获取pkg配置
|
|
27
28
|
* @param isNpm - 是否是npm包
|
|
28
29
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
/**
|
|
30
|
+
pkg(isNpm) {
|
|
31
|
+
const filePath = isNpm ? path.join(this.karinDir, 'package.json') : path.join(process.cwd(), 'package.json');
|
|
32
|
+
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
36
|
* 获取配置文件路径
|
|
37
37
|
* @param name - 配置文件名
|
|
38
38
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
getConfigPath(name) {
|
|
40
|
+
const filePath = `./config/config/${name}.yaml`;
|
|
41
|
+
if (!fs.existsSync(filePath))
|
|
42
|
+
return `${this.karinDir}/config/config/${name}.yaml`;
|
|
43
|
+
return filePath;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
46
|
* 获取pm2配置
|
|
47
47
|
* @param name - 配置文件名
|
|
48
48
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
49
|
+
getConfigData(name) {
|
|
50
|
+
const _path = this.getConfigPath(name);
|
|
51
|
+
const data = yaml.parse(fs.readFileSync(_path, 'utf-8'));
|
|
52
|
+
return data;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
56
55
|
* 启动
|
|
57
56
|
* @param mode - 模式
|
|
58
57
|
* @param lang - 语言环境
|
|
59
58
|
* @param runner - 运行器
|
|
60
59
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
60
|
+
start(mode, lang, runner) {
|
|
61
|
+
process.env.karin_app_mode = mode;
|
|
62
|
+
process.env.karin_app_lang = lang;
|
|
63
|
+
process.env.karin_app_runner = runner;
|
|
64
|
+
let cmd;
|
|
65
|
+
switch (runner) {
|
|
66
|
+
case "node" /* Runner.Node */:
|
|
67
|
+
cmd = [this.file];
|
|
68
|
+
break;
|
|
69
|
+
case "tsx" /* Runner.Tsx */:
|
|
70
|
+
cmd = [this.file];
|
|
71
|
+
break;
|
|
72
|
+
case "pm2" /* Runner.Pm2 */: {
|
|
73
|
+
this.pm2();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** 启动 */
|
|
78
|
+
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === "tsx" /* Runner.Tsx */ });
|
|
79
|
+
/** 监听退出 */
|
|
80
|
+
this.child.once('exit', (code) => process.exit(code));
|
|
81
|
+
/** 监听子进程消息 */
|
|
82
|
+
this.child.on('message', (data) => {
|
|
83
|
+
/** pm2重启 */
|
|
84
|
+
if (data.env.pm_id)
|
|
85
|
+
return this.restart();
|
|
86
|
+
try {
|
|
87
|
+
/** 先结束进程 */
|
|
88
|
+
this.child.kill('SIGINT');
|
|
89
|
+
/** 停止监听 */
|
|
90
|
+
this.child.removeAllListeners();
|
|
91
|
+
/** 重启次数+1 */
|
|
92
|
+
const count = Number(process.env.karin_app_start_count) || 0;
|
|
93
|
+
process.env.karin_app_start_count = String(count + 1);
|
|
94
|
+
}
|
|
95
|
+
catch { }
|
|
96
|
+
return this.start(mode, lang, runner);
|
|
97
|
+
});
|
|
77
98
|
}
|
|
78
|
-
/**
|
|
79
|
-
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === 'tsx' /* Runner.Tsx */ })
|
|
80
|
-
/** 监听退出 */
|
|
81
|
-
this.child.once('exit', (code) => process.exit(code))
|
|
82
|
-
/** 监听子进程消息 */
|
|
83
|
-
this.child.on('message', (data) => {
|
|
84
|
-
/** pm2重启 */
|
|
85
|
-
if (data.env.pm_id) { return this.restart() }
|
|
86
|
-
try {
|
|
87
|
-
/** 先结束进程 */
|
|
88
|
-
this.child.kill('SIGINT')
|
|
89
|
-
/** 停止监听 */
|
|
90
|
-
this.child.removeAllListeners()
|
|
91
|
-
/** 重启次数+1 */
|
|
92
|
-
const count = Number(process.env.karin_app_start_count) || 0
|
|
93
|
-
process.env.karin_app_start_count = String(count + 1)
|
|
94
|
-
} catch { }
|
|
95
|
-
return this.start(mode, lang, runner)
|
|
96
|
-
})
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
99
|
+
/**
|
|
100
100
|
* pm2重启
|
|
101
101
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
async restart() {
|
|
103
|
+
const pm2Cfg = this.getConfigData('pm2');
|
|
104
|
+
const serverCfg = this.getConfigData('server');
|
|
105
|
+
/** 尝试获取pm2的进程id */
|
|
106
|
+
const port = serverCfg?.http?.port || 7000;
|
|
107
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000);
|
|
108
|
+
if (res) {
|
|
109
|
+
await this.exec(`pm2 restart ${res.pm2_id}`);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
await this.exec(`pm2 restart ${pm2Cfg?.apps[0]?.name || 'Karin'}`);
|
|
113
|
+
}
|
|
114
|
+
console.log('pm2服务已重启');
|
|
115
|
+
process.exit(0);
|
|
112
116
|
}
|
|
113
|
-
|
|
114
|
-
process.exit(0)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
117
|
+
/**
|
|
118
118
|
* pm2启动
|
|
119
119
|
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
120
|
+
async pm2() {
|
|
121
|
+
console.log('pm2启动中...');
|
|
122
|
+
const filePath = this.getConfigPath('pm2');
|
|
123
|
+
const data = this.getConfigData('pm2');
|
|
124
|
+
/** 修正入口文件路径 兼容0.6.28以前的版本 */
|
|
125
|
+
if (!fs.existsSync('./src') && filePath === './config/config/pm2.yaml') {
|
|
126
|
+
const script = './node_modules/node-karin/lib/index.js';
|
|
127
|
+
if (data.apps[0].script !== script) {
|
|
128
|
+
data.apps[0].script = script;
|
|
129
|
+
fs.writeFileSync(filePath, yaml.stringify(data));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const cmd = `pm2 start ${filePath} --env ${JSON.stringify(process.env)}`;
|
|
133
|
+
await this.exec(cmd);
|
|
134
|
+
console.log('pm2服务已启动 可执行 【npx karin log】 查看日志');
|
|
135
|
+
process.exit(0);
|
|
131
136
|
}
|
|
132
|
-
|
|
133
|
-
await this.exec(cmd)
|
|
134
|
-
console.log('pm2服务已启动 可执行 【npx karin log】 查看日志')
|
|
135
|
-
process.exit(0)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
137
|
+
/**
|
|
139
138
|
* pm2结束进程
|
|
140
139
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
140
|
+
async stop() {
|
|
141
|
+
console.log('pm2服务停止中...');
|
|
142
|
+
const pm2Cfg = this.getConfigData('pm2');
|
|
143
|
+
const serverCfg = this.getConfigData('server');
|
|
144
|
+
const port = serverCfg?.http?.port || 7000;
|
|
145
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000);
|
|
146
|
+
if (res) {
|
|
147
|
+
await this.exec(`pm2 delete ${res.pm2_id}`);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
await this.exec(`pm2 delete ${pm2Cfg?.apps[0]?.name || 'Karin'}`);
|
|
151
|
+
}
|
|
152
|
+
console.log('pm2服务已停止');
|
|
153
|
+
process.exit(0);
|
|
151
154
|
}
|
|
152
|
-
|
|
153
|
-
process.exit(0)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
155
|
+
/**
|
|
157
156
|
* pm2查看日志
|
|
158
157
|
*/
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
/**
|
|
158
|
+
async log() {
|
|
159
|
+
const pm2Cfg = this.getConfigData('pm2');
|
|
160
|
+
const serverCfg = this.getConfigData('server');
|
|
161
|
+
const port = serverCfg?.http?.port || 7000;
|
|
162
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000);
|
|
163
|
+
const lines = pm2Cfg?.lines || 1000;
|
|
164
|
+
const cmd = process.platform === 'win32' ? 'pm2.cmd' : 'pm2';
|
|
165
|
+
const type = res ? res.pm_id : pm2Cfg?.apps[0]?.name || 'Karin';
|
|
166
|
+
spawn(cmd, ['logs', type, '--lines', lines], { stdio: 'inherit', shell: true, cwd: process.cwd() });
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
171
169
|
* 更新依赖
|
|
172
170
|
*/
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
171
|
+
async update() {
|
|
172
|
+
/** 屏蔽的依赖包列表 */
|
|
173
|
+
const pkgdependencies = [
|
|
174
|
+
'art-template',
|
|
175
|
+
'axios',
|
|
176
|
+
'chalk',
|
|
177
|
+
'chokidar',
|
|
178
|
+
'commander',
|
|
179
|
+
'express',
|
|
180
|
+
'level',
|
|
181
|
+
'lodash',
|
|
182
|
+
'log4js',
|
|
183
|
+
'moment',
|
|
184
|
+
'node-schedule',
|
|
185
|
+
'redis',
|
|
186
|
+
'ws',
|
|
187
|
+
'yaml',
|
|
188
|
+
];
|
|
189
|
+
const list = Object.keys(this.pkg(false).dependencies).filter(key => !pkgdependencies.includes(key));
|
|
190
|
+
/** 获取包管理器 */
|
|
191
|
+
const pkg = getRegistry();
|
|
192
|
+
const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`;
|
|
193
|
+
await Promise.all(list.map(async (item) => {
|
|
194
|
+
try {
|
|
195
|
+
/** 检查是否已经是最新版本 */
|
|
196
|
+
const local = await this.getLocalVersion(item, pkg);
|
|
197
|
+
const remote = await this.getRemoteVersion(item, pkg);
|
|
198
|
+
if (local === remote) {
|
|
199
|
+
console.log(`[依赖更新] ${item} 已经是最新~`);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
console.log(`[依赖更新] ${item} 当前版本: ${local} 最新版本: ${remote}`);
|
|
203
|
+
await this.exec(`${cmd} ${item}@latest`);
|
|
204
|
+
console.log(`[依赖更新] ${item} 更新完成~`);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
console.error(`[依赖更新] ${item} 更新失败:`);
|
|
208
|
+
console.error(error.stack || error.message || error);
|
|
209
|
+
}
|
|
210
|
+
}));
|
|
211
|
+
console.log('[依赖更新] 所有npm依赖已更新完成~');
|
|
212
|
+
console.log('[依赖更新] 开始更新git插件...');
|
|
213
|
+
const gitList = this.getGitPlugins();
|
|
214
|
+
if (!gitList.length)
|
|
215
|
+
return console.log('[依赖更新] 没有git插件需要更新~');
|
|
216
|
+
await Promise.all(gitList.map(async (item) => {
|
|
217
|
+
const dir = path.resolve(process.cwd(), 'plugins', item);
|
|
218
|
+
try {
|
|
219
|
+
await this.exec('git pull', { cwd: dir });
|
|
220
|
+
console.log(`[依赖更新] ${item} 更新完成~`);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
console.error(`[依赖更新] ${item} 更新失败`);
|
|
224
|
+
console.error(error.stack || error.message || error);
|
|
225
|
+
}
|
|
226
|
+
}));
|
|
227
|
+
console.log('[依赖更新] 所有git插件已更新完成~');
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
218
230
|
* 获取指定包的本地版本
|
|
219
231
|
* @param name - 包名
|
|
220
232
|
* @param pkg - 包管理器
|
|
221
233
|
* @returns - 版本号
|
|
222
234
|
*/
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
235
|
+
async getLocalVersion(name, pkg) {
|
|
236
|
+
const cmd = pkg === 'yarn' ? `yarn list --pattern ${name}` : `${pkg} list ${name} --depth=0`;
|
|
237
|
+
const text = await this.exec(cmd);
|
|
238
|
+
/** pnpm特殊处理 */
|
|
239
|
+
if (pkg === 'pnpm') {
|
|
240
|
+
const reg = new RegExp(`${name}\\s+([\\d.]+)`, 'gm');
|
|
241
|
+
const res = reg.exec(text);
|
|
242
|
+
return res?.[1] || '0.0.0';
|
|
243
|
+
}
|
|
244
|
+
const reg = new RegExp(`${name}@(\\d+\\.\\d+\\.\\d+)`, 'gm');
|
|
245
|
+
const res = reg.exec(text);
|
|
246
|
+
return res?.[1] || '0.0.0';
|
|
231
247
|
}
|
|
232
|
-
|
|
233
|
-
const res = reg.exec(text)
|
|
234
|
-
return res?.[1] || '0.0.0'
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
248
|
+
/**
|
|
238
249
|
* 获取指定包的最新版本
|
|
239
250
|
* @param name - 包名
|
|
240
251
|
* @param pkg - 包管理器
|
|
241
252
|
*/
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
253
|
+
async getRemoteVersion(name, pkg) {
|
|
254
|
+
const cmd = `${pkg} info ${name} version`;
|
|
255
|
+
const text = await this.exec(cmd);
|
|
256
|
+
/** yarn特殊处理 */
|
|
257
|
+
if (pkg === 'yarn') {
|
|
258
|
+
const lines = text.split('\n').map(line => line.trim());
|
|
259
|
+
const ver = lines.find(line => /^\d+\.\d+\.\d+$/.test(line));
|
|
260
|
+
return ver || '';
|
|
261
|
+
}
|
|
262
|
+
return text.trim();
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 获取git插件列表
|
|
266
|
+
*/
|
|
267
|
+
getGitPlugins() {
|
|
268
|
+
const dir = path.resolve(process.cwd(), 'plugins');
|
|
269
|
+
let list = fs.readdirSync(dir, { withFileTypes: true });
|
|
270
|
+
/** 忽略非文件夹、非 karin-plugin-开头的文件夹 */
|
|
271
|
+
list = list.filter(v => v.isDirectory() && v.name.startsWith('karin-plugin-'));
|
|
272
|
+
list = list.filter(v => fs.existsSync(`${dir}/${v.name}/package.json`));
|
|
273
|
+
const arr = [];
|
|
274
|
+
list.map(v => arr.push(v.name));
|
|
275
|
+
return arr;
|
|
250
276
|
}
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
277
|
+
/**
|
|
255
278
|
* 封装exec
|
|
256
279
|
* @param cmd - 命令
|
|
257
280
|
*/
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
281
|
+
exec(cmd, options) {
|
|
282
|
+
return new Promise((resolve, reject) => {
|
|
283
|
+
execCmd(cmd, options, (error, stdout, stderr) => {
|
|
284
|
+
if (stdout)
|
|
285
|
+
return resolve(stdout.toString().trim());
|
|
286
|
+
if (error)
|
|
287
|
+
return reject(error);
|
|
288
|
+
return reject(stderr);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
269
293
|
* 封装axios 超时返回false
|
|
270
294
|
* @param url - 请求地址
|
|
271
295
|
* @param timeout - 超时时间
|
|
272
296
|
* @returns - 请求结果
|
|
273
297
|
*/
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
298
|
+
async Axios(url, timeout) {
|
|
299
|
+
try {
|
|
300
|
+
const res = await axios.get(url, { timeout });
|
|
301
|
+
return res.data;
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
280
306
|
}
|
|
281
|
-
}
|
|
282
307
|
}
|