-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
313 lines (250 loc) · 7.44 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import plugin from '../../lib/plugins/plugin.js'
import { createRequire } from 'module'
import lodash from 'lodash'
import fs from 'node:fs'
import { Restart } from './restart.js'
import common from '../../lib/common/common.js'
const require = createRequire(import.meta.url)
const { exec, execSync } = require('child_process')
let uping = false
export class update extends plugin {
constructor () {
super({
name: '更新',
dsc: '#更新 #强制更新',
event: 'message',
priority: 4000,
rule: [
{
reg: '^#更新日志$',
fnc: 'updateLog'
},
{
reg: '^#(强制)*更新(.*)',
fnc: 'update'
},
{
reg: '^#全部更新$',
fnc: 'updateAll',
permission: 'master'
}
]
})
this.typeName = 'Yunzai-Bot'
}
async update () {
if (!this.e.isMaster) return false
if (uping) {
await this.reply('已有命令更新中..请勿重复操作')
return
}
if (/详细|详情|面板|面版/.test(this.e.msg)) return false
/** 获取插件 */
let plugin = this.getPlugin()
if (plugin === false) return false
/** 检查git安装 */
if (!await this.checkGit()) return
/** 执行更新 */
await this.runUpdate(plugin)
/** 是否需要重启 */
if (this.isUp) {
// await this.reply('即将执行重启,以应用更新')
setTimeout(() => this.restart(), 2000)
}
}
async checkGit () {
let ret = await execSync('git --version', { encoding: 'utf-8' })
if (!ret || !ret.includes('git version')) {
await this.reply('请先安装git')
return false
}
return true
}
getPlugin (plugin = '') {
if (!plugin) {
plugin = this.e.msg.replace(/#|更新|强制/g, '')
if (!plugin) return ''
}
let path = `./plugins/${plugin}/.git`
if (!fs.existsSync(path)) return false
this.typeName = plugin
return plugin
}
async execSync (cmd) {
return new Promise((resolve, reject) => {
exec(cmd, { windowsHide: true }, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
})
})
}
async runUpdate (plugin = '') {
this.isNowUp = false
let cm = 'git pull --no-rebase'
let type = '更新'
if (this.e.msg.includes('强制')) {
type = '强制更新'
cm = `git reset --hard origin/master && ${cm}`
}
if (plugin) {
cm = `git -C ./plugins/${plugin}/ pull --no-rebase`
}
this.oldCommitId = await this.getcommitId(plugin)
logger.mark(`${this.e.logFnc} 开始${type}:${this.typeName}`)
await this.reply(`开始#${type}${this.typeName}`)
uping = true
let ret = await this.execSync(cm)
uping = false
if (ret.error) {
logger.mark(`${this.e.logFnc} 更新失败:${this.typeName}`)
this.gitErr(ret.error, ret.stdout)
return false
}
let time = await this.getTime(plugin)
if (/Already up|已经是最新/g.test(ret.stdout)) {
await this.reply(`${this.typeName}已经是最新\n最后更新时间:${time}`)
} else {
await this.reply(`${this.typeName}更新成功\n更新时间:${time}`)
this.isUp = true
let log = await this.getLog(plugin)
await this.reply(log)
}
logger.mark(`${this.e.logFnc} 最后更新时间:${time}`)
return true
}
async getcommitId (plugin = '') {
let cm = 'git rev-parse --short HEAD'
if (plugin) {
cm = `git -C ./plugins/${plugin}/ rev-parse --short HEAD`
}
let commitId = await execSync(cm, { encoding: 'utf-8' })
commitId = lodash.trim(commitId)
return commitId
}
async getTime (plugin = '') {
let cm = 'git log -1 --oneline --pretty=format:"%cd" --date=format:"%m-%d %H:%M"'
if (plugin) {
cm = `cd ./plugins/${plugin}/ && git log -1 --oneline --pretty=format:"%cd" --date=format:"%m-%d %H:%M"`
}
let time = ''
try {
time = await execSync(cm, { encoding: 'utf-8' })
time = lodash.trim(time)
} catch (error) {
logger.error(error.toString())
time = '获取时间失败'
}
return time
}
async gitErr (err, stdout) {
let msg = '更新失败!'
let errMsg = err.toString()
stdout = stdout.toString()
if (errMsg.includes('Timed out')) {
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '')
await this.reply(msg + `\n连接超时:${remote}`)
return
}
if (/Failed to connect|unable to access/g.test(errMsg)) {
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '')
await this.reply(msg + `\n连接失败:${remote}`)
return
}
if (errMsg.includes('be overwritten by merge')) {
await this.reply(msg + `存在冲突:\n${errMsg}\n` + '请解决冲突后再更新,或者执行#强制更新,放弃本地修改')
return
}
if (stdout.includes('CONFLICT')) {
await this.reply([msg + '存在冲突\n', errMsg, stdout, '\n请解决冲突后再更新,或者执行#强制更新,放弃本地修改'])
return
}
await this.reply([errMsg, stdout])
}
async updateAll () {
let dirs = fs.readdirSync('./plugins/')
await this.runUpdate()
for (let plu of dirs) {
plu = this.getPlugin(plu)
if (plu === false) continue
await common.sleep(1500)
await this.runUpdate(plu)
}
if (this.isUp) {
// await this.reply('即将执行重启,以应用更新')
setTimeout(() => this.restart(), 2000)
}
}
restart () {
new Restart(this.e).restart()
}
async getLog (plugin = '') {
let cm = 'git log -20 --oneline --pretty=format:"%h||[%cd] %s" --date=format:"%m-%d %H:%M"'
if (plugin) {
cm = `cd ./plugins/${plugin}/ && ${cm}`
}
let logAll
try {
logAll = await execSync(cm, { encoding: 'utf-8' })
} catch (error) {
logger.error(error.toString())
this.reply(error.toString())
}
if (!logAll) return false
logAll = logAll.split('\n')
let log = []
for (let str of logAll) {
str = str.split('||')
if (str[0] == this.oldCommitId) break
if (str[1].includes('Merge branch')) continue
log.push(str[1])
}
let line = log.length
log = log.join('\n\n')
if (log.length <= 0) return ''
let end = ''
log = await this.makeForwardMsg(`${plugin || 'Yunzai-Bot'}更新日志,共${line}条`, log, end)
return log
}
async makeForwardMsg (title, msg, end) {
let nickname = Bot.nickname
if (this.e.isGroup) {
let info = await Bot.getGroupMemberInfo(this.e.group_id, Bot.uin)
nickname = info.card ?? info.nickname
}
let userInfo = {
user_id: Bot.uin,
nickname
}
let forwardMsg = [
{
...userInfo,
message: title
},
{
...userInfo,
message: msg
}
]
if (end) {
forwardMsg.push({
...userInfo,
message: end
})
}
/** 制作转发内容 */
if (this.e.isGroup) {
forwardMsg = await this.e.group.makeForwardMsg(forwardMsg)
} else {
forwardMsg = await this.e.friend.makeForwardMsg(forwardMsg)
}
/** 处理描述 */
forwardMsg.data = forwardMsg.data
.replace(/\n/g, '')
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
.replace(/___+/, `<title color="#777777" size="26">${title}</title>`)
return forwardMsg
}
async updateLog () {
let log = await this.getLog()
await this.reply(log)
}
}