node-karin 0.11.12 → 0.11.14
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/view/config.yaml +5 -5
- package/lib/adapter/index.js +2 -2
- package/lib/cli/index.js +229 -232
- package/lib/cli/init.js +14 -14
- package/lib/cli/karin.js +15 -15
- package/lib/cli/start.js +8 -0
- package/lib/core/index.js +10 -10
- package/lib/core/init/init.js +1 -0
- package/lib/core/plugin/loader.js +83 -77
- package/lib/core/process/process.js +1 -1
- 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/index.js +13 -13
- package/lib/types/type/global.d.ts +2 -0
- package/lib/utils/common/common.d.ts +8 -1
- package/lib/utils/common/common.js +11 -2
- package/lib/utils/index.js +11 -11
- package/package.json +6 -3
- package/lib/cli/dev.js +0 -3
- /package/lib/cli/{dev.d.ts → start.d.ts} +0 -0
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.js
CHANGED
|
@@ -1,282 +1,279 @@
|
|
|
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 { exec as execCmd, spawn } from 'child_process'
|
|
7
|
-
import { KarinCfgInit } from '../core/init/config.js'
|
|
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 { exec as execCmd, spawn } from 'child_process';
|
|
7
|
+
import { KarinCfgInit } from '../core/init/config.js';
|
|
8
8
|
export class KarinCli {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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_version = this.pkg(true).version;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
26
|
* 获取pkg
|
|
27
27
|
* @param isNpm - 是否是npm包
|
|
28
28
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
/**
|
|
29
|
+
pkg(isNpm) {
|
|
30
|
+
const filePath = isNpm ? path.join(this.karinDir, 'package.json') : path.join(process.cwd(), 'package.json');
|
|
31
|
+
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
36
35
|
* 获取配置文件路径
|
|
37
36
|
* @param name - 配置文件名
|
|
38
37
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
getConfigPath(name) {
|
|
39
|
+
const filePath = `./config/config/${name}.yaml`;
|
|
40
|
+
if (!fs.existsSync(filePath))
|
|
41
|
+
return `${this.karinDir}/config/config/${name}.yaml`;
|
|
42
|
+
return filePath;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
46
45
|
* 获取pm2配置
|
|
47
46
|
* @param name - 配置文件名
|
|
48
47
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
48
|
+
getConfigData(name) {
|
|
49
|
+
const _path = this.getConfigPath(name);
|
|
50
|
+
const data = yaml.parse(fs.readFileSync(_path, 'utf-8'));
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
56
54
|
* 启动
|
|
57
55
|
* @param mode - 模式
|
|
58
56
|
* @param lang - 语言环境
|
|
59
57
|
* @param runner - 运行器
|
|
60
58
|
*/
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
59
|
+
start(mode, lang, runner) {
|
|
60
|
+
process.env.karin_app_mode = mode;
|
|
61
|
+
process.env.karin_app_lang = lang;
|
|
62
|
+
process.env.karin_app_runner = runner;
|
|
63
|
+
let cmd;
|
|
64
|
+
switch (runner) {
|
|
65
|
+
case "node" /* Runner.Node */:
|
|
66
|
+
cmd = [this.file];
|
|
67
|
+
break;
|
|
68
|
+
case "tsx" /* Runner.Tsx */:
|
|
69
|
+
cmd = [this.file];
|
|
70
|
+
break;
|
|
71
|
+
case "pm2" /* Runner.Pm2 */: {
|
|
72
|
+
this.pm2();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** 启动 */
|
|
77
|
+
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === "tsx" /* Runner.Tsx */ });
|
|
78
|
+
/** 监听退出 */
|
|
79
|
+
this.child.once('exit', (code) => process.exit(code));
|
|
80
|
+
/** 监听子进程消息 */
|
|
81
|
+
this.child.on('message', (data) => {
|
|
82
|
+
/** pm2重启 */
|
|
83
|
+
if (data.env.pm_id)
|
|
84
|
+
return this.restart();
|
|
85
|
+
try {
|
|
86
|
+
/** 先结束进程 */
|
|
87
|
+
this.child.kill('SIGINT');
|
|
88
|
+
/** 停止监听 */
|
|
89
|
+
this.child.removeAllListeners();
|
|
90
|
+
/** 重启次数+1 */
|
|
91
|
+
const count = Number(process.env.karin_app_start_count) || 0;
|
|
92
|
+
process.env.karin_app_start_count = String(count + 1);
|
|
93
|
+
}
|
|
94
|
+
catch { }
|
|
95
|
+
return this.start(mode, lang, runner);
|
|
96
|
+
});
|
|
77
97
|
}
|
|
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
|
-
/**
|
|
98
|
+
/**
|
|
100
99
|
* pm2重启
|
|
101
100
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
101
|
+
async restart() {
|
|
102
|
+
const pm2Cfg = this.getConfigData('pm2');
|
|
103
|
+
const serverCfg = this.getConfigData('server');
|
|
104
|
+
/** 尝试获取pm2的进程id */
|
|
105
|
+
const port = serverCfg?.http?.port || 7000;
|
|
106
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000);
|
|
107
|
+
if (res) {
|
|
108
|
+
await this.exec(`pm2 restart ${res.pm2_id}`);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
await this.exec(`pm2 restart ${pm2Cfg?.apps[0]?.name || 'Karin'}`);
|
|
112
|
+
}
|
|
113
|
+
console.log('pm2服务已重启');
|
|
114
|
+
process.exit(0);
|
|
112
115
|
}
|
|
113
|
-
|
|
114
|
-
process.exit(0)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
116
|
+
/**
|
|
118
117
|
* pm2启动
|
|
119
118
|
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
119
|
+
async pm2() {
|
|
120
|
+
console.log('pm2启动中...');
|
|
121
|
+
const filePath = this.getConfigPath('pm2');
|
|
122
|
+
const data = this.getConfigData('pm2');
|
|
123
|
+
/** 修正入口文件路径 兼容0.6.28以前的版本 */
|
|
124
|
+
if (!fs.existsSync('./src') && filePath === './config/config/pm2.yaml') {
|
|
125
|
+
const script = './node_modules/node-karin/lib/index.js';
|
|
126
|
+
if (data.apps[0].script !== script) {
|
|
127
|
+
data.apps[0].script = script;
|
|
128
|
+
fs.writeFileSync(filePath, yaml.stringify(data));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const cmd = `pm2 start ${filePath} --env ${JSON.stringify(process.env)}`;
|
|
132
|
+
await this.exec(cmd);
|
|
133
|
+
console.log('pm2服务已启动 可执行 【npx karin log】 查看日志');
|
|
134
|
+
process.exit(0);
|
|
131
135
|
}
|
|
132
|
-
|
|
133
|
-
await this.exec(cmd)
|
|
134
|
-
console.log('pm2服务已启动 可执行 【npx karin log】 查看日志')
|
|
135
|
-
process.exit(0)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
136
|
+
/**
|
|
139
137
|
* pm2结束进程
|
|
140
138
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
139
|
+
async stop() {
|
|
140
|
+
console.log('pm2服务停止中...');
|
|
141
|
+
const pm2Cfg = this.getConfigData('pm2');
|
|
142
|
+
const serverCfg = this.getConfigData('server');
|
|
143
|
+
const port = serverCfg?.http?.port || 7000;
|
|
144
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000);
|
|
145
|
+
if (res) {
|
|
146
|
+
await this.exec(`pm2 delete ${res.pm2_id}`);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
await this.exec(`pm2 delete ${pm2Cfg?.apps[0]?.name || 'Karin'}`);
|
|
150
|
+
}
|
|
151
|
+
console.log('pm2服务已停止');
|
|
152
|
+
process.exit(0);
|
|
151
153
|
}
|
|
152
|
-
|
|
153
|
-
process.exit(0)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
154
|
+
/**
|
|
157
155
|
* pm2查看日志
|
|
158
156
|
*/
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
/**
|
|
157
|
+
async log() {
|
|
158
|
+
const pm2Cfg = this.getConfigData('pm2');
|
|
159
|
+
const serverCfg = this.getConfigData('server');
|
|
160
|
+
const port = serverCfg?.http?.port || 7000;
|
|
161
|
+
const res = await this.Axios(`http://127.0.0.1:${port}/api/ping`, 1000);
|
|
162
|
+
const lines = pm2Cfg?.lines || 1000;
|
|
163
|
+
const cmd = process.platform === 'win32' ? 'pm2.cmd' : 'pm2';
|
|
164
|
+
const type = res ? res.pm_id : pm2Cfg?.apps[0]?.name || 'Karin';
|
|
165
|
+
spawn(cmd, ['logs', type, '--lines', lines], { stdio: 'inherit', shell: true, cwd: process.cwd() });
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
171
168
|
* 更新依赖
|
|
172
169
|
*/
|
|
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
|
-
|
|
170
|
+
async update() {
|
|
171
|
+
/** 屏蔽的依赖包列表 */
|
|
172
|
+
const pkgdependencies = [
|
|
173
|
+
'art-template',
|
|
174
|
+
'axios',
|
|
175
|
+
'chalk',
|
|
176
|
+
'chokidar',
|
|
177
|
+
'commander',
|
|
178
|
+
'express',
|
|
179
|
+
'level',
|
|
180
|
+
'lodash',
|
|
181
|
+
'log4js',
|
|
182
|
+
'moment',
|
|
183
|
+
'node-schedule',
|
|
184
|
+
'redis',
|
|
185
|
+
'ws',
|
|
186
|
+
'yaml',
|
|
187
|
+
];
|
|
188
|
+
const list = Object.keys(this.pkg(false).dependencies).filter(key => !pkgdependencies.includes(key));
|
|
189
|
+
/** 获取包管理器 */
|
|
190
|
+
const pkg = new KarinCfgInit().getRegistry();
|
|
191
|
+
const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`;
|
|
192
|
+
/** 异步并发更新依赖 */
|
|
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.stack}`);
|
|
209
|
+
console.error(`error.message: ${error.message}`);
|
|
210
|
+
}
|
|
211
|
+
}));
|
|
212
|
+
console.log('所有依赖已更新完成~');
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
218
215
|
* 获取指定包的本地版本
|
|
219
216
|
* @param name - 包名
|
|
220
217
|
* @param pkg - 包管理器
|
|
221
218
|
* @returns - 版本号
|
|
222
219
|
*/
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
220
|
+
async getLocalVersion(name, pkg) {
|
|
221
|
+
const cmd = pkg === 'yarn' ? `yarn list --pattern ${name}` : `${pkg} list ${name} --depth=0`;
|
|
222
|
+
const text = await this.exec(cmd);
|
|
223
|
+
/** pnpm特殊处理 */
|
|
224
|
+
if (pkg === 'pnpm') {
|
|
225
|
+
const reg = new RegExp(`${name}\\s+([\\d.]+)`, 'gm');
|
|
226
|
+
const res = reg.exec(text);
|
|
227
|
+
return res?.[1] || '0.0.0';
|
|
228
|
+
}
|
|
229
|
+
const reg = new RegExp(`${name}@(\\d+\\.\\d+\\.\\d+)`, 'gm');
|
|
230
|
+
const res = reg.exec(text);
|
|
231
|
+
return res?.[1] || '0.0.0';
|
|
231
232
|
}
|
|
232
|
-
|
|
233
|
-
const res = reg.exec(text)
|
|
234
|
-
return res?.[1] || '0.0.0'
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
233
|
+
/**
|
|
238
234
|
* 获取指定包的最新版本
|
|
239
235
|
* @param name - 包名
|
|
240
236
|
* @param pkg - 包管理器
|
|
241
237
|
*/
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
238
|
+
async getRemoteVersion(name, pkg) {
|
|
239
|
+
const cmd = `${pkg} info ${name} version`;
|
|
240
|
+
const text = await this.exec(cmd);
|
|
241
|
+
/** yarn特殊处理 */
|
|
242
|
+
if (pkg === 'yarn') {
|
|
243
|
+
const lines = text.split('\n').map(line => line.trim());
|
|
244
|
+
const ver = lines.find(line => /^\d+\.\d+\.\d+$/.test(line));
|
|
245
|
+
return ver || '';
|
|
246
|
+
}
|
|
247
|
+
return text.trim();
|
|
250
248
|
}
|
|
251
|
-
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
249
|
+
/**
|
|
255
250
|
* 封装exec
|
|
256
251
|
* @param cmd - 命令
|
|
257
252
|
*/
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
253
|
+
exec(cmd) {
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
255
|
+
execCmd(cmd, (error, stdout, stderr) => {
|
|
256
|
+
if (stdout)
|
|
257
|
+
return resolve(stdout.trim());
|
|
258
|
+
if (error)
|
|
259
|
+
return reject(error);
|
|
260
|
+
return reject(stderr);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
269
265
|
* 封装axios 超时返回false
|
|
270
266
|
* @param url - 请求地址
|
|
271
267
|
* @param timeout - 超时时间
|
|
272
268
|
* @returns - 请求结果
|
|
273
269
|
*/
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
270
|
+
async Axios(url, timeout) {
|
|
271
|
+
try {
|
|
272
|
+
const res = await axios.get(url, { timeout });
|
|
273
|
+
return res.data;
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
280
278
|
}
|
|
281
|
-
}
|
|
282
279
|
}
|
package/lib/cli/init.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { KarinCfgInit } from '../core/init/config.js'
|
|
2
|
+
import { KarinCfgInit } from '../core/init/config.js';
|
|
3
3
|
/**
|
|
4
4
|
* 休眠函数
|
|
5
5
|
* @param ms 毫秒
|
|
6
6
|
*/
|
|
7
|
-
function sleep
|
|
8
|
-
|
|
7
|
+
function sleep(ms) {
|
|
8
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
9
9
|
}
|
|
10
|
-
async function main
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
10
|
+
async function main() {
|
|
11
|
+
/** 捕获错误 打印日志 */
|
|
12
|
+
process.on('uncaughtException', err => console.error(err));
|
|
13
|
+
process.on('unhandledRejection', err => console.error(err));
|
|
14
|
+
const init = new KarinCfgInit();
|
|
15
|
+
init.init();
|
|
16
|
+
await sleep(1000);
|
|
17
|
+
const pkg = init.getRegistry();
|
|
18
|
+
/** 结果 */
|
|
19
|
+
await init.install(pkg);
|
|
20
20
|
}
|
|
21
|
-
main().then(() => process.exit(0)).catch(() => process.exit(0))
|
|
21
|
+
main().then(() => process.exit(0)).catch(() => process.exit(0));
|
package/lib/cli/karin.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { program } from 'commander'
|
|
3
|
-
import { KarinCli } from './index.js'
|
|
4
|
-
const cli = new KarinCli()
|
|
5
|
-
program.version(cli.pkg(true).version, '-v, --version', '显示版本号')
|
|
6
|
-
program.command('.').description('启动karin').action(() => cli.start(
|
|
7
|
-
program.command('start').description('启动karin').action(() => cli.start(
|
|
8
|
-
program.command('pm2').description('后台运行karin').action(() => cli.start(
|
|
9
|
-
program.command('stop').description('停止后台运行').action(() => cli.stop())
|
|
10
|
-
program.command('rs').description('重启pm2服务').action(() => cli.restart())
|
|
11
|
-
program.command('dev').description('JavaScript开发模式').action(() => cli.start(
|
|
12
|
-
program.command('ts').description('TypeScript开发模式').action(() => cli.start(
|
|
13
|
-
program.command('log').description('查看日志').action(() => cli.log())
|
|
14
|
-
program.command('up').description('更新依赖').action(() => cli.update())
|
|
15
|
-
program.command('init').description('初始化karin').action(() => { import('./init.js') })
|
|
16
|
-
program.parse(process.argv)
|
|
2
|
+
import { program } from 'commander';
|
|
3
|
+
import { KarinCli } from './index.js';
|
|
4
|
+
const cli = new KarinCli();
|
|
5
|
+
program.version(cli.pkg(true).version, '-v, --version', '显示版本号');
|
|
6
|
+
program.command('.').description('启动karin').action(() => cli.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
|
|
7
|
+
program.command('start').description('启动karin').action(() => cli.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
|
|
8
|
+
program.command('pm2').description('后台运行karin').action(() => cli.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "pm2" /* Runner.Pm2 */));
|
|
9
|
+
program.command('stop').description('停止后台运行').action(() => cli.stop());
|
|
10
|
+
program.command('rs').description('重启pm2服务').action(() => cli.restart());
|
|
11
|
+
program.command('dev').description('JavaScript开发模式').action(() => cli.start("dev" /* Mode.Dev */, "js" /* Lang.Js */, "node" /* Runner.Node */));
|
|
12
|
+
program.command('ts').description('TypeScript开发模式').action(() => cli.start("dev" /* Mode.Dev */, "ts" /* Lang.Ts */, "tsx" /* Runner.Tsx */));
|
|
13
|
+
program.command('log').description('查看日志').action(() => cli.log());
|
|
14
|
+
program.command('up').description('更新依赖').action(() => cli.update());
|
|
15
|
+
program.command('init').description('初始化karin').action(() => { import('./init.js'); });
|
|
16
|
+
program.parse(process.argv);
|
package/lib/cli/start.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { program } from 'commander';
|
|
2
|
+
import { KarinCli } from './index.js';
|
|
3
|
+
const karin = new KarinCli();
|
|
4
|
+
program.version(karin.pkg(true).version, '-v, --version', '显示版本号');
|
|
5
|
+
program.command('.').description('启动karin').action(() => karin.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
|
|
6
|
+
program.command('start').description('启动karin').action(() => karin.start("prod" /* Mode.Prod */, "js" /* Lang.Js */, "node" /* Runner.Node */));
|
|
7
|
+
program.command('dev').description('dev开发模式').action(() => karin.start("dev" /* Mode.Dev */, "js" /* Lang.Js */, "node" /* Runner.Node */));
|
|
8
|
+
program.parse(process.argv);
|