Skip to content

Commit c64366f

Browse files
committed
fix: #124 #121
1 parent 3192340 commit c64366f

File tree

14 files changed

+212
-134
lines changed

14 files changed

+212
-134
lines changed

CHANGELOG.md

+8-29
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@
707707

708708
## 0.0.3 (2024-6-19)
709709

710-
### 新增功能
710+
### Features
711711

712712
- feat: vue渲染兼容 ([4470d02](https://github.com/KarinJS/Karin/commit/4470d02b3521e6e91e7abb7c20c02a855728b1d7))
713713
- feat: ReactMessageWithEmojiRequest ([b92770b](https://github.com/KarinJS/Karin/commit/b92770bcaeb0d2b0966d125e7ad984f3dbe90263))
@@ -716,7 +716,7 @@
716716
- feat: redis 集群 触发fnc加上颜色使其更明确指令 ([d53560f](https://github.com/KarinJS/Karin/commit/d53560f3e4f852338693e2fddcd3f9c8761e3909))
717717
- feat: add updateVersion ([10c6446](https://github.com/KarinJS/Karin/commit/10c6446ee48ee4598b1fae336a9baf0b0a9415ca))
718718

719-
### Bug修复
719+
### Bug Fixes
720720

721721
- fix: 修正正向ws重连错误 修正getBot 失败 ([1cba613](https://github.com/KarinJS/Karin/commit/1cba6136fff9726a3cd2e2189f5c83357fd17de7))
722722
- fix: ob11的uid暂时全部默认为uin ([8fc3ccd](https://github.com/KarinJS/Karin/commit/8fc3ccd894d1bfd806f3882435a5e9cdd2a3f022))
@@ -735,13 +735,13 @@
735735
- Update Wormhole.js ([46a9797](https://github.com/KarinJS/Karin/commit/46a979734fb0ab98198129155716c983288cbede))
736736

737737

738-
## 0.0.2 (2024-6-4)
738+
## [0.1.1](https://github.com/KarinJS/Karin/compare/v0.0.1...v0.0.2) (2024-06-04)
739739

740740
### Releases
741741

742742
- release: 0.0.2 ([5cf693a](https://github.com/KarinJS/Karin/commit/5cf693a6a9a06446654ffc65a6bdb936322401ee))
743743

744-
### 新增功能
744+
### Features
745745

746746
- feat: 添加配置文件视图 ([f567b61](https://github.com/KarinJS/Karin/commit/f567b6104a8970f86771763915aa730779368f2d))
747747
- feat: YamlEditor root ([a46b036](https://github.com/KarinJS/Karin/commit/a46b036b338359c0f921b5f7f97630dee63eacdb))
@@ -789,7 +789,7 @@
789789
- feat:完善结构、通知事件 ([933bbb9](https://github.com/KarinJS/Karin/commit/933bbb95a237ad02b26c0a8c6e8d0a23c8a7734b))
790790
- feat:`puppeteer` ([8928729](https://github.com/KarinJS/Karin/commit/8928729ca0d7df5e8af92e7bf6929ce840fc7b4a))
791791

792-
### Bug修复
792+
### Bug Fixes
793793

794794
- fix: 移除子模块 并屏蔽git跟踪 ([b838fd7](https://github.com/KarinJS/Karin/commit/b838fd7f72cc1048d69fc07d681d858149350576))
795795
- fix: 修正描述 继续挖坑 咕咕咕 ([2e18050](https://github.com/KarinJS/Karin/commit/2e1805014e0b5d1d1f9fda3eef46e22816d806cf))
@@ -872,30 +872,9 @@
872872
- fix:分离消息事件处理 ([2bbd5bd](https://github.com/KarinJS/Karin/commit/2bbd5bd077518e4a2d3892c683cae35ca8629bbd))
873873
- fix:`static` ([c6d4856](https://github.com/KarinJS/Karin/commit/c6d4856b4038036b1cfdec7f61b1a7e55cd5d342))
874874

875-
### 文档更新
876-
877-
- docs: add repobeats ([13a58a8](https://github.com/KarinJS/Karin/commit/13a58a820b4d5c508628c514031c44980240d865))
878-
- docs:更新 README.md ([a88bd8b](https://github.com/KarinJS/Karin/commit/a88bd8b64c9b57e7bf07e2871b6be4929f144f7f))
879-
- docs:Update README.md (#3) ([f1ab4c2](https://github.com/KarinJS/Karin/commit/f1ab4c237396a9b901f2d2858b2f4edd223f2854))
880-
- docs:更新 README.md ([a8a0963](https://github.com/KarinJS/Karin/commit/a8a0963ae549e2ba5120e1cfa36847331d456bc3))
881875

882-
### 代码重构
876+
## [0.0.1](https://github.com/KarinJS/Karin/compare/v0.0.1) (2024-03-21)
883877

884-
- refactor: 将proto迁移到npm ([408fedb](https://github.com/KarinJS/Karin/commit/408fedbd95954a46e351614791aea2bd2d38e7c7))
885-
- :art: refactor: adapter and forward ([efb86fd](https://github.com/KarinJS/Karin/commit/efb86fdf8a22dc890be2d6c76dac2de34995f092))
886-
- :fire: refactor: 解耦事件 完善日志 ([f02a3e2](https://github.com/KarinJS/Karin/commit/f02a3e26f6291c32017540d982feb4c046713099))
887-
- refactor: kritor converter and struct definition ([427b524](https://github.com/KarinJS/Karin/commit/427b524aa72768c15069d566c4a1c931b85434fe))
888-
889-
### 其他提交
890-
891-
- rm gitmodules ([c4c0c57](https://github.com/KarinJS/Karin/commit/c4c0c579fedef1c172c2a7fada51c5152f95d816))
892-
- rm eslint-plugin-import ([fae7fa3](https://github.com/KarinJS/Karin/commit/fae7fa3a1a071eefd15451084d84c426ee8c282d))
893-
- rm scripts.build ([45e4dac](https://github.com/KarinJS/Karin/commit/45e4dacbf9b53a09463623d00ad7058f42a9a4da))
894-
- rm .npmrc ([fb6448a](https://github.com/KarinJS/Karin/commit/fb6448a5de365762a2266072e1b76a4844435281))
895-
- Create LICENSE ([2b5b6b0](https://github.com/KarinJS/Karin/commit/2b5b6b089e23afd371ad90107344c237662f4af2))
896-
- :heavy_minus_sign: Update package.json ([d623748](https://github.com/KarinJS/Karin/commit/d623748f14469b7ffe59b40b54b8ceae8bff7fb1))
897-
- use uid (#4) ([2abe1a5](https://github.com/KarinJS/Karin/commit/2abe1a531683bdd4a34b68a9d94a7f2987ae3f61))
898-
899-
## 0.0.1 (2024-3-21)
878+
### Features
900879

901-
- :tada: 初次提交 ([9e12137](https://github.com/KarinJS/Karin/commit/9e12137b6cf0e778a28df78daaeea77d5e325f4c))
880+
- feat: 初次提交 ([9e12137](https://github.com/KarinJS/Karin/commit/9e12137b6cf0e778a28df78daaeea77d5e325f4c))

config/defSet/config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ log4jsCfg:
1111
# 日志文件最大大小 MB
1212
maxLogSize: 30
1313

14-
# 关闭后台进程失败后是否继续启动 继续启动会导致多进程
15-
multi_progress: false
16-
1714
# 控制台触发插件日志颜色 十六进制 默认#FFFF00 不支持热更新
1815
log_color: "#E1D919"
1916

17+
# 重启是否调用pm2 如果不调用则会直接关机 此配置适合有进程守护的程序
18+
pm2Restart: true
19+
2020
# 私聊设置
2121
private:
2222
# 关闭私聊后回复的提示词 为空则不回复

src/cli/index.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import path from 'path'
33
import yaml from 'yaml'
44
import axios from 'axios'
55
import { fileURLToPath } from 'url'
6-
import { exec as execCmd, spawn, ChildProcess } from 'child_process'
7-
import { KarinCfgInit } from '../core/init/config'
6+
import { getRegistry } from './pkg'
7+
import { exec as execCmd, spawn, ChildProcess, ExecOptions } from 'child_process'
88

99
export const enum Runner {
1010
Node = 'node',
@@ -38,11 +38,12 @@ export class KarinCli {
3838
/** 入口文件(注意后缀) */
3939
this.file = path.join(path.dirname(this.filename), '../index.js')
4040
this.child = null as unknown as ChildProcess
41+
process.env.karin_app_pkg = getRegistry()
4142
process.env.karin_app_version = this.pkg(true).version
4243
}
4344

4445
/**
45-
* 获取pkg
46+
* 获取pkg配置
4647
* @param isNpm - 是否是npm包
4748
*/
4849
pkg (isNpm: boolean) {
@@ -226,10 +227,9 @@ export class KarinCli {
226227
const list = Object.keys(this.pkg(false).dependencies).filter(key => !pkgdependencies.includes(key))
227228

228229
/** 获取包管理器 */
229-
const pkg = new KarinCfgInit().getRegistry()
230+
const pkg = getRegistry()
230231
const cmd = pkg === 'yarn' ? 'yarn upgrade' : `${pkg} update`
231232

232-
/** 异步并发更新依赖 */
233233
await Promise.all(list.map(async item => {
234234
try {
235235
/** 检查是否已经是最新版本 */
@@ -246,12 +246,26 @@ export class KarinCli {
246246
console.log(`[依赖更新] ${item} 更新完成~`)
247247
} catch (error: any) {
248248
console.error(`[依赖更新] ${item} 更新失败:`)
249-
console.error(`error.stack: ${error.stack}`)
250-
console.error(`error.message: ${error.message}`)
249+
console.error(error.stack || error.message || error)
251250
}
252251
}))
253252

254-
console.log('所有依赖已更新完成~')
253+
console.log('[依赖更新] 所有npm依赖已更新完成~')
254+
console.log('[依赖更新] 开始更新git插件...')
255+
const gitList = this.getGitPlugins()
256+
if (!gitList.length) return console.log('[依赖更新] 没有git插件需要更新~')
257+
258+
await Promise.all(gitList.map(async item => {
259+
const dir = path.resolve(process.cwd(), 'plugins', item)
260+
try {
261+
await this.exec('git pull', { cwd: dir })
262+
console.log(`[依赖更新] ${item} 更新完成~`)
263+
} catch (error: any) {
264+
console.error(`[依赖更新] ${item} 更新失败`)
265+
console.error(error.stack || error.message || error)
266+
}
267+
}))
268+
console.log('[依赖更新] 所有git插件已更新完成~')
255269
}
256270

257271
/**
@@ -294,14 +308,28 @@ export class KarinCli {
294308
return text.trim()
295309
}
296310

311+
/**
312+
* 获取git插件列表
313+
*/
314+
getGitPlugins (): Array<string> {
315+
const dir = path.resolve(process.cwd(), 'plugins')
316+
let list = fs.readdirSync(dir, { withFileTypes: true })
317+
/** 忽略非文件夹、非 karin-plugin-开头的文件夹 */
318+
list = list.filter(v => v.isDirectory() && v.name.startsWith('karin-plugin-'))
319+
list = list.filter(v => fs.existsSync(`${dir}/${v.name}/package.json`))
320+
const arr: string[] = []
321+
list.map(v => arr.push(v.name))
322+
return arr
323+
}
324+
297325
/**
298326
* 封装exec
299327
* @param cmd - 命令
300328
*/
301-
exec (cmd: string): Promise<string> {
329+
exec (cmd: string, options?: ExecOptions): Promise<string> {
302330
return new Promise((resolve, reject) => {
303-
execCmd(cmd, (error, stdout, stderr) => {
304-
if (stdout) return resolve(stdout.trim())
331+
execCmd(cmd, options, (error, stdout, stderr) => {
332+
if (stdout) return resolve(stdout.toString().trim())
305333
if (error) return reject(error)
306334
return reject(stderr)
307335
})

src/cli/init.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22

3+
import { getRegistry } from './pkg'
34
import { KarinCfgInit } from '../core/init/config'
45

56
/**
@@ -20,7 +21,7 @@ async function main () {
2021

2122
await sleep(1000)
2223

23-
const pkg = init.getRegistry()
24+
const pkg = getRegistry()
2425

2526
/** 结果 */
2627
await init.install(pkg)

src/cli/pkg.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'fs'
2+
3+
/**
4+
* 获取当前的包管理器 根据锁文件判断 优先级 pnpm > yarn > npm > cnpm
5+
*/
6+
export const getRegistry = (): 'pnpm' | 'cnpm' | 'yarn' | 'npm' => {
7+
if (fs.existsSync('./pnpm-lock.yaml')) return 'pnpm'
8+
if (fs.existsSync('./yarn.lock')) return 'yarn'
9+
if (fs.existsSync('./package-lock.json')) return 'npm'
10+
// cnpm 没有锁文件
11+
return 'cnpm'
12+
}

src/core/init/config.ts

-11
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,4 @@ export class KarinCfgInit {
209209
})
210210
})
211211
}
212-
213-
/**
214-
* 获取当前的包管理器 根据锁文件判断
215-
*/
216-
getRegistry (): 'pnpm' | 'cnpm' | 'yarn' | 'npm' {
217-
if (fs.existsSync('./pnpm-lock.yaml')) return 'pnpm'
218-
if (fs.existsSync('./yarn.lock')) return 'yarn'
219-
if (fs.existsSync('./package-lock.json')) return 'npm'
220-
// cnpm 没有锁文件
221-
return 'cnpm'
222-
}
223212
}

src/core/listener/listener.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class Listeners extends EventEmitter {
137137
}
138138
const element = [
139139
segment.reply(message_id),
140-
segment.text(`Karin 重启成功:${restartTime}秒`),
140+
segment.text(`\nKarin 重启成功:${restartTime}秒`),
141141
]
142142
await this.sendMsg(id, contact, element)
143143
await level.del(key)

src/core/process/process.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ export default class Process {
7777
* 根据配置文件判断是否继续
7878
*/
7979
logger.error(logger.red(`后台进程关闭失败,请检查是否有进程正在占用端口${config.Server.http.port}`))
80-
if (!config.Config.multi_progress) {
81-
logger.error(logger.red('当前配置不允许多进程运行,程序即将退出'))
82-
await this.exit(1)
83-
}
84-
logger.error(logger.red('当前配置允许多进程运行,程序继续运行'))
80+
await this.exit()
8581
return this
8682
}
8783

src/types/config/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ export interface Config {
123123
*/
124124
log_color: string
125125
/**
126-
* 关闭后台进程失败后是否继续启动 继续启动会导致多进程
126+
* 重启是否调用pm2 如果不调用则会直接关机 此配置适合有进程守护的程序
127127
*/
128-
multi_progress: boolean
128+
pm2Restart: boolean
129129
/**
130130
* ffmpeg配置
131131
*/

src/utils/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export * from './tools/update'
99
export * from './config/yamlEditor'
1010
export * from './core/logger'
1111
export * from './config/updateVersion'
12+
export * from './tools/restart'
13+
export * from './tools/stop'

src/utils/tools/exec.ts

+32-40
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,60 @@
11
import logger from '../core/logger'
2-
import { exec as execCmd } from 'child_process'
2+
import { exec as execCmd, ExecOptions } from 'child_process'
33

44
/**
55
* 执行 shell 命令
66
* @param cmd 命令
77
* @param log 是否打印日志
88
* @param options 选项
99
*/
10-
export const exec = (
10+
export const exec = async (
1111
cmd: string,
1212
log = true,
13-
options = { cwd: process.cwd(), encoding: 'utf-8' }
14-
): Promise<{
15-
/**
16-
* - 执行状态
17-
*/
18-
status: 'ok' | 'failed'
19-
/**
20-
* - 错误信息
21-
*/
22-
error: Error | null
23-
stdout: string | ''
24-
stderr: string | ''
25-
}> => {
26-
return new Promise(resolve => {
27-
const logMessage = (level: 'trace' | 'debug' | 'mark' | 'info' | 'warn' | 'error' | 'fatal', message: string) => {
28-
if (log) logger[level](message)
29-
}
13+
options: ExecOptions = { cwd: process.cwd() }
14+
): Promise<{ status: 'ok' | 'failed', error: Error | null, stdout: string, stderr: string }> => {
15+
const logType = (status: 'start' | 'ok' | 'failed') => ({
16+
start: '[exec][开始执行]',
17+
ok: '[exec][执行成功]',
18+
failed: '[exec][执行失败]',
19+
})[status]
3020

31-
const logType = (status: string) => {
32-
switch (status) {
33-
case '开始执行':
34-
return logger.yellow('[exec][开始执行]')
35-
case 'ok':
36-
return logger.green('[exec][执行成功]')
37-
case 'failed':
38-
return logger.red('[exec][执行失败]')
39-
}
40-
}
21+
const logMessage = (status: 'start' | 'ok' | 'failed', details = '') => {
22+
if (log) {
23+
const colorFunc = {
24+
start: logger.yellow,
25+
ok: logger.green,
26+
failed: logger.red,
27+
}[status]
4128

42-
const formatMessage = (status: string, details: string) => [logType(status), `cmd: ${cmd}`, `cwd: ${options.cwd || process.cwd()}`, details, '--------'].join('\n')
29+
logger.info([
30+
colorFunc(logType(status)),
31+
`cmd: ${cmd}`,
32+
`cwd: ${options.cwd}`,
33+
details,
34+
'--------',
35+
].join('\n'))
36+
}
37+
}
4338

44-
logMessage('info', formatMessage('开始执行', ''))
39+
logMessage('start')
4540

46-
execCmd(cmd, options, (error, stdout, stderr) => {
47-
if (error) {
48-
logMessage('error', formatMessage('failed', `Error: ${error.message || error.stack || error.toString()}`))
49-
return resolve({ status: 'failed', error, stdout, stderr })
50-
}
51-
logMessage('mark', formatMessage('ok', `stdout: ${stdout}\nstderr: ${stderr}`))
52-
resolve({ status: 'ok', error, stdout, stderr })
41+
return new Promise(resolve => {
42+
execCmd(cmd, options, (error, stdout = '', stderr = '') => {
43+
const status = error ? 'failed' : 'ok'
44+
logMessage(status, error ? `Error: ${error.message}` : `stdout: ${stdout}\nstderr: ${stderr}`)
45+
resolve({ status, error, stdout, stderr })
5346
})
5447
})
5548
}
5649

5750
/**
5851
* 执行 shell 命令
5952
* @param cmd 命令
60-
* @param log 是否打印日志
6153
* @param options 选项
6254
*/
6355
export const execs = (
6456
cmd: string,
65-
options = { cwd: process.cwd(), encoding: 'utf-8' }
57+
options: ExecOptions = { cwd: process.cwd() }
6658
): Promise<string> => {
6759
return new Promise((resolve, reject) => {
6860
execCmd(cmd, options, (error, stdout) => {

0 commit comments

Comments
 (0)