2023-03-04 14:30:13 +08:00
|
|
|
|
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 {
|
2023-07-26 05:23:14 +08:00
|
|
|
|
constructor() {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
super({
|
|
|
|
|
name: '更新',
|
|
|
|
|
dsc: '#更新 #强制更新',
|
|
|
|
|
event: 'message',
|
|
|
|
|
priority: 4000,
|
|
|
|
|
rule: [
|
|
|
|
|
{
|
2023-10-12 21:48:12 +08:00
|
|
|
|
reg: '^#更新日志$',
|
2023-03-04 14:30:13 +08:00
|
|
|
|
fnc: 'updateLog'
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-10-12 21:48:12 +08:00
|
|
|
|
reg: '^#(强制)?更新$',
|
2023-03-04 14:30:13 +08:00
|
|
|
|
fnc: 'update'
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-06-12 20:35:53 +08:00
|
|
|
|
reg: '^#全部(强制)?更新$',
|
2023-03-04 14:30:13 +08:00
|
|
|
|
fnc: 'updateAll',
|
|
|
|
|
permission: 'master'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
2023-04-24 15:15:05 +08:00
|
|
|
|
this.typeName = 'Miao-Yunzai'
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async update() {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (!this.e.isMaster) return false
|
2023-09-06 03:00:17 +08:00
|
|
|
|
if (uping) return this.reply('已有命令更新中..请勿重复操作')
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
if (/详细|详情|面板|面版/.test(this.e.msg)) return false
|
|
|
|
|
|
|
|
|
|
/** 获取插件 */
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const plugin = this.getPlugin()
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (plugin === false) return false
|
|
|
|
|
|
|
|
|
|
/** 执行更新 */
|
|
|
|
|
await this.runUpdate(plugin)
|
|
|
|
|
|
|
|
|
|
/** 是否需要重启 */
|
|
|
|
|
if (this.isUp) {
|
|
|
|
|
// await this.reply('即将执行重启,以应用更新')
|
|
|
|
|
setTimeout(() => this.restart(), 2000)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
getPlugin(plugin = '') {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (!plugin) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
plugin = this.e.msg.replace(/#(强制)?更新(日志)?/, '')
|
2023-03-04 14:30:13 +08:00
|
|
|
|
if (!plugin) return ''
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
if (!fs.existsSync(`plugins/${plugin}/.git`)) return false
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
this.typeName = plugin
|
|
|
|
|
return plugin
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async execSync(cmd) {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
exec(cmd, { windowsHide: true }, (error, stdout, stderr) => {
|
|
|
|
|
resolve({ error, stdout, stderr })
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async runUpdate(plugin = '') {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
this.isNowUp = false
|
|
|
|
|
|
|
|
|
|
let cm = 'git pull --no-rebase'
|
|
|
|
|
|
|
|
|
|
let type = '更新'
|
|
|
|
|
if (this.e.msg.includes('强制')) {
|
|
|
|
|
type = '强制更新'
|
2023-07-26 05:23:14 +08:00
|
|
|
|
cm = `git reset --hard && git pull --rebase --allow-unrelated-histories`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
2023-09-06 03:00:17 +08:00
|
|
|
|
if (plugin) cm = `cd "plugins/${plugin}" && ${cm}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
this.oldCommitId = await this.getcommitId(plugin)
|
|
|
|
|
|
|
|
|
|
logger.mark(`${this.e.logFnc} 开始${type}:${this.typeName}`)
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
await this.reply(`开始${type} ${this.typeName}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
uping = true
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const ret = await this.execSync(cm)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
uping = false
|
|
|
|
|
|
|
|
|
|
if (ret.error) {
|
|
|
|
|
logger.mark(`${this.e.logFnc} 更新失败:${this.typeName}`)
|
|
|
|
|
this.gitErr(ret.error, ret.stdout)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const time = await this.getTime(plugin)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
if (/Already up|已经是最新/g.test(ret.stdout)) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
await this.reply(`${this.typeName} 已是最新\n最后更新时间:${time}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
} else {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
await this.reply(`${this.typeName} 更新成功\n更新时间:${time}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
this.isUp = true
|
2023-09-06 03:00:17 +08:00
|
|
|
|
await this.reply(await this.getLog(plugin))
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.mark(`${this.e.logFnc} 最后更新时间:${time}`)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async getcommitId(plugin = '') {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
let cm = 'git rev-parse --short HEAD'
|
2023-09-06 03:00:17 +08:00
|
|
|
|
if (plugin) cm = `cd "plugins/${plugin}" && ${cm}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const commitId = await execSync(cm, { encoding: 'utf-8' })
|
|
|
|
|
return lodash.trim(commitId)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async getTime(plugin = '') {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
let cm = 'git log -1 --pretty=%cd --date=format:"%F %T"'
|
|
|
|
|
if (plugin) cm = `cd "plugins/${plugin}" && ${cm}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
let time = ''
|
|
|
|
|
try {
|
|
|
|
|
time = await execSync(cm, { encoding: 'utf-8' })
|
|
|
|
|
time = lodash.trim(time)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(error.toString())
|
|
|
|
|
time = '获取时间失败'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return time
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async gitErr(err, stdout) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const msg = '更新失败!'
|
|
|
|
|
const errMsg = err.toString()
|
2023-03-04 14:30:13 +08:00
|
|
|
|
stdout = stdout.toString()
|
|
|
|
|
|
|
|
|
|
if (errMsg.includes('Timed out')) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '')
|
|
|
|
|
return this.reply(`${msg}\n连接超时:${remote}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (/Failed to connect|unable to access/g.test(errMsg)) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '')
|
|
|
|
|
return this.reply(`${msg}\n连接失败:${remote}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (errMsg.includes('be overwritten by merge')) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
return this.reply(`${msg}\n存在冲突:\n${errMsg}\n请解决冲突后再更新,或者执行#强制更新,放弃本地修改`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stdout.includes('CONFLICT')) {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
return this.reply(`${msg}\n存在冲突:\n${errMsg}${stdout}\n请解决冲突后再更新,或者执行#强制更新,放弃本地修改`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
return this.reply([errMsg, stdout])
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async updateAll() {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const dirs = fs.readdirSync('./plugins/')
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
restart() {
|
2023-03-04 14:30:13 +08:00
|
|
|
|
new Restart(this.e).restart()
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async getLog(plugin = '') {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
let cm = 'git log -100 --pretty="%h||[%cd] %s" --date=format:"%F %T"'
|
|
|
|
|
if (plugin) cm = `cd "plugins/${plugin}" && ${cm}`
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
let logAll
|
|
|
|
|
try {
|
|
|
|
|
logAll = await execSync(cm, { encoding: 'utf-8' })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(error.toString())
|
2023-09-06 03:00:17 +08:00
|
|
|
|
await this.reply(error.toString())
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!logAll) return false
|
|
|
|
|
|
2023-09-08 10:37:12 +08:00
|
|
|
|
logAll = logAll.trim().split('\n')
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
|
|
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 ''
|
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
let end = ''
|
|
|
|
|
try {
|
|
|
|
|
cm = 'git config -l'
|
|
|
|
|
if (plugin) cm = `cd "plugins/${plugin}" && ${cm}`
|
|
|
|
|
end = await execSync(cm, { encoding: 'utf-8' })
|
2023-10-09 02:46:33 +08:00
|
|
|
|
end = end.match(/remote\..*\.url=.+/g).join('\n\n').replace(/remote\..*\.url=/g, '').replace(/\/\/([^@]+)@/, '//')
|
2023-09-06 03:00:17 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(error.toString())
|
|
|
|
|
await this.reply(error.toString())
|
|
|
|
|
}
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
2023-09-06 03:00:17 +08:00
|
|
|
|
return common.makeForwardMsg(this.e, [log, end], `${plugin || 'Miao-Yunzai'} 更新日志,共${line}条`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
|
async updateLog() {
|
2023-09-06 03:00:17 +08:00
|
|
|
|
const plugin = this.getPlugin()
|
|
|
|
|
if (plugin === false) return false
|
|
|
|
|
return this.reply(await this.getLog(plugin))
|
2023-03-04 14:30:13 +08:00
|
|
|
|
}
|
2023-09-06 03:00:17 +08:00
|
|
|
|
}
|